Skip to content

Commit fc8d46b

Browse files
committed
Use CStr::from_bytes_until_nul to read rdkafka config
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.
1 parent 8c26619 commit fc8d46b

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/config.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,10 @@ impl NativeClientConfig {
150150
}
151151

152152
// Convert the C string to a Rust string.
153-
Ok(String::from_utf8_lossy(&buf).to_string())
153+
Ok(CStr::from_bytes_until_nul(&buf)
154+
.expect("rd_kafka_conf_get to write a NUL-terminated string.")
155+
.to_string_lossy()
156+
.to_string())
154157
}
155158
}
156159

0 commit comments

Comments
 (0)