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

Use CStr::from_bytes_until_nul to read rdkafka config #671

Closed

Conversation

chklauser
Copy link

@chklauser chklauser commented Apr 13, 2024

Commit 353812f replaced the more finicky CStr::from_bytes_with_nul with String::from_utf8_lossy. This causes tests to fail, both for me locally and in CI.

That commit eliminated a panic but now the returned Rust string (from NativeClientConfig.get) contains NUL bytes. This might have accidentally worked in FFI APIs that take C strings, but it fails when such a string is passed to a Rust API, such as integer parsing in stream_consumer.rs:217.

The newer CStr::from_bytes_until_nul function

  1. does not panic
  2. ends the string at the first NUL byte

It does return an error when there is no NUL byte in the input slice.

While it would be possible to fall back to String::from_utf8_lossy in that case, I'm not sure that would be a good idea. If we are in that situation, librdkafka clearly has messed something up completely. The contents of the buffer are then more likely complete garbage.

@chklauser
Copy link
Author

353812f has not been released on crates.io as far as I can tell. => No need to add the bugfix to the changelog.

@Fenex
Copy link

Fenex commented Jun 8, 2024

Important note: CStr::from_bytes_until_nul required update MSRV to 1.69.0 (current is 1.61.0).

We want to use `CStr::from_bytes_until_nul` 
for reading values from rdkafka.
Commit 353812f replaced the more finicky
`CStr::from_bytes_with_nul` with `String::from_utf8_lossy`.

That eliminated a panic but the returned Rust string now contained NUL bytes.
This might have accidentally worked in FFI APIs that take C strings, but it
fails when such a string is passed to a Rust API, such as integer parsing in
`stream_consumer.rs:217`.

The newer `CStr::from_bytes_until_nul` function
 1. does not panic
 2. ends the string at the first NUL byte

It does return an error when there is no NUL byte in the input slice.

While it would be possible to fall back to `String::from_utf8_lossy` in that
case, I'm not sure that would be a good idea. If we are in that situation,
librdkafka clearly has messed something up completely. The contents of the
buffer are then more likely complete garbage.
@chklauser chklauser force-pushed the fix-cstr-conversion-from-config branch from cb71e7c to fc8d46b Compare June 16, 2024 10:48
@chklauser
Copy link
Author

Indeed! I've included a bump to version 1.69 in the PR (as a separate commit).

Rust 1.69 was released 20 April, 2023. Seems a reasonable upgrade to me, but I don't really have a stake in that decision (we are using latest stable).

@chklauser
Copy link
Author

@mati865
Copy link

mati865 commented Jul 31, 2024

There is one more PR with the fix but contains also other changes: #647

@chklauser
Copy link
Author

Closed in favor of #688

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