Skip to content

Commit a087427

Browse files
Preserve pathname bytes in refcount unlink (#664)
Refcount `unlink` can delete another file when a pathname contains non-UTF-8 bytes. Lossy text conversion can turn a missing byte-named target into the name of an existing file containing the UTF-8 replacement character. --- Made by [Perfloop](https://app.perfloop.ai). --------- Signed-off-by: Perfloop Agent <agent@perfloop.ai> Co-authored-by: Perfloop Agent <agent@perfloop.ai>
1 parent 12d0b80 commit a087427

14 files changed

Lines changed: 151 additions & 18 deletions

‎rules/unistd/tgt_refcount.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ fn f3(a0: i32, a1: AnyPtr, a2: usize) -> isize {
3737
}
3838

3939
fn f4(a0: Ptr<i8>) -> i32 {
40-
match nix::unistd::unlink(a0.to_rust_string().as_str()) {
40+
match a0.with_c_bytes(|__path| nix::unistd::unlink(__path)) {
4141
Ok(()) => 0,
4242
Err(__e) => {
4343
libcc2rs::cpp2rust_errno().write(__e as i32);

‎tests/unit/out/refcount/fd_io.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ fn main_0() -> i32 {
128128
);
129129
assert!((FdRegistry::close(fd) == 0));
130130
assert!(
131-
(match nix::unistd::unlink(path.to_rust_string().as_str()) {
131+
(match path.with_c_bytes(|__path| nix::unistd::unlink(__path)) {
132132
Ok(()) => 0,
133133
Err(__e) => {
134134
libcc2rs::cpp2rust_errno().write(__e as i32);

‎tests/unit/out/refcount/fstat.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ fn main_0() -> i32 {
6666
assert!(({ ({ (*st.borrow()).st_size } == (5 as ::libc::off_t)) }));
6767
assert!((FdRegistry::close(fd) == 0));
6868
assert!(
69-
(match nix::unistd::unlink(path.to_rust_string().as_str()) {
69+
(match path.with_c_bytes(|__path| nix::unistd::unlink(__path)) {
7070
Ok(()) => 0,
7171
Err(__e) => {
7272
libcc2rs::cpp2rust_errno().write(__e as i32);

‎tests/unit/out/refcount/isatty.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ fn main_0() -> i32 {
4343
);
4444
assert!((FdRegistry::close(fd) == 0));
4545
assert!(
46-
(match nix::unistd::unlink(path.to_rust_string().as_str()) {
46+
(match path.with_c_bytes(|__path| nix::unistd::unlink(__path)) {
4747
Ok(()) => 0,
4848
Err(__e) => {
4949
libcc2rs::cpp2rust_errno().write(__e as i32);

‎tests/unit/out/refcount/lseek_ftruncate.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ fn main_0() -> i32 {
159159
);
160160
assert!((FdRegistry::close(fd) == 0));
161161
assert!(
162-
(match nix::unistd::unlink(path.to_rust_string().as_str()) {
162+
(match path.with_c_bytes(|__path| nix::unistd::unlink(__path)) {
163163
Ok(()) => 0,
164164
Err(__e) => {
165165
libcc2rs::cpp2rust_errno().write(__e as i32);

‎tests/unit/out/refcount/socket_unix_name.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ fn main_0() -> i32 {
211211
({ check_name_0(fd, array_field_ptr!(un, sun_path)) });
212212
assert!((FdRegistry::close(fd) == 0));
213213
assert!(
214-
(match nix::unistd::unlink(array_field_ptr!(un, sun_path).to_rust_string().as_str()) {
214+
(match array_field_ptr!(un, sun_path).with_c_bytes(|__path| nix::unistd::unlink(__path)) {
215215
Ok(()) => 0,
216216
Err(__e) => {
217217
libcc2rs::cpp2rust_errno().write(__e as i32);

‎tests/unit/out/refcount/stdcopy_ostream.rs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ fn main_0() -> i32 {
6666
&& (((*buf.borrow())[(15) as usize] as i32) == (('2' as i8) as i32))
6767
})
6868
);
69-
match nix::unistd::unlink(file.decay().to_rust_string().as_str()) {
69+
match file
70+
.decay()
71+
.with_c_bytes(|__path| nix::unistd::unlink(__path))
72+
{
7073
Ok(()) => 0,
7174
Err(__e) => {
7275
libcc2rs::cpp2rust_errno().write(__e as i32);

‎tests/unit/out/refcount/stdio.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ pub fn test_fileno_3() {
116116
__r
117117
};
118118
assert!(
119-
(match nix::unistd::unlink(file.to_rust_string().as_str()) {
119+
(match file.with_c_bytes(|__path| nix::unistd::unlink(__path)) {
120120
Ok(()) => 0,
121121
Err(__e) => {
122122
libcc2rs::cpp2rust_errno().write(__e as i32);

‎tests/unit/out/refcount/stdio_nofd.rs‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ pub fn test_fputc_fputs_0() {
8888
} == 0)
8989
);
9090
assert!(
91-
(match nix::unistd::unlink(path.to_rust_string().as_str()) {
91+
(match path.with_c_bytes(|__path| nix::unistd::unlink(__path)) {
9292
Ok(()) => 0,
9393
Err(__e) => {
9494
libcc2rs::cpp2rust_errno().write(__e as i32);
@@ -327,7 +327,7 @@ pub fn test_fgets_getc_2() {
327327
} == 0)
328328
);
329329
assert!(
330-
(match nix::unistd::unlink(path.to_rust_string().as_str()) {
330+
(match path.with_c_bytes(|__path| nix::unistd::unlink(__path)) {
331331
Ok(()) => 0,
332332
Err(__e) => {
333333
libcc2rs::cpp2rust_errno().write(__e as i32);
@@ -476,7 +476,7 @@ pub fn test_fgets_size_one_and_error_flag_3() {
476476
} == 0)
477477
);
478478
assert!(
479-
(match nix::unistd::unlink(path.to_rust_string().as_str()) {
479+
(match path.with_c_bytes(|__path| nix::unistd::unlink(__path)) {
480480
Ok(()) => 0,
481481
Err(__e) => {
482482
libcc2rs::cpp2rust_errno().write(__e as i32);
@@ -542,7 +542,7 @@ pub fn test_freopen_4() {
542542
} == 0)
543543
);
544544
assert!(
545-
(match nix::unistd::unlink(path.to_rust_string().as_str()) {
545+
(match path.with_c_bytes(|__path| nix::unistd::unlink(__path)) {
546546
Ok(()) => 0,
547547
Err(__e) => {
548548
libcc2rs::cpp2rust_errno().write(__e as i32);
@@ -623,7 +623,7 @@ pub fn test_fseeko_5() {
623623
} == 0)
624624
);
625625
assert!(
626-
(match nix::unistd::unlink(path.to_rust_string().as_str()) {
626+
(match path.with_c_bytes(|__path| nix::unistd::unlink(__path)) {
627627
Ok(()) => 0,
628628
Err(__e) => {
629629
libcc2rs::cpp2rust_errno().write(__e as i32);
@@ -702,7 +702,7 @@ pub fn test_rename_6() {
702702
} == -1_i32)
703703
);
704704
assert!(
705-
(match nix::unistd::unlink(to.to_rust_string().as_str()) {
705+
(match to.with_c_bytes(|__path| nix::unistd::unlink(__path)) {
706706
Ok(()) => 0,
707707
Err(__e) => {
708708
libcc2rs::cpp2rust_errno().write(__e as i32);
@@ -738,7 +738,7 @@ pub fn test_setvbuf_7() {
738738
} == 0)
739739
);
740740
assert!(
741-
(match nix::unistd::unlink(path.to_rust_string().as_str()) {
741+
(match path.with_c_bytes(|__path| nix::unistd::unlink(__path)) {
742742
Ok(()) => 0,
743743
Err(__e) => {
744744
libcc2rs::cpp2rust_errno().write(__e as i32);

‎tests/unit/out/refcount/sys_stat.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub fn test_stat_0() {
4848
);
4949
assert!(({ ({ (*st.borrow()).st_size } == (5 as ::libc::off_t)) }));
5050
assert!(((field_ptr!(st.as_pointer(), st_mtime).read()) > (0 as ::libc::time_t)));
51-
match nix::unistd::unlink(path.to_rust_string().as_str()) {
51+
match path.with_c_bytes(|__path| nix::unistd::unlink(__path)) {
5252
Ok(()) => 0,
5353
Err(__e) => {
5454
libcc2rs::cpp2rust_errno().write(__e as i32);
@@ -100,7 +100,7 @@ pub fn test_fstat_1() {
100100
__r
101101
} == 0)
102102
);
103-
match nix::unistd::unlink(path.to_rust_string().as_str()) {
103+
match path.with_c_bytes(|__path| nix::unistd::unlink(__path)) {
104104
Ok(()) => 0,
105105
Err(__e) => {
106106
libcc2rs::cpp2rust_errno().write(__e as i32);

0 commit comments

Comments
 (0)