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

Exclude NUL characters from OSStrings returned by getsockopt #2557

Merged
merged 3 commits into from
Dec 8, 2024

Conversation

asomers
Copy link
Member

@asomers asomers commented Dec 5, 2024

On FreeBSD, getsockopt appends a single NUL to the returned string. On Linux, it pads the entire buffer with NULs. But both of those behaviors may be version-dependent. Adjust GetOsString to handle any number of NUL characters.

What does this PR do

Checklist:

  • I have read CONTRIBUTING.md
  • I have written necessary tests and rustdoc comments
  • A change log has been added if this PR modifies nix's API

On FreeBSD, getsockopt appends a single NUL to the returned string.  On
Linux, it pads the entire buffer with NULs.  But both of those behaviors
may be version-dependent.  Adjust GetOsString to handle any number of
NUL characters.
@asomers asomers requested a review from SteveLauC December 5, 2024 18:35
Copy link
Member

@SteveLauC SteveLauC left a comment

Choose a reason for hiding this comment

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

But both of those behaviors may be version-dependent.

So this is a defensive patch, but anyway, it harms nothing:)

Comment on lines 1748 to 1753
if let Ok(cs) = CStr::from_bytes_until_nul(&v.as_mut()[0..len]) {
OsStr::from_bytes(cs.to_bytes())
} else {
// The OS returned a non-NUL-terminated string
OsStr::from_bytes(&v.as_mut()[0..len])
}
Copy link
Member

Choose a reason for hiding this comment

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

Considering that this behavior is version-dependent, i.e., some contributors, with specific kernel versions, may find this check useless, perhaps we should document why we do this here 🤔

@SteveLauC SteveLauC added this pull request to the merge queue Dec 8, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Dec 8, 2024
@SteveLauC SteveLauC added this pull request to the merge queue Dec 8, 2024
Merged via the queue into nix-rust:master with commit cfbaa08 Dec 8, 2024
40 checks passed
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.

2 participants