From e0cc1e44e547ac12843b6f0cbda30d99254e5132 Mon Sep 17 00:00:00 2001 From: Perfloop Agent Date: Thu, 19 Nov 2026 08:48:45 +0000 Subject: [PATCH 1/2] Preserve pathname bytes in refcount unlink Signed-off-by: Perfloop Agent --- rules/unistd/tgt_refcount.rs | 2 +- tests/unit/out/refcount/fd_io.rs | 2 +- tests/unit/out/refcount/fstat.rs | 2 +- tests/unit/out/refcount/isatty.rs | 2 +- tests/unit/out/refcount/lseek_ftruncate.rs | 2 +- tests/unit/out/refcount/socket_unix_name.rs | 2 +- tests/unit/out/refcount/stdcopy_ostream.rs | 5 +- tests/unit/out/refcount/stdio.rs | 2 +- tests/unit/out/refcount/stdio_nofd.rs | 14 +-- tests/unit/out/refcount/sys_stat.rs | 4 +- tests/unit/out/refcount/termios.rs | 2 +- tests/unit/out/refcount/unlink_bytes.rs | 94 +++++++++++++++++++++ tests/unit/out/unsafe/unlink_bytes.rs | 60 +++++++++++++ tests/unit/unlink_bytes.c | 26 ++++++ 14 files changed, 201 insertions(+), 18 deletions(-) create mode 100644 tests/unit/out/refcount/unlink_bytes.rs create mode 100644 tests/unit/out/unsafe/unlink_bytes.rs create mode 100644 tests/unit/unlink_bytes.c diff --git a/rules/unistd/tgt_refcount.rs b/rules/unistd/tgt_refcount.rs index 1af45b5f63..40260dc31b 100644 --- a/rules/unistd/tgt_refcount.rs +++ b/rules/unistd/tgt_refcount.rs @@ -37,7 +37,7 @@ fn f3(a0: i32, a1: AnyPtr, a2: usize) -> isize { } fn f4(a0: Ptr) -> i32 { - match nix::unistd::unlink(a0.to_rust_string().as_str()) { + match a0.with_c_bytes(|__path| nix::unistd::unlink(__path)) { Ok(()) => 0, Err(__e) => { libcc2rs::cpp2rust_errno().write(__e as i32); diff --git a/tests/unit/out/refcount/fd_io.rs b/tests/unit/out/refcount/fd_io.rs index b5a3377640..57dcae5b61 100644 --- a/tests/unit/out/refcount/fd_io.rs +++ b/tests/unit/out/refcount/fd_io.rs @@ -128,7 +128,7 @@ fn main_0() -> i32 { ); assert!((FdRegistry::close(fd) == 0)); assert!( - (match nix::unistd::unlink(path.to_rust_string().as_str()) { + (match path.with_c_bytes(|__path| nix::unistd::unlink(__path)) { Ok(()) => 0, Err(__e) => { libcc2rs::cpp2rust_errno().write(__e as i32); diff --git a/tests/unit/out/refcount/fstat.rs b/tests/unit/out/refcount/fstat.rs index 61b56451aa..d248307f8b 100644 --- a/tests/unit/out/refcount/fstat.rs +++ b/tests/unit/out/refcount/fstat.rs @@ -66,7 +66,7 @@ fn main_0() -> i32 { assert!(({ ({ (*st.borrow()).st_size } == (5 as ::libc::off_t)) })); assert!((FdRegistry::close(fd) == 0)); assert!( - (match nix::unistd::unlink(path.to_rust_string().as_str()) { + (match path.with_c_bytes(|__path| nix::unistd::unlink(__path)) { Ok(()) => 0, Err(__e) => { libcc2rs::cpp2rust_errno().write(__e as i32); diff --git a/tests/unit/out/refcount/isatty.rs b/tests/unit/out/refcount/isatty.rs index 7feff75475..b99935eb06 100644 --- a/tests/unit/out/refcount/isatty.rs +++ b/tests/unit/out/refcount/isatty.rs @@ -43,7 +43,7 @@ fn main_0() -> i32 { ); assert!((FdRegistry::close(fd) == 0)); assert!( - (match nix::unistd::unlink(path.to_rust_string().as_str()) { + (match path.with_c_bytes(|__path| nix::unistd::unlink(__path)) { Ok(()) => 0, Err(__e) => { libcc2rs::cpp2rust_errno().write(__e as i32); diff --git a/tests/unit/out/refcount/lseek_ftruncate.rs b/tests/unit/out/refcount/lseek_ftruncate.rs index 46c4c55a66..84a24cd75e 100644 --- a/tests/unit/out/refcount/lseek_ftruncate.rs +++ b/tests/unit/out/refcount/lseek_ftruncate.rs @@ -159,7 +159,7 @@ fn main_0() -> i32 { ); assert!((FdRegistry::close(fd) == 0)); assert!( - (match nix::unistd::unlink(path.to_rust_string().as_str()) { + (match path.with_c_bytes(|__path| nix::unistd::unlink(__path)) { Ok(()) => 0, Err(__e) => { libcc2rs::cpp2rust_errno().write(__e as i32); diff --git a/tests/unit/out/refcount/socket_unix_name.rs b/tests/unit/out/refcount/socket_unix_name.rs index 3193591af2..3771c19043 100644 --- a/tests/unit/out/refcount/socket_unix_name.rs +++ b/tests/unit/out/refcount/socket_unix_name.rs @@ -211,7 +211,7 @@ fn main_0() -> i32 { ({ check_name_0(fd, array_field_ptr!(un, sun_path)) }); assert!((FdRegistry::close(fd) == 0)); assert!( - (match nix::unistd::unlink(array_field_ptr!(un, sun_path).to_rust_string().as_str()) { + (match array_field_ptr!(un, sun_path).with_c_bytes(|__path| nix::unistd::unlink(__path)) { Ok(()) => 0, Err(__e) => { libcc2rs::cpp2rust_errno().write(__e as i32); diff --git a/tests/unit/out/refcount/stdcopy_ostream.rs b/tests/unit/out/refcount/stdcopy_ostream.rs index a73f3d4416..007909d4f3 100644 --- a/tests/unit/out/refcount/stdcopy_ostream.rs +++ b/tests/unit/out/refcount/stdcopy_ostream.rs @@ -66,7 +66,10 @@ fn main_0() -> i32 { && (((*buf.borrow())[(15) as usize] as i32) == (('2' as i8) as i32)) }) ); - match nix::unistd::unlink(file.decay().to_rust_string().as_str()) { + match file + .decay() + .with_c_bytes(|__path| nix::unistd::unlink(__path)) + { Ok(()) => 0, Err(__e) => { libcc2rs::cpp2rust_errno().write(__e as i32); diff --git a/tests/unit/out/refcount/stdio.rs b/tests/unit/out/refcount/stdio.rs index 4a790f3951..02690306c0 100644 --- a/tests/unit/out/refcount/stdio.rs +++ b/tests/unit/out/refcount/stdio.rs @@ -116,7 +116,7 @@ pub fn test_fileno_3() { __r }; assert!( - (match nix::unistd::unlink(file.to_rust_string().as_str()) { + (match file.with_c_bytes(|__path| nix::unistd::unlink(__path)) { Ok(()) => 0, Err(__e) => { libcc2rs::cpp2rust_errno().write(__e as i32); diff --git a/tests/unit/out/refcount/stdio_nofd.rs b/tests/unit/out/refcount/stdio_nofd.rs index 1f2b0334d6..36d03a036b 100644 --- a/tests/unit/out/refcount/stdio_nofd.rs +++ b/tests/unit/out/refcount/stdio_nofd.rs @@ -88,7 +88,7 @@ pub fn test_fputc_fputs_0() { } == 0) ); assert!( - (match nix::unistd::unlink(path.to_rust_string().as_str()) { + (match path.with_c_bytes(|__path| nix::unistd::unlink(__path)) { Ok(()) => 0, Err(__e) => { libcc2rs::cpp2rust_errno().write(__e as i32); @@ -327,7 +327,7 @@ pub fn test_fgets_getc_2() { } == 0) ); assert!( - (match nix::unistd::unlink(path.to_rust_string().as_str()) { + (match path.with_c_bytes(|__path| nix::unistd::unlink(__path)) { Ok(()) => 0, Err(__e) => { libcc2rs::cpp2rust_errno().write(__e as i32); @@ -476,7 +476,7 @@ pub fn test_fgets_size_one_and_error_flag_3() { } == 0) ); assert!( - (match nix::unistd::unlink(path.to_rust_string().as_str()) { + (match path.with_c_bytes(|__path| nix::unistd::unlink(__path)) { Ok(()) => 0, Err(__e) => { libcc2rs::cpp2rust_errno().write(__e as i32); @@ -542,7 +542,7 @@ pub fn test_freopen_4() { } == 0) ); assert!( - (match nix::unistd::unlink(path.to_rust_string().as_str()) { + (match path.with_c_bytes(|__path| nix::unistd::unlink(__path)) { Ok(()) => 0, Err(__e) => { libcc2rs::cpp2rust_errno().write(__e as i32); @@ -623,7 +623,7 @@ pub fn test_fseeko_5() { } == 0) ); assert!( - (match nix::unistd::unlink(path.to_rust_string().as_str()) { + (match path.with_c_bytes(|__path| nix::unistd::unlink(__path)) { Ok(()) => 0, Err(__e) => { libcc2rs::cpp2rust_errno().write(__e as i32); @@ -702,7 +702,7 @@ pub fn test_rename_6() { } == -1_i32) ); assert!( - (match nix::unistd::unlink(to.to_rust_string().as_str()) { + (match to.with_c_bytes(|__path| nix::unistd::unlink(__path)) { Ok(()) => 0, Err(__e) => { libcc2rs::cpp2rust_errno().write(__e as i32); @@ -738,7 +738,7 @@ pub fn test_setvbuf_7() { } == 0) ); assert!( - (match nix::unistd::unlink(path.to_rust_string().as_str()) { + (match path.with_c_bytes(|__path| nix::unistd::unlink(__path)) { Ok(()) => 0, Err(__e) => { libcc2rs::cpp2rust_errno().write(__e as i32); diff --git a/tests/unit/out/refcount/sys_stat.rs b/tests/unit/out/refcount/sys_stat.rs index 85a989f8b3..154ea10988 100644 --- a/tests/unit/out/refcount/sys_stat.rs +++ b/tests/unit/out/refcount/sys_stat.rs @@ -48,7 +48,7 @@ pub fn test_stat_0() { ); assert!(({ ({ (*st.borrow()).st_size } == (5 as ::libc::off_t)) })); assert!(((field_ptr!(st.as_pointer(), st_mtime).read()) > (0 as ::libc::time_t))); - match nix::unistd::unlink(path.to_rust_string().as_str()) { + match path.with_c_bytes(|__path| nix::unistd::unlink(__path)) { Ok(()) => 0, Err(__e) => { libcc2rs::cpp2rust_errno().write(__e as i32); @@ -100,7 +100,7 @@ pub fn test_fstat_1() { __r } == 0) ); - match nix::unistd::unlink(path.to_rust_string().as_str()) { + match path.with_c_bytes(|__path| nix::unistd::unlink(__path)) { Ok(()) => 0, Err(__e) => { libcc2rs::cpp2rust_errno().write(__e as i32); diff --git a/tests/unit/out/refcount/termios.rs b/tests/unit/out/refcount/termios.rs index f8de25daa5..38b4c08e82 100644 --- a/tests/unit/out/refcount/termios.rs +++ b/tests/unit/out/refcount/termios.rs @@ -72,7 +72,7 @@ fn main_0() -> i32 { ); assert!((FdRegistry::close(fd) == 0)); assert!( - (match nix::unistd::unlink(path.to_rust_string().as_str()) { + (match path.with_c_bytes(|__path| nix::unistd::unlink(__path)) { Ok(()) => 0, Err(__e) => { libcc2rs::cpp2rust_errno().write(__e as i32); diff --git a/tests/unit/out/refcount/unlink_bytes.rs b/tests/unit/out/refcount/unlink_bytes.rs new file mode 100644 index 0000000000..7753975bd0 --- /dev/null +++ b/tests/unit/out/refcount/unlink_bytes.rs @@ -0,0 +1,94 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +pub fn main() { + __cpp2rust_init_globals(); + libcc2rs::cpp2rust_exit(main_0()); +} +fn main_0() -> i32 { + let mut sibling: Ptr = Ptr::::from_string_literal(b"unlink-\xef\xbf\xbd.tmp"); + let mut fp: Ptr = match CFile::open( + &sibling.to_rust_string(), + &Ptr::::from_string_literal(b"wb").to_rust_string(), + ) { + Some(__f) => Ptr::alloc(__f), + None => Ptr::null(), + }; + assert!((!((fp).is_null()))); + assert!( + (match Ptr::::from_string_literal(b"sentinel") + .with_c_bytes(|__bytes| fp.with_mut(|__f| __f.write(__bytes)) == __bytes.len()) + { + true => 0, + false => -1, + } >= 0) + ); + assert!( + ({ + let __r = fp.with(|__f| __f.close()); + fp.delete(); + __r + } == 0) + ); + libcc2rs::cpp2rust_errno().write(0); + assert!( + (match Ptr::::from_string_literal(b"unlink-\xff.tmp") + .with_c_bytes(|__path| nix::unistd::unlink(__path)) + { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == -1_i32) + ); + assert!(((libcc2rs::cpp2rust_errno().read()) == libc::ENOENT)); + fp = match CFile::open( + &sibling.to_rust_string(), + &Ptr::::from_string_literal(b"rb").to_rust_string(), + ) { + Some(__f) => Ptr::alloc(__f), + None => Ptr::null(), + }; + assert!((!((fp).is_null()))); + let buf: Value> = Rc::new(RefCell::new((0..8).map(|_| 0_i8).collect::>())); + assert!( + ({ + libcc2rs::fread_refcount( + (buf.decay()).to_any(), + 1_usize, + ::std::mem::size_of::<[i8; 8]>(), + (fp).clone(), + ) + } == { ::std::mem::size_of::<[i8; 8]>() }) + ); + assert!( + ((buf.decay()).to_any().memcmp( + &(Ptr::::from_string_literal(b"sentinel")).to_any(), + ::std::mem::size_of::<[i8; 8]>() + ) == 0) + ); + assert!( + ({ + let __r = fp.with(|__f| __f.close()); + fp.delete(); + __r + } == 0) + ); + assert!( + (match sibling.with_c_bytes(|__path| nix::unistd::unlink(__path)) { + Ok(()) => 0, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + -1 + } + } == 0) + ); + return 0; +} +pub fn __cpp2rust_init_globals() {} diff --git a/tests/unit/out/unsafe/unlink_bytes.rs b/tests/unit/out/unsafe/unlink_bytes.rs new file mode 100644 index 0000000000..973ab43e32 --- /dev/null +++ b/tests/unit/out/unsafe/unlink_bytes.rs @@ -0,0 +1,60 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + libcc2rs::cpp2rust_exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut sibling: *const libc::c_char = + (c"unlink-\xef\xbf\xbd.tmp".as_ptr().cast_mut()).cast_const(); + let mut fp: *mut ::libc::FILE = libc::fopen(sibling, (c"wb".as_ptr().cast_mut()).cast_const()); + assert!((!((fp).is_null()))); + assert!(((libc::fputs((c"sentinel".as_ptr().cast_mut()).cast_const(), fp)) >= (0))); + assert!(((libc::fclose(fp)) == (0))); + (*libcc2rs::cpp2rust_errno_unsafe()) = 0; + assert!(((libc::unlink((c"unlink-\xff.tmp".as_ptr().cast_mut()).cast_const())) == (-1_i32))); + assert!(((*libcc2rs::cpp2rust_errno_unsafe()) == (libc::ENOENT))); + fp = libc::fopen(sibling, (c"rb".as_ptr().cast_mut()).cast_const()); + assert!((!((fp).is_null()))); + let mut buf: [libc::c_char; 8] = [(0 as libc::c_char); 8]; + assert!( + ((libcc2rs::fread_unsafe( + (buf.as_mut_ptr() as *mut ::libc::c_void), + 1_usize, + ::std::mem::size_of::<[libc::c_char; 8]>(), + fp + )) == (::std::mem::size_of::<[libc::c_char; 8]>())) + ); + assert!( + (({ + let sa = core::slice::from_raw_parts( + (buf.as_mut_ptr() as *const ::libc::c_void) as *const u8, + ::std::mem::size_of::<[libc::c_char; 8]>(), + ); + let sb = core::slice::from_raw_parts( + (c"sentinel".as_ptr().cast_mut() as *const ::libc::c_void) as *const u8, + ::std::mem::size_of::<[libc::c_char; 8]>(), + ); + let mut diff = 0_i32; + for (x, y) in sa.iter().zip(sb.iter()) { + if x != y { + diff = (*x as i32) - (*y as i32); + break; + } + } + diff + }) == (0)) + ); + assert!(((libc::fclose(fp)) == (0))); + assert!(((libc::unlink(sibling)) == (0))); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() {} diff --git a/tests/unit/unlink_bytes.c b/tests/unit/unlink_bytes.c new file mode 100644 index 0000000000..4ac3b7043d --- /dev/null +++ b/tests/unit/unlink_bytes.c @@ -0,0 +1,26 @@ +#include +#include +#include +#include +#include + +int main(void) { + const char *sibling = "unlink-\xef\xbf\xbd.tmp"; + FILE *fp = fopen(sibling, "wb"); + assert(fp != NULL); + assert(fputs("sentinel", fp) >= 0); + assert(fclose(fp) == 0); + + errno = 0; + assert(unlink("unlink-\xff.tmp") == -1); + assert(errno == ENOENT); + fp = fopen(sibling, "rb"); + assert(fp != NULL); + char buf[8]; + assert(fread(buf, 1, sizeof(buf), fp) == sizeof(buf)); + assert(memcmp(buf, "sentinel", sizeof(buf)) == 0); + assert(fclose(fp) == 0); + + assert(unlink(sibling) == 0); + return 0; +} From eb99a2a450d0ee5eaad4805c4bb835e8df9d2053 Mon Sep 17 00:00:00 2001 From: Perfloop Agent Date: Thu, 19 Nov 2026 09:07:22 +0000 Subject: [PATCH 2/2] Keep unlink byte-path regression focused on sibling survival Signed-off-by: Perfloop Agent --- tests/unit/out/refcount/unlink_bytes.rs | 17 --------------- tests/unit/out/unsafe/unlink_bytes.rs | 29 ------------------------- tests/unit/unlink_bytes.c | 4 ---- 3 files changed, 50 deletions(-) diff --git a/tests/unit/out/refcount/unlink_bytes.rs b/tests/unit/out/refcount/unlink_bytes.rs index 7753975bd0..859c329f47 100644 --- a/tests/unit/out/refcount/unlink_bytes.rs +++ b/tests/unit/out/refcount/unlink_bytes.rs @@ -56,23 +56,6 @@ fn main_0() -> i32 { None => Ptr::null(), }; assert!((!((fp).is_null()))); - let buf: Value> = Rc::new(RefCell::new((0..8).map(|_| 0_i8).collect::>())); - assert!( - ({ - libcc2rs::fread_refcount( - (buf.decay()).to_any(), - 1_usize, - ::std::mem::size_of::<[i8; 8]>(), - (fp).clone(), - ) - } == { ::std::mem::size_of::<[i8; 8]>() }) - ); - assert!( - ((buf.decay()).to_any().memcmp( - &(Ptr::::from_string_literal(b"sentinel")).to_any(), - ::std::mem::size_of::<[i8; 8]>() - ) == 0) - ); assert!( ({ let __r = fp.with(|__f| __f.close()); diff --git a/tests/unit/out/unsafe/unlink_bytes.rs b/tests/unit/out/unsafe/unlink_bytes.rs index 973ab43e32..c49ec24d5b 100644 --- a/tests/unit/out/unsafe/unlink_bytes.rs +++ b/tests/unit/out/unsafe/unlink_bytes.rs @@ -24,35 +24,6 @@ unsafe fn main_0() -> i32 { assert!(((*libcc2rs::cpp2rust_errno_unsafe()) == (libc::ENOENT))); fp = libc::fopen(sibling, (c"rb".as_ptr().cast_mut()).cast_const()); assert!((!((fp).is_null()))); - let mut buf: [libc::c_char; 8] = [(0 as libc::c_char); 8]; - assert!( - ((libcc2rs::fread_unsafe( - (buf.as_mut_ptr() as *mut ::libc::c_void), - 1_usize, - ::std::mem::size_of::<[libc::c_char; 8]>(), - fp - )) == (::std::mem::size_of::<[libc::c_char; 8]>())) - ); - assert!( - (({ - let sa = core::slice::from_raw_parts( - (buf.as_mut_ptr() as *const ::libc::c_void) as *const u8, - ::std::mem::size_of::<[libc::c_char; 8]>(), - ); - let sb = core::slice::from_raw_parts( - (c"sentinel".as_ptr().cast_mut() as *const ::libc::c_void) as *const u8, - ::std::mem::size_of::<[libc::c_char; 8]>(), - ); - let mut diff = 0_i32; - for (x, y) in sa.iter().zip(sb.iter()) { - if x != y { - diff = (*x as i32) - (*y as i32); - break; - } - } - diff - }) == (0)) - ); assert!(((libc::fclose(fp)) == (0))); assert!(((libc::unlink(sibling)) == (0))); return 0; diff --git a/tests/unit/unlink_bytes.c b/tests/unit/unlink_bytes.c index 4ac3b7043d..b03cb80886 100644 --- a/tests/unit/unlink_bytes.c +++ b/tests/unit/unlink_bytes.c @@ -1,7 +1,6 @@ #include #include #include -#include #include int main(void) { @@ -16,9 +15,6 @@ int main(void) { assert(errno == ENOENT); fp = fopen(sibling, "rb"); assert(fp != NULL); - char buf[8]; - assert(fread(buf, 1, sizeof(buf), fp) == sizeof(buf)); - assert(memcmp(buf, "sentinel", sizeof(buf)) == 0); assert(fclose(fp) == 0); assert(unlink(sibling) == 0);