@@ -113,7 +113,7 @@ impl PlatformLogWriter<'_> {
113
113
/// Output buffer up until the \0 which will be placed at `len` position.
114
114
///
115
115
/// # Safety
116
- /// The first `len` bytes of `self.buffer` must be initialized.
116
+ /// The first `len` bytes of `self.buffer` must be initialized and not contain nullbytes .
117
117
unsafe fn output_specified_len ( & mut self , len : usize ) {
118
118
let mut last_byte = MaybeUninit :: new ( b'\0' ) ;
119
119
@@ -152,7 +152,13 @@ impl fmt::Write for PlatformLogWriter<'_> {
152
152
. zip ( incoming_bytes)
153
153
. enumerate ( )
154
154
. fold ( None , |acc, ( i, ( output, input) ) | {
155
- output. write ( * input) ;
155
+ if * input == b'\0' {
156
+ // Replace nullbytes with whitespace, so we can put the message in a CStr
157
+ // later to pass it through a const char*.
158
+ output. write ( b' ' ) ;
159
+ } else {
160
+ output. write ( * input) ;
161
+ }
156
162
if * input == b'\n' { Some ( i) } else { acc }
157
163
} ) ;
158
164
@@ -297,6 +303,20 @@ pub mod tests {
297
303
) ;
298
304
}
299
305
306
+ #[ test]
307
+ fn writer_substitutes_nullbytes_with_spaces ( ) {
308
+ let test_string = "Test_string_with\0 \0 \0 \0 nullbytes\0 " ;
309
+ let mut writer = get_tag_writer ( ) ;
310
+ writer
311
+ . write_str ( test_string)
312
+ . expect ( "Unable to write to PlatformLogWriter" ) ;
313
+
314
+ assert_eq ! (
315
+ unsafe { slice_assume_init_ref( & writer. buffer[ ..test_string. len( ) ] ) } ,
316
+ test_string. replace( "\0 " , " " ) . as_bytes( )
317
+ ) ;
318
+ }
319
+
300
320
fn get_tag_writer ( ) -> PlatformLogWriter < ' static > {
301
321
PlatformLogWriter :: new (
302
322
None ,
0 commit comments