@@ -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
@@ -123,7 +123,7 @@ impl PlatformLogWriter<'_> {
123
123
) ;
124
124
125
125
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)
127
127
. expect ( "Unreachable: nul terminator was placed at `len`" ) ;
128
128
android_log ( self . buf_id , self . priority , self . tag , msg) ;
129
129
@@ -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
@@ -265,23 +271,6 @@ pub mod tests {
265
271
) ;
266
272
}
267
273
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
-
285
274
#[ test]
286
275
fn copy_bytes_to_start ( ) {
287
276
let mut writer = get_tag_writer ( ) ;
@@ -314,6 +303,20 @@ pub mod tests {
314
303
) ;
315
304
}
316
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
+
317
320
fn get_tag_writer ( ) -> PlatformLogWriter < ' static > {
318
321
PlatformLogWriter :: new (
319
322
None ,
0 commit comments