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 1 year ago • 1 comments

Rebased version of #671 as per the discussion in #688 (forgot to re-open before I pushed the updated branch). Original PR text below


Commit 353812ff958b4b65f9e65dd22a60e770ed6ba948 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 avatar Aug 06 '24 16:08 chklauser

I created #706 today, without knowing that yesterday this one has been created. The same issue found in other place.

andrei-ionescu avatar Aug 07 '24 16:08 andrei-ionescu