-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
libpod: hostname written to /etc/hostname ends with a newline character #22859
Conversation
2b2d189
to
3284240
Compare
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() |
libpod/container_internal_linux.go
Outdated
@@ -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())) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thx, updated
please add a commit message more than a link |
3284240
to
63b7330
Compare
There was a problem hiding this 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.
libpod/container_internal_linux.go
Outdated
@@ -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") |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
test/e2e/run_dns_test.go
Outdated
@@ -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")) |
There was a problem hiding this comment.
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.
test/e2e/run_dns_test.go
Outdated
@@ -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")) |
There was a problem hiding this comment.
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"))
There was a problem hiding this comment.
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]>
63b7330
to
7243c71
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
[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 |
/lgtm |
37339f5
into
containers:main
Closes #22729
Does this PR introduce a user-facing change?