@@ -113,7 +113,7 @@ impl PlatformLogWriter<'_> {
113113 /// Output buffer up until the \0 which will be placed at `len` position.
114114 ///
115115 /// # 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 .
117117 unsafe fn output_specified_len ( & mut self , len : usize ) {
118118 let mut last_byte = MaybeUninit :: new ( b'\0' ) ;
119119
@@ -152,7 +152,13 @@ impl fmt::Write for PlatformLogWriter<'_> {
152152 . zip ( incoming_bytes)
153153 . enumerate ( )
154154 . 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+ }
156162 if * input == b'\n' { Some ( i) } else { acc }
157163 } ) ;
158164
@@ -297,6 +303,20 @@ pub mod tests {
297303 ) ;
298304 }
299305
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+
300320 fn get_tag_writer ( ) -> PlatformLogWriter < ' static > {
301321 PlatformLogWriter :: new (
302322 None ,
0 commit comments