examples: fix local socket address lengths#3601
Open
Old-Ding wants to merge 1 commit into
Open
Conversation
The udgram and ustream examples used sizeof(CONFIG_EXAMPLES_*_ADDR) as the pathname length and then added another NUL byte to the sockaddr length. That makes the maximum address length one byte larger than struct sockaddr_un, and ustream also wrote a terminator at sun_path[UNIX_PATH_MAX]. Track the pathname length with strnlen(), copy with room for the terminator, and pass sizeof(sa_family_t) + strlen(path) + 1 to the socket calls. Generated-by: OpenAI Codex Signed-off-by: Old-Ding <35417409+Old-Ding@users.noreply.github.com>
|
|
||
| server.sun_family = AF_LOCAL; | ||
| strlcpy(server.sun_path, CONFIG_EXAMPLES_UDGRAM_ADDR, addrlen); | ||
| strlcpy(server.sun_path, CONFIG_EXAMPLES_UDGRAM_ADDR, addrlen + 1); |
Contributor
There was a problem hiding this comment.
why change? strlcpy could ensure '\0' is terminated at the end of server.sun_path
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
strnlen()sizeof(sa_family_t) + strlen(path) + 1tobind(),connect(), andsendto()Why
The examples used
sizeof(CONFIG_EXAMPLES_*_ADDR)as the pathname length and then added another NUL byte to the sockaddr length. For maximum-length paths this can make the supplied address length one byte larger thanstruct sockaddr_un.The stream example also wrote
sun_path[addrlen] = '\0'after clampingaddrlentoUNIX_PATH_MAX, which can write one byte pastsun_path.Testing
git diff --check HEAD~1..HEADgit show --stat --check --format=fuller HEADgcc,clang, orccinstalled.