@@ -74,8 +74,11 @@ fn test_dup_stdout_stderr() {
74
74
unsafe {
75
75
let new_stdout = libc:: fcntl ( 1 , libc:: F_DUPFD , 0 ) ;
76
76
let new_stderr = libc:: fcntl ( 2 , libc:: F_DUPFD , 0 ) ;
77
- libc:: write ( new_stdout, bytes. as_ptr ( ) as * const libc:: c_void , bytes. len ( ) ) ;
78
- libc:: write ( new_stderr, bytes. as_ptr ( ) as * const libc:: c_void , bytes. len ( ) ) ;
77
+ // We write byte-for-byte as that's the easiest way to deal with partial writes...
78
+ for b in bytes. iter ( ) {
79
+ assert_eq ! ( libc:: write( new_stdout, & raw const * b as * const libc:: c_void, 1 ) , 1 ) ;
80
+ assert_eq ! ( libc:: write( new_stderr, & raw const * b as * const libc:: c_void, 1 ) , 1 ) ;
81
+ }
79
82
}
80
83
}
81
84
@@ -92,16 +95,24 @@ fn test_dup() {
92
95
let new_fd2 = libc:: dup2 ( fd, 8 ) ;
93
96
94
97
let mut first_buf = [ 0u8 ; 4 ] ;
95
- libc:: read ( fd, first_buf. as_mut_ptr ( ) as * mut libc:: c_void , 4 ) ;
96
- assert_eq ! ( & first_buf, b"dup " ) ;
98
+ let first_len = libc:: read ( fd, first_buf. as_mut_ptr ( ) as * mut libc:: c_void , 4 ) ;
99
+ assert ! ( first_len > 0 ) ;
100
+ let first_len = first_len as usize ;
101
+ assert_eq ! ( first_buf[ ..first_len] , bytes[ ..first_len] ) ;
102
+ let remaining_bytes = & bytes[ first_len..] ;
97
103
98
104
let mut second_buf = [ 0u8 ; 4 ] ;
99
- libc:: read ( new_fd, second_buf. as_mut_ptr ( ) as * mut libc:: c_void , 4 ) ;
100
- assert_eq ! ( & second_buf, b"and " ) ;
105
+ let second_len = libc:: read ( new_fd, second_buf. as_mut_ptr ( ) as * mut libc:: c_void , 4 ) ;
106
+ assert ! ( second_len > 0 ) ;
107
+ let second_len = second_len as usize ;
108
+ assert_eq ! ( second_buf[ ..second_len] , remaining_bytes[ ..second_len] ) ;
109
+ let remaining_bytes = & remaining_bytes[ second_len..] ;
101
110
102
111
let mut third_buf = [ 0u8 ; 4 ] ;
103
- libc:: read ( new_fd2, third_buf. as_mut_ptr ( ) as * mut libc:: c_void , 4 ) ;
104
- assert_eq ! ( & third_buf, b"dup2" ) ;
112
+ let third_len = libc:: read ( new_fd2, third_buf. as_mut_ptr ( ) as * mut libc:: c_void , 4 ) ;
113
+ assert ! ( third_len > 0 ) ;
114
+ let third_len = third_len as usize ;
115
+ assert_eq ! ( third_buf[ ..third_len] , remaining_bytes[ ..third_len] ) ;
105
116
}
106
117
}
107
118
@@ -145,7 +156,7 @@ fn test_ftruncate<T: From<i32>>(
145
156
let bytes = b"hello" ;
146
157
let path = utils:: prepare ( "miri_test_libc_fs_ftruncate.txt" ) ;
147
158
let mut file = File :: create ( & path) . unwrap ( ) ;
148
- file. write ( bytes) . unwrap ( ) ;
159
+ file. write_all ( bytes) . unwrap ( ) ;
149
160
file. sync_all ( ) . unwrap ( ) ;
150
161
assert_eq ! ( file. metadata( ) . unwrap( ) . len( ) , 5 ) ;
151
162
@@ -402,25 +413,33 @@ fn test_read_and_uninit() {
402
413
unsafe {
403
414
let fd = libc:: open ( cpath. as_ptr ( ) , libc:: O_RDONLY ) ;
404
415
assert_ne ! ( fd, -1 ) ;
405
- let mut buf: MaybeUninit < [ u8 ; 2 ] > = std:: mem:: MaybeUninit :: uninit ( ) ;
406
- assert_eq ! ( libc:: read( fd, buf. as_mut_ptr( ) . cast:: <std:: ffi:: c_void>( ) , 2 ) , 2 ) ;
416
+ let mut buf: MaybeUninit < u8 > = std:: mem:: MaybeUninit :: uninit ( ) ;
417
+ assert_eq ! ( libc:: read( fd, buf. as_mut_ptr( ) . cast:: <std:: ffi:: c_void>( ) , 1 ) , 1 ) ;
407
418
let buf = buf. assume_init ( ) ;
408
- assert_eq ! ( buf, [ 1 , 2 ] ) ;
419
+ assert_eq ! ( buf, 1 ) ;
409
420
assert_eq ! ( libc:: close( fd) , 0 ) ;
410
421
}
411
422
remove_file ( & path) . unwrap ( ) ;
412
423
}
413
424
{
414
425
// We test that if we requested to read 4 bytes, but actually read 3 bytes, then
415
426
// 3 bytes (not 4) will be overwritten, and remaining byte will be left as-is.
416
- let path = utils:: prepare_with_content ( "pass-libc-read-and-uninit-2.txt" , & [ 1u8 , 2 , 3 ] ) ;
427
+ let data = [ 1u8 , 2 , 3 ] ;
428
+ let path = utils:: prepare_with_content ( "pass-libc-read-and-uninit-2.txt" , & data) ;
417
429
let cpath = CString :: new ( path. clone ( ) . into_os_string ( ) . into_encoded_bytes ( ) ) . unwrap ( ) ;
418
430
unsafe {
419
431
let fd = libc:: open ( cpath. as_ptr ( ) , libc:: O_RDONLY ) ;
420
432
assert_ne ! ( fd, -1 ) ;
421
433
let mut buf = [ 42u8 ; 5 ] ;
422
- assert_eq ! ( libc:: read( fd, buf. as_mut_ptr( ) . cast:: <std:: ffi:: c_void>( ) , 4 ) , 3 ) ;
423
- assert_eq ! ( buf, [ 1 , 2 , 3 , 42 , 42 ] ) ;
434
+ let res = libc:: read ( fd, buf. as_mut_ptr ( ) . cast :: < std:: ffi:: c_void > ( ) , 4 ) ;
435
+ assert ! ( res > 0 && res < 4 ) ;
436
+ for i in 0 ..buf. len ( ) {
437
+ assert_eq ! (
438
+ buf[ i] ,
439
+ if i < res as usize { data[ i] } else { 42 } ,
440
+ "wrong result at pos {i}"
441
+ ) ;
442
+ }
424
443
assert_eq ! ( libc:: close( fd) , 0 ) ;
425
444
}
426
445
remove_file ( & path) . unwrap ( ) ;
0 commit comments