@@ -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
@@ -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_until_nul ( initialized)
126+ let msg = CStr :: from_bytes_with_nul ( initialized)
127127 . expect ( "Unreachable: nul terminator was placed at `len`" ) ;
128128 android_log ( self . buf_id , self . priority , self . tag , msg) ;
129129
@@ -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
@@ -265,23 +271,6 @@ pub mod tests {
265271 ) ;
266272 }
267273
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-
285274 #[ test]
286275 fn copy_bytes_to_start ( ) {
287276 let mut writer = get_tag_writer ( ) ;
@@ -314,6 +303,20 @@ pub mod tests {
314303 ) ;
315304 }
316305
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+
317320 fn get_tag_writer ( ) -> PlatformLogWriter < ' static > {
318321 PlatformLogWriter :: new (
319322 None ,
0 commit comments