Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libpod: hostname written to /etc/hostname ends with a newline character #22859

Merged
merged 1 commit into from
Jun 4, 2024

Conversation

ut-wangbo
Copy link
Contributor

Closes #22729

Does this PR introduce a user-facing change?

None

@Luap99
Copy link
Member

Luap99 commented May 31, 2024

Please add a test for it, i.e.

diff --git a/test/e2e/run_dns_test.go b/test/e2e/run_dns_test.go
index 341831349..126c17a74 100644
--- a/test/e2e/run_dns_test.go
+++ b/test/e2e/run_dns_test.go
@@ -60,7 +60,7 @@ var _ = Describe("Podman run dns", func() {
                session := podmanTest.Podman([]string{"run", "--hostname=foobar", ALPINE, "cat", "/etc/hostname"})
                session.WaitWithDefaultTimeout()
                Expect(session).Should(ExitCleanly())
-               Expect(session.OutputToString()).To(Equal("foobar"))
+               Expect(string(session.Out.Contents())).To(Equal("foobar\n"))
 
                session = podmanTest.Podman([]string{"run", "--hostname=foobar", ALPINE, "hostname"})
                session.WaitWithDefaultTimeout()

@@ -664,7 +664,7 @@ func (c *Container) makePlatformBindMounts() error {
// Make /etc/hostname
// This should never change, so no need to recreate if it exists
if _, ok := c.state.BindMounts["/etc/hostname"]; !ok {
hostnamePath, err := c.writeStringToRundir("hostname", c.Hostname())
hostnamePath, err := c.writeStringToRundir("hostname", fmt.Sprintf("%s\n", c.Hostname()))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there isn't really a strong reason for sprintf here, you could do c.Hostname() + "\n" instead

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, looks like the linter wants the spaces removed c.Hostname()+"\n" though

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thx, updated

@baude
Copy link
Member

baude commented May 31, 2024

please add a commit message more than a link

Copy link

@dagraff-cisco dagraff-cisco left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for letting me know that you had started this PR.

@@ -664,7 +664,7 @@ func (c *Container) makePlatformBindMounts() error {
// Make /etc/hostname
// This should never change, so no need to recreate if it exists
if _, ok := c.state.BindMounts["/etc/hostname"]; !ok {
hostnamePath, err := c.writeStringToRundir("hostname", c.Hostname())
hostnamePath, err := c.writeStringToRundir("hostname", c.Hostname() + "\n")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not do a string concat like this.

fmt.Sprintf("%s\n", c.Hostname()) instead.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, it is better like this.
fmt.Sprintf is much slower and this really isn't a complex format where is using Sprintf is required or improves readability

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, I have seen "bad behaviors" resultant from just string concat. Specific case in point would be SQL injection attacks.

In terms of speed, this is negligible. If I were to be building 1000s of hostnames I would be concerned about the cumulative effect.

Additionally, and I apologize as I'm fairly new to the Go language, I have not gone into great depth of in terms of what the memory consumption would look like with this type of computation.

I'm fine with this either way as it reaches the goal.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, I have seen "bad behaviors" resultant from just string concat. Specific case in point would be SQL injection attacks.

Well Sprintf would not guard against that at all.

In terms of speed, this is negligible. If I were to be building 1000s of hostnames I would be concerned about the cumulative effect.

Sure but if you do it everywhere it will get noticeable eventually.
There is simply no benifit of fmt.Sprintf() unless it uses more complex specifiers so we should default to the faster variant.

@@ -60,7 +60,7 @@ var _ = Describe("Podman run dns", func() {
session := podmanTest.Podman([]string{"run", "--hostname=foobar", ALPINE, "cat", "/etc/hostname"})
session.WaitWithDefaultTimeout()
Expect(session).Should(ExitCleanly())
Expect(session.OutputToString()).To(Equal("foobar"))
Expect(session.OutputToString()).To(Equal("foobar\n"))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not realize that this test existed, otherwise I would have done this in my PR.

@@ -60,7 +60,7 @@ var _ = Describe("Podman run dns", func() {
session := podmanTest.Podman([]string{"run", "--hostname=foobar", ALPINE, "cat", "/etc/hostname"})
session.WaitWithDefaultTimeout()
Expect(session).Should(ExitCleanly())
Expect(session.OutputToString()).To(Equal("foobar"))
Expect(session.OutputToString()).To(Equal("foobar\n"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use the exact diff I posted this will not work like that because OutputToString() will trim the newline Expect(string(session.Out.Contents())).To(Equal("foobar\n"))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it

… file

debian's man (5) hostname page states "The file should contain a single newline-terminated hostname
string."
[NO NEW TESTS NEEDED]

fix containers#22729

Signed-off-by: Bo Wang <[email protected]>
@ut-wangbo ut-wangbo force-pushed the hostname-new-line branch from 63b7330 to 7243c71 Compare June 4, 2024 07:23
Copy link
Member

@Luap99 Luap99 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

openshift-ci bot commented Jun 4, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: Luap99, ut-wangbo

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@openshift-ci openshift-ci bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jun 4, 2024
@rhatdan
Copy link
Member

rhatdan commented Jun 4, 2024

/lgtm

@openshift-ci openshift-ci bot added the lgtm Indicates that a PR is ready to be merged. label Jun 4, 2024
@openshift-merge-bot openshift-merge-bot bot merged commit 37339f5 into containers:main Jun 4, 2024
88 of 89 checks passed
@stale-locking-app stale-locking-app bot added the locked - please file new issue/PR Assist humans wanting to comment on an old issue or PR with locked comments. label Sep 3, 2024
@stale-locking-app stale-locking-app bot locked as resolved and limited conversation to collaborators Sep 3, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. lgtm Indicates that a PR is ready to be merged. locked - please file new issue/PR Assist humans wanting to comment on an old issue or PR with locked comments. release-note-none
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Hostname written to /etc/hostname does not include new-line at end
5 participants