Skip to content

Commit bfecd10

Browse files
author
Marcin Radomski
committed
Avoid panic on buffers with embedded nul bytes
Some crates use log crate with a message padded with a number of nullbytes [1]. This currently causes panics. Using `CStr::from_bytes_until_nul` accepts multiple null-bytes, and instead stops at the first nullbyte in a buffer. This may truncate some logs with text interspersed with nullbytes. However, I'd say logging _something_ there is a less-bad option than crashing just because we got a nullbyte in the &str. [1] https://github.com/cloudflare/quiche/blob/d0efd2c5278b9dbe8d6544c3015f8c772f3513b4/quiche/src/tls/mod.rs#L1040
1 parent 634c027 commit bfecd10

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

src/platform_log_writer.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ impl PlatformLogWriter<'_> {
123123
);
124124

125125
let initialized = unsafe { slice_assume_init_ref(&self.buffer[..len + 1]) };
126-
let msg = CStr::from_bytes_with_nul(initialized)
126+
let msg = CStr::from_bytes_until_nul(initialized)
127127
.expect("Unreachable: nul terminator was placed at `len`");
128128
android_log(self.buf_id, self.priority, self.tag, msg);
129129

@@ -265,6 +265,23 @@ pub mod tests {
265265
);
266266
}
267267

268+
#[test]
269+
fn output_specified_len_accepts_extra_trailing_nuls() {
270+
let mut writer = get_tag_writer();
271+
let log_string = "abcde\0\0\0";
272+
let first_nul = log_string.find('\0').unwrap();
273+
writer
274+
.write_str(log_string)
275+
.expect("Unable to write to PlatformLogWriter");
276+
277+
unsafe { writer.output_specified_len(8) };
278+
279+
assert_eq!(
280+
unsafe { slice_assume_init_ref(&writer.buffer[..first_nul]) },
281+
&log_string.as_bytes()[..first_nul]
282+
);
283+
}
284+
268285
#[test]
269286
fn copy_bytes_to_start() {
270287
let mut writer = get_tag_writer();

0 commit comments

Comments
 (0)