rust-rdkafka icon indicating copy to clipboard operation
rust-rdkafka copied to clipboard

Use CStr::from_bytes_until_nul to read rdkafka config

Open chklauser opened this issue 10 months ago • 4 comments

Commit 353812ff958b4b65f9e65dd22a60e770ed6ba948 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 avatar Apr 13 '24 14:04 chklauser

https://github.com/fede1024/rust-rdkafka/commit/353812ff958b4b65f9e65dd22a60e770ed6ba948 has not been released on crates.io as far as I can tell. => No need to add the bugfix to the changelog.

chklauser avatar Apr 13 '24 16:04 chklauser

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

Fenex avatar Jun 08 '24 17:06 Fenex

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 avatar Jun 16 '24 10:06 chklauser

It seems there are a total of three PRs for this error now :(

  1. https://github.com/fede1024/rust-rdkafka/pull/671 (this PR)
  2. https://github.com/fede1024/rust-rdkafka/pull/688
  3. https://github.com/fede1024/rust-rdkafka/pull/691

chklauser avatar Jun 16 '24 10:06 chklauser

There is one more PR with the fix but contains also other changes: https://github.com/fede1024/rust-rdkafka/pull/647

mati865 avatar Jul 31 '24 12:07 mati865

Closed in favor of #688

chklauser avatar Aug 05 '24 23:08 chklauser