Skip to content

Ensure that sethostname null terminates the hostname correctly#5107

Merged
xiaoxiang781216 merged 1 commit into
apache:masterfrom
normanr:sethostname
Dec 29, 2021
Merged

Ensure that sethostname null terminates the hostname correctly#5107
xiaoxiang781216 merged 1 commit into
apache:masterfrom
normanr:sethostname

Conversation

@normanr

@normanr normanr commented Dec 29, 2021

Copy link
Copy Markdown
Contributor

Summary

commit e1c306f added sethostname. The null terminator should be added after the new hostname (and only at the very end if the new hostname is the maximum length).

Impact

Without this if the hostname is set to hello-world and then it's set to short, the resulting hostname is incorrectly short-world.

Also renamed the size argument (for sethostname code and for gethostname and sethostname in the header file) to namelen to match the gethostname code and the blockcomment input parameter names.

Testing

Manually tested with medium and then short hostnames. Also tested with hostnames 31, 32, and 33 characters in length to check that they were truncated correctly.

Comment thread libs/libc/unistd/lib_sethostname.c Outdated
@xiaoxiang781216

Copy link
Copy Markdown
Contributor

Let's squash the patch into one

@jerpelea jerpelea self-requested a review December 29, 2021 12:04
commit e1c306f added sethostname using
strncpy. This replaces it with strlcpy and uses sizeof() instead of
re-calculating the buffer size.

Rename size argument for get/sethostname to match the implementation
@xiaoxiang781216 xiaoxiang781216 merged commit df956b0 into apache:master Dec 29, 2021
@normanr normanr deleted the sethostname branch December 29, 2021 17:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants