Skip to content

Exclude NUL characters from OSStrings returned by getsockopt#2557

Merged
SteveLauC merged 3 commits intonix-rust:masterfrom
asomers:sockopt-nul
Dec 8, 2024
Merged

Exclude NUL characters from OSStrings returned by getsockopt#2557
SteveLauC merged 3 commits intonix-rust:masterfrom
asomers:sockopt-nul

Conversation

@asomers
Copy link
Copy Markdown
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
Copy Markdown
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 thread src/sys/socket/sockopt.rs
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
Copy Markdown
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
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