From b6f0fd88f2925224b9b91baad459148b4b57c8e6 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 7 Jul 2026 19:05:43 +0100 Subject: [PATCH 01/15] Add safe pton and ntop rules --- rules/arpa_inet/tgt_refcount.rs | 60 +++ rules/src/modules.rs | 2 + tests/unit/inet_pton_ntop.c | 24 ++ tests/unit/out/refcount/inet_pton_ntop.rs | 427 ++++++++++++++++++++++ tests/unit/out/unsafe/inet_pton_ntop.rs | 174 +++++++++ 5 files changed, 687 insertions(+) create mode 100644 rules/arpa_inet/tgt_refcount.rs create mode 100644 tests/unit/inet_pton_ntop.c create mode 100644 tests/unit/out/refcount/inet_pton_ntop.rs create mode 100644 tests/unit/out/unsafe/inet_pton_ntop.rs diff --git a/rules/arpa_inet/tgt_refcount.rs b/rules/arpa_inet/tgt_refcount.rs new file mode 100644 index 00000000..6f595551 --- /dev/null +++ b/rules/arpa_inet/tgt_refcount.rs @@ -0,0 +1,60 @@ +// Copyright (c) 2022-present INESC-ID. +// Distributed under the MIT license that can be found in the LICENSE file. + +use libcc2rs::*; + +fn f5(a0: i32, a1: Ptr, a2: AnyPtr) -> i32 { + if a0 == libc::AF_INET { + match a1.to_rust_string().parse::() { + Ok(__ip) => { + let __octets = __ip.octets(); + for __i in 0..4 { + a2.reinterpret_cast::().offset(__i).write(__octets[__i]); + } + 1 + } + Err(_) => 0, + } + } else if a0 == libc::AF_INET6 { + match a1.to_rust_string().parse::() { + Ok(__ip) => { + let __octets = __ip.octets(); + for __i in 0..16 { + a2.reinterpret_cast::().offset(__i).write(__octets[__i]); + } + 1 + } + Err(_) => 0, + } + } else { + -1 + } +} + +fn f6(a0: i32, a1: AnyPtr, a2: Ptr, a3: u32) -> Ptr { + let __text = if a0 == libc::AF_INET { + let mut __b = [0u8; 4]; + for __i in 0..4 { + __b[__i] = a1.reinterpret_cast::().offset(__i).read(); + } + Some(std::net::Ipv4Addr::from(__b).to_string()) + } else if a0 == libc::AF_INET6 { + let mut __b = [0u8; 16]; + for __i in 0..16 { + __b[__i] = a1.reinterpret_cast::().offset(__i).read(); + } + Some(std::net::Ipv6Addr::from(__b).to_string()) + } else { + None + }; + match __text { + Some(__s) if (__s.len() as u32) < a3 => { + for __i in 0..__s.len() { + a2.offset(__i).write(__s.as_bytes()[__i]); + } + a2.offset(__s.len()).write(0); + a2.clone() + } + _ => Ptr::null(), + } +} diff --git a/rules/src/modules.rs b/rules/src/modules.rs index b05d690c..1d86705d 100644 --- a/rules/src/modules.rs +++ b/rules/src/modules.rs @@ -2,6 +2,8 @@ pub mod algorithm_tgt_refcount; #[path = r#"../algorithm/tgt_unsafe.rs"#] pub mod algorithm_tgt_unsafe; +#[path = r#"../arpa_inet/tgt_refcount.rs"#] +pub mod arpa_inet_tgt_refcount; #[path = r#"../arpa_inet/tgt_unsafe.rs"#] pub mod arpa_inet_tgt_unsafe; #[path = r#"../array/tgt_refcount.rs"#] diff --git a/tests/unit/inet_pton_ntop.c b/tests/unit/inet_pton_ntop.c new file mode 100644 index 00000000..757c65ae --- /dev/null +++ b/tests/unit/inet_pton_ntop.c @@ -0,0 +1,24 @@ +#include +#include +#include + +int main(void) { + unsigned char buf[16]; + assert(inet_pton(AF_INET, "1.2.3.4", buf) == 1); + assert(buf[0] == 1 && buf[1] == 2 && buf[2] == 3 && buf[3] == 4); + assert(inet_pton(AF_INET, "999.1.1.1", buf) == 0); + assert(inet_pton(AF_INET, "not an ip", buf) == 0); + assert(inet_pton(AF_INET6, "::1", buf) == 1); + assert(buf[0] == 0 && buf[15] == 1); + assert(inet_pton(AF_INET6, "2001:db8::5", buf) == 1); + assert(buf[0] == 0x20 && buf[1] == 0x01 && buf[15] == 5); + + char text[64]; + unsigned char four[4] = {10, 0, 0, 1}; + assert(strcmp(inet_ntop(AF_INET, four, text, sizeof(text)), "10.0.0.1") == 0); + unsigned char sixteen[16] = {0}; + sixteen[15] = 1; + assert(strcmp(inet_ntop(AF_INET6, sixteen, text, sizeof(text)), "::1") == 0); + assert(inet_ntop(AF_INET, four, text, 4) == 0); + return 0; +} diff --git a/tests/unit/out/refcount/inet_pton_ntop.rs b/tests/unit/out/refcount/inet_pton_ntop.rs new file mode 100644 index 00000000..1f1e02b6 --- /dev/null +++ b/tests/unit/out/refcount/inet_pton_ntop.rs @@ -0,0 +1,427 @@ +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() { + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let buf: Value> = Rc::new(RefCell::new( + (0..16).map(|_| ::default()).collect::>(), + )); + assert!( + (((if 2 == libc::AF_INET { + match Ptr::from_string_literal(b"1.2.3.4") + .to_rust_string() + .parse::() + { + Ok(__ip) => { + let __octets = __ip.octets(); + for __i in 0..4 { + ((buf.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .offset(__i) + .write(__octets[__i]); + } + 1 + } + Err(_) => 0, + } + } else if 2 == libc::AF_INET6 { + match Ptr::from_string_literal(b"1.2.3.4") + .to_rust_string() + .parse::() + { + Ok(__ip) => { + let __octets = __ip.octets(); + for __i in 0..16 { + ((buf.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .offset(__i) + .write(__octets[__i]); + } + 1 + } + Err(_) => 0, + } + } else { + -1 + } == 1) as i32) + != 0) + ); + assert!( + ((((((((((((((*buf.borrow())[(0) as usize] as i32) == 1) as i32) != 0) + && (((((*buf.borrow())[(1) as usize] as i32) == 2) as i32) != 0)) + as i32) + != 0) + && (((((*buf.borrow())[(2) as usize] as i32) == 3) as i32) != 0)) as i32) + != 0) + && (((((*buf.borrow())[(3) as usize] as i32) == 4) as i32) != 0)) as i32) + != 0) + ); + assert!( + (((if 2 == libc::AF_INET { + match Ptr::from_string_literal(b"999.1.1.1") + .to_rust_string() + .parse::() + { + Ok(__ip) => { + let __octets = __ip.octets(); + for __i in 0..4 { + ((buf.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .offset(__i) + .write(__octets[__i]); + } + 1 + } + Err(_) => 0, + } + } else if 2 == libc::AF_INET6 { + match Ptr::from_string_literal(b"999.1.1.1") + .to_rust_string() + .parse::() + { + Ok(__ip) => { + let __octets = __ip.octets(); + for __i in 0..16 { + ((buf.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .offset(__i) + .write(__octets[__i]); + } + 1 + } + Err(_) => 0, + } + } else { + -1 + } == 0) as i32) + != 0) + ); + assert!( + (((if 2 == libc::AF_INET { + match Ptr::from_string_literal(b"not an ip") + .to_rust_string() + .parse::() + { + Ok(__ip) => { + let __octets = __ip.octets(); + for __i in 0..4 { + ((buf.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .offset(__i) + .write(__octets[__i]); + } + 1 + } + Err(_) => 0, + } + } else if 2 == libc::AF_INET6 { + match Ptr::from_string_literal(b"not an ip") + .to_rust_string() + .parse::() + { + Ok(__ip) => { + let __octets = __ip.octets(); + for __i in 0..16 { + ((buf.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .offset(__i) + .write(__octets[__i]); + } + 1 + } + Err(_) => 0, + } + } else { + -1 + } == 0) as i32) + != 0) + ); + assert!( + (((if 10 == libc::AF_INET { + match Ptr::from_string_literal(b"::1") + .to_rust_string() + .parse::() + { + Ok(__ip) => { + let __octets = __ip.octets(); + for __i in 0..4 { + ((buf.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .offset(__i) + .write(__octets[__i]); + } + 1 + } + Err(_) => 0, + } + } else if 10 == libc::AF_INET6 { + match Ptr::from_string_literal(b"::1") + .to_rust_string() + .parse::() + { + Ok(__ip) => { + let __octets = __ip.octets(); + for __i in 0..16 { + ((buf.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .offset(__i) + .write(__octets[__i]); + } + 1 + } + Err(_) => 0, + } + } else { + -1 + } == 1) as i32) + != 0) + ); + assert!( + ((((((((*buf.borrow())[(0) as usize] as i32) == 0) as i32) != 0) + && (((((*buf.borrow())[(15) as usize] as i32) == 1) as i32) != 0)) as i32) + != 0) + ); + assert!( + (((if 10 == libc::AF_INET { + match Ptr::from_string_literal(b"2001:db8::5") + .to_rust_string() + .parse::() + { + Ok(__ip) => { + let __octets = __ip.octets(); + for __i in 0..4 { + ((buf.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .offset(__i) + .write(__octets[__i]); + } + 1 + } + Err(_) => 0, + } + } else if 10 == libc::AF_INET6 { + match Ptr::from_string_literal(b"2001:db8::5") + .to_rust_string() + .parse::() + { + Ok(__ip) => { + let __octets = __ip.octets(); + for __i in 0..16 { + ((buf.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .offset(__i) + .write(__octets[__i]); + } + 1 + } + Err(_) => 0, + } + } else { + -1 + } == 1) as i32) + != 0) + ); + assert!( + (((((((((((*buf.borrow())[(0) as usize] as i32) == 32) as i32) != 0) + && (((((*buf.borrow())[(1) as usize] as i32) == 1) as i32) != 0)) as i32) + != 0) + && (((((*buf.borrow())[(15) as usize] as i32) == 5) as i32) != 0)) as i32) + != 0) + ); + let text: Value> = Rc::new(RefCell::new( + (0..64).map(|_| ::default()).collect::>(), + )); + let four: Value> = Rc::new(RefCell::new(Box::new([10_u8, 0_u8, 0_u8, 1_u8]))); + assert!( + ((({ + let mut __i: usize = 0; + loop { + let __c1 = { + let __text = if 2 == libc::AF_INET { + let mut __b = [0u8; 4]; + for __i in 0..4 { + __b[__i] = ((four.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .offset(__i) + .read(); + } + Some(std::net::Ipv4Addr::from(__b).to_string()) + } else if 2 == libc::AF_INET6 { + let mut __b = [0u8; 16]; + for __i in 0..16 { + __b[__i] = ((four.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .offset(__i) + .read(); + } + Some(std::net::Ipv6Addr::from(__b).to_string()) + } else { + None + }; + match __text { + Some(__s) + if (__s.len() as u32) < (::std::mem::size_of::<[u8; 64]>() as u32) => + { + for __i in 0..__s.len() { + (text.as_pointer() as Ptr) + .offset(__i) + .write(__s.as_bytes()[__i]); + } + (text.as_pointer() as Ptr).offset(__s.len()).write(0); + (text.as_pointer() as Ptr).clone() + } + _ => Ptr::null(), + } + } + .offset(__i) + .read(); + let __c2 = Ptr::from_string_literal(b"10.0.0.1").offset(__i).read(); + if __c1 != __c2 { + break (__c1 as i32) - (__c2 as i32); + } + if __c1 == 0 { + break 0; + } + __i += 1; + } + } == 0) as i32) + != 0) + ); + let sixteen: Value> = Rc::new(RefCell::new(Box::new([ + 0_u8, + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ]))); + (*sixteen.borrow_mut())[(15) as usize] = 1_u8; + assert!( + ((({ + let mut __i: usize = 0; + loop { + let __c1 = { + let __text = if 10 == libc::AF_INET { + let mut __b = [0u8; 4]; + for __i in 0..4 { + __b[__i] = ((sixteen.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .offset(__i) + .read(); + } + Some(std::net::Ipv4Addr::from(__b).to_string()) + } else if 10 == libc::AF_INET6 { + let mut __b = [0u8; 16]; + for __i in 0..16 { + __b[__i] = ((sixteen.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .offset(__i) + .read(); + } + Some(std::net::Ipv6Addr::from(__b).to_string()) + } else { + None + }; + match __text { + Some(__s) + if (__s.len() as u32) < (::std::mem::size_of::<[u8; 64]>() as u32) => + { + for __i in 0..__s.len() { + (text.as_pointer() as Ptr) + .offset(__i) + .write(__s.as_bytes()[__i]); + } + (text.as_pointer() as Ptr).offset(__s.len()).write(0); + (text.as_pointer() as Ptr).clone() + } + _ => Ptr::null(), + } + } + .offset(__i) + .read(); + let __c2 = Ptr::from_string_literal(b"::1").offset(__i).read(); + if __c1 != __c2 { + break (__c1 as i32) - (__c2 as i32); + } + if __c1 == 0 { + break 0; + } + __i += 1; + } + } == 0) as i32) + != 0) + ); + assert!( + (((({ + let __text = if 2 == libc::AF_INET { + let mut __b = [0u8; 4]; + for __i in 0..4 { + __b[__i] = ((four.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .offset(__i) + .read(); + } + Some(std::net::Ipv4Addr::from(__b).to_string()) + } else if 2 == libc::AF_INET6 { + let mut __b = [0u8; 16]; + for __i in 0..16 { + __b[__i] = ((four.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .offset(__i) + .read(); + } + Some(std::net::Ipv6Addr::from(__b).to_string()) + } else { + None + }; + match __text { + Some(__s) if (__s.len() as u32) < 4_u32 => { + for __i in 0..__s.len() { + (text.as_pointer() as Ptr) + .offset(__i) + .write(__s.as_bytes()[__i]); + } + (text.as_pointer() as Ptr).offset(__s.len()).write(0); + (text.as_pointer() as Ptr).clone() + } + _ => Ptr::null(), + } + }) + .is_null()) as i32) + != 0) + ); + return 0; +} diff --git a/tests/unit/out/unsafe/inet_pton_ntop.rs b/tests/unit/out/unsafe/inet_pton_ntop.rs new file mode 100644 index 00000000..45b8908f --- /dev/null +++ b/tests/unit/out/unsafe/inet_pton_ntop.rs @@ -0,0 +1,174 @@ +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 { + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut buf: [u8; 16] = [0_u8; 16]; + assert!( + (((({ + unsafe extern "C" { + fn inet_pton(af: i32, src: *const libc::c_char, dst: *mut ::libc::c_void) -> i32; + } + inet_pton( + 2, + (c"1.2.3.4".as_ptr().cast_mut()).cast_const(), + (buf.as_mut_ptr() as *mut u8 as *mut ::libc::c_void), + ) + }) == (1)) as i32) + != 0) + ); + assert!( + (((((((((((((buf[(0) as usize] as i32) == (1)) as i32) != 0) + && ((((buf[(1) as usize] as i32) == (2)) as i32) != 0)) as i32) + != 0) + && ((((buf[(2) as usize] as i32) == (3)) as i32) != 0)) as i32) + != 0) + && ((((buf[(3) as usize] as i32) == (4)) as i32) != 0)) as i32) + != 0) + ); + assert!( + (((({ + unsafe extern "C" { + fn inet_pton(af: i32, src: *const libc::c_char, dst: *mut ::libc::c_void) -> i32; + } + inet_pton( + 2, + (c"999.1.1.1".as_ptr().cast_mut()).cast_const(), + (buf.as_mut_ptr() as *mut u8 as *mut ::libc::c_void), + ) + }) == (0)) as i32) + != 0) + ); + assert!( + (((({ + unsafe extern "C" { + fn inet_pton(af: i32, src: *const libc::c_char, dst: *mut ::libc::c_void) -> i32; + } + inet_pton( + 2, + (c"not an ip".as_ptr().cast_mut()).cast_const(), + (buf.as_mut_ptr() as *mut u8 as *mut ::libc::c_void), + ) + }) == (0)) as i32) + != 0) + ); + assert!( + (((({ + unsafe extern "C" { + fn inet_pton(af: i32, src: *const libc::c_char, dst: *mut ::libc::c_void) -> i32; + } + inet_pton( + 10, + (c"::1".as_ptr().cast_mut()).cast_const(), + (buf.as_mut_ptr() as *mut u8 as *mut ::libc::c_void), + ) + }) == (1)) as i32) + != 0) + ); + assert!( + (((((((buf[(0) as usize] as i32) == (0)) as i32) != 0) + && ((((buf[(15) as usize] as i32) == (1)) as i32) != 0)) as i32) + != 0) + ); + assert!( + (((({ + unsafe extern "C" { + fn inet_pton(af: i32, src: *const libc::c_char, dst: *mut ::libc::c_void) -> i32; + } + inet_pton( + 10, + (c"2001:db8::5".as_ptr().cast_mut()).cast_const(), + (buf.as_mut_ptr() as *mut u8 as *mut ::libc::c_void), + ) + }) == (1)) as i32) + != 0) + ); + assert!( + ((((((((((buf[(0) as usize] as i32) == (32)) as i32) != 0) + && ((((buf[(1) as usize] as i32) == (1)) as i32) != 0)) as i32) + != 0) + && ((((buf[(15) as usize] as i32) == (5)) as i32) != 0)) as i32) + != 0) + ); + let mut text: [libc::c_char; 64] = [(0 as libc::c_char); 64]; + let mut four: [u8; 4] = [10_u8, 0_u8, 0_u8, 1_u8]; + assert!( + ((((libc::strcmp( + { + unsafe extern "C" { + fn inet_ntop( + af: i32, + src: *const ::libc::c_void, + dst: *mut libc::c_char, + size: u32, + ) -> *const libc::c_char; + } + inet_ntop( + 2, + (four.as_mut_ptr() as *const u8 as *const ::libc::c_void), + text.as_mut_ptr(), + (::std::mem::size_of::<[libc::c_char; 64]>() as u32), + ) + }, + (c"10.0.0.1".as_ptr().cast_mut()).cast_const() + )) == (0)) as i32) + != 0) + ); + let mut sixteen: [u8; 16] = [ + 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, 0_u8, + 0_u8, + ]; + sixteen[(15) as usize] = 1_u8; + assert!( + ((((libc::strcmp( + { + unsafe extern "C" { + fn inet_ntop( + af: i32, + src: *const ::libc::c_void, + dst: *mut libc::c_char, + size: u32, + ) -> *const libc::c_char; + } + inet_ntop( + 10, + (sixteen.as_mut_ptr() as *const u8 as *const ::libc::c_void), + text.as_mut_ptr(), + (::std::mem::size_of::<[libc::c_char; 64]>() as u32), + ) + }, + (c"::1".as_ptr().cast_mut()).cast_const() + )) == (0)) as i32) + != 0) + ); + assert!( + (((({ + unsafe extern "C" { + fn inet_ntop( + af: i32, + src: *const ::libc::c_void, + dst: *mut libc::c_char, + size: u32, + ) -> *const libc::c_char; + } + inet_ntop( + 2, + (four.as_mut_ptr() as *const u8 as *const ::libc::c_void), + text.as_mut_ptr(), + 4_u32, + ) + }) + .is_null()) as i32) + != 0) + ); + return 0; +} From f6b6eeb641e9ba1a1bf022f53dd5aeaf8cc43c9c Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 7 Jul 2026 14:46:51 +0100 Subject: [PATCH 02/15] Add safe rules for cstring --- .../converter/models/converter_refcount.cpp | 3 + rule-preprocessor/src/semantic.rs | 4 +- rules/cstring/tgt_refcount.rs | 233 +++- tests/unit/out/refcount/string_h.rs | 1193 +++++++++++++++++ tests/unit/out/refcount/va_arg_printf.rs | 17 +- tests/unit/out/unsafe/strdup.rs | 44 + tests/unit/out/unsafe/string_h.rs | 56 +- tests/unit/strdup.c | 22 + tests/unit/string_h.c | 25 +- 9 files changed, 1530 insertions(+), 67 deletions(-) create mode 100644 tests/unit/out/refcount/string_h.rs create mode 100644 tests/unit/out/unsafe/strdup.rs create mode 100644 tests/unit/strdup.c diff --git a/cpp2rust/converter/models/converter_refcount.cpp b/cpp2rust/converter/models/converter_refcount.cpp index 483c7fdb..f2d36eb2 100644 --- a/cpp2rust/converter/models/converter_refcount.cpp +++ b/cpp2rust/converter/models/converter_refcount.cpp @@ -1161,6 +1161,9 @@ bool ConverterRefCount::VisitImplicitCastExpr(clang::ImplicitCastExpr *expr) { if (expr->getCastKind() == clang::CastKind::CK_BitCast) { if (expr->getType()->isVoidPointerType()) { + if (sub_expr->getType()->isVoidPointerType()) { + return Convert(sub_expr); + } PushConversionKind push(*this, ConversionKind::Unboxed); if (sub_expr->getType()->isPointerType() && sub_expr->getType()->getPointeeType()->isArrayType()) { diff --git a/rule-preprocessor/src/semantic.rs b/rule-preprocessor/src/semantic.rs index 77a12f5c..3fc04c3f 100644 --- a/rule-preprocessor/src/semantic.rs +++ b/rule-preprocessor/src/semantic.rs @@ -399,10 +399,10 @@ impl<'a, 'tcx> AstVisitor<'a, 'tcx> { rustc_hir::ExprKind::Lit(_) | rustc_hir::ExprKind::Path(_) | rustc_hir::ExprKind::Ret(None) - | rustc_hir::ExprKind::Break(_, _) + | rustc_hir::ExprKind::Break(_, None) | rustc_hir::ExprKind::Continue(_) => {} - rustc_hir::ExprKind::Ret(Some(e)) => { + rustc_hir::ExprKind::Ret(Some(e)) | rustc_hir::ExprKind::Break(_, Some(e)) => { self.visit_expr(e, Access::Read); } diff --git a/rules/cstring/tgt_refcount.rs b/rules/cstring/tgt_refcount.rs index 5d2eb6c4..6098e12e 100644 --- a/rules/cstring/tgt_refcount.rs +++ b/rules/cstring/tgt_refcount.rs @@ -18,14 +18,243 @@ fn f3(a0: AnyPtr, a1: AnyPtr, a2: usize) -> i32 { } fn f4(a0: AnyPtr, a1: AnyPtr, a2: usize) -> AnyPtr { - a0.memcpy(&a1, a2 as usize); + let tmp: Vec = (0..a2) + .map(|i| a1.reinterpret_cast::().offset(i).read()) + .collect(); + for i in 0..a2 { + a0.reinterpret_cast::().offset(i).write(tmp[i]); + } a0.clone() } +fn f5(a0: Ptr, a1: i32) -> Ptr { + let mut i: usize = 0; + loop { + let c = a0.offset(i).read(); + if c == a1 as u8 { + break a0.offset(i); + } + if c == 0 { + break Ptr::null(); + } + i += 1; + } +} + unsafe fn f7(a0: Ptr) -> usize { - a0.to_string_iterator().count() + let mut i: usize = 0; + while a0.offset(i).read() != 0 { + i += 1; + } + i +} + +fn f8(a0: Ptr, a1: Ptr) -> i32 { + let mut i: usize = 0; + loop { + let c1 = a0.offset(i).read(); + let c2 = a1.offset(i).read(); + if c1 != c2 { + break (c1 as i32) - (c2 as i32); + } + if c1 == 0 { + break 0; + } + i += 1; + } +} + +fn f9(a0: Ptr, a1: Ptr, a2: usize) -> i32 { + let mut i: usize = 0; + loop { + if i == a2 { + break 0; + } + let c1 = a0.offset(i).read(); + let c2 = a1.offset(i).read(); + if c1 != c2 { + break (c1 as i32) - (c2 as i32); + } + if c1 == 0 { + break 0; + } + i += 1; + } +} + +fn f10(a0: AnyPtr, a1: i32, a2: usize) -> AnyPtr { + match (0..a2).find(|&i| a0.reinterpret_cast::().offset(i).read() == a1 as u8) { + Some(i) => a0.reinterpret_cast::().offset(i).to_any(), + None => Ptr::::null().to_any(), + } +} + +fn f11(a0: Ptr, a1: i32) -> Ptr { + let mut i: usize = 0; + let mut found = Ptr::null(); + loop { + let c = a0.offset(i).read(); + if c == a1 as u8 { + found = a0.offset(i); + } + if c == 0 { + break found; + } + i += 1; + } } fn f15(a0: Ptr) -> Ptr { libcc2rs::strdup_refcount(a0) } + +fn f16(a0: Ptr, a1: Ptr) -> usize { + let mut i: usize = 0; + loop { + let c = a0.offset(i).read(); + if c == 0 { + break i; + } + let mut j: usize = 0; + let hit = loop { + let r = a1.offset(j).read(); + if r == 0 { + break false; + } + if r == c { + break true; + } + j += 1; + }; + if hit { + break i; + } + i += 1; + } +} + +fn f17(a0: Ptr, a1: Ptr) -> usize { + let mut i: usize = 0; + loop { + let c = a0.offset(i).read(); + if c == 0 { + break i; + } + let mut j: usize = 0; + let hit = loop { + let r = a1.offset(j).read(); + if r == 0 { + break false; + } + if r == c { + break true; + } + j += 1; + }; + if !hit { + break i; + } + i += 1; + } +} + +fn f18(a0: Ptr, a1: Ptr) -> Ptr { + let mut s: usize = 0; + loop { + let mut i: usize = 0; + let matched = loop { + let n = a1.offset(i).read(); + if n == 0 { + break true; + } + if a0.offset(s + i).read() != n { + break false; + } + i += 1; + }; + if matched { + break a0.offset(s); + } + if a0.offset(s).read() == 0 { + break Ptr::null(); + } + s += 1; + } +} + +fn f21(a0: Ptr, a1: Ptr) -> Ptr { + let mut i: usize = 0; + loop { + let c = a0.offset(i).read(); + if c == 0 { + break Ptr::null(); + } + let mut j: usize = 0; + let hit = loop { + let r = a1.offset(j).read(); + if r == 0 { + break false; + } + if r == c { + break true; + } + j += 1; + }; + if hit { + break a0.offset(i); + } + i += 1; + } +} + +#[cfg(target_os = "linux")] +fn f24(a0: AnyPtr, a1: i32, a2: usize) -> AnyPtr { + match (0..a2) + .rev() + .find(|&i| a0.reinterpret_cast::().offset(i).read() == a1 as u8) + { + Some(i) => a0.reinterpret_cast::().offset(i).to_any(), + None => Ptr::::null().to_any(), + } +} + +fn f27(a0: Ptr, a1: Ptr) -> i32 { + let mut i: usize = 0; + loop { + let c1 = a0.offset(i).read().to_ascii_lowercase(); + let c2 = a1.offset(i).read().to_ascii_lowercase(); + if c1 != c2 { + break (c1 as i32) - (c2 as i32); + } + if c1 == 0 { + break 0; + } + i += 1; + } +} + +#[cfg(target_os = "linux")] +fn f28(a0: i32, a1: Ptr, a2: usize) -> Ptr { + let msg = std::io::Error::from_raw_os_error(a0).to_string(); + let len = msg.len().min(a2.saturating_sub(1)); + for i in 0..len { + a1.offset(i).write(msg.as_bytes()[i]); + } + if a2 > 0 { + a1.offset(len).write(0); + } + a1 +} + +#[cfg(target_os = "macos")] +fn f28(a0: i32, a1: Ptr, a2: usize) -> i32 { + let msg = std::io::Error::from_raw_os_error(a0).to_string(); + let len = msg.len().min(a2.saturating_sub(1)); + for i in 0..len { + a1.offset(i).write(msg.as_bytes()[i]); + } + if a2 > 0 { + a1.offset(len).write(0); + } + 0 +} diff --git a/tests/unit/out/refcount/string_h.rs b/tests/unit/out/refcount/string_h.rs new file mode 100644 index 00000000..4f2a680e --- /dev/null +++ b/tests/unit/out/refcount/string_h.rs @@ -0,0 +1,1193 @@ +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 test_memcpy_0() { + let src: Value> = Rc::new(RefCell::new(Box::from(*b"hello\0"))); + let dst: Value> = Rc::new(RefCell::new(Box::new([ + 0_u8, + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ]))); + let r: Value = Rc::new(RefCell::new({ + ((dst.as_pointer() as Ptr) as Ptr).to_any().memcpy( + &((src.as_pointer() as Ptr) as Ptr).to_any(), + 6_usize as usize, + ); + ((dst.as_pointer() as Ptr) as Ptr).to_any().clone() + })); + assert!( + ((({ + let _lhs = (*r.borrow()).clone(); + _lhs == ((dst.as_pointer() as Ptr) as Ptr).to_any() + }) as i32) + != 0) + ); + assert!( + (((((((((((*dst.borrow())[(0) as usize] as i32) == ('h' as i32)) as i32) != 0) + && (((((*dst.borrow())[(1) as usize] as i32) == ('e' as i32)) as i32) != 0)) + as i32) + != 0) + && (((((*dst.borrow())[(2) as usize] as i32) == ('l' as i32)) as i32) != 0)) + as i32) + != 0) + ); + assert!( + (((((((((((*dst.borrow())[(3) as usize] as i32) == ('l' as i32)) as i32) != 0) + && (((((*dst.borrow())[(4) as usize] as i32) == ('o' as i32)) as i32) != 0)) + as i32) + != 0) + && (((((*dst.borrow())[(5) as usize] as i32) == ('\0' as i32)) as i32) != 0)) + as i32) + != 0) + ); +} +pub fn test_memset_1() { + let buf: Value> = Rc::new(RefCell::new( + (0..4).map(|_| ::default()).collect::>(), + )); + let r: Value = Rc::new(RefCell::new({ + ((buf.as_pointer() as Ptr) as Ptr) + .to_any() + .memset(('x' as i32) as u8, 4_usize as usize); + ((buf.as_pointer() as Ptr) as Ptr).to_any().clone() + })); + assert!( + ((({ + let _lhs = (*r.borrow()).clone(); + _lhs == ((buf.as_pointer() as Ptr) as Ptr).to_any() + }) as i32) + != 0) + ); + assert!( + ((((((((((((((*buf.borrow())[(0) as usize] as i32) == ('x' as i32)) as i32) != 0) + && (((((*buf.borrow())[(1) as usize] as i32) == ('x' as i32)) as i32) != 0)) + as i32) + != 0) + && (((((*buf.borrow())[(2) as usize] as i32) == ('x' as i32)) as i32) != 0)) + as i32) + != 0) + && (((((*buf.borrow())[(3) as usize] as i32) == ('x' as i32)) as i32) != 0)) + as i32) + != 0) + ); +} +pub fn test_memcmp_2() { + let a: Value> = Rc::new(RefCell::new(Box::new([1_u8, 2_u8, 3_u8, 4_u8]))); + let b: Value> = Rc::new(RefCell::new(Box::new([1_u8, 2_u8, 3_u8, 4_u8]))); + let c: Value> = Rc::new(RefCell::new(Box::new([1_u8, 2_u8, 9_u8, 4_u8]))); + assert!( + (((((a.as_pointer() as Ptr::) as Ptr::) + .to_any() + .memcmp( + &((b.as_pointer() as Ptr::) as Ptr::).to_any(), + 4_usize + ) + == 0) as i32) + != 0) + ); + assert!( + (((((a.as_pointer() as Ptr::) as Ptr::) + .to_any() + .memcmp( + &((c.as_pointer() as Ptr::) as Ptr::).to_any(), + 4_usize + ) + < 0) as i32) + != 0) + ); + assert!( + (((((c.as_pointer() as Ptr::) as Ptr::) + .to_any() + .memcmp( + &((a.as_pointer() as Ptr::) as Ptr::).to_any(), + 4_usize + ) + > 0) as i32) + != 0) + ); +} +pub fn test_memmove_3() { + let buf: Value> = Rc::new(RefCell::new(Box::new([ + (('a' as i32) as u8), + (('b' as i32) as u8), + (('c' as i32) as u8), + (('d' as i32) as u8), + (('e' as i32) as u8), + (('\0' as i32) as u8), + ]))); + let r: Value = Rc::new(RefCell::new({ + let tmp: Vec = (0..4_usize) + .map(|i| { + ((buf.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .offset(i) + .read() + }) + .collect(); + for i in 0..4_usize { + ((buf.as_pointer() as Ptr).offset((1) as isize) as Ptr) + .to_any() + .reinterpret_cast::() + .offset(i) + .write(tmp[i]); + } + ((buf.as_pointer() as Ptr).offset((1) as isize) as Ptr) + .to_any() + .clone() + })); + assert!( + ((({ + let _lhs = (*r.borrow()).clone(); + _lhs == ((buf.as_pointer() as Ptr).offset((1) as isize) as Ptr).to_any() + }) as i32) + != 0) + ); + assert!( + (((((((((((*buf.borrow())[(0) as usize] as i32) == ('a' as i32)) as i32) != 0) + && (((((*buf.borrow())[(1) as usize] as i32) == ('a' as i32)) as i32) != 0)) + as i32) + != 0) + && (((((*buf.borrow())[(2) as usize] as i32) == ('b' as i32)) as i32) != 0)) + as i32) + != 0) + ); + assert!( + (((((((((((*buf.borrow())[(3) as usize] as i32) == ('c' as i32)) as i32) != 0) + && (((((*buf.borrow())[(4) as usize] as i32) == ('d' as i32)) as i32) != 0)) + as i32) + != 0) + && (((((*buf.borrow())[(5) as usize] as i32) == ('\0' as i32)) as i32) != 0)) + as i32) + != 0) + ); +} +pub fn test_strchr_4() { + let s: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"hello world"))); + let r: Value> = Rc::new(RefCell::new({ + let mut i: usize = 0; + loop { + let c = (*s.borrow()).reinterpret_cast::().offset(i).read(); + if c == ('w' as i32) as u8 { + break (*s.borrow()).reinterpret_cast::().offset(i); + } + if c == 0 { + break Ptr::null(); + } + i += 1; + } + })); + assert!((((!((*r.borrow()).is_null())) as i32) != 0)); + assert!(((((((*r.borrow()).read()) as i32) == ('w' as i32)) as i32) != 0)); + assert!( + (((({ + let mut i: usize = 0; + loop { + let c = (*s.borrow()).reinterpret_cast::().offset(i).read(); + if c == ('z' as i32) as u8 { + break (*s.borrow()).reinterpret_cast::().offset(i); + } + if c == 0 { + break Ptr::null(); + } + i += 1; + } + }) + .is_null()) as i32) + != 0) + ); +} +pub fn test_strlen_5() { + assert!( + ((({ + let mut i: usize = 0; + while Ptr::from_string_literal(b"").offset(i).read() != 0 { + i += 1; + } + i + } == 0_usize) as i32) + != 0) + ); + assert!( + ((({ + let mut i: usize = 0; + while Ptr::from_string_literal(b"hello").offset(i).read() != 0 { + i += 1; + } + i + } == 5_usize) as i32) + != 0) + ); + assert!( + ((({ + let mut i: usize = 0; + while Ptr::from_string_literal(b"hello world").offset(i).read() != 0 { + i += 1; + } + i + } == 11_usize) as i32) + != 0) + ); + let buf: Value> = Rc::new(RefCell::new(Box::from(*b"one\0two\0"))); + let first: Value> = Rc::new(RefCell::new((buf.as_pointer() as Ptr))); + let second: Value> = Rc::new(RefCell::new( + ((buf.as_pointer() as Ptr).offset( + ({ + let mut i: usize = 0; + while (*first.borrow()).offset(i).read() != 0 { + i += 1; + } + i + }) + .wrapping_add(1_usize), + )), + )); + assert!( + ((({ + let mut i: usize = 0; + loop { + let c1 = (*second.borrow()).offset(i).read(); + let c2 = Ptr::from_string_literal(b"two").offset(i).read(); + if c1 != c2 { + break (c1 as i32) - (c2 as i32); + } + if c1 == 0 { + break 0; + } + i += 1; + } + } == 0) as i32) + != 0) + ); + let big : Value > = Rc::new(RefCell::new(Box::from(*b"hi\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0") )) ; + assert!( + ((({ + let mut i: usize = 0; + while (big.as_pointer() as Ptr).offset(i).read() != 0 { + i += 1; + } + i + } == 2_usize) as i32) + != 0) + ); + (*big.borrow_mut())[(2) as usize] = (('x' as i32) as u8); + (*big.borrow_mut())[(3) as usize] = (('\0' as i32) as u8); + assert!( + ((({ + let mut i: usize = 0; + while (big.as_pointer() as Ptr).offset(i).read() != 0 { + i += 1; + } + i + } == 3_usize) as i32) + != 0) + ); +} +pub fn test_strcmp_6() { + assert!( + ((({ + let mut i: usize = 0; + loop { + let c1 = Ptr::from_string_literal(b"abc").offset(i).read(); + let c2 = Ptr::from_string_literal(b"abc").offset(i).read(); + if c1 != c2 { + break (c1 as i32) - (c2 as i32); + } + if c1 == 0 { + break 0; + } + i += 1; + } + } == 0) as i32) + != 0) + ); + assert!( + ((({ + let mut i: usize = 0; + loop { + let c1 = Ptr::from_string_literal(b"abc").offset(i).read(); + let c2 = Ptr::from_string_literal(b"abd").offset(i).read(); + if c1 != c2 { + break (c1 as i32) - (c2 as i32); + } + if c1 == 0 { + break 0; + } + i += 1; + } + } < 0) as i32) + != 0) + ); + assert!( + ((({ + let mut i: usize = 0; + loop { + let c1 = Ptr::from_string_literal(b"abd").offset(i).read(); + let c2 = Ptr::from_string_literal(b"abc").offset(i).read(); + if c1 != c2 { + break (c1 as i32) - (c2 as i32); + } + if c1 == 0 { + break 0; + } + i += 1; + } + } > 0) as i32) + != 0) + ); + let p: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"abc"))); + let q: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"abd"))); + let buf: Value> = Rc::new(RefCell::new(Box::new([ + (('a' as i32) as u8), + (('b' as i32) as u8), + (('c' as i32) as u8), + (('\0' as i32) as u8), + ]))); + assert!( + ((({ + let mut i: usize = 0; + loop { + let c1 = (*p.borrow()).offset(i).read(); + let c2 = (*p.borrow()).offset(i).read(); + if c1 != c2 { + break (c1 as i32) - (c2 as i32); + } + if c1 == 0 { + break 0; + } + i += 1; + } + } == 0) as i32) + != 0) + ); + assert!( + ((({ + let mut i: usize = 0; + loop { + let c1 = (*p.borrow()).offset(i).read(); + let c2 = (*q.borrow()).offset(i).read(); + if c1 != c2 { + break (c1 as i32) - (c2 as i32); + } + if c1 == 0 { + break 0; + } + i += 1; + } + } < 0) as i32) + != 0) + ); + assert!( + ((({ + let mut i: usize = 0; + loop { + let c1 = (buf.as_pointer() as Ptr).offset(i).read(); + let c2 = (*p.borrow()).offset(i).read(); + if c1 != c2 { + break (c1 as i32) - (c2 as i32); + } + if c1 == 0 { + break 0; + } + i += 1; + } + } == 0) as i32) + != 0) + ); +} +pub fn test_strncmp_7() { + assert!( + ((({ + let mut i: usize = 0; + loop { + if i == 3_usize { + break 0; + } + let c1 = Ptr::from_string_literal(b"abcdef").offset(i).read(); + let c2 = Ptr::from_string_literal(b"abcxyz").offset(i).read(); + if c1 != c2 { + break (c1 as i32) - (c2 as i32); + } + if c1 == 0 { + break 0; + } + i += 1; + } + } == 0) as i32) + != 0) + ); + assert!( + ((({ + let mut i: usize = 0; + loop { + if i == 4_usize { + break 0; + } + let c1 = Ptr::from_string_literal(b"abcdef").offset(i).read(); + let c2 = Ptr::from_string_literal(b"abcxyz").offset(i).read(); + if c1 != c2 { + break (c1 as i32) - (c2 as i32); + } + if c1 == 0 { + break 0; + } + i += 1; + } + } < 0) as i32) + != 0) + ); + assert!( + ((({ + let mut i: usize = 0; + loop { + if i == 4_usize { + break 0; + } + let c1 = Ptr::from_string_literal(b"abcxyz").offset(i).read(); + let c2 = Ptr::from_string_literal(b"abcdef").offset(i).read(); + if c1 != c2 { + break (c1 as i32) - (c2 as i32); + } + if c1 == 0 { + break 0; + } + i += 1; + } + } > 0) as i32) + != 0) + ); + let p: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"abcdef"))); + let q: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"abcxyz"))); + let buf: Value> = Rc::new(RefCell::new(Box::new([ + (('a' as i32) as u8), + (('b' as i32) as u8), + (('c' as i32) as u8), + (('d' as i32) as u8), + (('e' as i32) as u8), + (('f' as i32) as u8), + (('\0' as i32) as u8), + ]))); + let n: Value = Rc::new(RefCell::new(3_usize)); + assert!( + ((({ + let mut i: usize = 0; + loop { + if i == (*n.borrow()) { + break 0; + } + let c1 = (*p.borrow()).offset(i).read(); + let c2 = (*q.borrow()).offset(i).read(); + if c1 != c2 { + break (c1 as i32) - (c2 as i32); + } + if c1 == 0 { + break 0; + } + i += 1; + } + } == 0) as i32) + != 0) + ); + assert!( + ((({ + let mut i: usize = 0; + loop { + if i == (*n.borrow()).wrapping_add(1_usize) { + break 0; + } + let c1 = (*p.borrow()).offset(i).read(); + let c2 = (*q.borrow()).offset(i).read(); + if c1 != c2 { + break (c1 as i32) - (c2 as i32); + } + if c1 == 0 { + break 0; + } + i += 1; + } + } < 0) as i32) + != 0) + ); + assert!( + ((({ + let mut i: usize = 0; + loop { + if i == 6_usize { + break 0; + } + let c1 = (buf.as_pointer() as Ptr).offset(i).read(); + let c2 = (*p.borrow()).offset(i).read(); + if c1 != c2 { + break (c1 as i32) - (c2 as i32); + } + if c1 == 0 { + break 0; + } + i += 1; + } + } == 0) as i32) + != 0) + ); +} +pub fn test_memchr_8() { + let data: Value> = Rc::new(RefCell::new(Box::new([16_u8, 32_u8, 48_u8, 64_u8]))); + let r: Value = Rc::new(RefCell::new( + match (0..4_usize).find(|&i| { + ((data.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .offset(i) + .read() + == 48 as u8 + }) { + Some(i) => ((data.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .offset(i) + .to_any(), + None => Ptr::::null().to_any(), + }, + )); + assert!( + ((({ + let _lhs = (*r.borrow()).clone(); + _lhs == (((data.as_pointer() as Ptr).offset(2)) as Ptr).to_any() + }) as i32) + != 0) + ); + assert!( + ((((match (0..4_usize).find(|&i| ((data.as_pointer() as Ptr::) as Ptr::) + .to_any() + .reinterpret_cast::() + .offset(i) + .read() + == 153 as u8) + { + Some(i) => ((data.as_pointer() as Ptr::) as Ptr::) + .to_any() + .reinterpret_cast::() + .offset(i) + .to_any(), + None => Ptr::::null().to_any(), + }) + .is_null()) as i32) + != 0) + ); + let p: Value = Rc::new(RefCell::new( + ((data.as_pointer() as Ptr) as Ptr).to_any(), + )); + let n: Value = Rc::new(RefCell::new(4_usize)); + assert!( + ((({ + let _lhs = match (0..(*n.borrow())) + .find(|&i| (*p.borrow()).reinterpret_cast::().offset(i).read() == 16 as u8) + { + Some(i) => (*p.borrow()).reinterpret_cast::().offset(i).to_any(), + None => Ptr::::null().to_any(), + }; + _lhs == (*p.borrow()).clone() + }) as i32) + != 0) + ); +} +pub fn test_strrchr_9() { + let s: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"hello world"))); + let r: Value> = Rc::new(RefCell::new({ + let mut i: usize = 0; + let mut found = Ptr::null(); + loop { + let c = (*s.borrow()).reinterpret_cast::().offset(i).read(); + if c == ('l' as i32) as u8 { + found = (*s.borrow()).reinterpret_cast::().offset(i); + } + if c == 0 { + break found; + } + i += 1; + } + })); + assert!((((!((*r.borrow()).is_null())) as i32) != 0)); + assert!(((((((*r.borrow()).read()) as i32) == ('l' as i32)) as i32) != 0)); + assert!( + ((({ + let _lhs = (*r.borrow()).clone(); + _lhs == (*s.borrow()).offset((9) as isize) + }) as i32) + != 0) + ); + assert!( + (((({ + let mut i: usize = 0; + let mut found = Ptr::null(); + loop { + let c = (*s.borrow()).reinterpret_cast::().offset(i).read(); + if c == ('z' as i32) as u8 { + found = (*s.borrow()).reinterpret_cast::().offset(i); + } + if c == 0 { + break found; + } + i += 1; + } + }) + .is_null()) as i32) + != 0) + ); + let buf: Value> = Rc::new(RefCell::new(Box::new([ + (('a' as i32) as u8), + (('b' as i32) as u8), + (('a' as i32) as u8), + (('\0' as i32) as u8), + ]))); + assert!( + ((({ + let mut i: usize = 0; + let mut found = Ptr::null(); + loop { + let c = (buf.as_pointer() as Ptr).offset(i).read(); + if c == ('a' as i32) as u8 { + found = (buf.as_pointer() as Ptr).offset(i); + } + if c == 0 { + break found; + } + i += 1; + } + } == ((buf.as_pointer() as Ptr).offset(2))) as i32) + != 0) + ); +} +pub fn test_strcspn_10() { + assert!( + ((({ + let mut i: usize = 0; + loop { + let c = Ptr::from_string_literal(b"hello").offset(i).read(); + if c == 0 { + break i; + } + let mut j: usize = 0; + let hit = loop { + let r = Ptr::from_string_literal(b"el").offset(j).read(); + if r == 0 { + break false; + } + if r == c { + break true; + } + j += 1; + }; + if hit { + break i; + } + i += 1; + } + } == 1_usize) as i32) + != 0) + ); + assert!( + ((({ + let mut i: usize = 0; + loop { + let c = Ptr::from_string_literal(b"abc").offset(i).read(); + if c == 0 { + break i; + } + let mut j: usize = 0; + let hit = loop { + let r = Ptr::from_string_literal(b"xyz").offset(j).read(); + if r == 0 { + break false; + } + if r == c { + break true; + } + j += 1; + }; + if hit { + break i; + } + i += 1; + } + } == 3_usize) as i32) + != 0) + ); + assert!( + ((({ + let mut i: usize = 0; + loop { + let c = Ptr::from_string_literal(b"").offset(i).read(); + if c == 0 { + break i; + } + let mut j: usize = 0; + let hit = loop { + let r = Ptr::from_string_literal(b"abc").offset(j).read(); + if r == 0 { + break false; + } + if r == c { + break true; + } + j += 1; + }; + if hit { + break i; + } + i += 1; + } + } == 0_usize) as i32) + != 0) + ); + let s: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"hello"))); + let rej: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"el"))); + assert!( + ((({ + let mut i: usize = 0; + loop { + let c = (*s.borrow()).offset(i).read(); + if c == 0 { + break i; + } + let mut j: usize = 0; + let hit = loop { + let r = (*rej.borrow()).offset(j).read(); + if r == 0 { + break false; + } + if r == c { + break true; + } + j += 1; + }; + if hit { + break i; + } + i += 1; + } + } == 1_usize) as i32) + != 0) + ); +} +pub fn test_strspn_11() { + assert!( + ((({ + let mut i: usize = 0; + loop { + let c = Ptr::from_string_literal(b"hello").offset(i).read(); + if c == 0 { + break i; + } + let mut j: usize = 0; + let hit = loop { + let r = Ptr::from_string_literal(b"hel").offset(j).read(); + if r == 0 { + break false; + } + if r == c { + break true; + } + j += 1; + }; + if !hit { + break i; + } + i += 1; + } + } == 4_usize) as i32) + != 0) + ); + assert!( + ((({ + let mut i: usize = 0; + loop { + let c = Ptr::from_string_literal(b"abc").offset(i).read(); + if c == 0 { + break i; + } + let mut j: usize = 0; + let hit = loop { + let r = Ptr::from_string_literal(b"xyz").offset(j).read(); + if r == 0 { + break false; + } + if r == c { + break true; + } + j += 1; + }; + if !hit { + break i; + } + i += 1; + } + } == 0_usize) as i32) + != 0) + ); + assert!( + ((({ + let mut i: usize = 0; + loop { + let c = Ptr::from_string_literal(b"aaa").offset(i).read(); + if c == 0 { + break i; + } + let mut j: usize = 0; + let hit = loop { + let r = Ptr::from_string_literal(b"a").offset(j).read(); + if r == 0 { + break false; + } + if r == c { + break true; + } + j += 1; + }; + if !hit { + break i; + } + i += 1; + } + } == 3_usize) as i32) + != 0) + ); + let s: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"hello"))); + let acc: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"hel"))); + assert!( + ((({ + let mut i: usize = 0; + loop { + let c = (*s.borrow()).offset(i).read(); + if c == 0 { + break i; + } + let mut j: usize = 0; + let hit = loop { + let r = (*acc.borrow()).offset(j).read(); + if r == 0 { + break false; + } + if r == c { + break true; + } + j += 1; + }; + if !hit { + break i; + } + i += 1; + } + } == 4_usize) as i32) + != 0) + ); +} +pub fn test_strstr_12() { + let h: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"hello world"))); + let r: Value> = Rc::new(RefCell::new({ + let mut s: usize = 0; + loop { + let mut i: usize = 0; + let matched = loop { + let n = Ptr::from_string_literal(b"world").offset(i).read(); + if n == 0 { + break true; + } + if (*h.borrow()).reinterpret_cast::().offset(s + i).read() != n { + break false; + } + i += 1; + }; + if matched { + break (*h.borrow()).reinterpret_cast::().offset(s); + } + if (*h.borrow()).reinterpret_cast::().offset(s).read() == 0 { + break Ptr::null(); + } + s += 1; + } + })); + assert!((((!((*r.borrow()).is_null())) as i32) != 0)); + assert!( + ((({ + let _lhs = (*r.borrow()).clone(); + _lhs == (*h.borrow()).offset((6) as isize) + }) as i32) + != 0) + ); + assert!( + (((({ + let mut s: usize = 0; + loop { + let mut i: usize = 0; + let matched = loop { + let n = Ptr::from_string_literal(b"xyz").offset(i).read(); + if n == 0 { + break true; + } + if (*h.borrow()).reinterpret_cast::().offset(s + i).read() != n { + break false; + } + i += 1; + }; + if matched { + break (*h.borrow()).reinterpret_cast::().offset(s); + } + if (*h.borrow()).reinterpret_cast::().offset(s).read() == 0 { + break Ptr::null(); + } + s += 1; + } + }) + .is_null()) as i32) + != 0) + ); + let buf: Value> = Rc::new(RefCell::new(Box::new([ + (('h' as i32) as u8), + (('e' as i32) as u8), + (('l' as i32) as u8), + (('l' as i32) as u8), + (('o' as i32) as u8), + (('\0' as i32) as u8), + ]))); + assert!( + ((({ + let mut s: usize = 0; + loop { + let mut i: usize = 0; + let matched = loop { + let n = Ptr::from_string_literal(b"ll").offset(i).read(); + if n == 0 { + break true; + } + if (buf.as_pointer() as Ptr).offset(s + i).read() != n { + break false; + } + i += 1; + }; + if matched { + break (buf.as_pointer() as Ptr).offset(s); + } + if (buf.as_pointer() as Ptr).offset(s).read() == 0 { + break Ptr::null(); + } + s += 1; + } + } == ((buf.as_pointer() as Ptr).offset(2))) as i32) + != 0) + ); +} +pub fn test_strpbrk_13() { + let s: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"hello world"))); + let r: Value> = Rc::new(RefCell::new({ + let mut i: usize = 0; + loop { + let c = (*s.borrow()).reinterpret_cast::().offset(i).read(); + if c == 0 { + break Ptr::null(); + } + let mut j: usize = 0; + let hit = loop { + let r = Ptr::from_string_literal(b"wo").offset(j).read(); + if r == 0 { + break false; + } + if r == c { + break true; + } + j += 1; + }; + if hit { + break (*s.borrow()).reinterpret_cast::().offset(i); + } + i += 1; + } + })); + assert!((((!((*r.borrow()).is_null())) as i32) != 0)); + assert!( + ((({ + let _lhs = (*r.borrow()).clone(); + _lhs == (*s.borrow()).offset((4) as isize) + }) as i32) + != 0) + ); + assert!( + (((({ + let mut i: usize = 0; + loop { + let c = (*s.borrow()).reinterpret_cast::().offset(i).read(); + if c == 0 { + break Ptr::null(); + } + let mut j: usize = 0; + let hit = loop { + let r = Ptr::from_string_literal(b"xyz").offset(j).read(); + if r == 0 { + break false; + } + if r == c { + break true; + } + j += 1; + }; + if hit { + break (*s.borrow()).reinterpret_cast::().offset(i); + } + i += 1; + } + }) + .is_null()) as i32) + != 0) + ); + let buf: Value> = Rc::new(RefCell::new(Box::new([ + (('a' as i32) as u8), + (('b' as i32) as u8), + (('c' as i32) as u8), + (('\0' as i32) as u8), + ]))); + assert!( + ((({ + let mut i: usize = 0; + loop { + let c = (buf.as_pointer() as Ptr).offset(i).read(); + if c == 0 { + break Ptr::null(); + } + let mut j: usize = 0; + let hit = loop { + let r = Ptr::from_string_literal(b"b").offset(j).read(); + if r == 0 { + break false; + } + if r == c { + break true; + } + j += 1; + }; + if hit { + break (buf.as_pointer() as Ptr).offset(i); + } + i += 1; + } + } == ((buf.as_pointer() as Ptr).offset(1))) as i32) + != 0) + ); +} +pub fn test_strcasecmp_14() { + assert!( + ((({ + let mut i: usize = 0; + loop { + let c1 = Ptr::from_string_literal(b"HELLO") + .offset(i) + .read() + .to_ascii_lowercase(); + let c2 = Ptr::from_string_literal(b"hello") + .offset(i) + .read() + .to_ascii_lowercase(); + if c1 != c2 { + break (c1 as i32) - (c2 as i32); + } + if c1 == 0 { + break 0; + } + i += 1; + } + } == 0) as i32) + != 0) + ); + assert!( + ((({ + let mut i: usize = 0; + loop { + let c1 = Ptr::from_string_literal(b"abc") + .offset(i) + .read() + .to_ascii_lowercase(); + let c2 = Ptr::from_string_literal(b"abd") + .offset(i) + .read() + .to_ascii_lowercase(); + if c1 != c2 { + break (c1 as i32) - (c2 as i32); + } + if c1 == 0 { + break 0; + } + i += 1; + } + } < 0) as i32) + != 0) + ); + assert!( + ((({ + let mut i: usize = 0; + loop { + let c1 = Ptr::from_string_literal(b"abd") + .offset(i) + .read() + .to_ascii_lowercase(); + let c2 = Ptr::from_string_literal(b"abc") + .offset(i) + .read() + .to_ascii_lowercase(); + if c1 != c2 { + break (c1 as i32) - (c2 as i32); + } + if c1 == 0 { + break 0; + } + i += 1; + } + } > 0) as i32) + != 0) + ); + let p: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"FOO"))); + let q: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"foo"))); + assert!( + ((({ + let mut i: usize = 0; + loop { + let c1 = (*p.borrow()).offset(i).read().to_ascii_lowercase(); + let c2 = (*q.borrow()).offset(i).read().to_ascii_lowercase(); + if c1 != c2 { + break (c1 as i32) - (c2 as i32); + } + if c1 == 0 { + break 0; + } + i += 1; + } + } == 0) as i32) + != 0) + ); +} +pub fn main() { + std::process::exit(main_0()); +} +fn main_0() -> i32 { + ({ test_memcpy_0() }); + ({ test_memset_1() }); + ({ test_memcmp_2() }); + ({ test_memmove_3() }); + ({ test_strchr_4() }); + ({ test_strlen_5() }); + ({ test_strcmp_6() }); + ({ test_strncmp_7() }); + ({ test_memchr_8() }); + ({ test_strrchr_9() }); + ({ test_strcspn_10() }); + ({ test_strspn_11() }); + ({ test_strstr_12() }); + ({ test_strpbrk_13() }); + ({ test_strcasecmp_14() }); + return 0; +} diff --git a/tests/unit/out/refcount/va_arg_printf.rs b/tests/unit/out/refcount/va_arg_printf.rs index cd3d4009..c4b946e2 100644 --- a/tests/unit/out/refcount/va_arg_printf.rs +++ b/tests/unit/out/refcount/va_arg_printf.rs @@ -30,7 +30,13 @@ pub fn lenf_2(fmt: Ptr, __args: &[VaArg]) -> i32 { (*ap.borrow_mut()) = VaList::new(__args); let s: Value> = Rc::new(RefCell::new(((*ap.borrow_mut()).arg::>()).clone())); let result: Value = Rc::new(RefCell::new( - ((*s.borrow()).to_string_iterator().count() as i32), + ({ + let mut i: usize = 0; + while (*s.borrow()).offset(i).read() != 0 { + i += 1; + } + i + } as i32), )); return (*result.borrow()); } @@ -45,7 +51,14 @@ fn main_0() -> i32 { Ptr::from_string_literal(b"hello %d %d"), &[ (10).into(), - ((*dummy.borrow()).to_string_iterator().count()).into(), + ({ + let mut i: usize = 0; + while (*dummy.borrow()).offset(i).read() != 0 { + i += 1; + } + i + }) + .into(), ], ) }) == 15) as i32) diff --git a/tests/unit/out/unsafe/strdup.rs b/tests/unit/out/unsafe/strdup.rs new file mode 100644 index 00000000..8ea2c9c5 --- /dev/null +++ b/tests/unit/out/unsafe/strdup.rs @@ -0,0 +1,44 @@ +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 { + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut d: *mut libc::c_char = + libcc2rs::strdup_unsafe((c"hello".as_ptr().cast_mut()).cast_const()); + assert!((((!((d).is_null())) as i32) != 0)); + assert!( + ((((libc::strcmp( + (d).cast_const(), + (c"hello".as_ptr().cast_mut()).cast_const() + )) == (0)) as i32) + != 0) + ); + libcc2rs::free_unsafe((d as *mut libc::c_char as *mut ::libc::c_void)); + let mut p: *const libc::c_char = (c"world".as_ptr().cast_mut()).cast_const(); + let mut buf: [libc::c_char; 4] = [ + (('a' as i32) as libc::c_char), + (('b' as i32) as libc::c_char), + (('c' as i32) as libc::c_char), + (('\0' as i32) as libc::c_char), + ]; + let mut d2: *mut libc::c_char = libcc2rs::strdup_unsafe(p); + assert!((((!((d2).is_null())) as i32) != 0)); + assert!(((((libc::strcmp((d2).cast_const(), p)) == (0)) as i32) != 0)); + libcc2rs::free_unsafe((d2 as *mut libc::c_char as *mut ::libc::c_void)); + let mut d3: *mut libc::c_char = libcc2rs::strdup_unsafe((buf.as_mut_ptr()).cast_const()); + assert!((((!((d3).is_null())) as i32) != 0)); + assert!( + ((((libc::strcmp((d3).cast_const(), (buf.as_mut_ptr()).cast_const())) == (0)) as i32) != 0) + ); + libcc2rs::free_unsafe((d3 as *mut libc::c_char as *mut ::libc::c_void)); + return 0; +} diff --git a/tests/unit/out/unsafe/string_h.rs b/tests/unit/out/unsafe/string_h.rs index 9c5faac7..fbe4f580 100644 --- a/tests/unit/out/unsafe/string_h.rs +++ b/tests/unit/out/unsafe/string_h.rs @@ -218,6 +218,11 @@ pub unsafe fn test_strlen_5() { assert!( ((((libc::strcmp(second, (c"two".as_ptr().cast_mut()).cast_const())) == (0)) as i32) != 0) ); + let mut big : [ libc::c_char ; 64] = std::mem::transmute(*b"hi\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0") ; + assert!(((((libc::strlen((big.as_mut_ptr()).cast_const())) == (2_usize)) as i32) != 0)); + big[(2) as usize] = (('x' as i32) as libc::c_char); + big[(3) as usize] = (('\0' as i32) as libc::c_char); + assert!(((((libc::strlen((big.as_mut_ptr()).cast_const())) == (3_usize)) as i32) != 0)); } pub unsafe fn test_strcmp_6() { assert!( @@ -357,37 +362,7 @@ pub unsafe fn test_strrchr_9() { != 0) ); } -pub unsafe fn test_strdup_10() { - let mut d: *mut libc::c_char = - libcc2rs::strdup_unsafe((c"hello".as_ptr().cast_mut()).cast_const()); - assert!((((!((d).is_null())) as i32) != 0)); - assert!( - ((((libc::strcmp( - (d).cast_const(), - (c"hello".as_ptr().cast_mut()).cast_const() - )) == (0)) as i32) - != 0) - ); - libcc2rs::free_unsafe((d as *mut libc::c_char as *mut ::libc::c_void)); - let mut p: *const libc::c_char = (c"world".as_ptr().cast_mut()).cast_const(); - let mut buf: [libc::c_char; 4] = [ - (('a' as i32) as libc::c_char), - (('b' as i32) as libc::c_char), - (('c' as i32) as libc::c_char), - (('\0' as i32) as libc::c_char), - ]; - let mut d2: *mut libc::c_char = libcc2rs::strdup_unsafe(p); - assert!((((!((d2).is_null())) as i32) != 0)); - assert!(((((libc::strcmp((d2).cast_const(), p)) == (0)) as i32) != 0)); - libcc2rs::free_unsafe((d2 as *mut libc::c_char as *mut ::libc::c_void)); - let mut d3: *mut libc::c_char = libcc2rs::strdup_unsafe((buf.as_mut_ptr()).cast_const()); - assert!((((!((d3).is_null())) as i32) != 0)); - assert!( - ((((libc::strcmp((d3).cast_const(), (buf.as_mut_ptr()).cast_const())) == (0)) as i32) != 0) - ); - libcc2rs::free_unsafe((d3 as *mut libc::c_char as *mut ::libc::c_void)); -} -pub unsafe fn test_strcspn_11() { +pub unsafe fn test_strcspn_10() { assert!( ((((libc::strcspn( (c"hello".as_ptr().cast_mut()).cast_const(), @@ -413,7 +388,7 @@ pub unsafe fn test_strcspn_11() { let mut rej: *const libc::c_char = (c"el".as_ptr().cast_mut()).cast_const(); assert!(((((libc::strcspn(s, rej)) == (1_usize)) as i32) != 0)); } -pub unsafe fn test_strspn_12() { +pub unsafe fn test_strspn_11() { assert!( ((((libc::strspn( (c"hello".as_ptr().cast_mut()).cast_const(), @@ -439,7 +414,7 @@ pub unsafe fn test_strspn_12() { let mut acc: *const libc::c_char = (c"hel".as_ptr().cast_mut()).cast_const(); assert!(((((libc::strspn(s, acc)) == (4_usize)) as i32) != 0)); } -pub unsafe fn test_strstr_13() { +pub unsafe fn test_strstr_12() { let mut h: *const libc::c_char = (c"hello world".as_ptr().cast_mut()).cast_const(); let mut r: *mut libc::c_char = libc::strstr( (h as *mut libc::c_char).cast_const(), @@ -471,7 +446,7 @@ pub unsafe fn test_strstr_13() { != 0) ); } -pub unsafe fn test_strpbrk_14() { +pub unsafe fn test_strpbrk_13() { let mut s: *const libc::c_char = (c"hello world".as_ptr().cast_mut()).cast_const(); let mut r: *mut libc::c_char = libc::strpbrk( (s as *mut libc::c_char).cast_const(), @@ -501,7 +476,7 @@ pub unsafe fn test_strpbrk_14() { != 0) ); } -pub unsafe fn test_strcasecmp_15() { +pub unsafe fn test_strcasecmp_14() { assert!( ((((libc::strcasecmp( (c"HELLO".as_ptr().cast_mut()).cast_const(), @@ -543,11 +518,10 @@ unsafe fn main_0() -> i32 { (unsafe { test_strncmp_7() }); (unsafe { test_memchr_8() }); (unsafe { test_strrchr_9() }); - (unsafe { test_strdup_10() }); - (unsafe { test_strcspn_11() }); - (unsafe { test_strspn_12() }); - (unsafe { test_strstr_13() }); - (unsafe { test_strpbrk_14() }); - (unsafe { test_strcasecmp_15() }); + (unsafe { test_strcspn_10() }); + (unsafe { test_strspn_11() }); + (unsafe { test_strstr_12() }); + (unsafe { test_strpbrk_13() }); + (unsafe { test_strcasecmp_14() }); return 0; } diff --git a/tests/unit/strdup.c b/tests/unit/strdup.c new file mode 100644 index 00000000..c90a6eaa --- /dev/null +++ b/tests/unit/strdup.c @@ -0,0 +1,22 @@ +// no-compile: refcount +#include +#include +#include + +int main(void) { + char *d = strdup("hello"); + assert(d != NULL); + assert(strcmp(d, "hello") == 0); + free(d); + const char *p = "world"; + char buf[] = {'a', 'b', 'c', '\0'}; + char *d2 = strdup(p); + assert(d2 != NULL); + assert(strcmp(d2, p) == 0); + free(d2); + char *d3 = strdup(buf); + assert(d3 != NULL); + assert(strcmp(d3, buf) == 0); + free(d3); + return 0; +} diff --git a/tests/unit/string_h.c b/tests/unit/string_h.c index 3d47628b..c123fd26 100644 --- a/tests/unit/string_h.c +++ b/tests/unit/string_h.c @@ -1,7 +1,5 @@ -// no-compile: refcount #define _GNU_SOURCE #include -#include #include #include @@ -54,6 +52,11 @@ static void test_strlen(void) { const char *first = buf; const char *second = &buf[strlen(first) + 1]; assert(strcmp(second, "two") == 0); + char big[64] = "hi"; + assert(strlen(big) == 2); + big[2] = 'x'; + big[3] = '\0'; + assert(strlen(big) == 3); } static void test_strcmp(void) { @@ -102,23 +105,6 @@ static void test_strrchr(void) { assert(strrchr(buf, 'a') == &buf[2]); } -static void test_strdup(void) { - char *d = strdup("hello"); - assert(d != NULL); - assert(strcmp(d, "hello") == 0); - free(d); - const char *p = "world"; - char buf[] = {'a', 'b', 'c', '\0'}; - char *d2 = strdup(p); - assert(d2 != NULL); - assert(strcmp(d2, p) == 0); - free(d2); - char *d3 = strdup(buf); - assert(d3 != NULL); - assert(strcmp(d3, buf) == 0); - free(d3); -} - static void test_strcspn(void) { assert(strcspn("hello", "el") == 1); assert(strcspn("abc", "xyz") == 3); @@ -177,7 +163,6 @@ int main(void) { test_strncmp(); test_memchr(); test_strrchr(); - test_strdup(); test_strcspn(); test_strspn(); test_strstr(); From a870f5c4b6850c0f9fc7efd788847820e27df3ab Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 7 Jul 2026 15:00:06 +0100 Subject: [PATCH 03/15] Fix strdup argument passing --- rules/cstring/tgt_refcount.rs | 2 +- tests/unit/out/refcount/strdup.rs | 161 ++++++++++++++++++++++++++++++ tests/unit/out/unsafe/strdup.rs | 18 ++++ tests/unit/strdup.c | 17 +++- 4 files changed, 196 insertions(+), 2 deletions(-) create mode 100644 tests/unit/out/refcount/strdup.rs diff --git a/rules/cstring/tgt_refcount.rs b/rules/cstring/tgt_refcount.rs index 6098e12e..ef69c750 100644 --- a/rules/cstring/tgt_refcount.rs +++ b/rules/cstring/tgt_refcount.rs @@ -105,7 +105,7 @@ fn f11(a0: Ptr, a1: i32) -> Ptr { } fn f15(a0: Ptr) -> Ptr { - libcc2rs::strdup_refcount(a0) + libcc2rs::strdup_refcount(a0.clone()) } fn f16(a0: Ptr, a1: Ptr) -> usize { diff --git a/tests/unit/out/refcount/strdup.rs b/tests/unit/out/refcount/strdup.rs new file mode 100644 index 00000000..b5658394 --- /dev/null +++ b/tests/unit/out/refcount/strdup.rs @@ -0,0 +1,161 @@ +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}; +#[derive(Default)] +pub struct record { + pub name: Value>, +} +impl Clone for record { + fn clone(&self) -> Self { + Self { + name: Rc::new(RefCell::new((*self.name.borrow()).clone())), + } + } +} +impl ByteRepr for record { + fn byte_size() -> usize { + 8 + } + fn to_bytes(&self, buf: &mut [u8]) { + (*self.name.borrow()).to_bytes(&mut buf[0..8]); + } + fn from_bytes(buf: &[u8]) -> Self { + Self { + name: Rc::new(RefCell::new(>::from_bytes(&buf[0..8]))), + } + } +} +pub fn main() { + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let d: Value> = Rc::new(RefCell::new(libcc2rs::strdup_refcount( + Ptr::from_string_literal(b"hello").clone(), + ))); + assert!((((!((*d.borrow()).is_null())) as i32) != 0)); + assert!( + ((({ + let mut i: usize = 0; + loop { + let c1 = (*d.borrow()).offset(i).read(); + let c2 = Ptr::from_string_literal(b"hello").offset(i).read(); + if c1 != c2 { + break (c1 as i32) - (c2 as i32); + } + if c1 == 0 { + break 0; + } + i += 1; + } + } == 0) as i32) + != 0) + ); + libcc2rs::free_refcount(((*d.borrow()).clone() as Ptr).to_any()); + let p: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"world"))); + let buf: Value> = Rc::new(RefCell::new(Box::new([ + (('a' as i32) as u8), + (('b' as i32) as u8), + (('c' as i32) as u8), + (('\0' as i32) as u8), + ]))); + let d2: Value> = Rc::new(RefCell::new(libcc2rs::strdup_refcount( + (*p.borrow()).clone(), + ))); + assert!((((!((*d2.borrow()).is_null())) as i32) != 0)); + assert!( + ((({ + let mut i: usize = 0; + loop { + let c1 = (*d2.borrow()).offset(i).read(); + let c2 = (*p.borrow()).offset(i).read(); + if c1 != c2 { + break (c1 as i32) - (c2 as i32); + } + if c1 == 0 { + break 0; + } + i += 1; + } + } == 0) as i32) + != 0) + ); + libcc2rs::free_refcount(((*d2.borrow()).clone() as Ptr).to_any()); + let d3: Value> = Rc::new(RefCell::new(libcc2rs::strdup_refcount( + (buf.as_pointer() as Ptr).clone(), + ))); + assert!((((!((*d3.borrow()).is_null())) as i32) != 0)); + assert!( + ((({ + let mut i: usize = 0; + loop { + let c1 = (*d3.borrow()).offset(i).read(); + let c2 = (buf.as_pointer() as Ptr).offset(i).read(); + if c1 != c2 { + break (c1 as i32) - (c2 as i32); + } + if c1 == 0 { + break 0; + } + i += 1; + } + } == 0) as i32) + != 0) + ); + libcc2rs::free_refcount(((*d3.borrow()).clone() as Ptr).to_any()); + let d4: Value> = Rc::new(RefCell::new(Ptr::::null())); + (*d4.borrow_mut()) = libcc2rs::strdup_refcount((*p.borrow()).clone()); + assert!((((!((*d4.borrow()).is_null())) as i32) != 0)); + assert!( + ((({ + let mut i: usize = 0; + loop { + let c1 = (*d4.borrow()).offset(i).read(); + let c2 = (*p.borrow()).offset(i).read(); + if c1 != c2 { + break (c1 as i32) - (c2 as i32); + } + if c1 == 0 { + break 0; + } + i += 1; + } + } == 0) as i32) + != 0) + ); + libcc2rs::free_refcount(((*d4.borrow()).clone() as Ptr).to_any()); + let rec: Value = Rc::new(RefCell::new(record { + name: Rc::new(RefCell::new(Ptr::::null())), + })); + let r: Value> = Rc::new(RefCell::new((rec.as_pointer()))); + (*(*(*r.borrow()).upgrade().deref()).name.borrow_mut()) = + libcc2rs::strdup_refcount((*p.borrow()).clone()); + assert!((((!((*(*(*r.borrow()).upgrade().deref()).name.borrow()).is_null())) as i32) != 0)); + assert!( + ((({ + let mut i: usize = 0; + loop { + let c1 = (*(*(*r.borrow()).upgrade().deref()).name.borrow()) + .offset(i) + .read(); + let c2 = (*p.borrow()).offset(i).read(); + if c1 != c2 { + break (c1 as i32) - (c2 as i32); + } + if c1 == 0 { + break 0; + } + i += 1; + } + } == 0) as i32) + != 0) + ); + libcc2rs::free_refcount( + ((*(*(*r.borrow()).upgrade().deref()).name.borrow()).clone() as Ptr).to_any(), + ); + return 0; +} diff --git a/tests/unit/out/unsafe/strdup.rs b/tests/unit/out/unsafe/strdup.rs index 8ea2c9c5..b60b8a55 100644 --- a/tests/unit/out/unsafe/strdup.rs +++ b/tests/unit/out/unsafe/strdup.rs @@ -6,6 +6,11 @@ use std::collections::BTreeMap; use std::io::{Read, Seek, Write}; use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; use std::rc::Rc; +#[repr(C)] +#[derive(Copy, Clone, Default)] +pub struct record { + pub name: *mut libc::c_char, +} pub fn main() { unsafe { std::process::exit(main_0() as i32); @@ -40,5 +45,18 @@ unsafe fn main_0() -> i32 { ((((libc::strcmp((d3).cast_const(), (buf.as_mut_ptr()).cast_const())) == (0)) as i32) != 0) ); libcc2rs::free_unsafe((d3 as *mut libc::c_char as *mut ::libc::c_void)); + let mut d4: *mut libc::c_char = std::ptr::null_mut(); + d4 = libcc2rs::strdup_unsafe(p); + assert!((((!((d4).is_null())) as i32) != 0)); + assert!(((((libc::strcmp((d4).cast_const(), p)) == (0)) as i32) != 0)); + libcc2rs::free_unsafe((d4 as *mut libc::c_char as *mut ::libc::c_void)); + let mut rec: record = record { + name: std::ptr::null_mut(), + }; + let mut r: *mut record = (&mut rec as *mut record); + (*r).name = libcc2rs::strdup_unsafe(p); + assert!((((!(((*r).name).is_null())) as i32) != 0)); + assert!(((((libc::strcmp(((*r).name).cast_const(), p)) == (0)) as i32) != 0)); + libcc2rs::free_unsafe(((*r).name as *mut libc::c_char as *mut ::libc::c_void)); return 0; } diff --git a/tests/unit/strdup.c b/tests/unit/strdup.c index c90a6eaa..eb1f4cc2 100644 --- a/tests/unit/strdup.c +++ b/tests/unit/strdup.c @@ -1,8 +1,12 @@ -// no-compile: refcount +// panic: refcount #include #include #include +struct record { + char *name; +}; + int main(void) { char *d = strdup("hello"); assert(d != NULL); @@ -18,5 +22,16 @@ int main(void) { assert(d3 != NULL); assert(strcmp(d3, buf) == 0); free(d3); + char *d4 = 0; + d4 = strdup(p); + assert(d4 != NULL); + assert(strcmp(d4, p) == 0); + free(d4); + struct record rec = {0}; + struct record *r = &rec; + r->name = strdup(p); + assert(r->name != NULL); + assert(strcmp(r->name, p) == 0); + free(r->name); return 0; } From 1f25c92b551772e0442ba300bb2634e11099721e Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 7 Jul 2026 16:08:10 +0100 Subject: [PATCH 04/15] Use __ for rule variables to avoid clashes --- rules/cstring/tgt_refcount.rs | 214 +++--- tests/unit/out/refcount/strdup.rs | 72 +-- tests/unit/out/refcount/string_h.rs | 786 ++++++++++++----------- tests/unit/out/refcount/va_arg_printf.rs | 16 +- 4 files changed, 549 insertions(+), 539 deletions(-) diff --git a/rules/cstring/tgt_refcount.rs b/rules/cstring/tgt_refcount.rs index ef69c750..425d28ee 100644 --- a/rules/cstring/tgt_refcount.rs +++ b/rules/cstring/tgt_refcount.rs @@ -18,89 +18,89 @@ fn f3(a0: AnyPtr, a1: AnyPtr, a2: usize) -> i32 { } fn f4(a0: AnyPtr, a1: AnyPtr, a2: usize) -> AnyPtr { - let tmp: Vec = (0..a2) - .map(|i| a1.reinterpret_cast::().offset(i).read()) + let __tmp: Vec = (0..a2) + .map(|__i| a1.reinterpret_cast::().offset(__i).read()) .collect(); - for i in 0..a2 { - a0.reinterpret_cast::().offset(i).write(tmp[i]); + for __i in 0..a2 { + a0.reinterpret_cast::().offset(__i).write(__tmp[__i]); } a0.clone() } fn f5(a0: Ptr, a1: i32) -> Ptr { - let mut i: usize = 0; + let mut __i: usize = 0; loop { - let c = a0.offset(i).read(); - if c == a1 as u8 { - break a0.offset(i); + let __c = a0.offset(__i).read(); + if __c == a1 as u8 { + break a0.offset(__i); } - if c == 0 { + if __c == 0 { break Ptr::null(); } - i += 1; + __i += 1; } } unsafe fn f7(a0: Ptr) -> usize { - let mut i: usize = 0; - while a0.offset(i).read() != 0 { - i += 1; + let mut __i: usize = 0; + while a0.offset(__i).read() != 0 { + __i += 1; } - i + __i } fn f8(a0: Ptr, a1: Ptr) -> i32 { - let mut i: usize = 0; + let mut __i: usize = 0; loop { - let c1 = a0.offset(i).read(); - let c2 = a1.offset(i).read(); - if c1 != c2 { - break (c1 as i32) - (c2 as i32); + let __c1 = a0.offset(__i).read(); + let __c2 = a1.offset(__i).read(); + if __c1 != __c2 { + break (__c1 as i32) - (__c2 as i32); } - if c1 == 0 { + if __c1 == 0 { break 0; } - i += 1; + __i += 1; } } fn f9(a0: Ptr, a1: Ptr, a2: usize) -> i32 { - let mut i: usize = 0; + let mut __i: usize = 0; loop { - if i == a2 { + if __i == a2 { break 0; } - let c1 = a0.offset(i).read(); - let c2 = a1.offset(i).read(); - if c1 != c2 { - break (c1 as i32) - (c2 as i32); + let __c1 = a0.offset(__i).read(); + let __c2 = a1.offset(__i).read(); + if __c1 != __c2 { + break (__c1 as i32) - (__c2 as i32); } - if c1 == 0 { + if __c1 == 0 { break 0; } - i += 1; + __i += 1; } } fn f10(a0: AnyPtr, a1: i32, a2: usize) -> AnyPtr { - match (0..a2).find(|&i| a0.reinterpret_cast::().offset(i).read() == a1 as u8) { - Some(i) => a0.reinterpret_cast::().offset(i).to_any(), + match (0..a2).find(|&__i| a0.reinterpret_cast::().offset(__i).read() == a1 as u8) { + Some(__i) => a0.reinterpret_cast::().offset(__i).to_any(), None => Ptr::::null().to_any(), } } fn f11(a0: Ptr, a1: i32) -> Ptr { - let mut i: usize = 0; - let mut found = Ptr::null(); + let mut __i: usize = 0; + let mut __found = Ptr::null(); loop { - let c = a0.offset(i).read(); - if c == a1 as u8 { - found = a0.offset(i); + let __c = a0.offset(__i).read(); + if __c == a1 as u8 { + __found = a0.offset(__i); } - if c == 0 { - break found; + if __c == 0 { + break __found; } - i += 1; + __i += 1; } } @@ -109,101 +109,101 @@ fn f15(a0: Ptr) -> Ptr { } fn f16(a0: Ptr, a1: Ptr) -> usize { - let mut i: usize = 0; + let mut __i: usize = 0; loop { - let c = a0.offset(i).read(); - if c == 0 { - break i; + let __c = a0.offset(__i).read(); + if __c == 0 { + break __i; } - let mut j: usize = 0; - let hit = loop { - let r = a1.offset(j).read(); - if r == 0 { + let mut __j: usize = 0; + let __hit = loop { + let __r = a1.offset(__j).read(); + if __r == 0 { break false; } - if r == c { + if __r == __c { break true; } - j += 1; + __j += 1; }; - if hit { - break i; + if __hit { + break __i; } - i += 1; + __i += 1; } } fn f17(a0: Ptr, a1: Ptr) -> usize { - let mut i: usize = 0; + let mut __i: usize = 0; loop { - let c = a0.offset(i).read(); - if c == 0 { - break i; + let __c = a0.offset(__i).read(); + if __c == 0 { + break __i; } - let mut j: usize = 0; - let hit = loop { - let r = a1.offset(j).read(); - if r == 0 { + let mut __j: usize = 0; + let __hit = loop { + let __r = a1.offset(__j).read(); + if __r == 0 { break false; } - if r == c { + if __r == __c { break true; } - j += 1; + __j += 1; }; - if !hit { - break i; + if !__hit { + break __i; } - i += 1; + __i += 1; } } fn f18(a0: Ptr, a1: Ptr) -> Ptr { - let mut s: usize = 0; + let mut __s: usize = 0; loop { - let mut i: usize = 0; - let matched = loop { - let n = a1.offset(i).read(); - if n == 0 { + let mut __i: usize = 0; + let __matched = loop { + let __n = a1.offset(__i).read(); + if __n == 0 { break true; } - if a0.offset(s + i).read() != n { + if a0.offset(__s + __i).read() != __n { break false; } - i += 1; + __i += 1; }; - if matched { - break a0.offset(s); + if __matched { + break a0.offset(__s); } - if a0.offset(s).read() == 0 { + if a0.offset(__s).read() == 0 { break Ptr::null(); } - s += 1; + __s += 1; } } fn f21(a0: Ptr, a1: Ptr) -> Ptr { - let mut i: usize = 0; + let mut __i: usize = 0; loop { - let c = a0.offset(i).read(); - if c == 0 { + let __c = a0.offset(__i).read(); + if __c == 0 { break Ptr::null(); } - let mut j: usize = 0; - let hit = loop { - let r = a1.offset(j).read(); - if r == 0 { + let mut __j: usize = 0; + let __hit = loop { + let __r = a1.offset(__j).read(); + if __r == 0 { break false; } - if r == c { + if __r == __c { break true; } - j += 1; + __j += 1; }; - if hit { - break a0.offset(i); + if __hit { + break a0.offset(__i); } - i += 1; + __i += 1; } } @@ -211,50 +211,50 @@ fn f21(a0: Ptr, a1: Ptr) -> Ptr { fn f24(a0: AnyPtr, a1: i32, a2: usize) -> AnyPtr { match (0..a2) .rev() - .find(|&i| a0.reinterpret_cast::().offset(i).read() == a1 as u8) + .find(|&__i| a0.reinterpret_cast::().offset(__i).read() == a1 as u8) { - Some(i) => a0.reinterpret_cast::().offset(i).to_any(), + Some(__i) => a0.reinterpret_cast::().offset(__i).to_any(), None => Ptr::::null().to_any(), } } fn f27(a0: Ptr, a1: Ptr) -> i32 { - let mut i: usize = 0; + let mut __i: usize = 0; loop { - let c1 = a0.offset(i).read().to_ascii_lowercase(); - let c2 = a1.offset(i).read().to_ascii_lowercase(); - if c1 != c2 { - break (c1 as i32) - (c2 as i32); + let __c1 = a0.offset(__i).read().to_ascii_lowercase(); + let __c2 = a1.offset(__i).read().to_ascii_lowercase(); + if __c1 != __c2 { + break (__c1 as i32) - (__c2 as i32); } - if c1 == 0 { + if __c1 == 0 { break 0; } - i += 1; + __i += 1; } } #[cfg(target_os = "linux")] fn f28(a0: i32, a1: Ptr, a2: usize) -> Ptr { - let msg = std::io::Error::from_raw_os_error(a0).to_string(); - let len = msg.len().min(a2.saturating_sub(1)); - for i in 0..len { - a1.offset(i).write(msg.as_bytes()[i]); + let __msg = std::io::Error::from_raw_os_error(a0).to_string(); + let __len = __msg.len().min(a2.saturating_sub(1)); + for __i in 0..__len { + a1.offset(__i).write(__msg.as_bytes()[__i]); } if a2 > 0 { - a1.offset(len).write(0); + a1.offset(__len).write(0); } a1 } #[cfg(target_os = "macos")] fn f28(a0: i32, a1: Ptr, a2: usize) -> i32 { - let msg = std::io::Error::from_raw_os_error(a0).to_string(); - let len = msg.len().min(a2.saturating_sub(1)); - for i in 0..len { - a1.offset(i).write(msg.as_bytes()[i]); + let __msg = std::io::Error::from_raw_os_error(a0).to_string(); + let __len = __msg.len().min(a2.saturating_sub(1)); + for __i in 0..__len { + a1.offset(__i).write(__msg.as_bytes()[__i]); } if a2 > 0 { - a1.offset(len).write(0); + a1.offset(__len).write(0); } 0 } diff --git a/tests/unit/out/refcount/strdup.rs b/tests/unit/out/refcount/strdup.rs index b5658394..f854384e 100644 --- a/tests/unit/out/refcount/strdup.rs +++ b/tests/unit/out/refcount/strdup.rs @@ -40,17 +40,17 @@ fn main_0() -> i32 { assert!((((!((*d.borrow()).is_null())) as i32) != 0)); assert!( ((({ - let mut i: usize = 0; + let mut __i: usize = 0; loop { - let c1 = (*d.borrow()).offset(i).read(); - let c2 = Ptr::from_string_literal(b"hello").offset(i).read(); - if c1 != c2 { - break (c1 as i32) - (c2 as i32); + let __c1 = (*d.borrow()).offset(__i).read(); + let __c2 = Ptr::from_string_literal(b"hello").offset(__i).read(); + if __c1 != __c2 { + break (__c1 as i32) - (__c2 as i32); } - if c1 == 0 { + if __c1 == 0 { break 0; } - i += 1; + __i += 1; } } == 0) as i32) != 0) @@ -69,17 +69,17 @@ fn main_0() -> i32 { assert!((((!((*d2.borrow()).is_null())) as i32) != 0)); assert!( ((({ - let mut i: usize = 0; + let mut __i: usize = 0; loop { - let c1 = (*d2.borrow()).offset(i).read(); - let c2 = (*p.borrow()).offset(i).read(); - if c1 != c2 { - break (c1 as i32) - (c2 as i32); + let __c1 = (*d2.borrow()).offset(__i).read(); + let __c2 = (*p.borrow()).offset(__i).read(); + if __c1 != __c2 { + break (__c1 as i32) - (__c2 as i32); } - if c1 == 0 { + if __c1 == 0 { break 0; } - i += 1; + __i += 1; } } == 0) as i32) != 0) @@ -91,17 +91,17 @@ fn main_0() -> i32 { assert!((((!((*d3.borrow()).is_null())) as i32) != 0)); assert!( ((({ - let mut i: usize = 0; + let mut __i: usize = 0; loop { - let c1 = (*d3.borrow()).offset(i).read(); - let c2 = (buf.as_pointer() as Ptr).offset(i).read(); - if c1 != c2 { - break (c1 as i32) - (c2 as i32); + let __c1 = (*d3.borrow()).offset(__i).read(); + let __c2 = (buf.as_pointer() as Ptr).offset(__i).read(); + if __c1 != __c2 { + break (__c1 as i32) - (__c2 as i32); } - if c1 == 0 { + if __c1 == 0 { break 0; } - i += 1; + __i += 1; } } == 0) as i32) != 0) @@ -112,17 +112,17 @@ fn main_0() -> i32 { assert!((((!((*d4.borrow()).is_null())) as i32) != 0)); assert!( ((({ - let mut i: usize = 0; + let mut __i: usize = 0; loop { - let c1 = (*d4.borrow()).offset(i).read(); - let c2 = (*p.borrow()).offset(i).read(); - if c1 != c2 { - break (c1 as i32) - (c2 as i32); + let __c1 = (*d4.borrow()).offset(__i).read(); + let __c2 = (*p.borrow()).offset(__i).read(); + if __c1 != __c2 { + break (__c1 as i32) - (__c2 as i32); } - if c1 == 0 { + if __c1 == 0 { break 0; } - i += 1; + __i += 1; } } == 0) as i32) != 0) @@ -137,19 +137,19 @@ fn main_0() -> i32 { assert!((((!((*(*(*r.borrow()).upgrade().deref()).name.borrow()).is_null())) as i32) != 0)); assert!( ((({ - let mut i: usize = 0; + let mut __i: usize = 0; loop { - let c1 = (*(*(*r.borrow()).upgrade().deref()).name.borrow()) - .offset(i) + let __c1 = (*(*(*r.borrow()).upgrade().deref()).name.borrow()) + .offset(__i) .read(); - let c2 = (*p.borrow()).offset(i).read(); - if c1 != c2 { - break (c1 as i32) - (c2 as i32); + let __c2 = (*p.borrow()).offset(__i).read(); + if __c1 != __c2 { + break (__c1 as i32) - (__c2 as i32); } - if c1 == 0 { + if __c1 == 0 { break 0; } - i += 1; + __i += 1; } } == 0) as i32) != 0) diff --git a/tests/unit/out/refcount/string_h.rs b/tests/unit/out/refcount/string_h.rs index 4f2a680e..9963d22f 100644 --- a/tests/unit/out/refcount/string_h.rs +++ b/tests/unit/out/refcount/string_h.rs @@ -124,21 +124,21 @@ pub fn test_memmove_3() { (('\0' as i32) as u8), ]))); let r: Value = Rc::new(RefCell::new({ - let tmp: Vec = (0..4_usize) - .map(|i| { + let __tmp: Vec = (0..4_usize) + .map(|__i| { ((buf.as_pointer() as Ptr) as Ptr) .to_any() .reinterpret_cast::() - .offset(i) + .offset(__i) .read() }) .collect(); - for i in 0..4_usize { + for __i in 0..4_usize { ((buf.as_pointer() as Ptr).offset((1) as isize) as Ptr) .to_any() .reinterpret_cast::() - .offset(i) - .write(tmp[i]); + .offset(__i) + .write(__tmp[__i]); } ((buf.as_pointer() as Ptr).offset((1) as isize) as Ptr) .to_any() @@ -173,32 +173,32 @@ pub fn test_memmove_3() { pub fn test_strchr_4() { let s: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"hello world"))); let r: Value> = Rc::new(RefCell::new({ - let mut i: usize = 0; + let mut __i: usize = 0; loop { - let c = (*s.borrow()).reinterpret_cast::().offset(i).read(); - if c == ('w' as i32) as u8 { - break (*s.borrow()).reinterpret_cast::().offset(i); + let __c = (*s.borrow()).reinterpret_cast::().offset(__i).read(); + if __c == ('w' as i32) as u8 { + break (*s.borrow()).reinterpret_cast::().offset(__i); } - if c == 0 { + if __c == 0 { break Ptr::null(); } - i += 1; + __i += 1; } })); assert!((((!((*r.borrow()).is_null())) as i32) != 0)); assert!(((((((*r.borrow()).read()) as i32) == ('w' as i32)) as i32) != 0)); assert!( (((({ - let mut i: usize = 0; + let mut __i: usize = 0; loop { - let c = (*s.borrow()).reinterpret_cast::().offset(i).read(); - if c == ('z' as i32) as u8 { - break (*s.borrow()).reinterpret_cast::().offset(i); + let __c = (*s.borrow()).reinterpret_cast::().offset(__i).read(); + if __c == ('z' as i32) as u8 { + break (*s.borrow()).reinterpret_cast::().offset(__i); } - if c == 0 { + if __c == 0 { break Ptr::null(); } - i += 1; + __i += 1; } }) .is_null()) as i32) @@ -208,31 +208,31 @@ pub fn test_strchr_4() { pub fn test_strlen_5() { assert!( ((({ - let mut i: usize = 0; - while Ptr::from_string_literal(b"").offset(i).read() != 0 { - i += 1; + let mut __i: usize = 0; + while Ptr::from_string_literal(b"").offset(__i).read() != 0 { + __i += 1; } - i + __i } == 0_usize) as i32) != 0) ); assert!( ((({ - let mut i: usize = 0; - while Ptr::from_string_literal(b"hello").offset(i).read() != 0 { - i += 1; + let mut __i: usize = 0; + while Ptr::from_string_literal(b"hello").offset(__i).read() != 0 { + __i += 1; } - i + __i } == 5_usize) as i32) != 0) ); assert!( ((({ - let mut i: usize = 0; - while Ptr::from_string_literal(b"hello world").offset(i).read() != 0 { - i += 1; + let mut __i: usize = 0; + while Ptr::from_string_literal(b"hello world").offset(__i).read() != 0 { + __i += 1; } - i + __i } == 11_usize) as i32) != 0) ); @@ -241,28 +241,28 @@ pub fn test_strlen_5() { let second: Value> = Rc::new(RefCell::new( ((buf.as_pointer() as Ptr).offset( ({ - let mut i: usize = 0; - while (*first.borrow()).offset(i).read() != 0 { - i += 1; + let mut __i: usize = 0; + while (*first.borrow()).offset(__i).read() != 0 { + __i += 1; } - i + __i }) .wrapping_add(1_usize), )), )); assert!( ((({ - let mut i: usize = 0; + let mut __i: usize = 0; loop { - let c1 = (*second.borrow()).offset(i).read(); - let c2 = Ptr::from_string_literal(b"two").offset(i).read(); - if c1 != c2 { - break (c1 as i32) - (c2 as i32); + let __c1 = (*second.borrow()).offset(__i).read(); + let __c2 = Ptr::from_string_literal(b"two").offset(__i).read(); + if __c1 != __c2 { + break (__c1 as i32) - (__c2 as i32); } - if c1 == 0 { + if __c1 == 0 { break 0; } - i += 1; + __i += 1; } } == 0) as i32) != 0) @@ -270,11 +270,11 @@ pub fn test_strlen_5() { let big : Value > = Rc::new(RefCell::new(Box::from(*b"hi\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0") )) ; assert!( ((({ - let mut i: usize = 0; - while (big.as_pointer() as Ptr).offset(i).read() != 0 { - i += 1; + let mut __i: usize = 0; + while (big.as_pointer() as Ptr).offset(__i).read() != 0 { + __i += 1; } - i + __i } == 2_usize) as i32) != 0) ); @@ -282,11 +282,11 @@ pub fn test_strlen_5() { (*big.borrow_mut())[(3) as usize] = (('\0' as i32) as u8); assert!( ((({ - let mut i: usize = 0; - while (big.as_pointer() as Ptr).offset(i).read() != 0 { - i += 1; + let mut __i: usize = 0; + while (big.as_pointer() as Ptr).offset(__i).read() != 0 { + __i += 1; } - i + __i } == 3_usize) as i32) != 0) ); @@ -294,51 +294,51 @@ pub fn test_strlen_5() { pub fn test_strcmp_6() { assert!( ((({ - let mut i: usize = 0; + let mut __i: usize = 0; loop { - let c1 = Ptr::from_string_literal(b"abc").offset(i).read(); - let c2 = Ptr::from_string_literal(b"abc").offset(i).read(); - if c1 != c2 { - break (c1 as i32) - (c2 as i32); + let __c1 = Ptr::from_string_literal(b"abc").offset(__i).read(); + let __c2 = Ptr::from_string_literal(b"abc").offset(__i).read(); + if __c1 != __c2 { + break (__c1 as i32) - (__c2 as i32); } - if c1 == 0 { + if __c1 == 0 { break 0; } - i += 1; + __i += 1; } } == 0) as i32) != 0) ); assert!( ((({ - let mut i: usize = 0; + let mut __i: usize = 0; loop { - let c1 = Ptr::from_string_literal(b"abc").offset(i).read(); - let c2 = Ptr::from_string_literal(b"abd").offset(i).read(); - if c1 != c2 { - break (c1 as i32) - (c2 as i32); + let __c1 = Ptr::from_string_literal(b"abc").offset(__i).read(); + let __c2 = Ptr::from_string_literal(b"abd").offset(__i).read(); + if __c1 != __c2 { + break (__c1 as i32) - (__c2 as i32); } - if c1 == 0 { + if __c1 == 0 { break 0; } - i += 1; + __i += 1; } } < 0) as i32) != 0) ); assert!( ((({ - let mut i: usize = 0; + let mut __i: usize = 0; loop { - let c1 = Ptr::from_string_literal(b"abd").offset(i).read(); - let c2 = Ptr::from_string_literal(b"abc").offset(i).read(); - if c1 != c2 { - break (c1 as i32) - (c2 as i32); + let __c1 = Ptr::from_string_literal(b"abd").offset(__i).read(); + let __c2 = Ptr::from_string_literal(b"abc").offset(__i).read(); + if __c1 != __c2 { + break (__c1 as i32) - (__c2 as i32); } - if c1 == 0 { + if __c1 == 0 { break 0; } - i += 1; + __i += 1; } } > 0) as i32) != 0) @@ -353,51 +353,51 @@ pub fn test_strcmp_6() { ]))); assert!( ((({ - let mut i: usize = 0; + let mut __i: usize = 0; loop { - let c1 = (*p.borrow()).offset(i).read(); - let c2 = (*p.borrow()).offset(i).read(); - if c1 != c2 { - break (c1 as i32) - (c2 as i32); + let __c1 = (*p.borrow()).offset(__i).read(); + let __c2 = (*p.borrow()).offset(__i).read(); + if __c1 != __c2 { + break (__c1 as i32) - (__c2 as i32); } - if c1 == 0 { + if __c1 == 0 { break 0; } - i += 1; + __i += 1; } } == 0) as i32) != 0) ); assert!( ((({ - let mut i: usize = 0; + let mut __i: usize = 0; loop { - let c1 = (*p.borrow()).offset(i).read(); - let c2 = (*q.borrow()).offset(i).read(); - if c1 != c2 { - break (c1 as i32) - (c2 as i32); + let __c1 = (*p.borrow()).offset(__i).read(); + let __c2 = (*q.borrow()).offset(__i).read(); + if __c1 != __c2 { + break (__c1 as i32) - (__c2 as i32); } - if c1 == 0 { + if __c1 == 0 { break 0; } - i += 1; + __i += 1; } } < 0) as i32) != 0) ); assert!( ((({ - let mut i: usize = 0; + let mut __i: usize = 0; loop { - let c1 = (buf.as_pointer() as Ptr).offset(i).read(); - let c2 = (*p.borrow()).offset(i).read(); - if c1 != c2 { - break (c1 as i32) - (c2 as i32); + let __c1 = (buf.as_pointer() as Ptr).offset(__i).read(); + let __c2 = (*p.borrow()).offset(__i).read(); + if __c1 != __c2 { + break (__c1 as i32) - (__c2 as i32); } - if c1 == 0 { + if __c1 == 0 { break 0; } - i += 1; + __i += 1; } } == 0) as i32) != 0) @@ -406,60 +406,60 @@ pub fn test_strcmp_6() { pub fn test_strncmp_7() { assert!( ((({ - let mut i: usize = 0; + let mut __i: usize = 0; loop { - if i == 3_usize { + if __i == 3_usize { break 0; } - let c1 = Ptr::from_string_literal(b"abcdef").offset(i).read(); - let c2 = Ptr::from_string_literal(b"abcxyz").offset(i).read(); - if c1 != c2 { - break (c1 as i32) - (c2 as i32); + let __c1 = Ptr::from_string_literal(b"abcdef").offset(__i).read(); + let __c2 = Ptr::from_string_literal(b"abcxyz").offset(__i).read(); + if __c1 != __c2 { + break (__c1 as i32) - (__c2 as i32); } - if c1 == 0 { + if __c1 == 0 { break 0; } - i += 1; + __i += 1; } } == 0) as i32) != 0) ); assert!( ((({ - let mut i: usize = 0; + let mut __i: usize = 0; loop { - if i == 4_usize { + if __i == 4_usize { break 0; } - let c1 = Ptr::from_string_literal(b"abcdef").offset(i).read(); - let c2 = Ptr::from_string_literal(b"abcxyz").offset(i).read(); - if c1 != c2 { - break (c1 as i32) - (c2 as i32); + let __c1 = Ptr::from_string_literal(b"abcdef").offset(__i).read(); + let __c2 = Ptr::from_string_literal(b"abcxyz").offset(__i).read(); + if __c1 != __c2 { + break (__c1 as i32) - (__c2 as i32); } - if c1 == 0 { + if __c1 == 0 { break 0; } - i += 1; + __i += 1; } } < 0) as i32) != 0) ); assert!( ((({ - let mut i: usize = 0; + let mut __i: usize = 0; loop { - if i == 4_usize { + if __i == 4_usize { break 0; } - let c1 = Ptr::from_string_literal(b"abcxyz").offset(i).read(); - let c2 = Ptr::from_string_literal(b"abcdef").offset(i).read(); - if c1 != c2 { - break (c1 as i32) - (c2 as i32); + let __c1 = Ptr::from_string_literal(b"abcxyz").offset(__i).read(); + let __c2 = Ptr::from_string_literal(b"abcdef").offset(__i).read(); + if __c1 != __c2 { + break (__c1 as i32) - (__c2 as i32); } - if c1 == 0 { + if __c1 == 0 { break 0; } - i += 1; + __i += 1; } } > 0) as i32) != 0) @@ -478,60 +478,60 @@ pub fn test_strncmp_7() { let n: Value = Rc::new(RefCell::new(3_usize)); assert!( ((({ - let mut i: usize = 0; + let mut __i: usize = 0; loop { - if i == (*n.borrow()) { + if __i == (*n.borrow()) { break 0; } - let c1 = (*p.borrow()).offset(i).read(); - let c2 = (*q.borrow()).offset(i).read(); - if c1 != c2 { - break (c1 as i32) - (c2 as i32); + let __c1 = (*p.borrow()).offset(__i).read(); + let __c2 = (*q.borrow()).offset(__i).read(); + if __c1 != __c2 { + break (__c1 as i32) - (__c2 as i32); } - if c1 == 0 { + if __c1 == 0 { break 0; } - i += 1; + __i += 1; } } == 0) as i32) != 0) ); assert!( ((({ - let mut i: usize = 0; + let mut __i: usize = 0; loop { - if i == (*n.borrow()).wrapping_add(1_usize) { + if __i == (*n.borrow()).wrapping_add(1_usize) { break 0; } - let c1 = (*p.borrow()).offset(i).read(); - let c2 = (*q.borrow()).offset(i).read(); - if c1 != c2 { - break (c1 as i32) - (c2 as i32); + let __c1 = (*p.borrow()).offset(__i).read(); + let __c2 = (*q.borrow()).offset(__i).read(); + if __c1 != __c2 { + break (__c1 as i32) - (__c2 as i32); } - if c1 == 0 { + if __c1 == 0 { break 0; } - i += 1; + __i += 1; } } < 0) as i32) != 0) ); assert!( ((({ - let mut i: usize = 0; + let mut __i: usize = 0; loop { - if i == 6_usize { + if __i == 6_usize { break 0; } - let c1 = (buf.as_pointer() as Ptr).offset(i).read(); - let c2 = (*p.borrow()).offset(i).read(); - if c1 != c2 { - break (c1 as i32) - (c2 as i32); + let __c1 = (buf.as_pointer() as Ptr).offset(__i).read(); + let __c2 = (*p.borrow()).offset(__i).read(); + if __c1 != __c2 { + break (__c1 as i32) - (__c2 as i32); } - if c1 == 0 { + if __c1 == 0 { break 0; } - i += 1; + __i += 1; } } == 0) as i32) != 0) @@ -540,18 +540,18 @@ pub fn test_strncmp_7() { pub fn test_memchr_8() { let data: Value> = Rc::new(RefCell::new(Box::new([16_u8, 32_u8, 48_u8, 64_u8]))); let r: Value = Rc::new(RefCell::new( - match (0..4_usize).find(|&i| { + match (0..4_usize).find(|&__i| { ((data.as_pointer() as Ptr) as Ptr) .to_any() .reinterpret_cast::() - .offset(i) + .offset(__i) .read() == 48 as u8 }) { - Some(i) => ((data.as_pointer() as Ptr) as Ptr) + Some(__i) => ((data.as_pointer() as Ptr) as Ptr) .to_any() .reinterpret_cast::() - .offset(i) + .offset(__i) .to_any(), None => Ptr::::null().to_any(), }, @@ -564,17 +564,17 @@ pub fn test_memchr_8() { != 0) ); assert!( - ((((match (0..4_usize).find(|&i| ((data.as_pointer() as Ptr::) as Ptr::) + ((((match (0..4_usize).find(|&__i| ((data.as_pointer() as Ptr::) as Ptr::) .to_any() .reinterpret_cast::() - .offset(i) + .offset(__i) .read() == 153 as u8) { - Some(i) => ((data.as_pointer() as Ptr::) as Ptr::) + Some(__i) => ((data.as_pointer() as Ptr::) as Ptr::) .to_any() .reinterpret_cast::() - .offset(i) + .offset(__i) .to_any(), None => Ptr::::null().to_any(), }) @@ -588,9 +588,9 @@ pub fn test_memchr_8() { assert!( ((({ let _lhs = match (0..(*n.borrow())) - .find(|&i| (*p.borrow()).reinterpret_cast::().offset(i).read() == 16 as u8) + .find(|&__i| (*p.borrow()).reinterpret_cast::().offset(__i).read() == 16 as u8) { - Some(i) => (*p.borrow()).reinterpret_cast::().offset(i).to_any(), + Some(__i) => (*p.borrow()).reinterpret_cast::().offset(__i).to_any(), None => Ptr::::null().to_any(), }; _lhs == (*p.borrow()).clone() @@ -601,17 +601,17 @@ pub fn test_memchr_8() { pub fn test_strrchr_9() { let s: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"hello world"))); let r: Value> = Rc::new(RefCell::new({ - let mut i: usize = 0; - let mut found = Ptr::null(); + let mut __i: usize = 0; + let mut __found = Ptr::null(); loop { - let c = (*s.borrow()).reinterpret_cast::().offset(i).read(); - if c == ('l' as i32) as u8 { - found = (*s.borrow()).reinterpret_cast::().offset(i); + let __c = (*s.borrow()).reinterpret_cast::().offset(__i).read(); + if __c == ('l' as i32) as u8 { + __found = (*s.borrow()).reinterpret_cast::().offset(__i); } - if c == 0 { - break found; + if __c == 0 { + break __found; } - i += 1; + __i += 1; } })); assert!((((!((*r.borrow()).is_null())) as i32) != 0)); @@ -625,17 +625,17 @@ pub fn test_strrchr_9() { ); assert!( (((({ - let mut i: usize = 0; - let mut found = Ptr::null(); + let mut __i: usize = 0; + let mut __found = Ptr::null(); loop { - let c = (*s.borrow()).reinterpret_cast::().offset(i).read(); - if c == ('z' as i32) as u8 { - found = (*s.borrow()).reinterpret_cast::().offset(i); + let __c = (*s.borrow()).reinterpret_cast::().offset(__i).read(); + if __c == ('z' as i32) as u8 { + __found = (*s.borrow()).reinterpret_cast::().offset(__i); } - if c == 0 { - break found; + if __c == 0 { + break __found; } - i += 1; + __i += 1; } }) .is_null()) as i32) @@ -649,17 +649,17 @@ pub fn test_strrchr_9() { ]))); assert!( ((({ - let mut i: usize = 0; - let mut found = Ptr::null(); + let mut __i: usize = 0; + let mut __found = Ptr::null(); loop { - let c = (buf.as_pointer() as Ptr).offset(i).read(); - if c == ('a' as i32) as u8 { - found = (buf.as_pointer() as Ptr).offset(i); + let __c = (buf.as_pointer() as Ptr).offset(__i).read(); + if __c == ('a' as i32) as u8 { + __found = (buf.as_pointer() as Ptr).offset(__i); } - if c == 0 { - break found; + if __c == 0 { + break __found; } - i += 1; + __i += 1; } } == ((buf.as_pointer() as Ptr).offset(2))) as i32) != 0) @@ -668,81 +668,81 @@ pub fn test_strrchr_9() { pub fn test_strcspn_10() { assert!( ((({ - let mut i: usize = 0; + let mut __i: usize = 0; loop { - let c = Ptr::from_string_literal(b"hello").offset(i).read(); - if c == 0 { - break i; - } - let mut j: usize = 0; - let hit = loop { - let r = Ptr::from_string_literal(b"el").offset(j).read(); - if r == 0 { + let __c = Ptr::from_string_literal(b"hello").offset(__i).read(); + if __c == 0 { + break __i; + } + let mut __j: usize = 0; + let __hit = loop { + let __r = Ptr::from_string_literal(b"el").offset(__j).read(); + if __r == 0 { break false; } - if r == c { + if __r == __c { break true; } - j += 1; + __j += 1; }; - if hit { - break i; + if __hit { + break __i; } - i += 1; + __i += 1; } } == 1_usize) as i32) != 0) ); assert!( ((({ - let mut i: usize = 0; + let mut __i: usize = 0; loop { - let c = Ptr::from_string_literal(b"abc").offset(i).read(); - if c == 0 { - break i; - } - let mut j: usize = 0; - let hit = loop { - let r = Ptr::from_string_literal(b"xyz").offset(j).read(); - if r == 0 { + let __c = Ptr::from_string_literal(b"abc").offset(__i).read(); + if __c == 0 { + break __i; + } + let mut __j: usize = 0; + let __hit = loop { + let __r = Ptr::from_string_literal(b"xyz").offset(__j).read(); + if __r == 0 { break false; } - if r == c { + if __r == __c { break true; } - j += 1; + __j += 1; }; - if hit { - break i; + if __hit { + break __i; } - i += 1; + __i += 1; } } == 3_usize) as i32) != 0) ); assert!( ((({ - let mut i: usize = 0; + let mut __i: usize = 0; loop { - let c = Ptr::from_string_literal(b"").offset(i).read(); - if c == 0 { - break i; - } - let mut j: usize = 0; - let hit = loop { - let r = Ptr::from_string_literal(b"abc").offset(j).read(); - if r == 0 { + let __c = Ptr::from_string_literal(b"").offset(__i).read(); + if __c == 0 { + break __i; + } + let mut __j: usize = 0; + let __hit = loop { + let __r = Ptr::from_string_literal(b"abc").offset(__j).read(); + if __r == 0 { break false; } - if r == c { + if __r == __c { break true; } - j += 1; + __j += 1; }; - if hit { - break i; + if __hit { + break __i; } - i += 1; + __i += 1; } } == 0_usize) as i32) != 0) @@ -751,27 +751,27 @@ pub fn test_strcspn_10() { let rej: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"el"))); assert!( ((({ - let mut i: usize = 0; + let mut __i: usize = 0; loop { - let c = (*s.borrow()).offset(i).read(); - if c == 0 { - break i; - } - let mut j: usize = 0; - let hit = loop { - let r = (*rej.borrow()).offset(j).read(); - if r == 0 { + let __c = (*s.borrow()).offset(__i).read(); + if __c == 0 { + break __i; + } + let mut __j: usize = 0; + let __hit = loop { + let __r = (*rej.borrow()).offset(__j).read(); + if __r == 0 { break false; } - if r == c { + if __r == __c { break true; } - j += 1; + __j += 1; }; - if hit { - break i; + if __hit { + break __i; } - i += 1; + __i += 1; } } == 1_usize) as i32) != 0) @@ -780,81 +780,81 @@ pub fn test_strcspn_10() { pub fn test_strspn_11() { assert!( ((({ - let mut i: usize = 0; + let mut __i: usize = 0; loop { - let c = Ptr::from_string_literal(b"hello").offset(i).read(); - if c == 0 { - break i; - } - let mut j: usize = 0; - let hit = loop { - let r = Ptr::from_string_literal(b"hel").offset(j).read(); - if r == 0 { + let __c = Ptr::from_string_literal(b"hello").offset(__i).read(); + if __c == 0 { + break __i; + } + let mut __j: usize = 0; + let __hit = loop { + let __r = Ptr::from_string_literal(b"hel").offset(__j).read(); + if __r == 0 { break false; } - if r == c { + if __r == __c { break true; } - j += 1; + __j += 1; }; - if !hit { - break i; + if !__hit { + break __i; } - i += 1; + __i += 1; } } == 4_usize) as i32) != 0) ); assert!( ((({ - let mut i: usize = 0; + let mut __i: usize = 0; loop { - let c = Ptr::from_string_literal(b"abc").offset(i).read(); - if c == 0 { - break i; - } - let mut j: usize = 0; - let hit = loop { - let r = Ptr::from_string_literal(b"xyz").offset(j).read(); - if r == 0 { + let __c = Ptr::from_string_literal(b"abc").offset(__i).read(); + if __c == 0 { + break __i; + } + let mut __j: usize = 0; + let __hit = loop { + let __r = Ptr::from_string_literal(b"xyz").offset(__j).read(); + if __r == 0 { break false; } - if r == c { + if __r == __c { break true; } - j += 1; + __j += 1; }; - if !hit { - break i; + if !__hit { + break __i; } - i += 1; + __i += 1; } } == 0_usize) as i32) != 0) ); assert!( ((({ - let mut i: usize = 0; + let mut __i: usize = 0; loop { - let c = Ptr::from_string_literal(b"aaa").offset(i).read(); - if c == 0 { - break i; - } - let mut j: usize = 0; - let hit = loop { - let r = Ptr::from_string_literal(b"a").offset(j).read(); - if r == 0 { + let __c = Ptr::from_string_literal(b"aaa").offset(__i).read(); + if __c == 0 { + break __i; + } + let mut __j: usize = 0; + let __hit = loop { + let __r = Ptr::from_string_literal(b"a").offset(__j).read(); + if __r == 0 { break false; } - if r == c { + if __r == __c { break true; } - j += 1; + __j += 1; }; - if !hit { - break i; + if !__hit { + break __i; } - i += 1; + __i += 1; } } == 3_usize) as i32) != 0) @@ -863,27 +863,27 @@ pub fn test_strspn_11() { let acc: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"hel"))); assert!( ((({ - let mut i: usize = 0; + let mut __i: usize = 0; loop { - let c = (*s.borrow()).offset(i).read(); - if c == 0 { - break i; - } - let mut j: usize = 0; - let hit = loop { - let r = (*acc.borrow()).offset(j).read(); - if r == 0 { + let __c = (*s.borrow()).offset(__i).read(); + if __c == 0 { + break __i; + } + let mut __j: usize = 0; + let __hit = loop { + let __r = (*acc.borrow()).offset(__j).read(); + if __r == 0 { break false; } - if r == c { + if __r == __c { break true; } - j += 1; + __j += 1; }; - if !hit { - break i; + if !__hit { + break __i; } - i += 1; + __i += 1; } } == 4_usize) as i32) != 0) @@ -892,26 +892,31 @@ pub fn test_strspn_11() { pub fn test_strstr_12() { let h: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"hello world"))); let r: Value> = Rc::new(RefCell::new({ - let mut s: usize = 0; + let mut __s: usize = 0; loop { - let mut i: usize = 0; - let matched = loop { - let n = Ptr::from_string_literal(b"world").offset(i).read(); - if n == 0 { + let mut __i: usize = 0; + let __matched = loop { + let __n = Ptr::from_string_literal(b"world").offset(__i).read(); + if __n == 0 { break true; } - if (*h.borrow()).reinterpret_cast::().offset(s + i).read() != n { + if (*h.borrow()) + .reinterpret_cast::() + .offset(__s + __i) + .read() + != __n + { break false; } - i += 1; + __i += 1; }; - if matched { - break (*h.borrow()).reinterpret_cast::().offset(s); + if __matched { + break (*h.borrow()).reinterpret_cast::().offset(__s); } - if (*h.borrow()).reinterpret_cast::().offset(s).read() == 0 { + if (*h.borrow()).reinterpret_cast::().offset(__s).read() == 0 { break Ptr::null(); } - s += 1; + __s += 1; } })); assert!((((!((*r.borrow()).is_null())) as i32) != 0)); @@ -924,26 +929,31 @@ pub fn test_strstr_12() { ); assert!( (((({ - let mut s: usize = 0; + let mut __s: usize = 0; loop { - let mut i: usize = 0; - let matched = loop { - let n = Ptr::from_string_literal(b"xyz").offset(i).read(); - if n == 0 { + let mut __i: usize = 0; + let __matched = loop { + let __n = Ptr::from_string_literal(b"xyz").offset(__i).read(); + if __n == 0 { break true; } - if (*h.borrow()).reinterpret_cast::().offset(s + i).read() != n { + if (*h.borrow()) + .reinterpret_cast::() + .offset(__s + __i) + .read() + != __n + { break false; } - i += 1; + __i += 1; }; - if matched { - break (*h.borrow()).reinterpret_cast::().offset(s); + if __matched { + break (*h.borrow()).reinterpret_cast::().offset(__s); } - if (*h.borrow()).reinterpret_cast::().offset(s).read() == 0 { + if (*h.borrow()).reinterpret_cast::().offset(__s).read() == 0 { break Ptr::null(); } - s += 1; + __s += 1; } }) .is_null()) as i32) @@ -959,26 +969,26 @@ pub fn test_strstr_12() { ]))); assert!( ((({ - let mut s: usize = 0; + let mut __s: usize = 0; loop { - let mut i: usize = 0; - let matched = loop { - let n = Ptr::from_string_literal(b"ll").offset(i).read(); - if n == 0 { + let mut __i: usize = 0; + let __matched = loop { + let __n = Ptr::from_string_literal(b"ll").offset(__i).read(); + if __n == 0 { break true; } - if (buf.as_pointer() as Ptr).offset(s + i).read() != n { + if (buf.as_pointer() as Ptr).offset(__s + __i).read() != __n { break false; } - i += 1; + __i += 1; }; - if matched { - break (buf.as_pointer() as Ptr).offset(s); + if __matched { + break (buf.as_pointer() as Ptr).offset(__s); } - if (buf.as_pointer() as Ptr).offset(s).read() == 0 { + if (buf.as_pointer() as Ptr).offset(__s).read() == 0 { break Ptr::null(); } - s += 1; + __s += 1; } } == ((buf.as_pointer() as Ptr).offset(2))) as i32) != 0) @@ -987,27 +997,27 @@ pub fn test_strstr_12() { pub fn test_strpbrk_13() { let s: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"hello world"))); let r: Value> = Rc::new(RefCell::new({ - let mut i: usize = 0; + let mut __i: usize = 0; loop { - let c = (*s.borrow()).reinterpret_cast::().offset(i).read(); - if c == 0 { + let __c = (*s.borrow()).reinterpret_cast::().offset(__i).read(); + if __c == 0 { break Ptr::null(); } - let mut j: usize = 0; - let hit = loop { - let r = Ptr::from_string_literal(b"wo").offset(j).read(); - if r == 0 { + let mut __j: usize = 0; + let __hit = loop { + let __r = Ptr::from_string_literal(b"wo").offset(__j).read(); + if __r == 0 { break false; } - if r == c { + if __r == __c { break true; } - j += 1; + __j += 1; }; - if hit { - break (*s.borrow()).reinterpret_cast::().offset(i); + if __hit { + break (*s.borrow()).reinterpret_cast::().offset(__i); } - i += 1; + __i += 1; } })); assert!((((!((*r.borrow()).is_null())) as i32) != 0)); @@ -1020,27 +1030,27 @@ pub fn test_strpbrk_13() { ); assert!( (((({ - let mut i: usize = 0; + let mut __i: usize = 0; loop { - let c = (*s.borrow()).reinterpret_cast::().offset(i).read(); - if c == 0 { + let __c = (*s.borrow()).reinterpret_cast::().offset(__i).read(); + if __c == 0 { break Ptr::null(); } - let mut j: usize = 0; - let hit = loop { - let r = Ptr::from_string_literal(b"xyz").offset(j).read(); - if r == 0 { + let mut __j: usize = 0; + let __hit = loop { + let __r = Ptr::from_string_literal(b"xyz").offset(__j).read(); + if __r == 0 { break false; } - if r == c { + if __r == __c { break true; } - j += 1; + __j += 1; }; - if hit { - break (*s.borrow()).reinterpret_cast::().offset(i); + if __hit { + break (*s.borrow()).reinterpret_cast::().offset(__i); } - i += 1; + __i += 1; } }) .is_null()) as i32) @@ -1054,27 +1064,27 @@ pub fn test_strpbrk_13() { ]))); assert!( ((({ - let mut i: usize = 0; + let mut __i: usize = 0; loop { - let c = (buf.as_pointer() as Ptr).offset(i).read(); - if c == 0 { + let __c = (buf.as_pointer() as Ptr).offset(__i).read(); + if __c == 0 { break Ptr::null(); } - let mut j: usize = 0; - let hit = loop { - let r = Ptr::from_string_literal(b"b").offset(j).read(); - if r == 0 { + let mut __j: usize = 0; + let __hit = loop { + let __r = Ptr::from_string_literal(b"b").offset(__j).read(); + if __r == 0 { break false; } - if r == c { + if __r == __c { break true; } - j += 1; + __j += 1; }; - if hit { - break (buf.as_pointer() as Ptr).offset(i); + if __hit { + break (buf.as_pointer() as Ptr).offset(__i); } - i += 1; + __i += 1; } } == ((buf.as_pointer() as Ptr).offset(1))) as i32) != 0) @@ -1083,69 +1093,69 @@ pub fn test_strpbrk_13() { pub fn test_strcasecmp_14() { assert!( ((({ - let mut i: usize = 0; + let mut __i: usize = 0; loop { - let c1 = Ptr::from_string_literal(b"HELLO") - .offset(i) + let __c1 = Ptr::from_string_literal(b"HELLO") + .offset(__i) .read() .to_ascii_lowercase(); - let c2 = Ptr::from_string_literal(b"hello") - .offset(i) + let __c2 = Ptr::from_string_literal(b"hello") + .offset(__i) .read() .to_ascii_lowercase(); - if c1 != c2 { - break (c1 as i32) - (c2 as i32); + if __c1 != __c2 { + break (__c1 as i32) - (__c2 as i32); } - if c1 == 0 { + if __c1 == 0 { break 0; } - i += 1; + __i += 1; } } == 0) as i32) != 0) ); assert!( ((({ - let mut i: usize = 0; + let mut __i: usize = 0; loop { - let c1 = Ptr::from_string_literal(b"abc") - .offset(i) + let __c1 = Ptr::from_string_literal(b"abc") + .offset(__i) .read() .to_ascii_lowercase(); - let c2 = Ptr::from_string_literal(b"abd") - .offset(i) + let __c2 = Ptr::from_string_literal(b"abd") + .offset(__i) .read() .to_ascii_lowercase(); - if c1 != c2 { - break (c1 as i32) - (c2 as i32); + if __c1 != __c2 { + break (__c1 as i32) - (__c2 as i32); } - if c1 == 0 { + if __c1 == 0 { break 0; } - i += 1; + __i += 1; } } < 0) as i32) != 0) ); assert!( ((({ - let mut i: usize = 0; + let mut __i: usize = 0; loop { - let c1 = Ptr::from_string_literal(b"abd") - .offset(i) + let __c1 = Ptr::from_string_literal(b"abd") + .offset(__i) .read() .to_ascii_lowercase(); - let c2 = Ptr::from_string_literal(b"abc") - .offset(i) + let __c2 = Ptr::from_string_literal(b"abc") + .offset(__i) .read() .to_ascii_lowercase(); - if c1 != c2 { - break (c1 as i32) - (c2 as i32); + if __c1 != __c2 { + break (__c1 as i32) - (__c2 as i32); } - if c1 == 0 { + if __c1 == 0 { break 0; } - i += 1; + __i += 1; } } > 0) as i32) != 0) @@ -1154,17 +1164,17 @@ pub fn test_strcasecmp_14() { let q: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"foo"))); assert!( ((({ - let mut i: usize = 0; + let mut __i: usize = 0; loop { - let c1 = (*p.borrow()).offset(i).read().to_ascii_lowercase(); - let c2 = (*q.borrow()).offset(i).read().to_ascii_lowercase(); - if c1 != c2 { - break (c1 as i32) - (c2 as i32); + let __c1 = (*p.borrow()).offset(__i).read().to_ascii_lowercase(); + let __c2 = (*q.borrow()).offset(__i).read().to_ascii_lowercase(); + if __c1 != __c2 { + break (__c1 as i32) - (__c2 as i32); } - if c1 == 0 { + if __c1 == 0 { break 0; } - i += 1; + __i += 1; } } == 0) as i32) != 0) diff --git a/tests/unit/out/refcount/va_arg_printf.rs b/tests/unit/out/refcount/va_arg_printf.rs index c4b946e2..b6b78e75 100644 --- a/tests/unit/out/refcount/va_arg_printf.rs +++ b/tests/unit/out/refcount/va_arg_printf.rs @@ -31,11 +31,11 @@ pub fn lenf_2(fmt: Ptr, __args: &[VaArg]) -> i32 { let s: Value> = Rc::new(RefCell::new(((*ap.borrow_mut()).arg::>()).clone())); let result: Value = Rc::new(RefCell::new( ({ - let mut i: usize = 0; - while (*s.borrow()).offset(i).read() != 0 { - i += 1; + let mut __i: usize = 0; + while (*s.borrow()).offset(__i).read() != 0 { + __i += 1; } - i + __i } as i32), )); return (*result.borrow()); @@ -52,11 +52,11 @@ fn main_0() -> i32 { &[ (10).into(), ({ - let mut i: usize = 0; - while (*dummy.borrow()).offset(i).read() != 0 { - i += 1; + let mut __i: usize = 0; + while (*dummy.borrow()).offset(__i).read() != 0 { + __i += 1; } - i + __i }) .into(), ], From 72decbd6dd38816eb9a050c5de0ad4c0607aa77a Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Fri, 10 Jul 2026 12:05:25 +0100 Subject: [PATCH 05/15] Add safe cstring rules --- rules/cstring/tgt_refcount.rs | 171 +++++ tests/unit/cstring.cpp | 1 - tests/unit/out/refcount/cstring.rs | 1128 ++++++++++++++++++++++++++++ 3 files changed, 1299 insertions(+), 1 deletion(-) create mode 100644 tests/unit/out/refcount/cstring.rs diff --git a/rules/cstring/tgt_refcount.rs b/rules/cstring/tgt_refcount.rs index 425d28ee..d762c964 100644 --- a/rules/cstring/tgt_refcount.rs +++ b/rules/cstring/tgt_refcount.rs @@ -258,3 +258,174 @@ fn f28(a0: i32, a1: Ptr, a2: usize) -> i32 { } 0 } + +fn f6(a0: Ptr, a1: i32) -> Ptr { + let mut __i: usize = 0; + loop { + let __c = a0.offset(__i).read(); + if __c == a1 as u8 { + break a0.offset(__i); + } + if __c == 0 { + break Ptr::null(); + } + __i += 1; + } +} + +fn f12(a0: AnyPtr, a1: i32, a2: usize) -> AnyPtr { + match (0..a2).find(|&__i| a0.reinterpret_cast::().offset(__i).read() == a1 as u8) { + Some(__i) => a0.reinterpret_cast::().offset(__i).to_any(), + None => Ptr::::null().to_any(), + } +} + +fn f13(a0: Ptr, a1: i32) -> Ptr { + let mut __i: usize = 0; + let mut __found = Ptr::null(); + loop { + let __c = a0.offset(__i).read(); + if __c == a1 as u8 { + __found = a0.offset(__i); + } + if __c == 0 { + break __found; + } + __i += 1; + } +} + +fn f14(a0: Ptr, a1: i32) -> Ptr { + let mut __i: usize = 0; + let mut __found = Ptr::null(); + loop { + let __c = a0.offset(__i).read(); + if __c == a1 as u8 { + __found = a0.offset(__i); + } + if __c == 0 { + break __found; + } + __i += 1; + } +} + +fn f19(a0: Ptr, a1: Ptr) -> Ptr { + let mut __s: usize = 0; + loop { + let mut __i: usize = 0; + let __matched = loop { + let __n = a1.offset(__i).read(); + if __n == 0 { + break true; + } + if a0.offset(__s + __i).read() != __n { + break false; + } + __i += 1; + }; + if __matched { + break a0.offset(__s); + } + if a0.offset(__s).read() == 0 { + break Ptr::null(); + } + __s += 1; + } +} + +fn f20(a0: Ptr, a1: Ptr) -> Ptr { + let mut __s: usize = 0; + loop { + let mut __i: usize = 0; + let __matched = loop { + let __n = a1.offset(__i).read(); + if __n == 0 { + break true; + } + if a0.offset(__s + __i).read() != __n { + break false; + } + __i += 1; + }; + if __matched { + break a0.offset(__s); + } + if a0.offset(__s).read() == 0 { + break Ptr::null(); + } + __s += 1; + } +} + +fn f22(a0: Ptr, a1: Ptr) -> Ptr { + let mut __i: usize = 0; + loop { + let __c = a0.offset(__i).read(); + if __c == 0 { + break Ptr::null(); + } + let mut __j: usize = 0; + let __hit = loop { + let __r = a1.offset(__j).read(); + if __r == 0 { + break false; + } + if __r == __c { + break true; + } + __j += 1; + }; + if __hit { + break a0.offset(__i); + } + __i += 1; + } +} + +fn f23(a0: Ptr, a1: Ptr) -> Ptr { + let mut __i: usize = 0; + loop { + let __c = a0.offset(__i).read(); + if __c == 0 { + break Ptr::null(); + } + let mut __j: usize = 0; + let __hit = loop { + let __r = a1.offset(__j).read(); + if __r == 0 { + break false; + } + if __r == __c { + break true; + } + __j += 1; + }; + if __hit { + break a0.offset(__i); + } + __i += 1; + } +} + +#[cfg(target_os = "linux")] +fn f25(a0: AnyPtr, a1: i32, a2: usize) -> AnyPtr { + match (0..a2) + .rev() + .find(|&__i| a0.reinterpret_cast::().offset(__i).read() == a1 as u8) + { + Some(__i) => a0.reinterpret_cast::().offset(__i).to_any(), + None => Ptr::::null().to_any(), + } +} + +#[cfg(target_os = "linux")] +fn f26(a0: AnyPtr, a1: i32, a2: usize) -> AnyPtr { + match (0..a2) + .rev() + .find(|&__i| a0.reinterpret_cast::().offset(__i).read() == a1 as u8) + { + Some(__i) => a0.reinterpret_cast::().offset(__i).to_any(), + None => Ptr::::null().to_any(), + } +} diff --git a/tests/unit/cstring.cpp b/tests/unit/cstring.cpp index e72c26df..a3434272 100644 --- a/tests/unit/cstring.cpp +++ b/tests/unit/cstring.cpp @@ -1,4 +1,3 @@ -// no-compile: refcount #define _GNU_SOURCE #include #include diff --git a/tests/unit/out/refcount/cstring.rs b/tests/unit/out/refcount/cstring.rs new file mode 100644 index 00000000..f469dcac --- /dev/null +++ b/tests/unit/out/refcount/cstring.rs @@ -0,0 +1,1128 @@ +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 test_memcpy_0() { + let src: Value> = Rc::new(RefCell::new(Box::from(*b"hello\0"))); + let dst: Value> = Rc::new(RefCell::new(Box::new([ + 0_u8, + ::default(), + ::default(), + ::default(), + ::default(), + ::default(), + ]))); + let r: Value = Rc::new(RefCell::new({ + ((dst.as_pointer() as Ptr) as Ptr).to_any().memcpy( + &((src.as_pointer() as Ptr) as Ptr).to_any(), + 6_usize as usize, + ); + ((dst.as_pointer() as Ptr) as Ptr).to_any().clone() + })); + assert!({ + let _lhs = (*r.borrow()).clone(); + _lhs == ((dst.as_pointer() as Ptr) as Ptr).to_any() + }); + assert!( + ((((*dst.borrow())[(0) as usize] as i32) == (('h' as u8) as i32)) + && (((*dst.borrow())[(1) as usize] as i32) == (('e' as u8) as i32))) + && (((*dst.borrow())[(2) as usize] as i32) == (('l' as u8) as i32)) + ); + assert!( + ((((*dst.borrow())[(3) as usize] as i32) == (('l' as u8) as i32)) + && (((*dst.borrow())[(4) as usize] as i32) == (('o' as u8) as i32))) + && (((*dst.borrow())[(5) as usize] as i32) == (('\0' as u8) as i32)) + ); +} +pub fn test_memset_1() { + let buf: Value> = Rc::new(RefCell::new( + (0..4).map(|_| ::default()).collect::>(), + )); + let r: Value = Rc::new(RefCell::new({ + ((buf.as_pointer() as Ptr) as Ptr) + .to_any() + .memset((('x' as u8) as i32) as u8, 4_usize as usize); + ((buf.as_pointer() as Ptr) as Ptr).to_any().clone() + })); + assert!({ + let _lhs = (*r.borrow()).clone(); + _lhs == ((buf.as_pointer() as Ptr) as Ptr).to_any() + }); + assert!( + (((((*buf.borrow())[(0) as usize] as i32) == (('x' as u8) as i32)) + && (((*buf.borrow())[(1) as usize] as i32) == (('x' as u8) as i32))) + && (((*buf.borrow())[(2) as usize] as i32) == (('x' as u8) as i32))) + && (((*buf.borrow())[(3) as usize] as i32) == (('x' as u8) as i32)) + ); +} +pub fn test_memcmp_2() { + let a: Value> = Rc::new(RefCell::new(Box::new([1_u8, 2_u8, 3_u8, 4_u8]))); + let b: Value> = Rc::new(RefCell::new(Box::new([1_u8, 2_u8, 3_u8, 4_u8]))); + let c: Value> = Rc::new(RefCell::new(Box::new([1_u8, 2_u8, 9_u8, 4_u8]))); + assert!( + (((a.as_pointer() as Ptr::) as Ptr::) + .to_any() + .memcmp( + &((b.as_pointer() as Ptr::) as Ptr::).to_any(), + 4_usize + ) + == 0) + ); + assert!( + (((a.as_pointer() as Ptr::) as Ptr::) + .to_any() + .memcmp( + &((c.as_pointer() as Ptr::) as Ptr::).to_any(), + 4_usize + ) + < 0) + ); + assert!( + (((c.as_pointer() as Ptr::) as Ptr::) + .to_any() + .memcmp( + &((a.as_pointer() as Ptr::) as Ptr::).to_any(), + 4_usize + ) + > 0) + ); +} +pub fn test_memmove_3() { + let buf: Value> = Rc::new(RefCell::new(Box::new([ + ('a' as u8), + ('b' as u8), + ('c' as u8), + ('d' as u8), + ('e' as u8), + ('\0' as u8), + ]))); + let r: Value = Rc::new(RefCell::new({ + let __tmp: Vec = (0..4_usize) + .map(|__i| { + ((buf.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .offset(__i) + .read() + }) + .collect(); + for __i in 0..4_usize { + ((buf.as_pointer() as Ptr).offset((1) as isize) as Ptr) + .to_any() + .reinterpret_cast::() + .offset(__i) + .write(__tmp[__i]); + } + ((buf.as_pointer() as Ptr).offset((1) as isize) as Ptr) + .to_any() + .clone() + })); + assert!({ + let _lhs = (*r.borrow()).clone(); + _lhs == ((buf.as_pointer() as Ptr).offset((1) as isize) as Ptr).to_any() + }); + assert!( + ((((*buf.borrow())[(0) as usize] as i32) == (('a' as u8) as i32)) + && (((*buf.borrow())[(1) as usize] as i32) == (('a' as u8) as i32))) + && (((*buf.borrow())[(2) as usize] as i32) == (('b' as u8) as i32)) + ); + assert!( + ((((*buf.borrow())[(3) as usize] as i32) == (('c' as u8) as i32)) + && (((*buf.borrow())[(4) as usize] as i32) == (('d' as u8) as i32))) + && (((*buf.borrow())[(5) as usize] as i32) == (('\0' as u8) as i32)) + ); +} +pub fn test_strchr_4() { + let s: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"hello world"))); + let r: Value> = Rc::new(RefCell::new({ + let mut __i: usize = 0; + loop { + let __c = (*s.borrow()).offset(__i).read(); + if __c == (('w' as u8) as i32) as u8 { + break (*s.borrow()).offset(__i); + } + if __c == 0 { + break Ptr::null(); + } + __i += 1; + } + })); + assert!(!((*r.borrow()).is_null())); + assert!(((((*r.borrow()).read()) as i32) == (('w' as u8) as i32))); + assert!( + ({ + let mut __i: usize = 0; + loop { + let __c = (*s.borrow()).offset(__i).read(); + if __c == (('z' as u8) as i32) as u8 { + break (*s.borrow()).offset(__i); + } + if __c == 0 { + break Ptr::null(); + } + __i += 1; + } + }) + .is_null() + ); +} +pub fn test_strlen_5() { + assert!( + ({ + let mut __i: usize = 0; + while Ptr::from_string_literal(b"").offset(__i).read() != 0 { + __i += 1; + } + __i + } == 0_usize) + ); + assert!( + ({ + let mut __i: usize = 0; + while Ptr::from_string_literal(b"hello").offset(__i).read() != 0 { + __i += 1; + } + __i + } == 5_usize) + ); + assert!( + ({ + let mut __i: usize = 0; + while Ptr::from_string_literal(b"hello world").offset(__i).read() != 0 { + __i += 1; + } + __i + } == 11_usize) + ); +} +pub fn test_strcmp_6() { + assert!( + ({ + let mut __i: usize = 0; + loop { + let __c1 = Ptr::from_string_literal(b"abc").offset(__i).read(); + let __c2 = Ptr::from_string_literal(b"abc").offset(__i).read(); + if __c1 != __c2 { + break (__c1 as i32) - (__c2 as i32); + } + if __c1 == 0 { + break 0; + } + __i += 1; + } + } == 0) + ); + assert!( + ({ + let mut __i: usize = 0; + loop { + let __c1 = Ptr::from_string_literal(b"abc").offset(__i).read(); + let __c2 = Ptr::from_string_literal(b"abd").offset(__i).read(); + if __c1 != __c2 { + break (__c1 as i32) - (__c2 as i32); + } + if __c1 == 0 { + break 0; + } + __i += 1; + } + } < 0) + ); + assert!( + ({ + let mut __i: usize = 0; + loop { + let __c1 = Ptr::from_string_literal(b"abd").offset(__i).read(); + let __c2 = Ptr::from_string_literal(b"abc").offset(__i).read(); + if __c1 != __c2 { + break (__c1 as i32) - (__c2 as i32); + } + if __c1 == 0 { + break 0; + } + __i += 1; + } + } > 0) + ); + let p: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"abc"))); + let q: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"abd"))); + let buf: Value> = Rc::new(RefCell::new(Box::new([ + ('a' as u8), + ('b' as u8), + ('c' as u8), + ('\0' as u8), + ]))); + assert!( + ({ + let mut __i: usize = 0; + loop { + let __c1 = (*p.borrow()).offset(__i).read(); + let __c2 = (*p.borrow()).offset(__i).read(); + if __c1 != __c2 { + break (__c1 as i32) - (__c2 as i32); + } + if __c1 == 0 { + break 0; + } + __i += 1; + } + } == 0) + ); + assert!( + ({ + let mut __i: usize = 0; + loop { + let __c1 = (*p.borrow()).offset(__i).read(); + let __c2 = (*q.borrow()).offset(__i).read(); + if __c1 != __c2 { + break (__c1 as i32) - (__c2 as i32); + } + if __c1 == 0 { + break 0; + } + __i += 1; + } + } < 0) + ); + assert!( + ({ + let mut __i: usize = 0; + loop { + let __c1 = (buf.as_pointer() as Ptr).offset(__i).read(); + let __c2 = (*p.borrow()).offset(__i).read(); + if __c1 != __c2 { + break (__c1 as i32) - (__c2 as i32); + } + if __c1 == 0 { + break 0; + } + __i += 1; + } + } == 0) + ); +} +pub fn test_strncmp_7() { + assert!( + ({ + let mut __i: usize = 0; + loop { + if __i == 3_usize { + break 0; + } + let __c1 = Ptr::from_string_literal(b"abcdef").offset(__i).read(); + let __c2 = Ptr::from_string_literal(b"abcxyz").offset(__i).read(); + if __c1 != __c2 { + break (__c1 as i32) - (__c2 as i32); + } + if __c1 == 0 { + break 0; + } + __i += 1; + } + } == 0) + ); + assert!( + ({ + let mut __i: usize = 0; + loop { + if __i == 4_usize { + break 0; + } + let __c1 = Ptr::from_string_literal(b"abcdef").offset(__i).read(); + let __c2 = Ptr::from_string_literal(b"abcxyz").offset(__i).read(); + if __c1 != __c2 { + break (__c1 as i32) - (__c2 as i32); + } + if __c1 == 0 { + break 0; + } + __i += 1; + } + } < 0) + ); + assert!( + ({ + let mut __i: usize = 0; + loop { + if __i == 4_usize { + break 0; + } + let __c1 = Ptr::from_string_literal(b"abcxyz").offset(__i).read(); + let __c2 = Ptr::from_string_literal(b"abcdef").offset(__i).read(); + if __c1 != __c2 { + break (__c1 as i32) - (__c2 as i32); + } + if __c1 == 0 { + break 0; + } + __i += 1; + } + } > 0) + ); + let p: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"abcdef"))); + let q: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"abcxyz"))); + let buf: Value> = Rc::new(RefCell::new(Box::new([ + ('a' as u8), + ('b' as u8), + ('c' as u8), + ('d' as u8), + ('e' as u8), + ('f' as u8), + ('\0' as u8), + ]))); + let n: Value = Rc::new(RefCell::new(3_usize)); + assert!( + ({ + let mut __i: usize = 0; + loop { + if __i == (*n.borrow()) { + break 0; + } + let __c1 = (*p.borrow()).offset(__i).read(); + let __c2 = (*q.borrow()).offset(__i).read(); + if __c1 != __c2 { + break (__c1 as i32) - (__c2 as i32); + } + if __c1 == 0 { + break 0; + } + __i += 1; + } + } == 0) + ); + assert!( + ({ + let mut __i: usize = 0; + loop { + if __i == (*n.borrow()).wrapping_add(1_usize) { + break 0; + } + let __c1 = (*p.borrow()).offset(__i).read(); + let __c2 = (*q.borrow()).offset(__i).read(); + if __c1 != __c2 { + break (__c1 as i32) - (__c2 as i32); + } + if __c1 == 0 { + break 0; + } + __i += 1; + } + } < 0) + ); + assert!( + ({ + let mut __i: usize = 0; + loop { + if __i == 6_usize { + break 0; + } + let __c1 = (buf.as_pointer() as Ptr).offset(__i).read(); + let __c2 = (*p.borrow()).offset(__i).read(); + if __c1 != __c2 { + break (__c1 as i32) - (__c2 as i32); + } + if __c1 == 0 { + break 0; + } + __i += 1; + } + } == 0) + ); +} +pub fn test_memchr_8() { + let data: Value> = Rc::new(RefCell::new(Box::new([16_u8, 32_u8, 48_u8, 64_u8]))); + let r: Value = Rc::new(RefCell::new( + match (0..4_usize).find(|&__i| { + ((data.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .offset(__i) + .read() + == 48 as u8 + }) { + Some(__i) => ((data.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .offset(__i) + .to_any(), + None => Ptr::::null().to_any(), + }, + )); + assert!({ + let _lhs = (*r.borrow()).clone(); + _lhs == (((data.as_pointer() as Ptr).offset(2)) as Ptr).to_any() + }); + assert!( + (match (0..4_usize).find(|&__i| ((data.as_pointer() as Ptr::) as Ptr::) + .to_any() + .reinterpret_cast::() + .offset(__i) + .read() + == 153 as u8) + { + Some(__i) => ((data.as_pointer() as Ptr::) as Ptr::) + .to_any() + .reinterpret_cast::() + .offset(__i) + .to_any(), + None => Ptr::::null().to_any(), + }) + .is_null() + ); + let p: Value = Rc::new(RefCell::new( + ((data.as_pointer() as Ptr) as Ptr).to_any(), + )); + let n: Value = Rc::new(RefCell::new(4_usize)); + assert!({ + let _lhs = match (0..(*n.borrow())) + .find(|&__i| (*p.borrow()).reinterpret_cast::().offset(__i).read() == 16 as u8) + { + Some(__i) => (*p.borrow()).reinterpret_cast::().offset(__i).to_any(), + None => Ptr::::null().to_any(), + }; + _lhs == (*p.borrow()).clone() + }); +} +pub fn test_strrchr_9() { + let s: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"hello world"))); + let r: Value> = Rc::new(RefCell::new({ + let mut __i: usize = 0; + let mut __found = Ptr::null(); + loop { + let __c = (*s.borrow()).offset(__i).read(); + if __c == (('l' as u8) as i32) as u8 { + __found = (*s.borrow()).offset(__i); + } + if __c == 0 { + break __found; + } + __i += 1; + } + })); + assert!(!((*r.borrow()).is_null())); + assert!(((((*r.borrow()).read()) as i32) == (('l' as u8) as i32))); + assert!({ + let _lhs = (*r.borrow()).clone(); + _lhs == (*s.borrow()).offset((9) as isize) + }); + assert!( + ({ + let mut __i: usize = 0; + let mut __found = Ptr::null(); + loop { + let __c = (*s.borrow()).offset(__i).read(); + if __c == (('z' as u8) as i32) as u8 { + __found = (*s.borrow()).offset(__i); + } + if __c == 0 { + break __found; + } + __i += 1; + } + }) + .is_null() + ); + let buf: Value> = Rc::new(RefCell::new(Box::new([ + ('a' as u8), + ('b' as u8), + ('a' as u8), + ('\0' as u8), + ]))); + assert!( + ({ + let mut __i: usize = 0; + let mut __found = Ptr::null(); + loop { + let __c = (buf.as_pointer() as Ptr).offset(__i).read(); + if __c == (('a' as u8) as i32) as u8 { + __found = (buf.as_pointer() as Ptr).offset(__i); + } + if __c == 0 { + break __found; + } + __i += 1; + } + } == ((buf.as_pointer() as Ptr).offset(2))) + ); +} +pub fn test_strdup_10() { + let d: Value> = Rc::new(RefCell::new(libcc2rs::strdup_refcount( + Ptr::from_string_literal(b"hello").clone(), + ))); + assert!(!((*d.borrow()).is_null())); + assert!( + ({ + let mut __i: usize = 0; + loop { + let __c1 = (*d.borrow()).offset(__i).read(); + let __c2 = Ptr::from_string_literal(b"hello").offset(__i).read(); + if __c1 != __c2 { + break (__c1 as i32) - (__c2 as i32); + } + if __c1 == 0 { + break 0; + } + __i += 1; + } + } == 0) + ); + libcc2rs::free_refcount(((*d.borrow()).clone() as Ptr).to_any()); + let p: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"world"))); + let buf: Value> = Rc::new(RefCell::new(Box::new([ + ('a' as u8), + ('b' as u8), + ('c' as u8), + ('\0' as u8), + ]))); + let d2: Value> = Rc::new(RefCell::new(libcc2rs::strdup_refcount( + (*p.borrow()).clone(), + ))); + assert!(!((*d2.borrow()).is_null())); + assert!( + ({ + let mut __i: usize = 0; + loop { + let __c1 = (*d2.borrow()).offset(__i).read(); + let __c2 = (*p.borrow()).offset(__i).read(); + if __c1 != __c2 { + break (__c1 as i32) - (__c2 as i32); + } + if __c1 == 0 { + break 0; + } + __i += 1; + } + } == 0) + ); + libcc2rs::free_refcount(((*d2.borrow()).clone() as Ptr).to_any()); + let d3: Value> = Rc::new(RefCell::new(libcc2rs::strdup_refcount( + (buf.as_pointer() as Ptr).clone(), + ))); + assert!(!((*d3.borrow()).is_null())); + assert!( + ({ + let mut __i: usize = 0; + loop { + let __c1 = (*d3.borrow()).offset(__i).read(); + let __c2 = (buf.as_pointer() as Ptr).offset(__i).read(); + if __c1 != __c2 { + break (__c1 as i32) - (__c2 as i32); + } + if __c1 == 0 { + break 0; + } + __i += 1; + } + } == 0) + ); + libcc2rs::free_refcount(((*d3.borrow()).clone() as Ptr).to_any()); +} +pub fn test_strcspn_11() { + assert!( + ({ + let mut __i: usize = 0; + loop { + let __c = Ptr::from_string_literal(b"hello").offset(__i).read(); + if __c == 0 { + break __i; + } + let mut __j: usize = 0; + let __hit = loop { + let __r = Ptr::from_string_literal(b"el").offset(__j).read(); + if __r == 0 { + break false; + } + if __r == __c { + break true; + } + __j += 1; + }; + if __hit { + break __i; + } + __i += 1; + } + } == 1_usize) + ); + assert!( + ({ + let mut __i: usize = 0; + loop { + let __c = Ptr::from_string_literal(b"abc").offset(__i).read(); + if __c == 0 { + break __i; + } + let mut __j: usize = 0; + let __hit = loop { + let __r = Ptr::from_string_literal(b"xyz").offset(__j).read(); + if __r == 0 { + break false; + } + if __r == __c { + break true; + } + __j += 1; + }; + if __hit { + break __i; + } + __i += 1; + } + } == 3_usize) + ); + assert!( + ({ + let mut __i: usize = 0; + loop { + let __c = Ptr::from_string_literal(b"").offset(__i).read(); + if __c == 0 { + break __i; + } + let mut __j: usize = 0; + let __hit = loop { + let __r = Ptr::from_string_literal(b"abc").offset(__j).read(); + if __r == 0 { + break false; + } + if __r == __c { + break true; + } + __j += 1; + }; + if __hit { + break __i; + } + __i += 1; + } + } == 0_usize) + ); + let s: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"hello"))); + let rej: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"el"))); + assert!( + ({ + let mut __i: usize = 0; + loop { + let __c = (*s.borrow()).offset(__i).read(); + if __c == 0 { + break __i; + } + let mut __j: usize = 0; + let __hit = loop { + let __r = (*rej.borrow()).offset(__j).read(); + if __r == 0 { + break false; + } + if __r == __c { + break true; + } + __j += 1; + }; + if __hit { + break __i; + } + __i += 1; + } + } == 1_usize) + ); +} +pub fn test_strspn_12() { + assert!( + ({ + let mut __i: usize = 0; + loop { + let __c = Ptr::from_string_literal(b"hello").offset(__i).read(); + if __c == 0 { + break __i; + } + let mut __j: usize = 0; + let __hit = loop { + let __r = Ptr::from_string_literal(b"hel").offset(__j).read(); + if __r == 0 { + break false; + } + if __r == __c { + break true; + } + __j += 1; + }; + if !__hit { + break __i; + } + __i += 1; + } + } == 4_usize) + ); + assert!( + ({ + let mut __i: usize = 0; + loop { + let __c = Ptr::from_string_literal(b"abc").offset(__i).read(); + if __c == 0 { + break __i; + } + let mut __j: usize = 0; + let __hit = loop { + let __r = Ptr::from_string_literal(b"xyz").offset(__j).read(); + if __r == 0 { + break false; + } + if __r == __c { + break true; + } + __j += 1; + }; + if !__hit { + break __i; + } + __i += 1; + } + } == 0_usize) + ); + assert!( + ({ + let mut __i: usize = 0; + loop { + let __c = Ptr::from_string_literal(b"aaa").offset(__i).read(); + if __c == 0 { + break __i; + } + let mut __j: usize = 0; + let __hit = loop { + let __r = Ptr::from_string_literal(b"a").offset(__j).read(); + if __r == 0 { + break false; + } + if __r == __c { + break true; + } + __j += 1; + }; + if !__hit { + break __i; + } + __i += 1; + } + } == 3_usize) + ); + let s: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"hello"))); + let acc: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"hel"))); + assert!( + ({ + let mut __i: usize = 0; + loop { + let __c = (*s.borrow()).offset(__i).read(); + if __c == 0 { + break __i; + } + let mut __j: usize = 0; + let __hit = loop { + let __r = (*acc.borrow()).offset(__j).read(); + if __r == 0 { + break false; + } + if __r == __c { + break true; + } + __j += 1; + }; + if !__hit { + break __i; + } + __i += 1; + } + } == 4_usize) + ); +} +pub fn test_strstr_13() { + let h: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"hello world"))); + let r: Value> = Rc::new(RefCell::new({ + let mut __s: usize = 0; + loop { + let mut __i: usize = 0; + let __matched = loop { + let __n = Ptr::from_string_literal(b"world").offset(__i).read(); + if __n == 0 { + break true; + } + if (*h.borrow()).offset(__s + __i).read() != __n { + break false; + } + __i += 1; + }; + if __matched { + break (*h.borrow()).offset(__s); + } + if (*h.borrow()).offset(__s).read() == 0 { + break Ptr::null(); + } + __s += 1; + } + })); + assert!(!((*r.borrow()).is_null())); + assert!({ + let _lhs = (*r.borrow()).clone(); + _lhs == (*h.borrow()).offset((6) as isize) + }); + assert!( + ({ + let mut __s: usize = 0; + loop { + let mut __i: usize = 0; + let __matched = loop { + let __n = Ptr::from_string_literal(b"xyz").offset(__i).read(); + if __n == 0 { + break true; + } + if (*h.borrow()).offset(__s + __i).read() != __n { + break false; + } + __i += 1; + }; + if __matched { + break (*h.borrow()).offset(__s); + } + if (*h.borrow()).offset(__s).read() == 0 { + break Ptr::null(); + } + __s += 1; + } + }) + .is_null() + ); + let buf: Value> = Rc::new(RefCell::new(Box::new([ + ('h' as u8), + ('e' as u8), + ('l' as u8), + ('l' as u8), + ('o' as u8), + ('\0' as u8), + ]))); + assert!( + ({ + let mut __s: usize = 0; + loop { + let mut __i: usize = 0; + let __matched = loop { + let __n = Ptr::from_string_literal(b"ll").offset(__i).read(); + if __n == 0 { + break true; + } + if (buf.as_pointer() as Ptr).offset(__s + __i).read() != __n { + break false; + } + __i += 1; + }; + if __matched { + break (buf.as_pointer() as Ptr).offset(__s); + } + if (buf.as_pointer() as Ptr).offset(__s).read() == 0 { + break Ptr::null(); + } + __s += 1; + } + } == ((buf.as_pointer() as Ptr).offset(2))) + ); +} +pub fn test_strpbrk_14() { + let s: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"hello world"))); + let r: Value> = Rc::new(RefCell::new({ + let mut __i: usize = 0; + loop { + let __c = (*s.borrow()).offset(__i).read(); + if __c == 0 { + break Ptr::null(); + } + let mut __j: usize = 0; + let __hit = loop { + let __r = Ptr::from_string_literal(b"wo").offset(__j).read(); + if __r == 0 { + break false; + } + if __r == __c { + break true; + } + __j += 1; + }; + if __hit { + break (*s.borrow()).offset(__i); + } + __i += 1; + } + })); + assert!(!((*r.borrow()).is_null())); + assert!({ + let _lhs = (*r.borrow()).clone(); + _lhs == (*s.borrow()).offset((4) as isize) + }); + assert!( + ({ + let mut __i: usize = 0; + loop { + let __c = (*s.borrow()).offset(__i).read(); + if __c == 0 { + break Ptr::null(); + } + let mut __j: usize = 0; + let __hit = loop { + let __r = Ptr::from_string_literal(b"xyz").offset(__j).read(); + if __r == 0 { + break false; + } + if __r == __c { + break true; + } + __j += 1; + }; + if __hit { + break (*s.borrow()).offset(__i); + } + __i += 1; + } + }) + .is_null() + ); + let buf: Value> = Rc::new(RefCell::new(Box::new([ + ('a' as u8), + ('b' as u8), + ('c' as u8), + ('\0' as u8), + ]))); + assert!( + ({ + let mut __i: usize = 0; + loop { + let __c = (buf.as_pointer() as Ptr).offset(__i).read(); + if __c == 0 { + break Ptr::null(); + } + let mut __j: usize = 0; + let __hit = loop { + let __r = Ptr::from_string_literal(b"b").offset(__j).read(); + if __r == 0 { + break false; + } + if __r == __c { + break true; + } + __j += 1; + }; + if __hit { + break (buf.as_pointer() as Ptr).offset(__i); + } + __i += 1; + } + } == ((buf.as_pointer() as Ptr).offset(1))) + ); +} +pub fn test_strcasecmp_15() { + assert!( + ({ + let mut __i: usize = 0; + loop { + let __c1 = Ptr::from_string_literal(b"HELLO") + .offset(__i) + .read() + .to_ascii_lowercase(); + let __c2 = Ptr::from_string_literal(b"hello") + .offset(__i) + .read() + .to_ascii_lowercase(); + if __c1 != __c2 { + break (__c1 as i32) - (__c2 as i32); + } + if __c1 == 0 { + break 0; + } + __i += 1; + } + } == 0) + ); + assert!( + ({ + let mut __i: usize = 0; + loop { + let __c1 = Ptr::from_string_literal(b"abc") + .offset(__i) + .read() + .to_ascii_lowercase(); + let __c2 = Ptr::from_string_literal(b"abd") + .offset(__i) + .read() + .to_ascii_lowercase(); + if __c1 != __c2 { + break (__c1 as i32) - (__c2 as i32); + } + if __c1 == 0 { + break 0; + } + __i += 1; + } + } < 0) + ); + assert!( + ({ + let mut __i: usize = 0; + loop { + let __c1 = Ptr::from_string_literal(b"abd") + .offset(__i) + .read() + .to_ascii_lowercase(); + let __c2 = Ptr::from_string_literal(b"abc") + .offset(__i) + .read() + .to_ascii_lowercase(); + if __c1 != __c2 { + break (__c1 as i32) - (__c2 as i32); + } + if __c1 == 0 { + break 0; + } + __i += 1; + } + } > 0) + ); + let p: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"FOO"))); + let q: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"foo"))); + assert!( + ({ + let mut __i: usize = 0; + loop { + let __c1 = (*p.borrow()).offset(__i).read().to_ascii_lowercase(); + let __c2 = (*q.borrow()).offset(__i).read().to_ascii_lowercase(); + if __c1 != __c2 { + break (__c1 as i32) - (__c2 as i32); + } + if __c1 == 0 { + break 0; + } + __i += 1; + } + } == 0) + ); +} +pub fn main() { + std::process::exit(main_0()); +} +fn main_0() -> i32 { + ({ test_memcpy_0() }); + ({ test_memset_1() }); + ({ test_memcmp_2() }); + ({ test_memmove_3() }); + ({ test_strchr_4() }); + ({ test_strlen_5() }); + ({ test_strcmp_6() }); + ({ test_strncmp_7() }); + ({ test_memchr_8() }); + ({ test_strrchr_9() }); + ({ test_strdup_10() }); + ({ test_strcspn_11() }); + ({ test_strspn_12() }); + ({ test_strstr_13() }); + ({ test_strpbrk_14() }); + ({ test_strcasecmp_15() }); + return 0; +} From 6355eac74a0cc30ee6dd6d202ad6a2fd040de4f5 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Mon, 13 Jul 2026 21:01:18 +0100 Subject: [PATCH 06/15] Update tests --- tests/unit/strdup.c | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/unit/strdup.c b/tests/unit/strdup.c index eb1f4cc2..49fa29cc 100644 --- a/tests/unit/strdup.c +++ b/tests/unit/strdup.c @@ -1,4 +1,3 @@ -// panic: refcount #include #include #include From 044dc9c103ae52ce543462dff7ced37a78967930 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 14 Jul 2026 23:25:33 +0100 Subject: [PATCH 07/15] Update tests --- tests/unit/out/refcount/errno.rs | 2 +- tests/unit/out/refcount/offsetof.rs | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/tests/unit/out/refcount/errno.rs b/tests/unit/out/refcount/errno.rs index 653f5b96..5960da10 100644 --- a/tests/unit/out/refcount/errno.rs +++ b/tests/unit/out/refcount/errno.rs @@ -18,7 +18,7 @@ pub fn test_errno_0() { pub fn test_errno_preserved_across_strdup_1() { libcc2rs::cpp2rust_errno().write(99); let d: Value> = Rc::new(RefCell::new(libcc2rs::strdup_refcount( - Ptr::from_string_literal(b"hello"), + Ptr::from_string_literal(b"hello").clone(), ))); assert!((((!((*d.borrow()).is_null())) as i32) != 0)); assert!(((((libcc2rs::cpp2rust_errno().read()) == 99) as i32) != 0)); diff --git a/tests/unit/out/refcount/offsetof.rs b/tests/unit/out/refcount/offsetof.rs index ffb50b8b..d0c2cf5a 100644 --- a/tests/unit/out/refcount/offsetof.rs +++ b/tests/unit/out/refcount/offsetof.rs @@ -102,7 +102,14 @@ fn main_0() -> i32 { assert!(((*(*v.borrow()).b.borrow()) == 305419896_u32)); let text: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"example-body"))); let len: Value = Rc::new(RefCell::new( - ((*text.borrow()).to_string_iterator().count()).wrapping_add(1_usize), + ({ + let mut __i: usize = 0; + while (*text.borrow()).offset(__i).read() != 0 { + __i += 1; + } + __i + }) + .wrapping_add(1_usize), )); let total: Value = Rc::new(RefCell::new( ((2_usize as u64).wrapping_add(((*len.borrow()) as u64)) as usize), From 60cddf0f1a4556fcb71179584445aa7b84c9dcd1 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 14 Jul 2026 23:40:43 +0100 Subject: [PATCH 08/15] Optimize rules --- rules/cstring/tgt_refcount.rs | 294 ++++++++----- tests/unit/out/refcount/cstring.rs | 534 ++++++++++++---------- tests/unit/out/refcount/offsetof.rs | 4 +- tests/unit/out/refcount/strdup.rs | 52 ++- tests/unit/out/refcount/string_h.rs | 536 +++++++++++++---------- tests/unit/out/refcount/va_arg_printf.rs | 8 +- 6 files changed, 815 insertions(+), 613 deletions(-) diff --git a/rules/cstring/tgt_refcount.rs b/rules/cstring/tgt_refcount.rs index d762c964..3e09e03f 100644 --- a/rules/cstring/tgt_refcount.rs +++ b/rules/cstring/tgt_refcount.rs @@ -18,89 +18,110 @@ fn f3(a0: AnyPtr, a1: AnyPtr, a2: usize) -> i32 { } fn f4(a0: AnyPtr, a1: AnyPtr, a2: usize) -> AnyPtr { - let __tmp: Vec = (0..a2) - .map(|__i| a1.reinterpret_cast::().offset(__i).read()) - .collect(); + let mut __src = a1.reinterpret_cast::(); + let mut __tmp: Vec = Vec::with_capacity(a2); + for _ in 0..a2 { + __tmp.push(__src.read()); + __src += 1; + } + let mut __dst = a0.reinterpret_cast::(); for __i in 0..a2 { - a0.reinterpret_cast::().offset(__i).write(__tmp[__i]); + __dst.write(__tmp[__i]); + __dst += 1; } a0.clone() } fn f5(a0: Ptr, a1: i32) -> Ptr { - let mut __i: usize = 0; + let mut __p = a0.clone(); loop { - let __c = a0.offset(__i).read(); + let __c = __p.read(); if __c == a1 as u8 { - break a0.offset(__i); + break __p; } if __c == 0 { break Ptr::null(); } - __i += 1; + __p += 1; } } unsafe fn f7(a0: Ptr) -> usize { + let mut __p = a0.clone(); let mut __i: usize = 0; - while a0.offset(__i).read() != 0 { + while __p.read() != 0 { + __p += 1; __i += 1; } __i } fn f8(a0: Ptr, a1: Ptr) -> i32 { - let mut __i: usize = 0; + let mut __p1 = a0.clone(); + let mut __p2 = a1.clone(); loop { - let __c1 = a0.offset(__i).read(); - let __c2 = a1.offset(__i).read(); + let __c1 = __p1.read(); + let __c2 = __p2.read(); if __c1 != __c2 { break (__c1 as i32) - (__c2 as i32); } if __c1 == 0 { break 0; } - __i += 1; + __p1 += 1; + __p2 += 1; } } fn f9(a0: Ptr, a1: Ptr, a2: usize) -> i32 { + let mut __p1 = a0.clone(); + let mut __p2 = a1.clone(); let mut __i: usize = 0; loop { if __i == a2 { break 0; } - let __c1 = a0.offset(__i).read(); - let __c2 = a1.offset(__i).read(); + let __c1 = __p1.read(); + let __c2 = __p2.read(); if __c1 != __c2 { break (__c1 as i32) - (__c2 as i32); } if __c1 == 0 { break 0; } + __p1 += 1; + __p2 += 1; __i += 1; } } fn f10(a0: AnyPtr, a1: i32, a2: usize) -> AnyPtr { - match (0..a2).find(|&__i| a0.reinterpret_cast::().offset(__i).read() == a1 as u8) { - Some(__i) => a0.reinterpret_cast::().offset(__i).to_any(), - None => Ptr::::null().to_any(), + let mut __p = a0.reinterpret_cast::(); + let mut __i: usize = 0; + loop { + if __i == a2 { + break Ptr::::null().to_any(); + } + if __p.read() == a1 as u8 { + break __p.to_any(); + } + __p += 1; + __i += 1; } } fn f11(a0: Ptr, a1: i32) -> Ptr { - let mut __i: usize = 0; + let mut __p = a0.clone(); let mut __found = Ptr::null(); loop { - let __c = a0.offset(__i).read(); + let __c = __p.read(); if __c == a1 as u8 { - __found = a0.offset(__i); + __found = __p.clone(); } if __c == 0 { break __found; } - __i += 1; + __p += 1; } } @@ -109,127 +130,140 @@ fn f15(a0: Ptr) -> Ptr { } fn f16(a0: Ptr, a1: Ptr) -> usize { + let mut __p = a0.clone(); let mut __i: usize = 0; loop { - let __c = a0.offset(__i).read(); + let __c = __p.read(); if __c == 0 { break __i; } - let mut __j: usize = 0; + let mut __q = a1.clone(); let __hit = loop { - let __r = a1.offset(__j).read(); + let __r = __q.read(); if __r == 0 { break false; } if __r == __c { break true; } - __j += 1; + __q += 1; }; if __hit { break __i; } + __p += 1; __i += 1; } } fn f17(a0: Ptr, a1: Ptr) -> usize { + let mut __p = a0.clone(); let mut __i: usize = 0; loop { - let __c = a0.offset(__i).read(); + let __c = __p.read(); if __c == 0 { break __i; } - let mut __j: usize = 0; + let mut __q = a1.clone(); let __hit = loop { - let __r = a1.offset(__j).read(); + let __r = __q.read(); if __r == 0 { break false; } if __r == __c { break true; } - __j += 1; + __q += 1; }; if !__hit { break __i; } + __p += 1; __i += 1; } } fn f18(a0: Ptr, a1: Ptr) -> Ptr { - let mut __s: usize = 0; + let mut __p = a0.clone(); loop { - let mut __i: usize = 0; + let mut __h = __p.clone(); + let mut __n = a1.clone(); let __matched = loop { - let __n = a1.offset(__i).read(); - if __n == 0 { + let __c = __n.read(); + if __c == 0 { break true; } - if a0.offset(__s + __i).read() != __n { + if __h.read() != __c { break false; } - __i += 1; + __h += 1; + __n += 1; }; if __matched { - break a0.offset(__s); + break __p; } - if a0.offset(__s).read() == 0 { + if __p.read() == 0 { break Ptr::null(); } - __s += 1; + __p += 1; } } fn f21(a0: Ptr, a1: Ptr) -> Ptr { - let mut __i: usize = 0; + let mut __p = a0.clone(); loop { - let __c = a0.offset(__i).read(); + let __c = __p.read(); if __c == 0 { break Ptr::null(); } - let mut __j: usize = 0; + let mut __q = a1.clone(); let __hit = loop { - let __r = a1.offset(__j).read(); + let __r = __q.read(); if __r == 0 { break false; } if __r == __c { break true; } - __j += 1; + __q += 1; }; if __hit { - break a0.offset(__i); + break __p; } - __i += 1; + __p += 1; } } #[cfg(target_os = "linux")] fn f24(a0: AnyPtr, a1: i32, a2: usize) -> AnyPtr { - match (0..a2) - .rev() - .find(|&__i| a0.reinterpret_cast::().offset(__i).read() == a1 as u8) - { - Some(__i) => a0.reinterpret_cast::().offset(__i).to_any(), - None => Ptr::::null().to_any(), + let mut __p = a0.reinterpret_cast::().offset(a2); + let mut __i: usize = a2; + loop { + if __i == 0 { + break Ptr::::null().to_any(); + } + __p -= 1; + __i -= 1; + if __p.read() == a1 as u8 { + break __p.to_any(); + } } } fn f27(a0: Ptr, a1: Ptr) -> i32 { - let mut __i: usize = 0; + let mut __p1 = a0.clone(); + let mut __p2 = a1.clone(); loop { - let __c1 = a0.offset(__i).read().to_ascii_lowercase(); - let __c2 = a1.offset(__i).read().to_ascii_lowercase(); + let __c1 = __p1.read().to_ascii_lowercase(); + let __c2 = __p2.read().to_ascii_lowercase(); if __c1 != __c2 { break (__c1 as i32) - (__c2 as i32); } if __c1 == 0 { break 0; } - __i += 1; + __p1 += 1; + __p2 += 1; } } @@ -237,8 +271,10 @@ fn f27(a0: Ptr, a1: Ptr) -> i32 { fn f28(a0: i32, a1: Ptr, a2: usize) -> Ptr { let __msg = std::io::Error::from_raw_os_error(a0).to_string(); let __len = __msg.len().min(a2.saturating_sub(1)); + let mut __p = a1.clone(); for __i in 0..__len { - a1.offset(__i).write(__msg.as_bytes()[__i]); + __p.write(__msg.as_bytes()[__i]); + __p += 1; } if a2 > 0 { a1.offset(__len).write(0); @@ -250,8 +286,10 @@ fn f28(a0: i32, a1: Ptr, a2: usize) -> Ptr { fn f28(a0: i32, a1: Ptr, a2: usize) -> i32 { let __msg = std::io::Error::from_raw_os_error(a0).to_string(); let __len = __msg.len().min(a2.saturating_sub(1)); + let mut __p = a1.clone(); for __i in 0..__len { - a1.offset(__i).write(__msg.as_bytes()[__i]); + __p.write(__msg.as_bytes()[__i]); + __p += 1; } if a2 > 0 { a1.offset(__len).write(0); @@ -260,172 +298,194 @@ fn f28(a0: i32, a1: Ptr, a2: usize) -> i32 { } fn f6(a0: Ptr, a1: i32) -> Ptr { - let mut __i: usize = 0; + let mut __p = a0.clone(); loop { - let __c = a0.offset(__i).read(); + let __c = __p.read(); if __c == a1 as u8 { - break a0.offset(__i); + break __p; } if __c == 0 { break Ptr::null(); } - __i += 1; + __p += 1; } } fn f12(a0: AnyPtr, a1: i32, a2: usize) -> AnyPtr { - match (0..a2).find(|&__i| a0.reinterpret_cast::().offset(__i).read() == a1 as u8) { - Some(__i) => a0.reinterpret_cast::().offset(__i).to_any(), - None => Ptr::::null().to_any(), + let mut __p = a0.reinterpret_cast::(); + let mut __i: usize = 0; + loop { + if __i == a2 { + break Ptr::::null().to_any(); + } + if __p.read() == a1 as u8 { + break __p.to_any(); + } + __p += 1; + __i += 1; } } fn f13(a0: Ptr, a1: i32) -> Ptr { - let mut __i: usize = 0; + let mut __p = a0.clone(); let mut __found = Ptr::null(); loop { - let __c = a0.offset(__i).read(); + let __c = __p.read(); if __c == a1 as u8 { - __found = a0.offset(__i); + __found = __p.clone(); } if __c == 0 { break __found; } - __i += 1; + __p += 1; } } fn f14(a0: Ptr, a1: i32) -> Ptr { - let mut __i: usize = 0; + let mut __p = a0.clone(); let mut __found = Ptr::null(); loop { - let __c = a0.offset(__i).read(); + let __c = __p.read(); if __c == a1 as u8 { - __found = a0.offset(__i); + __found = __p.clone(); } if __c == 0 { break __found; } - __i += 1; + __p += 1; } } fn f19(a0: Ptr, a1: Ptr) -> Ptr { - let mut __s: usize = 0; + let mut __p = a0.clone(); loop { - let mut __i: usize = 0; + let mut __h = __p.clone(); + let mut __n = a1.clone(); let __matched = loop { - let __n = a1.offset(__i).read(); - if __n == 0 { + let __c = __n.read(); + if __c == 0 { break true; } - if a0.offset(__s + __i).read() != __n { + if __h.read() != __c { break false; } - __i += 1; + __h += 1; + __n += 1; }; if __matched { - break a0.offset(__s); + break __p; } - if a0.offset(__s).read() == 0 { + if __p.read() == 0 { break Ptr::null(); } - __s += 1; + __p += 1; } } fn f20(a0: Ptr, a1: Ptr) -> Ptr { - let mut __s: usize = 0; + let mut __p = a0.clone(); loop { - let mut __i: usize = 0; + let mut __h = __p.clone(); + let mut __n = a1.clone(); let __matched = loop { - let __n = a1.offset(__i).read(); - if __n == 0 { + let __c = __n.read(); + if __c == 0 { break true; } - if a0.offset(__s + __i).read() != __n { + if __h.read() != __c { break false; } - __i += 1; + __h += 1; + __n += 1; }; if __matched { - break a0.offset(__s); + break __p; } - if a0.offset(__s).read() == 0 { + if __p.read() == 0 { break Ptr::null(); } - __s += 1; + __p += 1; } } fn f22(a0: Ptr, a1: Ptr) -> Ptr { - let mut __i: usize = 0; + let mut __p = a0.clone(); loop { - let __c = a0.offset(__i).read(); + let __c = __p.read(); if __c == 0 { break Ptr::null(); } - let mut __j: usize = 0; + let mut __q = a1.clone(); let __hit = loop { - let __r = a1.offset(__j).read(); + let __r = __q.read(); if __r == 0 { break false; } if __r == __c { break true; } - __j += 1; + __q += 1; }; if __hit { - break a0.offset(__i); + break __p; } - __i += 1; + __p += 1; } } fn f23(a0: Ptr, a1: Ptr) -> Ptr { - let mut __i: usize = 0; + let mut __p = a0.clone(); loop { - let __c = a0.offset(__i).read(); + let __c = __p.read(); if __c == 0 { break Ptr::null(); } - let mut __j: usize = 0; + let mut __q = a1.clone(); let __hit = loop { - let __r = a1.offset(__j).read(); + let __r = __q.read(); if __r == 0 { break false; } if __r == __c { break true; } - __j += 1; + __q += 1; }; if __hit { - break a0.offset(__i); + break __p; } - __i += 1; + __p += 1; } } #[cfg(target_os = "linux")] fn f25(a0: AnyPtr, a1: i32, a2: usize) -> AnyPtr { - match (0..a2) - .rev() - .find(|&__i| a0.reinterpret_cast::().offset(__i).read() == a1 as u8) - { - Some(__i) => a0.reinterpret_cast::().offset(__i).to_any(), - None => Ptr::::null().to_any(), + let mut __p = a0.reinterpret_cast::().offset(a2); + let mut __i: usize = a2; + loop { + if __i == 0 { + break Ptr::::null().to_any(); + } + __p -= 1; + __i -= 1; + if __p.read() == a1 as u8 { + break __p.to_any(); + } } } #[cfg(target_os = "linux")] fn f26(a0: AnyPtr, a1: i32, a2: usize) -> AnyPtr { - match (0..a2) - .rev() - .find(|&__i| a0.reinterpret_cast::().offset(__i).read() == a1 as u8) - { - Some(__i) => a0.reinterpret_cast::().offset(__i).to_any(), - None => Ptr::::null().to_any(), + let mut __p = a0.reinterpret_cast::().offset(a2); + let mut __i: usize = a2; + loop { + if __i == 0 { + break Ptr::::null().to_any(); + } + __p -= 1; + __i -= 1; + if __p.read() == a1 as u8 { + break __p.to_any(); + } } } diff --git a/tests/unit/out/refcount/cstring.rs b/tests/unit/out/refcount/cstring.rs index f469dcac..54061555 100644 --- a/tests/unit/out/refcount/cstring.rs +++ b/tests/unit/out/refcount/cstring.rs @@ -101,21 +101,20 @@ pub fn test_memmove_3() { ('\0' as u8), ]))); let r: Value = Rc::new(RefCell::new({ - let __tmp: Vec = (0..4_usize) - .map(|__i| { - ((buf.as_pointer() as Ptr) as Ptr) - .to_any() - .reinterpret_cast::() - .offset(__i) - .read() - }) - .collect(); + let mut __src = ((buf.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::(); + let mut __tmp: Vec = Vec::with_capacity(4_usize); + for _ in 0..4_usize { + __tmp.push(__src.read()); + __src += 1; + } + let mut __dst = ((buf.as_pointer() as Ptr).offset((1) as isize) as Ptr) + .to_any() + .reinterpret_cast::(); for __i in 0..4_usize { - ((buf.as_pointer() as Ptr).offset((1) as isize) as Ptr) - .to_any() - .reinterpret_cast::() - .offset(__i) - .write(__tmp[__i]); + __dst.write(__tmp[__i]); + __dst += 1; } ((buf.as_pointer() as Ptr).offset((1) as isize) as Ptr) .to_any() @@ -139,32 +138,32 @@ pub fn test_memmove_3() { pub fn test_strchr_4() { let s: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"hello world"))); let r: Value> = Rc::new(RefCell::new({ - let mut __i: usize = 0; + let mut __p = (*s.borrow()).clone(); loop { - let __c = (*s.borrow()).offset(__i).read(); + let __c = __p.read(); if __c == (('w' as u8) as i32) as u8 { - break (*s.borrow()).offset(__i); + break __p; } if __c == 0 { break Ptr::null(); } - __i += 1; + __p += 1; } })); assert!(!((*r.borrow()).is_null())); assert!(((((*r.borrow()).read()) as i32) == (('w' as u8) as i32))); assert!( ({ - let mut __i: usize = 0; + let mut __p = (*s.borrow()).clone(); loop { - let __c = (*s.borrow()).offset(__i).read(); + let __c = __p.read(); if __c == (('z' as u8) as i32) as u8 { - break (*s.borrow()).offset(__i); + break __p; } if __c == 0 { break Ptr::null(); } - __i += 1; + __p += 1; } }) .is_null() @@ -173,8 +172,10 @@ pub fn test_strchr_4() { pub fn test_strlen_5() { assert!( ({ + let mut __p = Ptr::from_string_literal(b"").clone(); let mut __i: usize = 0; - while Ptr::from_string_literal(b"").offset(__i).read() != 0 { + while __p.read() != 0 { + __p += 1; __i += 1; } __i @@ -182,8 +183,10 @@ pub fn test_strlen_5() { ); assert!( ({ + let mut __p = Ptr::from_string_literal(b"hello").clone(); let mut __i: usize = 0; - while Ptr::from_string_literal(b"hello").offset(__i).read() != 0 { + while __p.read() != 0 { + __p += 1; __i += 1; } __i @@ -191,8 +194,10 @@ pub fn test_strlen_5() { ); assert!( ({ + let mut __p = Ptr::from_string_literal(b"hello world").clone(); let mut __i: usize = 0; - while Ptr::from_string_literal(b"hello world").offset(__i).read() != 0 { + while __p.read() != 0 { + __p += 1; __i += 1; } __i @@ -202,49 +207,55 @@ pub fn test_strlen_5() { pub fn test_strcmp_6() { assert!( ({ - let mut __i: usize = 0; + let mut __p1 = Ptr::from_string_literal(b"abc").clone(); + let mut __p2 = Ptr::from_string_literal(b"abc").clone(); loop { - let __c1 = Ptr::from_string_literal(b"abc").offset(__i).read(); - let __c2 = Ptr::from_string_literal(b"abc").offset(__i).read(); + let __c1 = __p1.read(); + let __c2 = __p2.read(); if __c1 != __c2 { break (__c1 as i32) - (__c2 as i32); } if __c1 == 0 { break 0; } - __i += 1; + __p1 += 1; + __p2 += 1; } } == 0) ); assert!( ({ - let mut __i: usize = 0; + let mut __p1 = Ptr::from_string_literal(b"abc").clone(); + let mut __p2 = Ptr::from_string_literal(b"abd").clone(); loop { - let __c1 = Ptr::from_string_literal(b"abc").offset(__i).read(); - let __c2 = Ptr::from_string_literal(b"abd").offset(__i).read(); + let __c1 = __p1.read(); + let __c2 = __p2.read(); if __c1 != __c2 { break (__c1 as i32) - (__c2 as i32); } if __c1 == 0 { break 0; } - __i += 1; + __p1 += 1; + __p2 += 1; } } < 0) ); assert!( ({ - let mut __i: usize = 0; + let mut __p1 = Ptr::from_string_literal(b"abd").clone(); + let mut __p2 = Ptr::from_string_literal(b"abc").clone(); loop { - let __c1 = Ptr::from_string_literal(b"abd").offset(__i).read(); - let __c2 = Ptr::from_string_literal(b"abc").offset(__i).read(); + let __c1 = __p1.read(); + let __c2 = __p2.read(); if __c1 != __c2 { break (__c1 as i32) - (__c2 as i32); } if __c1 == 0 { break 0; } - __i += 1; + __p1 += 1; + __p2 += 1; } } > 0) ); @@ -258,49 +269,55 @@ pub fn test_strcmp_6() { ]))); assert!( ({ - let mut __i: usize = 0; + let mut __p1 = (*p.borrow()).clone(); + let mut __p2 = (*p.borrow()).clone(); loop { - let __c1 = (*p.borrow()).offset(__i).read(); - let __c2 = (*p.borrow()).offset(__i).read(); + let __c1 = __p1.read(); + let __c2 = __p2.read(); if __c1 != __c2 { break (__c1 as i32) - (__c2 as i32); } if __c1 == 0 { break 0; } - __i += 1; + __p1 += 1; + __p2 += 1; } } == 0) ); assert!( ({ - let mut __i: usize = 0; + let mut __p1 = (*p.borrow()).clone(); + let mut __p2 = (*q.borrow()).clone(); loop { - let __c1 = (*p.borrow()).offset(__i).read(); - let __c2 = (*q.borrow()).offset(__i).read(); + let __c1 = __p1.read(); + let __c2 = __p2.read(); if __c1 != __c2 { break (__c1 as i32) - (__c2 as i32); } if __c1 == 0 { break 0; } - __i += 1; + __p1 += 1; + __p2 += 1; } } < 0) ); assert!( ({ - let mut __i: usize = 0; + let mut __p1 = (buf.as_pointer() as Ptr).clone(); + let mut __p2 = (*p.borrow()).clone(); loop { - let __c1 = (buf.as_pointer() as Ptr).offset(__i).read(); - let __c2 = (*p.borrow()).offset(__i).read(); + let __c1 = __p1.read(); + let __c2 = __p2.read(); if __c1 != __c2 { break (__c1 as i32) - (__c2 as i32); } if __c1 == 0 { break 0; } - __i += 1; + __p1 += 1; + __p2 += 1; } } == 0) ); @@ -308,57 +325,69 @@ pub fn test_strcmp_6() { pub fn test_strncmp_7() { assert!( ({ + let mut __p1 = Ptr::from_string_literal(b"abcdef").clone(); + let mut __p2 = Ptr::from_string_literal(b"abcxyz").clone(); let mut __i: usize = 0; loop { if __i == 3_usize { break 0; } - let __c1 = Ptr::from_string_literal(b"abcdef").offset(__i).read(); - let __c2 = Ptr::from_string_literal(b"abcxyz").offset(__i).read(); + let __c1 = __p1.read(); + let __c2 = __p2.read(); if __c1 != __c2 { break (__c1 as i32) - (__c2 as i32); } if __c1 == 0 { break 0; } + __p1 += 1; + __p2 += 1; __i += 1; } } == 0) ); assert!( ({ + let mut __p1 = Ptr::from_string_literal(b"abcdef").clone(); + let mut __p2 = Ptr::from_string_literal(b"abcxyz").clone(); let mut __i: usize = 0; loop { if __i == 4_usize { break 0; } - let __c1 = Ptr::from_string_literal(b"abcdef").offset(__i).read(); - let __c2 = Ptr::from_string_literal(b"abcxyz").offset(__i).read(); + let __c1 = __p1.read(); + let __c2 = __p2.read(); if __c1 != __c2 { break (__c1 as i32) - (__c2 as i32); } if __c1 == 0 { break 0; } + __p1 += 1; + __p2 += 1; __i += 1; } } < 0) ); assert!( ({ + let mut __p1 = Ptr::from_string_literal(b"abcxyz").clone(); + let mut __p2 = Ptr::from_string_literal(b"abcdef").clone(); let mut __i: usize = 0; loop { if __i == 4_usize { break 0; } - let __c1 = Ptr::from_string_literal(b"abcxyz").offset(__i).read(); - let __c2 = Ptr::from_string_literal(b"abcdef").offset(__i).read(); + let __c1 = __p1.read(); + let __c2 = __p2.read(); if __c1 != __c2 { break (__c1 as i32) - (__c2 as i32); } if __c1 == 0 { break 0; } + __p1 += 1; + __p2 += 1; __i += 1; } } > 0) @@ -377,57 +406,69 @@ pub fn test_strncmp_7() { let n: Value = Rc::new(RefCell::new(3_usize)); assert!( ({ + let mut __p1 = (*p.borrow()).clone(); + let mut __p2 = (*q.borrow()).clone(); let mut __i: usize = 0; loop { if __i == (*n.borrow()) { break 0; } - let __c1 = (*p.borrow()).offset(__i).read(); - let __c2 = (*q.borrow()).offset(__i).read(); + let __c1 = __p1.read(); + let __c2 = __p2.read(); if __c1 != __c2 { break (__c1 as i32) - (__c2 as i32); } if __c1 == 0 { break 0; } + __p1 += 1; + __p2 += 1; __i += 1; } } == 0) ); assert!( ({ + let mut __p1 = (*p.borrow()).clone(); + let mut __p2 = (*q.borrow()).clone(); let mut __i: usize = 0; loop { if __i == (*n.borrow()).wrapping_add(1_usize) { break 0; } - let __c1 = (*p.borrow()).offset(__i).read(); - let __c2 = (*q.borrow()).offset(__i).read(); + let __c1 = __p1.read(); + let __c2 = __p2.read(); if __c1 != __c2 { break (__c1 as i32) - (__c2 as i32); } if __c1 == 0 { break 0; } + __p1 += 1; + __p2 += 1; __i += 1; } } < 0) ); assert!( ({ + let mut __p1 = (buf.as_pointer() as Ptr).clone(); + let mut __p2 = (*p.borrow()).clone(); let mut __i: usize = 0; loop { if __i == 6_usize { break 0; } - let __c1 = (buf.as_pointer() as Ptr).offset(__i).read(); - let __c2 = (*p.borrow()).offset(__i).read(); + let __c1 = __p1.read(); + let __c2 = __p2.read(); if __c1 != __c2 { break (__c1 as i32) - (__c2 as i32); } if __c1 == 0 { break 0; } + __p1 += 1; + __p2 += 1; __i += 1; } } == 0) @@ -435,41 +476,42 @@ pub fn test_strncmp_7() { } pub fn test_memchr_8() { let data: Value> = Rc::new(RefCell::new(Box::new([16_u8, 32_u8, 48_u8, 64_u8]))); - let r: Value = Rc::new(RefCell::new( - match (0..4_usize).find(|&__i| { - ((data.as_pointer() as Ptr) as Ptr) - .to_any() - .reinterpret_cast::() - .offset(__i) - .read() - == 48 as u8 - }) { - Some(__i) => ((data.as_pointer() as Ptr) as Ptr) - .to_any() - .reinterpret_cast::() - .offset(__i) - .to_any(), - None => Ptr::::null().to_any(), - }, - )); + let r: Value = Rc::new(RefCell::new({ + let mut __p = ((data.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::(); + let mut __i: usize = 0; + loop { + if __i == 4_usize { + break Ptr::::null().to_any(); + } + if __p.read() == 48 as u8 { + break __p.to_any(); + } + __p += 1; + __i += 1; + } + })); assert!({ let _lhs = (*r.borrow()).clone(); _lhs == (((data.as_pointer() as Ptr).offset(2)) as Ptr).to_any() }); assert!( - (match (0..4_usize).find(|&__i| ((data.as_pointer() as Ptr::) as Ptr::) - .to_any() - .reinterpret_cast::() - .offset(__i) - .read() - == 153 as u8) - { - Some(__i) => ((data.as_pointer() as Ptr::) as Ptr::) + ({ + let mut __p = ((data.as_pointer() as Ptr) as Ptr) .to_any() - .reinterpret_cast::() - .offset(__i) - .to_any(), - None => Ptr::::null().to_any(), + .reinterpret_cast::(); + let mut __i: usize = 0; + loop { + if __i == 4_usize { + break Ptr::::null().to_any(); + } + if __p.read() == 153 as u8 { + break __p.to_any(); + } + __p += 1; + __i += 1; + } }) .is_null() ); @@ -478,11 +520,19 @@ pub fn test_memchr_8() { )); let n: Value = Rc::new(RefCell::new(4_usize)); assert!({ - let _lhs = match (0..(*n.borrow())) - .find(|&__i| (*p.borrow()).reinterpret_cast::().offset(__i).read() == 16 as u8) - { - Some(__i) => (*p.borrow()).reinterpret_cast::().offset(__i).to_any(), - None => Ptr::::null().to_any(), + let _lhs = { + let mut __p = (*p.borrow()).reinterpret_cast::(); + let mut __i: usize = 0; + loop { + if __i == (*n.borrow()) { + break Ptr::::null().to_any(); + } + if __p.read() == 16 as u8 { + break __p.to_any(); + } + __p += 1; + __i += 1; + } }; _lhs == (*p.borrow()).clone() }); @@ -490,17 +540,17 @@ pub fn test_memchr_8() { pub fn test_strrchr_9() { let s: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"hello world"))); let r: Value> = Rc::new(RefCell::new({ - let mut __i: usize = 0; + let mut __p = (*s.borrow()).clone(); let mut __found = Ptr::null(); loop { - let __c = (*s.borrow()).offset(__i).read(); + let __c = __p.read(); if __c == (('l' as u8) as i32) as u8 { - __found = (*s.borrow()).offset(__i); + __found = __p.clone(); } if __c == 0 { break __found; } - __i += 1; + __p += 1; } })); assert!(!((*r.borrow()).is_null())); @@ -511,17 +561,17 @@ pub fn test_strrchr_9() { }); assert!( ({ - let mut __i: usize = 0; + let mut __p = (*s.borrow()).clone(); let mut __found = Ptr::null(); loop { - let __c = (*s.borrow()).offset(__i).read(); + let __c = __p.read(); if __c == (('z' as u8) as i32) as u8 { - __found = (*s.borrow()).offset(__i); + __found = __p.clone(); } if __c == 0 { break __found; } - __i += 1; + __p += 1; } }) .is_null() @@ -534,17 +584,17 @@ pub fn test_strrchr_9() { ]))); assert!( ({ - let mut __i: usize = 0; + let mut __p = (buf.as_pointer() as Ptr).clone(); let mut __found = Ptr::null(); loop { - let __c = (buf.as_pointer() as Ptr).offset(__i).read(); + let __c = __p.read(); if __c == (('a' as u8) as i32) as u8 { - __found = (buf.as_pointer() as Ptr).offset(__i); + __found = __p.clone(); } if __c == 0 { break __found; } - __i += 1; + __p += 1; } } == ((buf.as_pointer() as Ptr).offset(2))) ); @@ -556,17 +606,19 @@ pub fn test_strdup_10() { assert!(!((*d.borrow()).is_null())); assert!( ({ - let mut __i: usize = 0; + let mut __p1 = (*d.borrow()).clone(); + let mut __p2 = Ptr::from_string_literal(b"hello").clone(); loop { - let __c1 = (*d.borrow()).offset(__i).read(); - let __c2 = Ptr::from_string_literal(b"hello").offset(__i).read(); + let __c1 = __p1.read(); + let __c2 = __p2.read(); if __c1 != __c2 { break (__c1 as i32) - (__c2 as i32); } if __c1 == 0 { break 0; } - __i += 1; + __p1 += 1; + __p2 += 1; } } == 0) ); @@ -584,17 +636,19 @@ pub fn test_strdup_10() { assert!(!((*d2.borrow()).is_null())); assert!( ({ - let mut __i: usize = 0; + let mut __p1 = (*d2.borrow()).clone(); + let mut __p2 = (*p.borrow()).clone(); loop { - let __c1 = (*d2.borrow()).offset(__i).read(); - let __c2 = (*p.borrow()).offset(__i).read(); + let __c1 = __p1.read(); + let __c2 = __p2.read(); if __c1 != __c2 { break (__c1 as i32) - (__c2 as i32); } if __c1 == 0 { break 0; } - __i += 1; + __p1 += 1; + __p2 += 1; } } == 0) ); @@ -605,17 +659,19 @@ pub fn test_strdup_10() { assert!(!((*d3.borrow()).is_null())); assert!( ({ - let mut __i: usize = 0; + let mut __p1 = (*d3.borrow()).clone(); + let mut __p2 = (buf.as_pointer() as Ptr).clone(); loop { - let __c1 = (*d3.borrow()).offset(__i).read(); - let __c2 = (buf.as_pointer() as Ptr).offset(__i).read(); + let __c1 = __p1.read(); + let __c2 = __p2.read(); if __c1 != __c2 { break (__c1 as i32) - (__c2 as i32); } if __c1 == 0 { break 0; } - __i += 1; + __p1 += 1; + __p2 += 1; } } == 0) ); @@ -624,78 +680,84 @@ pub fn test_strdup_10() { pub fn test_strcspn_11() { assert!( ({ + let mut __p = Ptr::from_string_literal(b"hello").clone(); let mut __i: usize = 0; loop { - let __c = Ptr::from_string_literal(b"hello").offset(__i).read(); + let __c = __p.read(); if __c == 0 { break __i; } - let mut __j: usize = 0; + let mut __q = Ptr::from_string_literal(b"el").clone(); let __hit = loop { - let __r = Ptr::from_string_literal(b"el").offset(__j).read(); + let __r = __q.read(); if __r == 0 { break false; } if __r == __c { break true; } - __j += 1; + __q += 1; }; if __hit { break __i; } + __p += 1; __i += 1; } } == 1_usize) ); assert!( ({ + let mut __p = Ptr::from_string_literal(b"abc").clone(); let mut __i: usize = 0; loop { - let __c = Ptr::from_string_literal(b"abc").offset(__i).read(); + let __c = __p.read(); if __c == 0 { break __i; } - let mut __j: usize = 0; + let mut __q = Ptr::from_string_literal(b"xyz").clone(); let __hit = loop { - let __r = Ptr::from_string_literal(b"xyz").offset(__j).read(); + let __r = __q.read(); if __r == 0 { break false; } if __r == __c { break true; } - __j += 1; + __q += 1; }; if __hit { break __i; } + __p += 1; __i += 1; } } == 3_usize) ); assert!( ({ + let mut __p = Ptr::from_string_literal(b"").clone(); let mut __i: usize = 0; loop { - let __c = Ptr::from_string_literal(b"").offset(__i).read(); + let __c = __p.read(); if __c == 0 { break __i; } - let mut __j: usize = 0; + let mut __q = Ptr::from_string_literal(b"abc").clone(); let __hit = loop { - let __r = Ptr::from_string_literal(b"abc").offset(__j).read(); + let __r = __q.read(); if __r == 0 { break false; } if __r == __c { break true; } - __j += 1; + __q += 1; }; if __hit { break __i; } + __p += 1; __i += 1; } } == 0_usize) @@ -704,26 +766,28 @@ pub fn test_strcspn_11() { let rej: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"el"))); assert!( ({ + let mut __p = (*s.borrow()).clone(); let mut __i: usize = 0; loop { - let __c = (*s.borrow()).offset(__i).read(); + let __c = __p.read(); if __c == 0 { break __i; } - let mut __j: usize = 0; + let mut __q = (*rej.borrow()).clone(); let __hit = loop { - let __r = (*rej.borrow()).offset(__j).read(); + let __r = __q.read(); if __r == 0 { break false; } if __r == __c { break true; } - __j += 1; + __q += 1; }; if __hit { break __i; } + __p += 1; __i += 1; } } == 1_usize) @@ -732,78 +796,84 @@ pub fn test_strcspn_11() { pub fn test_strspn_12() { assert!( ({ + let mut __p = Ptr::from_string_literal(b"hello").clone(); let mut __i: usize = 0; loop { - let __c = Ptr::from_string_literal(b"hello").offset(__i).read(); + let __c = __p.read(); if __c == 0 { break __i; } - let mut __j: usize = 0; + let mut __q = Ptr::from_string_literal(b"hel").clone(); let __hit = loop { - let __r = Ptr::from_string_literal(b"hel").offset(__j).read(); + let __r = __q.read(); if __r == 0 { break false; } if __r == __c { break true; } - __j += 1; + __q += 1; }; if !__hit { break __i; } + __p += 1; __i += 1; } } == 4_usize) ); assert!( ({ + let mut __p = Ptr::from_string_literal(b"abc").clone(); let mut __i: usize = 0; loop { - let __c = Ptr::from_string_literal(b"abc").offset(__i).read(); + let __c = __p.read(); if __c == 0 { break __i; } - let mut __j: usize = 0; + let mut __q = Ptr::from_string_literal(b"xyz").clone(); let __hit = loop { - let __r = Ptr::from_string_literal(b"xyz").offset(__j).read(); + let __r = __q.read(); if __r == 0 { break false; } if __r == __c { break true; } - __j += 1; + __q += 1; }; if !__hit { break __i; } + __p += 1; __i += 1; } } == 0_usize) ); assert!( ({ + let mut __p = Ptr::from_string_literal(b"aaa").clone(); let mut __i: usize = 0; loop { - let __c = Ptr::from_string_literal(b"aaa").offset(__i).read(); + let __c = __p.read(); if __c == 0 { break __i; } - let mut __j: usize = 0; + let mut __q = Ptr::from_string_literal(b"a").clone(); let __hit = loop { - let __r = Ptr::from_string_literal(b"a").offset(__j).read(); + let __r = __q.read(); if __r == 0 { break false; } if __r == __c { break true; } - __j += 1; + __q += 1; }; if !__hit { break __i; } + __p += 1; __i += 1; } } == 3_usize) @@ -812,26 +882,28 @@ pub fn test_strspn_12() { let acc: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"hel"))); assert!( ({ + let mut __p = (*s.borrow()).clone(); let mut __i: usize = 0; loop { - let __c = (*s.borrow()).offset(__i).read(); + let __c = __p.read(); if __c == 0 { break __i; } - let mut __j: usize = 0; + let mut __q = (*acc.borrow()).clone(); let __hit = loop { - let __r = (*acc.borrow()).offset(__j).read(); + let __r = __q.read(); if __r == 0 { break false; } if __r == __c { break true; } - __j += 1; + __q += 1; }; if !__hit { break __i; } + __p += 1; __i += 1; } } == 4_usize) @@ -840,26 +912,28 @@ pub fn test_strspn_12() { pub fn test_strstr_13() { let h: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"hello world"))); let r: Value> = Rc::new(RefCell::new({ - let mut __s: usize = 0; + let mut __p = (*h.borrow()).clone(); loop { - let mut __i: usize = 0; + let mut __h = __p.clone(); + let mut __n = Ptr::from_string_literal(b"world").clone(); let __matched = loop { - let __n = Ptr::from_string_literal(b"world").offset(__i).read(); - if __n == 0 { + let __c = __n.read(); + if __c == 0 { break true; } - if (*h.borrow()).offset(__s + __i).read() != __n { + if __h.read() != __c { break false; } - __i += 1; + __h += 1; + __n += 1; }; if __matched { - break (*h.borrow()).offset(__s); + break __p; } - if (*h.borrow()).offset(__s).read() == 0 { + if __p.read() == 0 { break Ptr::null(); } - __s += 1; + __p += 1; } })); assert!(!((*r.borrow()).is_null())); @@ -869,26 +943,28 @@ pub fn test_strstr_13() { }); assert!( ({ - let mut __s: usize = 0; + let mut __p = (*h.borrow()).clone(); loop { - let mut __i: usize = 0; + let mut __h = __p.clone(); + let mut __n = Ptr::from_string_literal(b"xyz").clone(); let __matched = loop { - let __n = Ptr::from_string_literal(b"xyz").offset(__i).read(); - if __n == 0 { + let __c = __n.read(); + if __c == 0 { break true; } - if (*h.borrow()).offset(__s + __i).read() != __n { + if __h.read() != __c { break false; } - __i += 1; + __h += 1; + __n += 1; }; if __matched { - break (*h.borrow()).offset(__s); + break __p; } - if (*h.borrow()).offset(__s).read() == 0 { + if __p.read() == 0 { break Ptr::null(); } - __s += 1; + __p += 1; } }) .is_null() @@ -903,26 +979,28 @@ pub fn test_strstr_13() { ]))); assert!( ({ - let mut __s: usize = 0; + let mut __p = (buf.as_pointer() as Ptr).clone(); loop { - let mut __i: usize = 0; + let mut __h = __p.clone(); + let mut __n = Ptr::from_string_literal(b"ll").clone(); let __matched = loop { - let __n = Ptr::from_string_literal(b"ll").offset(__i).read(); - if __n == 0 { + let __c = __n.read(); + if __c == 0 { break true; } - if (buf.as_pointer() as Ptr).offset(__s + __i).read() != __n { + if __h.read() != __c { break false; } - __i += 1; + __h += 1; + __n += 1; }; if __matched { - break (buf.as_pointer() as Ptr).offset(__s); + break __p; } - if (buf.as_pointer() as Ptr).offset(__s).read() == 0 { + if __p.read() == 0 { break Ptr::null(); } - __s += 1; + __p += 1; } } == ((buf.as_pointer() as Ptr).offset(2))) ); @@ -930,27 +1008,27 @@ pub fn test_strstr_13() { pub fn test_strpbrk_14() { let s: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"hello world"))); let r: Value> = Rc::new(RefCell::new({ - let mut __i: usize = 0; + let mut __p = (*s.borrow()).clone(); loop { - let __c = (*s.borrow()).offset(__i).read(); + let __c = __p.read(); if __c == 0 { break Ptr::null(); } - let mut __j: usize = 0; + let mut __q = Ptr::from_string_literal(b"wo").clone(); let __hit = loop { - let __r = Ptr::from_string_literal(b"wo").offset(__j).read(); + let __r = __q.read(); if __r == 0 { break false; } if __r == __c { break true; } - __j += 1; + __q += 1; }; if __hit { - break (*s.borrow()).offset(__i); + break __p; } - __i += 1; + __p += 1; } })); assert!(!((*r.borrow()).is_null())); @@ -960,27 +1038,27 @@ pub fn test_strpbrk_14() { }); assert!( ({ - let mut __i: usize = 0; + let mut __p = (*s.borrow()).clone(); loop { - let __c = (*s.borrow()).offset(__i).read(); + let __c = __p.read(); if __c == 0 { break Ptr::null(); } - let mut __j: usize = 0; + let mut __q = Ptr::from_string_literal(b"xyz").clone(); let __hit = loop { - let __r = Ptr::from_string_literal(b"xyz").offset(__j).read(); + let __r = __q.read(); if __r == 0 { break false; } if __r == __c { break true; } - __j += 1; + __q += 1; }; if __hit { - break (*s.borrow()).offset(__i); + break __p; } - __i += 1; + __p += 1; } }) .is_null() @@ -993,27 +1071,27 @@ pub fn test_strpbrk_14() { ]))); assert!( ({ - let mut __i: usize = 0; + let mut __p = (buf.as_pointer() as Ptr).clone(); loop { - let __c = (buf.as_pointer() as Ptr).offset(__i).read(); + let __c = __p.read(); if __c == 0 { break Ptr::null(); } - let mut __j: usize = 0; + let mut __q = Ptr::from_string_literal(b"b").clone(); let __hit = loop { - let __r = Ptr::from_string_literal(b"b").offset(__j).read(); + let __r = __q.read(); if __r == 0 { break false; } if __r == __c { break true; } - __j += 1; + __q += 1; }; if __hit { - break (buf.as_pointer() as Ptr).offset(__i); + break __p; } - __i += 1; + __p += 1; } } == ((buf.as_pointer() as Ptr).offset(1))) ); @@ -1021,67 +1099,55 @@ pub fn test_strpbrk_14() { pub fn test_strcasecmp_15() { assert!( ({ - let mut __i: usize = 0; + let mut __p1 = Ptr::from_string_literal(b"HELLO").clone(); + let mut __p2 = Ptr::from_string_literal(b"hello").clone(); loop { - let __c1 = Ptr::from_string_literal(b"HELLO") - .offset(__i) - .read() - .to_ascii_lowercase(); - let __c2 = Ptr::from_string_literal(b"hello") - .offset(__i) - .read() - .to_ascii_lowercase(); + let __c1 = __p1.read().to_ascii_lowercase(); + let __c2 = __p2.read().to_ascii_lowercase(); if __c1 != __c2 { break (__c1 as i32) - (__c2 as i32); } if __c1 == 0 { break 0; } - __i += 1; + __p1 += 1; + __p2 += 1; } } == 0) ); assert!( ({ - let mut __i: usize = 0; + let mut __p1 = Ptr::from_string_literal(b"abc").clone(); + let mut __p2 = Ptr::from_string_literal(b"abd").clone(); loop { - let __c1 = Ptr::from_string_literal(b"abc") - .offset(__i) - .read() - .to_ascii_lowercase(); - let __c2 = Ptr::from_string_literal(b"abd") - .offset(__i) - .read() - .to_ascii_lowercase(); + let __c1 = __p1.read().to_ascii_lowercase(); + let __c2 = __p2.read().to_ascii_lowercase(); if __c1 != __c2 { break (__c1 as i32) - (__c2 as i32); } if __c1 == 0 { break 0; } - __i += 1; + __p1 += 1; + __p2 += 1; } } < 0) ); assert!( ({ - let mut __i: usize = 0; + let mut __p1 = Ptr::from_string_literal(b"abd").clone(); + let mut __p2 = Ptr::from_string_literal(b"abc").clone(); loop { - let __c1 = Ptr::from_string_literal(b"abd") - .offset(__i) - .read() - .to_ascii_lowercase(); - let __c2 = Ptr::from_string_literal(b"abc") - .offset(__i) - .read() - .to_ascii_lowercase(); + let __c1 = __p1.read().to_ascii_lowercase(); + let __c2 = __p2.read().to_ascii_lowercase(); if __c1 != __c2 { break (__c1 as i32) - (__c2 as i32); } if __c1 == 0 { break 0; } - __i += 1; + __p1 += 1; + __p2 += 1; } } > 0) ); @@ -1089,17 +1155,19 @@ pub fn test_strcasecmp_15() { let q: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"foo"))); assert!( ({ - let mut __i: usize = 0; + let mut __p1 = (*p.borrow()).clone(); + let mut __p2 = (*q.borrow()).clone(); loop { - let __c1 = (*p.borrow()).offset(__i).read().to_ascii_lowercase(); - let __c2 = (*q.borrow()).offset(__i).read().to_ascii_lowercase(); + let __c1 = __p1.read().to_ascii_lowercase(); + let __c2 = __p2.read().to_ascii_lowercase(); if __c1 != __c2 { break (__c1 as i32) - (__c2 as i32); } if __c1 == 0 { break 0; } - __i += 1; + __p1 += 1; + __p2 += 1; } } == 0) ); diff --git a/tests/unit/out/refcount/offsetof.rs b/tests/unit/out/refcount/offsetof.rs index d0c2cf5a..f183b9c3 100644 --- a/tests/unit/out/refcount/offsetof.rs +++ b/tests/unit/out/refcount/offsetof.rs @@ -103,8 +103,10 @@ fn main_0() -> i32 { let text: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"example-body"))); let len: Value = Rc::new(RefCell::new( ({ + let mut __p = (*text.borrow()).clone(); let mut __i: usize = 0; - while (*text.borrow()).offset(__i).read() != 0 { + while __p.read() != 0 { + __p += 1; __i += 1; } __i diff --git a/tests/unit/out/refcount/strdup.rs b/tests/unit/out/refcount/strdup.rs index f854384e..f75d5a0f 100644 --- a/tests/unit/out/refcount/strdup.rs +++ b/tests/unit/out/refcount/strdup.rs @@ -40,17 +40,19 @@ fn main_0() -> i32 { assert!((((!((*d.borrow()).is_null())) as i32) != 0)); assert!( ((({ - let mut __i: usize = 0; + let mut __p1 = (*d.borrow()).clone(); + let mut __p2 = Ptr::from_string_literal(b"hello").clone(); loop { - let __c1 = (*d.borrow()).offset(__i).read(); - let __c2 = Ptr::from_string_literal(b"hello").offset(__i).read(); + let __c1 = __p1.read(); + let __c2 = __p2.read(); if __c1 != __c2 { break (__c1 as i32) - (__c2 as i32); } if __c1 == 0 { break 0; } - __i += 1; + __p1 += 1; + __p2 += 1; } } == 0) as i32) != 0) @@ -69,17 +71,19 @@ fn main_0() -> i32 { assert!((((!((*d2.borrow()).is_null())) as i32) != 0)); assert!( ((({ - let mut __i: usize = 0; + let mut __p1 = (*d2.borrow()).clone(); + let mut __p2 = (*p.borrow()).clone(); loop { - let __c1 = (*d2.borrow()).offset(__i).read(); - let __c2 = (*p.borrow()).offset(__i).read(); + let __c1 = __p1.read(); + let __c2 = __p2.read(); if __c1 != __c2 { break (__c1 as i32) - (__c2 as i32); } if __c1 == 0 { break 0; } - __i += 1; + __p1 += 1; + __p2 += 1; } } == 0) as i32) != 0) @@ -91,17 +95,19 @@ fn main_0() -> i32 { assert!((((!((*d3.borrow()).is_null())) as i32) != 0)); assert!( ((({ - let mut __i: usize = 0; + let mut __p1 = (*d3.borrow()).clone(); + let mut __p2 = (buf.as_pointer() as Ptr).clone(); loop { - let __c1 = (*d3.borrow()).offset(__i).read(); - let __c2 = (buf.as_pointer() as Ptr).offset(__i).read(); + let __c1 = __p1.read(); + let __c2 = __p2.read(); if __c1 != __c2 { break (__c1 as i32) - (__c2 as i32); } if __c1 == 0 { break 0; } - __i += 1; + __p1 += 1; + __p2 += 1; } } == 0) as i32) != 0) @@ -112,17 +118,19 @@ fn main_0() -> i32 { assert!((((!((*d4.borrow()).is_null())) as i32) != 0)); assert!( ((({ - let mut __i: usize = 0; + let mut __p1 = (*d4.borrow()).clone(); + let mut __p2 = (*p.borrow()).clone(); loop { - let __c1 = (*d4.borrow()).offset(__i).read(); - let __c2 = (*p.borrow()).offset(__i).read(); + let __c1 = __p1.read(); + let __c2 = __p2.read(); if __c1 != __c2 { break (__c1 as i32) - (__c2 as i32); } if __c1 == 0 { break 0; } - __i += 1; + __p1 += 1; + __p2 += 1; } } == 0) as i32) != 0) @@ -137,19 +145,19 @@ fn main_0() -> i32 { assert!((((!((*(*(*r.borrow()).upgrade().deref()).name.borrow()).is_null())) as i32) != 0)); assert!( ((({ - let mut __i: usize = 0; + let mut __p1 = (*(*(*r.borrow()).upgrade().deref()).name.borrow()).clone(); + let mut __p2 = (*p.borrow()).clone(); loop { - let __c1 = (*(*(*r.borrow()).upgrade().deref()).name.borrow()) - .offset(__i) - .read(); - let __c2 = (*p.borrow()).offset(__i).read(); + let __c1 = __p1.read(); + let __c2 = __p2.read(); if __c1 != __c2 { break (__c1 as i32) - (__c2 as i32); } if __c1 == 0 { break 0; } - __i += 1; + __p1 += 1; + __p2 += 1; } } == 0) as i32) != 0) diff --git a/tests/unit/out/refcount/string_h.rs b/tests/unit/out/refcount/string_h.rs index 9963d22f..8cf22c36 100644 --- a/tests/unit/out/refcount/string_h.rs +++ b/tests/unit/out/refcount/string_h.rs @@ -124,21 +124,20 @@ pub fn test_memmove_3() { (('\0' as i32) as u8), ]))); let r: Value = Rc::new(RefCell::new({ - let __tmp: Vec = (0..4_usize) - .map(|__i| { - ((buf.as_pointer() as Ptr) as Ptr) - .to_any() - .reinterpret_cast::() - .offset(__i) - .read() - }) - .collect(); + let mut __src = ((buf.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::(); + let mut __tmp: Vec = Vec::with_capacity(4_usize); + for _ in 0..4_usize { + __tmp.push(__src.read()); + __src += 1; + } + let mut __dst = ((buf.as_pointer() as Ptr).offset((1) as isize) as Ptr) + .to_any() + .reinterpret_cast::(); for __i in 0..4_usize { - ((buf.as_pointer() as Ptr).offset((1) as isize) as Ptr) - .to_any() - .reinterpret_cast::() - .offset(__i) - .write(__tmp[__i]); + __dst.write(__tmp[__i]); + __dst += 1; } ((buf.as_pointer() as Ptr).offset((1) as isize) as Ptr) .to_any() @@ -173,32 +172,32 @@ pub fn test_memmove_3() { pub fn test_strchr_4() { let s: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"hello world"))); let r: Value> = Rc::new(RefCell::new({ - let mut __i: usize = 0; + let mut __p = (*s.borrow()).reinterpret_cast::().clone(); loop { - let __c = (*s.borrow()).reinterpret_cast::().offset(__i).read(); + let __c = __p.read(); if __c == ('w' as i32) as u8 { - break (*s.borrow()).reinterpret_cast::().offset(__i); + break __p; } if __c == 0 { break Ptr::null(); } - __i += 1; + __p += 1; } })); assert!((((!((*r.borrow()).is_null())) as i32) != 0)); assert!(((((((*r.borrow()).read()) as i32) == ('w' as i32)) as i32) != 0)); assert!( (((({ - let mut __i: usize = 0; + let mut __p = (*s.borrow()).reinterpret_cast::().clone(); loop { - let __c = (*s.borrow()).reinterpret_cast::().offset(__i).read(); + let __c = __p.read(); if __c == ('z' as i32) as u8 { - break (*s.borrow()).reinterpret_cast::().offset(__i); + break __p; } if __c == 0 { break Ptr::null(); } - __i += 1; + __p += 1; } }) .is_null()) as i32) @@ -208,8 +207,10 @@ pub fn test_strchr_4() { pub fn test_strlen_5() { assert!( ((({ + let mut __p = Ptr::from_string_literal(b"").clone(); let mut __i: usize = 0; - while Ptr::from_string_literal(b"").offset(__i).read() != 0 { + while __p.read() != 0 { + __p += 1; __i += 1; } __i @@ -218,8 +219,10 @@ pub fn test_strlen_5() { ); assert!( ((({ + let mut __p = Ptr::from_string_literal(b"hello").clone(); let mut __i: usize = 0; - while Ptr::from_string_literal(b"hello").offset(__i).read() != 0 { + while __p.read() != 0 { + __p += 1; __i += 1; } __i @@ -228,8 +231,10 @@ pub fn test_strlen_5() { ); assert!( ((({ + let mut __p = Ptr::from_string_literal(b"hello world").clone(); let mut __i: usize = 0; - while Ptr::from_string_literal(b"hello world").offset(__i).read() != 0 { + while __p.read() != 0 { + __p += 1; __i += 1; } __i @@ -241,8 +246,10 @@ pub fn test_strlen_5() { let second: Value> = Rc::new(RefCell::new( ((buf.as_pointer() as Ptr).offset( ({ + let mut __p = (*first.borrow()).clone(); let mut __i: usize = 0; - while (*first.borrow()).offset(__i).read() != 0 { + while __p.read() != 0 { + __p += 1; __i += 1; } __i @@ -252,17 +259,19 @@ pub fn test_strlen_5() { )); assert!( ((({ - let mut __i: usize = 0; + let mut __p1 = (*second.borrow()).clone(); + let mut __p2 = Ptr::from_string_literal(b"two").clone(); loop { - let __c1 = (*second.borrow()).offset(__i).read(); - let __c2 = Ptr::from_string_literal(b"two").offset(__i).read(); + let __c1 = __p1.read(); + let __c2 = __p2.read(); if __c1 != __c2 { break (__c1 as i32) - (__c2 as i32); } if __c1 == 0 { break 0; } - __i += 1; + __p1 += 1; + __p2 += 1; } } == 0) as i32) != 0) @@ -270,8 +279,10 @@ pub fn test_strlen_5() { let big : Value > = Rc::new(RefCell::new(Box::from(*b"hi\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0") )) ; assert!( ((({ + let mut __p = (big.as_pointer() as Ptr).clone(); let mut __i: usize = 0; - while (big.as_pointer() as Ptr).offset(__i).read() != 0 { + while __p.read() != 0 { + __p += 1; __i += 1; } __i @@ -282,8 +293,10 @@ pub fn test_strlen_5() { (*big.borrow_mut())[(3) as usize] = (('\0' as i32) as u8); assert!( ((({ + let mut __p = (big.as_pointer() as Ptr).clone(); let mut __i: usize = 0; - while (big.as_pointer() as Ptr).offset(__i).read() != 0 { + while __p.read() != 0 { + __p += 1; __i += 1; } __i @@ -294,51 +307,57 @@ pub fn test_strlen_5() { pub fn test_strcmp_6() { assert!( ((({ - let mut __i: usize = 0; + let mut __p1 = Ptr::from_string_literal(b"abc").clone(); + let mut __p2 = Ptr::from_string_literal(b"abc").clone(); loop { - let __c1 = Ptr::from_string_literal(b"abc").offset(__i).read(); - let __c2 = Ptr::from_string_literal(b"abc").offset(__i).read(); + let __c1 = __p1.read(); + let __c2 = __p2.read(); if __c1 != __c2 { break (__c1 as i32) - (__c2 as i32); } if __c1 == 0 { break 0; } - __i += 1; + __p1 += 1; + __p2 += 1; } } == 0) as i32) != 0) ); assert!( ((({ - let mut __i: usize = 0; + let mut __p1 = Ptr::from_string_literal(b"abc").clone(); + let mut __p2 = Ptr::from_string_literal(b"abd").clone(); loop { - let __c1 = Ptr::from_string_literal(b"abc").offset(__i).read(); - let __c2 = Ptr::from_string_literal(b"abd").offset(__i).read(); + let __c1 = __p1.read(); + let __c2 = __p2.read(); if __c1 != __c2 { break (__c1 as i32) - (__c2 as i32); } if __c1 == 0 { break 0; } - __i += 1; + __p1 += 1; + __p2 += 1; } } < 0) as i32) != 0) ); assert!( ((({ - let mut __i: usize = 0; + let mut __p1 = Ptr::from_string_literal(b"abd").clone(); + let mut __p2 = Ptr::from_string_literal(b"abc").clone(); loop { - let __c1 = Ptr::from_string_literal(b"abd").offset(__i).read(); - let __c2 = Ptr::from_string_literal(b"abc").offset(__i).read(); + let __c1 = __p1.read(); + let __c2 = __p2.read(); if __c1 != __c2 { break (__c1 as i32) - (__c2 as i32); } if __c1 == 0 { break 0; } - __i += 1; + __p1 += 1; + __p2 += 1; } } > 0) as i32) != 0) @@ -353,51 +372,57 @@ pub fn test_strcmp_6() { ]))); assert!( ((({ - let mut __i: usize = 0; + let mut __p1 = (*p.borrow()).clone(); + let mut __p2 = (*p.borrow()).clone(); loop { - let __c1 = (*p.borrow()).offset(__i).read(); - let __c2 = (*p.borrow()).offset(__i).read(); + let __c1 = __p1.read(); + let __c2 = __p2.read(); if __c1 != __c2 { break (__c1 as i32) - (__c2 as i32); } if __c1 == 0 { break 0; } - __i += 1; + __p1 += 1; + __p2 += 1; } } == 0) as i32) != 0) ); assert!( ((({ - let mut __i: usize = 0; + let mut __p1 = (*p.borrow()).clone(); + let mut __p2 = (*q.borrow()).clone(); loop { - let __c1 = (*p.borrow()).offset(__i).read(); - let __c2 = (*q.borrow()).offset(__i).read(); + let __c1 = __p1.read(); + let __c2 = __p2.read(); if __c1 != __c2 { break (__c1 as i32) - (__c2 as i32); } if __c1 == 0 { break 0; } - __i += 1; + __p1 += 1; + __p2 += 1; } } < 0) as i32) != 0) ); assert!( ((({ - let mut __i: usize = 0; + let mut __p1 = (buf.as_pointer() as Ptr).clone(); + let mut __p2 = (*p.borrow()).clone(); loop { - let __c1 = (buf.as_pointer() as Ptr).offset(__i).read(); - let __c2 = (*p.borrow()).offset(__i).read(); + let __c1 = __p1.read(); + let __c2 = __p2.read(); if __c1 != __c2 { break (__c1 as i32) - (__c2 as i32); } if __c1 == 0 { break 0; } - __i += 1; + __p1 += 1; + __p2 += 1; } } == 0) as i32) != 0) @@ -406,19 +431,23 @@ pub fn test_strcmp_6() { pub fn test_strncmp_7() { assert!( ((({ + let mut __p1 = Ptr::from_string_literal(b"abcdef").clone(); + let mut __p2 = Ptr::from_string_literal(b"abcxyz").clone(); let mut __i: usize = 0; loop { if __i == 3_usize { break 0; } - let __c1 = Ptr::from_string_literal(b"abcdef").offset(__i).read(); - let __c2 = Ptr::from_string_literal(b"abcxyz").offset(__i).read(); + let __c1 = __p1.read(); + let __c2 = __p2.read(); if __c1 != __c2 { break (__c1 as i32) - (__c2 as i32); } if __c1 == 0 { break 0; } + __p1 += 1; + __p2 += 1; __i += 1; } } == 0) as i32) @@ -426,19 +455,23 @@ pub fn test_strncmp_7() { ); assert!( ((({ + let mut __p1 = Ptr::from_string_literal(b"abcdef").clone(); + let mut __p2 = Ptr::from_string_literal(b"abcxyz").clone(); let mut __i: usize = 0; loop { if __i == 4_usize { break 0; } - let __c1 = Ptr::from_string_literal(b"abcdef").offset(__i).read(); - let __c2 = Ptr::from_string_literal(b"abcxyz").offset(__i).read(); + let __c1 = __p1.read(); + let __c2 = __p2.read(); if __c1 != __c2 { break (__c1 as i32) - (__c2 as i32); } if __c1 == 0 { break 0; } + __p1 += 1; + __p2 += 1; __i += 1; } } < 0) as i32) @@ -446,19 +479,23 @@ pub fn test_strncmp_7() { ); assert!( ((({ + let mut __p1 = Ptr::from_string_literal(b"abcxyz").clone(); + let mut __p2 = Ptr::from_string_literal(b"abcdef").clone(); let mut __i: usize = 0; loop { if __i == 4_usize { break 0; } - let __c1 = Ptr::from_string_literal(b"abcxyz").offset(__i).read(); - let __c2 = Ptr::from_string_literal(b"abcdef").offset(__i).read(); + let __c1 = __p1.read(); + let __c2 = __p2.read(); if __c1 != __c2 { break (__c1 as i32) - (__c2 as i32); } if __c1 == 0 { break 0; } + __p1 += 1; + __p2 += 1; __i += 1; } } > 0) as i32) @@ -478,19 +515,23 @@ pub fn test_strncmp_7() { let n: Value = Rc::new(RefCell::new(3_usize)); assert!( ((({ + let mut __p1 = (*p.borrow()).clone(); + let mut __p2 = (*q.borrow()).clone(); let mut __i: usize = 0; loop { if __i == (*n.borrow()) { break 0; } - let __c1 = (*p.borrow()).offset(__i).read(); - let __c2 = (*q.borrow()).offset(__i).read(); + let __c1 = __p1.read(); + let __c2 = __p2.read(); if __c1 != __c2 { break (__c1 as i32) - (__c2 as i32); } if __c1 == 0 { break 0; } + __p1 += 1; + __p2 += 1; __i += 1; } } == 0) as i32) @@ -498,19 +539,23 @@ pub fn test_strncmp_7() { ); assert!( ((({ + let mut __p1 = (*p.borrow()).clone(); + let mut __p2 = (*q.borrow()).clone(); let mut __i: usize = 0; loop { if __i == (*n.borrow()).wrapping_add(1_usize) { break 0; } - let __c1 = (*p.borrow()).offset(__i).read(); - let __c2 = (*q.borrow()).offset(__i).read(); + let __c1 = __p1.read(); + let __c2 = __p2.read(); if __c1 != __c2 { break (__c1 as i32) - (__c2 as i32); } if __c1 == 0 { break 0; } + __p1 += 1; + __p2 += 1; __i += 1; } } < 0) as i32) @@ -518,19 +563,23 @@ pub fn test_strncmp_7() { ); assert!( ((({ + let mut __p1 = (buf.as_pointer() as Ptr).clone(); + let mut __p2 = (*p.borrow()).clone(); let mut __i: usize = 0; loop { if __i == 6_usize { break 0; } - let __c1 = (buf.as_pointer() as Ptr).offset(__i).read(); - let __c2 = (*p.borrow()).offset(__i).read(); + let __c1 = __p1.read(); + let __c2 = __p2.read(); if __c1 != __c2 { break (__c1 as i32) - (__c2 as i32); } if __c1 == 0 { break 0; } + __p1 += 1; + __p2 += 1; __i += 1; } } == 0) as i32) @@ -539,23 +588,22 @@ pub fn test_strncmp_7() { } pub fn test_memchr_8() { let data: Value> = Rc::new(RefCell::new(Box::new([16_u8, 32_u8, 48_u8, 64_u8]))); - let r: Value = Rc::new(RefCell::new( - match (0..4_usize).find(|&__i| { - ((data.as_pointer() as Ptr) as Ptr) - .to_any() - .reinterpret_cast::() - .offset(__i) - .read() - == 48 as u8 - }) { - Some(__i) => ((data.as_pointer() as Ptr) as Ptr) - .to_any() - .reinterpret_cast::() - .offset(__i) - .to_any(), - None => Ptr::::null().to_any(), - }, - )); + let r: Value = Rc::new(RefCell::new({ + let mut __p = ((data.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::(); + let mut __i: usize = 0; + loop { + if __i == 4_usize { + break Ptr::::null().to_any(); + } + if __p.read() == 48 as u8 { + break __p.to_any(); + } + __p += 1; + __i += 1; + } + })); assert!( ((({ let _lhs = (*r.borrow()).clone(); @@ -564,19 +612,21 @@ pub fn test_memchr_8() { != 0) ); assert!( - ((((match (0..4_usize).find(|&__i| ((data.as_pointer() as Ptr::) as Ptr::) - .to_any() - .reinterpret_cast::() - .offset(__i) - .read() - == 153 as u8) - { - Some(__i) => ((data.as_pointer() as Ptr::) as Ptr::) + (((({ + let mut __p = ((data.as_pointer() as Ptr) as Ptr) .to_any() - .reinterpret_cast::() - .offset(__i) - .to_any(), - None => Ptr::::null().to_any(), + .reinterpret_cast::(); + let mut __i: usize = 0; + loop { + if __i == 4_usize { + break Ptr::::null().to_any(); + } + if __p.read() == 153 as u8 { + break __p.to_any(); + } + __p += 1; + __i += 1; + } }) .is_null()) as i32) != 0) @@ -587,11 +637,19 @@ pub fn test_memchr_8() { let n: Value = Rc::new(RefCell::new(4_usize)); assert!( ((({ - let _lhs = match (0..(*n.borrow())) - .find(|&__i| (*p.borrow()).reinterpret_cast::().offset(__i).read() == 16 as u8) - { - Some(__i) => (*p.borrow()).reinterpret_cast::().offset(__i).to_any(), - None => Ptr::::null().to_any(), + let _lhs = { + let mut __p = (*p.borrow()).reinterpret_cast::(); + let mut __i: usize = 0; + loop { + if __i == (*n.borrow()) { + break Ptr::::null().to_any(); + } + if __p.read() == 16 as u8 { + break __p.to_any(); + } + __p += 1; + __i += 1; + } }; _lhs == (*p.borrow()).clone() }) as i32) @@ -601,17 +659,17 @@ pub fn test_memchr_8() { pub fn test_strrchr_9() { let s: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"hello world"))); let r: Value> = Rc::new(RefCell::new({ - let mut __i: usize = 0; + let mut __p = (*s.borrow()).reinterpret_cast::().clone(); let mut __found = Ptr::null(); loop { - let __c = (*s.borrow()).reinterpret_cast::().offset(__i).read(); + let __c = __p.read(); if __c == ('l' as i32) as u8 { - __found = (*s.borrow()).reinterpret_cast::().offset(__i); + __found = __p.clone(); } if __c == 0 { break __found; } - __i += 1; + __p += 1; } })); assert!((((!((*r.borrow()).is_null())) as i32) != 0)); @@ -625,17 +683,17 @@ pub fn test_strrchr_9() { ); assert!( (((({ - let mut __i: usize = 0; + let mut __p = (*s.borrow()).reinterpret_cast::().clone(); let mut __found = Ptr::null(); loop { - let __c = (*s.borrow()).reinterpret_cast::().offset(__i).read(); + let __c = __p.read(); if __c == ('z' as i32) as u8 { - __found = (*s.borrow()).reinterpret_cast::().offset(__i); + __found = __p.clone(); } if __c == 0 { break __found; } - __i += 1; + __p += 1; } }) .is_null()) as i32) @@ -649,17 +707,17 @@ pub fn test_strrchr_9() { ]))); assert!( ((({ - let mut __i: usize = 0; + let mut __p = (buf.as_pointer() as Ptr).clone(); let mut __found = Ptr::null(); loop { - let __c = (buf.as_pointer() as Ptr).offset(__i).read(); + let __c = __p.read(); if __c == ('a' as i32) as u8 { - __found = (buf.as_pointer() as Ptr).offset(__i); + __found = __p.clone(); } if __c == 0 { break __found; } - __i += 1; + __p += 1; } } == ((buf.as_pointer() as Ptr).offset(2))) as i32) != 0) @@ -668,26 +726,28 @@ pub fn test_strrchr_9() { pub fn test_strcspn_10() { assert!( ((({ + let mut __p = Ptr::from_string_literal(b"hello").clone(); let mut __i: usize = 0; loop { - let __c = Ptr::from_string_literal(b"hello").offset(__i).read(); + let __c = __p.read(); if __c == 0 { break __i; } - let mut __j: usize = 0; + let mut __q = Ptr::from_string_literal(b"el").clone(); let __hit = loop { - let __r = Ptr::from_string_literal(b"el").offset(__j).read(); + let __r = __q.read(); if __r == 0 { break false; } if __r == __c { break true; } - __j += 1; + __q += 1; }; if __hit { break __i; } + __p += 1; __i += 1; } } == 1_usize) as i32) @@ -695,26 +755,28 @@ pub fn test_strcspn_10() { ); assert!( ((({ + let mut __p = Ptr::from_string_literal(b"abc").clone(); let mut __i: usize = 0; loop { - let __c = Ptr::from_string_literal(b"abc").offset(__i).read(); + let __c = __p.read(); if __c == 0 { break __i; } - let mut __j: usize = 0; + let mut __q = Ptr::from_string_literal(b"xyz").clone(); let __hit = loop { - let __r = Ptr::from_string_literal(b"xyz").offset(__j).read(); + let __r = __q.read(); if __r == 0 { break false; } if __r == __c { break true; } - __j += 1; + __q += 1; }; if __hit { break __i; } + __p += 1; __i += 1; } } == 3_usize) as i32) @@ -722,26 +784,28 @@ pub fn test_strcspn_10() { ); assert!( ((({ + let mut __p = Ptr::from_string_literal(b"").clone(); let mut __i: usize = 0; loop { - let __c = Ptr::from_string_literal(b"").offset(__i).read(); + let __c = __p.read(); if __c == 0 { break __i; } - let mut __j: usize = 0; + let mut __q = Ptr::from_string_literal(b"abc").clone(); let __hit = loop { - let __r = Ptr::from_string_literal(b"abc").offset(__j).read(); + let __r = __q.read(); if __r == 0 { break false; } if __r == __c { break true; } - __j += 1; + __q += 1; }; if __hit { break __i; } + __p += 1; __i += 1; } } == 0_usize) as i32) @@ -751,26 +815,28 @@ pub fn test_strcspn_10() { let rej: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"el"))); assert!( ((({ + let mut __p = (*s.borrow()).clone(); let mut __i: usize = 0; loop { - let __c = (*s.borrow()).offset(__i).read(); + let __c = __p.read(); if __c == 0 { break __i; } - let mut __j: usize = 0; + let mut __q = (*rej.borrow()).clone(); let __hit = loop { - let __r = (*rej.borrow()).offset(__j).read(); + let __r = __q.read(); if __r == 0 { break false; } if __r == __c { break true; } - __j += 1; + __q += 1; }; if __hit { break __i; } + __p += 1; __i += 1; } } == 1_usize) as i32) @@ -780,26 +846,28 @@ pub fn test_strcspn_10() { pub fn test_strspn_11() { assert!( ((({ + let mut __p = Ptr::from_string_literal(b"hello").clone(); let mut __i: usize = 0; loop { - let __c = Ptr::from_string_literal(b"hello").offset(__i).read(); + let __c = __p.read(); if __c == 0 { break __i; } - let mut __j: usize = 0; + let mut __q = Ptr::from_string_literal(b"hel").clone(); let __hit = loop { - let __r = Ptr::from_string_literal(b"hel").offset(__j).read(); + let __r = __q.read(); if __r == 0 { break false; } if __r == __c { break true; } - __j += 1; + __q += 1; }; if !__hit { break __i; } + __p += 1; __i += 1; } } == 4_usize) as i32) @@ -807,26 +875,28 @@ pub fn test_strspn_11() { ); assert!( ((({ + let mut __p = Ptr::from_string_literal(b"abc").clone(); let mut __i: usize = 0; loop { - let __c = Ptr::from_string_literal(b"abc").offset(__i).read(); + let __c = __p.read(); if __c == 0 { break __i; } - let mut __j: usize = 0; + let mut __q = Ptr::from_string_literal(b"xyz").clone(); let __hit = loop { - let __r = Ptr::from_string_literal(b"xyz").offset(__j).read(); + let __r = __q.read(); if __r == 0 { break false; } if __r == __c { break true; } - __j += 1; + __q += 1; }; if !__hit { break __i; } + __p += 1; __i += 1; } } == 0_usize) as i32) @@ -834,26 +904,28 @@ pub fn test_strspn_11() { ); assert!( ((({ + let mut __p = Ptr::from_string_literal(b"aaa").clone(); let mut __i: usize = 0; loop { - let __c = Ptr::from_string_literal(b"aaa").offset(__i).read(); + let __c = __p.read(); if __c == 0 { break __i; } - let mut __j: usize = 0; + let mut __q = Ptr::from_string_literal(b"a").clone(); let __hit = loop { - let __r = Ptr::from_string_literal(b"a").offset(__j).read(); + let __r = __q.read(); if __r == 0 { break false; } if __r == __c { break true; } - __j += 1; + __q += 1; }; if !__hit { break __i; } + __p += 1; __i += 1; } } == 3_usize) as i32) @@ -863,26 +935,28 @@ pub fn test_strspn_11() { let acc: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"hel"))); assert!( ((({ + let mut __p = (*s.borrow()).clone(); let mut __i: usize = 0; loop { - let __c = (*s.borrow()).offset(__i).read(); + let __c = __p.read(); if __c == 0 { break __i; } - let mut __j: usize = 0; + let mut __q = (*acc.borrow()).clone(); let __hit = loop { - let __r = (*acc.borrow()).offset(__j).read(); + let __r = __q.read(); if __r == 0 { break false; } if __r == __c { break true; } - __j += 1; + __q += 1; }; if !__hit { break __i; } + __p += 1; __i += 1; } } == 4_usize) as i32) @@ -892,31 +966,28 @@ pub fn test_strspn_11() { pub fn test_strstr_12() { let h: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"hello world"))); let r: Value> = Rc::new(RefCell::new({ - let mut __s: usize = 0; + let mut __p = (*h.borrow()).reinterpret_cast::().clone(); loop { - let mut __i: usize = 0; + let mut __h = __p.clone(); + let mut __n = Ptr::from_string_literal(b"world").clone(); let __matched = loop { - let __n = Ptr::from_string_literal(b"world").offset(__i).read(); - if __n == 0 { + let __c = __n.read(); + if __c == 0 { break true; } - if (*h.borrow()) - .reinterpret_cast::() - .offset(__s + __i) - .read() - != __n - { + if __h.read() != __c { break false; } - __i += 1; + __h += 1; + __n += 1; }; if __matched { - break (*h.borrow()).reinterpret_cast::().offset(__s); + break __p; } - if (*h.borrow()).reinterpret_cast::().offset(__s).read() == 0 { + if __p.read() == 0 { break Ptr::null(); } - __s += 1; + __p += 1; } })); assert!((((!((*r.borrow()).is_null())) as i32) != 0)); @@ -929,31 +1000,28 @@ pub fn test_strstr_12() { ); assert!( (((({ - let mut __s: usize = 0; + let mut __p = (*h.borrow()).reinterpret_cast::().clone(); loop { - let mut __i: usize = 0; + let mut __h = __p.clone(); + let mut __n = Ptr::from_string_literal(b"xyz").clone(); let __matched = loop { - let __n = Ptr::from_string_literal(b"xyz").offset(__i).read(); - if __n == 0 { + let __c = __n.read(); + if __c == 0 { break true; } - if (*h.borrow()) - .reinterpret_cast::() - .offset(__s + __i) - .read() - != __n - { + if __h.read() != __c { break false; } - __i += 1; + __h += 1; + __n += 1; }; if __matched { - break (*h.borrow()).reinterpret_cast::().offset(__s); + break __p; } - if (*h.borrow()).reinterpret_cast::().offset(__s).read() == 0 { + if __p.read() == 0 { break Ptr::null(); } - __s += 1; + __p += 1; } }) .is_null()) as i32) @@ -969,26 +1037,28 @@ pub fn test_strstr_12() { ]))); assert!( ((({ - let mut __s: usize = 0; + let mut __p = (buf.as_pointer() as Ptr).clone(); loop { - let mut __i: usize = 0; + let mut __h = __p.clone(); + let mut __n = Ptr::from_string_literal(b"ll").clone(); let __matched = loop { - let __n = Ptr::from_string_literal(b"ll").offset(__i).read(); - if __n == 0 { + let __c = __n.read(); + if __c == 0 { break true; } - if (buf.as_pointer() as Ptr).offset(__s + __i).read() != __n { + if __h.read() != __c { break false; } - __i += 1; + __h += 1; + __n += 1; }; if __matched { - break (buf.as_pointer() as Ptr).offset(__s); + break __p; } - if (buf.as_pointer() as Ptr).offset(__s).read() == 0 { + if __p.read() == 0 { break Ptr::null(); } - __s += 1; + __p += 1; } } == ((buf.as_pointer() as Ptr).offset(2))) as i32) != 0) @@ -997,27 +1067,27 @@ pub fn test_strstr_12() { pub fn test_strpbrk_13() { let s: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"hello world"))); let r: Value> = Rc::new(RefCell::new({ - let mut __i: usize = 0; + let mut __p = (*s.borrow()).reinterpret_cast::().clone(); loop { - let __c = (*s.borrow()).reinterpret_cast::().offset(__i).read(); + let __c = __p.read(); if __c == 0 { break Ptr::null(); } - let mut __j: usize = 0; + let mut __q = Ptr::from_string_literal(b"wo").clone(); let __hit = loop { - let __r = Ptr::from_string_literal(b"wo").offset(__j).read(); + let __r = __q.read(); if __r == 0 { break false; } if __r == __c { break true; } - __j += 1; + __q += 1; }; if __hit { - break (*s.borrow()).reinterpret_cast::().offset(__i); + break __p; } - __i += 1; + __p += 1; } })); assert!((((!((*r.borrow()).is_null())) as i32) != 0)); @@ -1030,27 +1100,27 @@ pub fn test_strpbrk_13() { ); assert!( (((({ - let mut __i: usize = 0; + let mut __p = (*s.borrow()).reinterpret_cast::().clone(); loop { - let __c = (*s.borrow()).reinterpret_cast::().offset(__i).read(); + let __c = __p.read(); if __c == 0 { break Ptr::null(); } - let mut __j: usize = 0; + let mut __q = Ptr::from_string_literal(b"xyz").clone(); let __hit = loop { - let __r = Ptr::from_string_literal(b"xyz").offset(__j).read(); + let __r = __q.read(); if __r == 0 { break false; } if __r == __c { break true; } - __j += 1; + __q += 1; }; if __hit { - break (*s.borrow()).reinterpret_cast::().offset(__i); + break __p; } - __i += 1; + __p += 1; } }) .is_null()) as i32) @@ -1064,27 +1134,27 @@ pub fn test_strpbrk_13() { ]))); assert!( ((({ - let mut __i: usize = 0; + let mut __p = (buf.as_pointer() as Ptr).clone(); loop { - let __c = (buf.as_pointer() as Ptr).offset(__i).read(); + let __c = __p.read(); if __c == 0 { break Ptr::null(); } - let mut __j: usize = 0; + let mut __q = Ptr::from_string_literal(b"b").clone(); let __hit = loop { - let __r = Ptr::from_string_literal(b"b").offset(__j).read(); + let __r = __q.read(); if __r == 0 { break false; } if __r == __c { break true; } - __j += 1; + __q += 1; }; if __hit { - break (buf.as_pointer() as Ptr).offset(__i); + break __p; } - __i += 1; + __p += 1; } } == ((buf.as_pointer() as Ptr).offset(1))) as i32) != 0) @@ -1093,69 +1163,57 @@ pub fn test_strpbrk_13() { pub fn test_strcasecmp_14() { assert!( ((({ - let mut __i: usize = 0; + let mut __p1 = Ptr::from_string_literal(b"HELLO").clone(); + let mut __p2 = Ptr::from_string_literal(b"hello").clone(); loop { - let __c1 = Ptr::from_string_literal(b"HELLO") - .offset(__i) - .read() - .to_ascii_lowercase(); - let __c2 = Ptr::from_string_literal(b"hello") - .offset(__i) - .read() - .to_ascii_lowercase(); + let __c1 = __p1.read().to_ascii_lowercase(); + let __c2 = __p2.read().to_ascii_lowercase(); if __c1 != __c2 { break (__c1 as i32) - (__c2 as i32); } if __c1 == 0 { break 0; } - __i += 1; + __p1 += 1; + __p2 += 1; } } == 0) as i32) != 0) ); assert!( ((({ - let mut __i: usize = 0; + let mut __p1 = Ptr::from_string_literal(b"abc").clone(); + let mut __p2 = Ptr::from_string_literal(b"abd").clone(); loop { - let __c1 = Ptr::from_string_literal(b"abc") - .offset(__i) - .read() - .to_ascii_lowercase(); - let __c2 = Ptr::from_string_literal(b"abd") - .offset(__i) - .read() - .to_ascii_lowercase(); + let __c1 = __p1.read().to_ascii_lowercase(); + let __c2 = __p2.read().to_ascii_lowercase(); if __c1 != __c2 { break (__c1 as i32) - (__c2 as i32); } if __c1 == 0 { break 0; } - __i += 1; + __p1 += 1; + __p2 += 1; } } < 0) as i32) != 0) ); assert!( ((({ - let mut __i: usize = 0; + let mut __p1 = Ptr::from_string_literal(b"abd").clone(); + let mut __p2 = Ptr::from_string_literal(b"abc").clone(); loop { - let __c1 = Ptr::from_string_literal(b"abd") - .offset(__i) - .read() - .to_ascii_lowercase(); - let __c2 = Ptr::from_string_literal(b"abc") - .offset(__i) - .read() - .to_ascii_lowercase(); + let __c1 = __p1.read().to_ascii_lowercase(); + let __c2 = __p2.read().to_ascii_lowercase(); if __c1 != __c2 { break (__c1 as i32) - (__c2 as i32); } if __c1 == 0 { break 0; } - __i += 1; + __p1 += 1; + __p2 += 1; } } > 0) as i32) != 0) @@ -1164,17 +1222,19 @@ pub fn test_strcasecmp_14() { let q: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"foo"))); assert!( ((({ - let mut __i: usize = 0; + let mut __p1 = (*p.borrow()).clone(); + let mut __p2 = (*q.borrow()).clone(); loop { - let __c1 = (*p.borrow()).offset(__i).read().to_ascii_lowercase(); - let __c2 = (*q.borrow()).offset(__i).read().to_ascii_lowercase(); + let __c1 = __p1.read().to_ascii_lowercase(); + let __c2 = __p2.read().to_ascii_lowercase(); if __c1 != __c2 { break (__c1 as i32) - (__c2 as i32); } if __c1 == 0 { break 0; } - __i += 1; + __p1 += 1; + __p2 += 1; } } == 0) as i32) != 0) diff --git a/tests/unit/out/refcount/va_arg_printf.rs b/tests/unit/out/refcount/va_arg_printf.rs index b6b78e75..854f3e13 100644 --- a/tests/unit/out/refcount/va_arg_printf.rs +++ b/tests/unit/out/refcount/va_arg_printf.rs @@ -31,8 +31,10 @@ pub fn lenf_2(fmt: Ptr, __args: &[VaArg]) -> i32 { let s: Value> = Rc::new(RefCell::new(((*ap.borrow_mut()).arg::>()).clone())); let result: Value = Rc::new(RefCell::new( ({ + let mut __p = (*s.borrow()).clone(); let mut __i: usize = 0; - while (*s.borrow()).offset(__i).read() != 0 { + while __p.read() != 0 { + __p += 1; __i += 1; } __i @@ -52,8 +54,10 @@ fn main_0() -> i32 { &[ (10).into(), ({ + let mut __p = (*dummy.borrow()).clone(); let mut __i: usize = 0; - while (*dummy.borrow()).offset(__i).read() != 0 { + while __p.read() != 0 { + __p += 1; __i += 1; } __i From c3efbcd6827ddc0cf77469dab4181c689f5e27a2 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Tue, 14 Jul 2026 23:57:53 +0100 Subject: [PATCH 09/15] Update tests --- tests/unit/out/refcount/inet_pton_ntop.rs | 166 +++++++++++----------- 1 file changed, 84 insertions(+), 82 deletions(-) diff --git a/tests/unit/out/refcount/inet_pton_ntop.rs b/tests/unit/out/refcount/inet_pton_ntop.rs index 1f1e02b6..6df25d74 100644 --- a/tests/unit/out/refcount/inet_pton_ntop.rs +++ b/tests/unit/out/refcount/inet_pton_ntop.rs @@ -251,57 +251,58 @@ fn main_0() -> i32 { let four: Value> = Rc::new(RefCell::new(Box::new([10_u8, 0_u8, 0_u8, 1_u8]))); assert!( ((({ - let mut __i: usize = 0; - loop { - let __c1 = { - let __text = if 2 == libc::AF_INET { - let mut __b = [0u8; 4]; - for __i in 0..4 { - __b[__i] = ((four.as_pointer() as Ptr) as Ptr) - .to_any() - .reinterpret_cast::() - .offset(__i) - .read(); - } - Some(std::net::Ipv4Addr::from(__b).to_string()) - } else if 2 == libc::AF_INET6 { - let mut __b = [0u8; 16]; - for __i in 0..16 { - __b[__i] = ((four.as_pointer() as Ptr) as Ptr) - .to_any() - .reinterpret_cast::() + let mut __p1 = { + let __text = if 2 == libc::AF_INET { + let mut __b = [0u8; 4]; + for __i in 0..4 { + __b[__i] = ((four.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .offset(__i) + .read(); + } + Some(std::net::Ipv4Addr::from(__b).to_string()) + } else if 2 == libc::AF_INET6 { + let mut __b = [0u8; 16]; + for __i in 0..16 { + __b[__i] = ((four.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .offset(__i) + .read(); + } + Some(std::net::Ipv6Addr::from(__b).to_string()) + } else { + None + }; + match __text { + Some(__s) + if (__s.len() as u32) < (::std::mem::size_of::<[u8; 64]>() as u32) => + { + for __i in 0..__s.len() { + (text.as_pointer() as Ptr) .offset(__i) - .read(); - } - Some(std::net::Ipv6Addr::from(__b).to_string()) - } else { - None - }; - match __text { - Some(__s) - if (__s.len() as u32) < (::std::mem::size_of::<[u8; 64]>() as u32) => - { - for __i in 0..__s.len() { - (text.as_pointer() as Ptr) - .offset(__i) - .write(__s.as_bytes()[__i]); - } - (text.as_pointer() as Ptr).offset(__s.len()).write(0); - (text.as_pointer() as Ptr).clone() + .write(__s.as_bytes()[__i]); } - _ => Ptr::null(), + (text.as_pointer() as Ptr).offset(__s.len()).write(0); + (text.as_pointer() as Ptr).clone() } + _ => Ptr::null(), } - .offset(__i) - .read(); - let __c2 = Ptr::from_string_literal(b"10.0.0.1").offset(__i).read(); + } + .clone(); + let mut __p2 = Ptr::from_string_literal(b"10.0.0.1").clone(); + loop { + let __c1 = __p1.read(); + let __c2 = __p2.read(); if __c1 != __c2 { break (__c1 as i32) - (__c2 as i32); } if __c1 == 0 { break 0; } - __i += 1; + __p1 += 1; + __p2 += 1; } } == 0) as i32) != 0) @@ -327,57 +328,58 @@ fn main_0() -> i32 { (*sixteen.borrow_mut())[(15) as usize] = 1_u8; assert!( ((({ - let mut __i: usize = 0; - loop { - let __c1 = { - let __text = if 10 == libc::AF_INET { - let mut __b = [0u8; 4]; - for __i in 0..4 { - __b[__i] = ((sixteen.as_pointer() as Ptr) as Ptr) - .to_any() - .reinterpret_cast::() - .offset(__i) - .read(); - } - Some(std::net::Ipv4Addr::from(__b).to_string()) - } else if 10 == libc::AF_INET6 { - let mut __b = [0u8; 16]; - for __i in 0..16 { - __b[__i] = ((sixteen.as_pointer() as Ptr) as Ptr) - .to_any() - .reinterpret_cast::() + let mut __p1 = { + let __text = if 10 == libc::AF_INET { + let mut __b = [0u8; 4]; + for __i in 0..4 { + __b[__i] = ((sixteen.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .offset(__i) + .read(); + } + Some(std::net::Ipv4Addr::from(__b).to_string()) + } else if 10 == libc::AF_INET6 { + let mut __b = [0u8; 16]; + for __i in 0..16 { + __b[__i] = ((sixteen.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .offset(__i) + .read(); + } + Some(std::net::Ipv6Addr::from(__b).to_string()) + } else { + None + }; + match __text { + Some(__s) + if (__s.len() as u32) < (::std::mem::size_of::<[u8; 64]>() as u32) => + { + for __i in 0..__s.len() { + (text.as_pointer() as Ptr) .offset(__i) - .read(); - } - Some(std::net::Ipv6Addr::from(__b).to_string()) - } else { - None - }; - match __text { - Some(__s) - if (__s.len() as u32) < (::std::mem::size_of::<[u8; 64]>() as u32) => - { - for __i in 0..__s.len() { - (text.as_pointer() as Ptr) - .offset(__i) - .write(__s.as_bytes()[__i]); - } - (text.as_pointer() as Ptr).offset(__s.len()).write(0); - (text.as_pointer() as Ptr).clone() + .write(__s.as_bytes()[__i]); } - _ => Ptr::null(), + (text.as_pointer() as Ptr).offset(__s.len()).write(0); + (text.as_pointer() as Ptr).clone() } + _ => Ptr::null(), } - .offset(__i) - .read(); - let __c2 = Ptr::from_string_literal(b"::1").offset(__i).read(); + } + .clone(); + let mut __p2 = Ptr::from_string_literal(b"::1").clone(); + loop { + let __c1 = __p1.read(); + let __c2 = __p2.read(); if __c1 != __c2 { break (__c1 as i32) - (__c2 as i32); } if __c1 == 0 { break 0; } - __i += 1; + __p1 += 1; + __p2 += 1; } } == 0) as i32) != 0) From 0d6c0f3bc528336480576f5b1fd0ffb9f9e22f80 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Wed, 15 Jul 2026 17:08:53 +0100 Subject: [PATCH 10/15] Drop unsafe from refcount rule --- rules/cstring/tgt_refcount.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rules/cstring/tgt_refcount.rs b/rules/cstring/tgt_refcount.rs index 3e09e03f..7c4d2c65 100644 --- a/rules/cstring/tgt_refcount.rs +++ b/rules/cstring/tgt_refcount.rs @@ -46,7 +46,7 @@ fn f5(a0: Ptr, a1: i32) -> Ptr { } } -unsafe fn f7(a0: Ptr) -> usize { +fn f7(a0: Ptr) -> usize { let mut __p = a0.clone(); let mut __i: usize = 0; while __p.read() != 0 { From 0e847668248b1990256174716c4ab1b47c5ccd34 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Wed, 15 Jul 2026 17:24:06 +0100 Subject: [PATCH 11/15] Use CStringIterator where possible --- rules/cstring/tgt_refcount.rs | 376 ++++------ tests/unit/out/refcount/cstring.rs | 862 ++++++++-------------- tests/unit/out/refcount/offsetof.rs | 11 +- tests/unit/out/refcount/strdup.rs | 71 +- tests/unit/out/refcount/string_h.rs | 873 ++++++++--------------- tests/unit/out/refcount/va_arg_printf.rs | 21 +- 6 files changed, 783 insertions(+), 1431 deletions(-) diff --git a/rules/cstring/tgt_refcount.rs b/rules/cstring/tgt_refcount.rs index 7c4d2c65..7de598f8 100644 --- a/rules/cstring/tgt_refcount.rs +++ b/rules/cstring/tgt_refcount.rs @@ -33,65 +33,52 @@ fn f4(a0: AnyPtr, a1: AnyPtr, a2: usize) -> AnyPtr { } fn f5(a0: Ptr, a1: i32) -> Ptr { - let mut __p = a0.clone(); - loop { - let __c = __p.read(); - if __c == a1 as u8 { - break __p; - } - if __c == 0 { - break Ptr::null(); + let __s = a0.clone(); + let __t = a1 as u8; + match __s.to_c_string_iterator().position(|__c| __c == __t) { + Some(__i) => __s.offset(__i), + None => { + if __t == 0 { + __s.offset(__s.to_c_string_iterator().count()) + } else { + Ptr::null() + } } - __p += 1; } } fn f7(a0: Ptr) -> usize { - let mut __p = a0.clone(); - let mut __i: usize = 0; - while __p.read() != 0 { - __p += 1; - __i += 1; - } - __i + a0.to_c_string_iterator().count() } fn f8(a0: Ptr, a1: Ptr) -> i32 { - let mut __p1 = a0.clone(); - let mut __p2 = a1.clone(); + let mut __it1 = a0.to_c_string_iterator(); + let mut __it2 = a1.to_c_string_iterator(); loop { - let __c1 = __p1.read(); - let __c2 = __p2.read(); + let __c1 = __it1.next(); + let __c2 = __it2.next(); if __c1 != __c2 { - break (__c1 as i32) - (__c2 as i32); + break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); } - if __c1 == 0 { + if __c1.is_none() { break 0; } - __p1 += 1; - __p2 += 1; } } fn f9(a0: Ptr, a1: Ptr, a2: usize) -> i32 { - let mut __p1 = a0.clone(); - let mut __p2 = a1.clone(); - let mut __i: usize = 0; + let __n = a2; + let mut __it1 = a0.to_c_string_iterator().take(__n); + let mut __it2 = a1.to_c_string_iterator().take(__n); loop { - if __i == a2 { - break 0; - } - let __c1 = __p1.read(); - let __c2 = __p2.read(); + let __c1 = __it1.next(); + let __c2 = __it2.next(); if __c1 != __c2 { - break (__c1 as i32) - (__c2 as i32); + break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); } - if __c1 == 0 { + if __c1.is_none() { break 0; } - __p1 += 1; - __p2 += 1; - __i += 1; } } @@ -111,17 +98,22 @@ fn f10(a0: AnyPtr, a1: i32, a2: usize) -> AnyPtr { } fn f11(a0: Ptr, a1: i32) -> Ptr { - let mut __p = a0.clone(); - let mut __found = Ptr::null(); - loop { - let __c = __p.read(); - if __c == a1 as u8 { - __found = __p.clone(); - } - if __c == 0 { - break __found; + let __s = a0.clone(); + let __t = a1 as u8; + match __s + .to_c_string_iterator() + .enumerate() + .filter(|__e| __e.1 == __t) + .last() + { + Some((__i, _)) => __s.offset(__i), + None => { + if __t == 0 { + __s.offset(__s.to_c_string_iterator().count()) + } else { + Ptr::null() + } } - __p += 1; } } @@ -130,76 +122,28 @@ fn f15(a0: Ptr) -> Ptr { } fn f16(a0: Ptr, a1: Ptr) -> usize { - let mut __p = a0.clone(); - let mut __i: usize = 0; - loop { - let __c = __p.read(); - if __c == 0 { - break __i; - } - let mut __q = a1.clone(); - let __hit = loop { - let __r = __q.read(); - if __r == 0 { - break false; - } - if __r == __c { - break true; - } - __q += 1; - }; - if __hit { - break __i; - } - __p += 1; - __i += 1; - } + let __set = a1.clone(); + a0.to_c_string_iterator() + .take_while(|__c| !__set.to_c_string_iterator().any(|__r| __r == *__c)) + .count() } fn f17(a0: Ptr, a1: Ptr) -> usize { - let mut __p = a0.clone(); - let mut __i: usize = 0; - loop { - let __c = __p.read(); - if __c == 0 { - break __i; - } - let mut __q = a1.clone(); - let __hit = loop { - let __r = __q.read(); - if __r == 0 { - break false; - } - if __r == __c { - break true; - } - __q += 1; - }; - if !__hit { - break __i; - } - __p += 1; - __i += 1; - } + let __set = a1.clone(); + a0.to_c_string_iterator() + .take_while(|__c| __set.to_c_string_iterator().any(|__r| __r == *__c)) + .count() } fn f18(a0: Ptr, a1: Ptr) -> Ptr { + let __needle = a1.clone(); let mut __p = a0.clone(); loop { - let mut __h = __p.clone(); - let mut __n = a1.clone(); - let __matched = loop { - let __c = __n.read(); - if __c == 0 { - break true; - } - if __h.read() != __c { - break false; - } - __h += 1; - __n += 1; - }; - if __matched { + let mut __h = __p.to_c_string_iterator(); + if __needle + .to_c_string_iterator() + .all(|__c| __h.next() == Some(__c)) + { break __p; } if __p.read() == 0 { @@ -210,27 +154,14 @@ fn f18(a0: Ptr, a1: Ptr) -> Ptr { } fn f21(a0: Ptr, a1: Ptr) -> Ptr { - let mut __p = a0.clone(); - loop { - let __c = __p.read(); - if __c == 0 { - break Ptr::null(); - } - let mut __q = a1.clone(); - let __hit = loop { - let __r = __q.read(); - if __r == 0 { - break false; - } - if __r == __c { - break true; - } - __q += 1; - }; - if __hit { - break __p; - } - __p += 1; + let __s = a0.clone(); + let __set = a1.clone(); + match __s + .to_c_string_iterator() + .position(|__c| __set.to_c_string_iterator().any(|__r| __r == __c)) + { + Some(__i) => __s.offset(__i), + None => Ptr::null(), } } @@ -251,19 +182,21 @@ fn f24(a0: AnyPtr, a1: i32, a2: usize) -> AnyPtr { } fn f27(a0: Ptr, a1: Ptr) -> i32 { - let mut __p1 = a0.clone(); - let mut __p2 = a1.clone(); + let mut __it1 = a0 + .to_c_string_iterator() + .map(|__c| __c.to_ascii_lowercase()); + let mut __it2 = a1 + .to_c_string_iterator() + .map(|__c| __c.to_ascii_lowercase()); loop { - let __c1 = __p1.read().to_ascii_lowercase(); - let __c2 = __p2.read().to_ascii_lowercase(); + let __c1 = __it1.next(); + let __c2 = __it2.next(); if __c1 != __c2 { - break (__c1 as i32) - (__c2 as i32); + break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); } - if __c1 == 0 { + if __c1.is_none() { break 0; } - __p1 += 1; - __p2 += 1; } } @@ -298,16 +231,17 @@ fn f28(a0: i32, a1: Ptr, a2: usize) -> i32 { } fn f6(a0: Ptr, a1: i32) -> Ptr { - let mut __p = a0.clone(); - loop { - let __c = __p.read(); - if __c == a1 as u8 { - break __p; - } - if __c == 0 { - break Ptr::null(); + let __s = a0.clone(); + let __t = a1 as u8; + match __s.to_c_string_iterator().position(|__c| __c == __t) { + Some(__i) => __s.offset(__i), + None => { + if __t == 0 { + __s.offset(__s.to_c_string_iterator().count()) + } else { + Ptr::null() + } } - __p += 1; } } @@ -327,52 +261,54 @@ fn f12(a0: AnyPtr, a1: i32, a2: usize) -> AnyPtr { } fn f13(a0: Ptr, a1: i32) -> Ptr { - let mut __p = a0.clone(); - let mut __found = Ptr::null(); - loop { - let __c = __p.read(); - if __c == a1 as u8 { - __found = __p.clone(); - } - if __c == 0 { - break __found; + let __s = a0.clone(); + let __t = a1 as u8; + match __s + .to_c_string_iterator() + .enumerate() + .filter(|__e| __e.1 == __t) + .last() + { + Some((__i, _)) => __s.offset(__i), + None => { + if __t == 0 { + __s.offset(__s.to_c_string_iterator().count()) + } else { + Ptr::null() + } } - __p += 1; } } fn f14(a0: Ptr, a1: i32) -> Ptr { - let mut __p = a0.clone(); - let mut __found = Ptr::null(); - loop { - let __c = __p.read(); - if __c == a1 as u8 { - __found = __p.clone(); - } - if __c == 0 { - break __found; + let __s = a0.clone(); + let __t = a1 as u8; + match __s + .to_c_string_iterator() + .enumerate() + .filter(|__e| __e.1 == __t) + .last() + { + Some((__i, _)) => __s.offset(__i), + None => { + if __t == 0 { + __s.offset(__s.to_c_string_iterator().count()) + } else { + Ptr::null() + } } - __p += 1; } } fn f19(a0: Ptr, a1: Ptr) -> Ptr { + let __needle = a1.clone(); let mut __p = a0.clone(); loop { - let mut __h = __p.clone(); - let mut __n = a1.clone(); - let __matched = loop { - let __c = __n.read(); - if __c == 0 { - break true; - } - if __h.read() != __c { - break false; - } - __h += 1; - __n += 1; - }; - if __matched { + let mut __h = __p.to_c_string_iterator(); + if __needle + .to_c_string_iterator() + .all(|__c| __h.next() == Some(__c)) + { break __p; } if __p.read() == 0 { @@ -383,22 +319,14 @@ fn f19(a0: Ptr, a1: Ptr) -> Ptr { } fn f20(a0: Ptr, a1: Ptr) -> Ptr { + let __needle = a1.clone(); let mut __p = a0.clone(); loop { - let mut __h = __p.clone(); - let mut __n = a1.clone(); - let __matched = loop { - let __c = __n.read(); - if __c == 0 { - break true; - } - if __h.read() != __c { - break false; - } - __h += 1; - __n += 1; - }; - if __matched { + let mut __h = __p.to_c_string_iterator(); + if __needle + .to_c_string_iterator() + .all(|__c| __h.next() == Some(__c)) + { break __p; } if __p.read() == 0 { @@ -409,52 +337,26 @@ fn f20(a0: Ptr, a1: Ptr) -> Ptr { } fn f22(a0: Ptr, a1: Ptr) -> Ptr { - let mut __p = a0.clone(); - loop { - let __c = __p.read(); - if __c == 0 { - break Ptr::null(); - } - let mut __q = a1.clone(); - let __hit = loop { - let __r = __q.read(); - if __r == 0 { - break false; - } - if __r == __c { - break true; - } - __q += 1; - }; - if __hit { - break __p; - } - __p += 1; + let __s = a0.clone(); + let __set = a1.clone(); + match __s + .to_c_string_iterator() + .position(|__c| __set.to_c_string_iterator().any(|__r| __r == __c)) + { + Some(__i) => __s.offset(__i), + None => Ptr::null(), } } fn f23(a0: Ptr, a1: Ptr) -> Ptr { - let mut __p = a0.clone(); - loop { - let __c = __p.read(); - if __c == 0 { - break Ptr::null(); - } - let mut __q = a1.clone(); - let __hit = loop { - let __r = __q.read(); - if __r == 0 { - break false; - } - if __r == __c { - break true; - } - __q += 1; - }; - if __hit { - break __p; - } - __p += 1; + let __s = a0.clone(); + let __set = a1.clone(); + match __s + .to_c_string_iterator() + .position(|__c| __set.to_c_string_iterator().any(|__r| __r == __c)) + { + Some(__i) => __s.offset(__i), + None => Ptr::null(), } } diff --git a/tests/unit/out/refcount/cstring.rs b/tests/unit/out/refcount/cstring.rs index 54061555..0a0a626c 100644 --- a/tests/unit/out/refcount/cstring.rs +++ b/tests/unit/out/refcount/cstring.rs @@ -138,124 +138,100 @@ pub fn test_memmove_3() { pub fn test_strchr_4() { let s: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"hello world"))); let r: Value> = Rc::new(RefCell::new({ - let mut __p = (*s.borrow()).clone(); - loop { - let __c = __p.read(); - if __c == (('w' as u8) as i32) as u8 { - break __p; - } - if __c == 0 { - break Ptr::null(); + let __s = (*s.borrow()).clone(); + let __t = (('w' as u8) as i32) as u8; + match __s.to_c_string_iterator().position(|__c| __c == __t) { + Some(__i) => __s.offset(__i), + None => { + if __t == 0 { + __s.offset(__s.to_c_string_iterator().count()) + } else { + Ptr::null() + } } - __p += 1; } })); assert!(!((*r.borrow()).is_null())); assert!(((((*r.borrow()).read()) as i32) == (('w' as u8) as i32))); assert!( ({ - let mut __p = (*s.borrow()).clone(); - loop { - let __c = __p.read(); - if __c == (('z' as u8) as i32) as u8 { - break __p; - } - if __c == 0 { - break Ptr::null(); + let __s = (*s.borrow()).clone(); + let __t = (('z' as u8) as i32) as u8; + match __s.to_c_string_iterator().position(|__c| __c == __t) { + Some(__i) => __s.offset(__i), + None => { + if __t == 0 { + __s.offset(__s.to_c_string_iterator().count()) + } else { + Ptr::null() + } } - __p += 1; } }) .is_null() ); } pub fn test_strlen_5() { + assert!((Ptr::from_string_literal(b"").to_c_string_iterator().count() == 0_usize)); assert!( - ({ - let mut __p = Ptr::from_string_literal(b"").clone(); - let mut __i: usize = 0; - while __p.read() != 0 { - __p += 1; - __i += 1; - } - __i - } == 0_usize) - ); - assert!( - ({ - let mut __p = Ptr::from_string_literal(b"hello").clone(); - let mut __i: usize = 0; - while __p.read() != 0 { - __p += 1; - __i += 1; - } - __i - } == 5_usize) + (Ptr::from_string_literal(b"hello") + .to_c_string_iterator() + .count() + == 5_usize) ); assert!( - ({ - let mut __p = Ptr::from_string_literal(b"hello world").clone(); - let mut __i: usize = 0; - while __p.read() != 0 { - __p += 1; - __i += 1; - } - __i - } == 11_usize) + (Ptr::from_string_literal(b"hello world") + .to_c_string_iterator() + .count() + == 11_usize) ); } pub fn test_strcmp_6() { assert!( ({ - let mut __p1 = Ptr::from_string_literal(b"abc").clone(); - let mut __p2 = Ptr::from_string_literal(b"abc").clone(); + let mut __it1 = Ptr::from_string_literal(b"abc").to_c_string_iterator(); + let mut __it2 = Ptr::from_string_literal(b"abc").to_c_string_iterator(); loop { - let __c1 = __p1.read(); - let __c2 = __p2.read(); + let __c1 = __it1.next(); + let __c2 = __it2.next(); if __c1 != __c2 { - break (__c1 as i32) - (__c2 as i32); + break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); } - if __c1 == 0 { + if __c1.is_none() { break 0; } - __p1 += 1; - __p2 += 1; } } == 0) ); assert!( ({ - let mut __p1 = Ptr::from_string_literal(b"abc").clone(); - let mut __p2 = Ptr::from_string_literal(b"abd").clone(); + let mut __it1 = Ptr::from_string_literal(b"abc").to_c_string_iterator(); + let mut __it2 = Ptr::from_string_literal(b"abd").to_c_string_iterator(); loop { - let __c1 = __p1.read(); - let __c2 = __p2.read(); + let __c1 = __it1.next(); + let __c2 = __it2.next(); if __c1 != __c2 { - break (__c1 as i32) - (__c2 as i32); + break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); } - if __c1 == 0 { + if __c1.is_none() { break 0; } - __p1 += 1; - __p2 += 1; } } < 0) ); assert!( ({ - let mut __p1 = Ptr::from_string_literal(b"abd").clone(); - let mut __p2 = Ptr::from_string_literal(b"abc").clone(); + let mut __it1 = Ptr::from_string_literal(b"abd").to_c_string_iterator(); + let mut __it2 = Ptr::from_string_literal(b"abc").to_c_string_iterator(); loop { - let __c1 = __p1.read(); - let __c2 = __p2.read(); + let __c1 = __it1.next(); + let __c2 = __it2.next(); if __c1 != __c2 { - break (__c1 as i32) - (__c2 as i32); + break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); } - if __c1 == 0 { + if __c1.is_none() { break 0; } - __p1 += 1; - __p2 += 1; } } > 0) ); @@ -269,55 +245,49 @@ pub fn test_strcmp_6() { ]))); assert!( ({ - let mut __p1 = (*p.borrow()).clone(); - let mut __p2 = (*p.borrow()).clone(); + let mut __it1 = (*p.borrow()).to_c_string_iterator(); + let mut __it2 = (*p.borrow()).to_c_string_iterator(); loop { - let __c1 = __p1.read(); - let __c2 = __p2.read(); + let __c1 = __it1.next(); + let __c2 = __it2.next(); if __c1 != __c2 { - break (__c1 as i32) - (__c2 as i32); + break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); } - if __c1 == 0 { + if __c1.is_none() { break 0; } - __p1 += 1; - __p2 += 1; } } == 0) ); assert!( ({ - let mut __p1 = (*p.borrow()).clone(); - let mut __p2 = (*q.borrow()).clone(); + let mut __it1 = (*p.borrow()).to_c_string_iterator(); + let mut __it2 = (*q.borrow()).to_c_string_iterator(); loop { - let __c1 = __p1.read(); - let __c2 = __p2.read(); + let __c1 = __it1.next(); + let __c2 = __it2.next(); if __c1 != __c2 { - break (__c1 as i32) - (__c2 as i32); + break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); } - if __c1 == 0 { + if __c1.is_none() { break 0; } - __p1 += 1; - __p2 += 1; } } < 0) ); assert!( ({ - let mut __p1 = (buf.as_pointer() as Ptr).clone(); - let mut __p2 = (*p.borrow()).clone(); + let mut __it1 = (buf.as_pointer() as Ptr).to_c_string_iterator(); + let mut __it2 = (*p.borrow()).to_c_string_iterator(); loop { - let __c1 = __p1.read(); - let __c2 = __p2.read(); + let __c1 = __it1.next(); + let __c2 = __it2.next(); if __c1 != __c2 { - break (__c1 as i32) - (__c2 as i32); + break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); } - if __c1 == 0 { + if __c1.is_none() { break 0; } - __p1 += 1; - __p2 += 1; } } == 0) ); @@ -325,70 +295,64 @@ pub fn test_strcmp_6() { pub fn test_strncmp_7() { assert!( ({ - let mut __p1 = Ptr::from_string_literal(b"abcdef").clone(); - let mut __p2 = Ptr::from_string_literal(b"abcxyz").clone(); - let mut __i: usize = 0; + let __n = 3_usize; + let mut __it1 = Ptr::from_string_literal(b"abcdef") + .to_c_string_iterator() + .take(__n); + let mut __it2 = Ptr::from_string_literal(b"abcxyz") + .to_c_string_iterator() + .take(__n); loop { - if __i == 3_usize { - break 0; - } - let __c1 = __p1.read(); - let __c2 = __p2.read(); + let __c1 = __it1.next(); + let __c2 = __it2.next(); if __c1 != __c2 { - break (__c1 as i32) - (__c2 as i32); + break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); } - if __c1 == 0 { + if __c1.is_none() { break 0; } - __p1 += 1; - __p2 += 1; - __i += 1; } } == 0) ); assert!( ({ - let mut __p1 = Ptr::from_string_literal(b"abcdef").clone(); - let mut __p2 = Ptr::from_string_literal(b"abcxyz").clone(); - let mut __i: usize = 0; + let __n = 4_usize; + let mut __it1 = Ptr::from_string_literal(b"abcdef") + .to_c_string_iterator() + .take(__n); + let mut __it2 = Ptr::from_string_literal(b"abcxyz") + .to_c_string_iterator() + .take(__n); loop { - if __i == 4_usize { - break 0; - } - let __c1 = __p1.read(); - let __c2 = __p2.read(); + let __c1 = __it1.next(); + let __c2 = __it2.next(); if __c1 != __c2 { - break (__c1 as i32) - (__c2 as i32); + break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); } - if __c1 == 0 { + if __c1.is_none() { break 0; } - __p1 += 1; - __p2 += 1; - __i += 1; } } < 0) ); assert!( ({ - let mut __p1 = Ptr::from_string_literal(b"abcxyz").clone(); - let mut __p2 = Ptr::from_string_literal(b"abcdef").clone(); - let mut __i: usize = 0; + let __n = 4_usize; + let mut __it1 = Ptr::from_string_literal(b"abcxyz") + .to_c_string_iterator() + .take(__n); + let mut __it2 = Ptr::from_string_literal(b"abcdef") + .to_c_string_iterator() + .take(__n); loop { - if __i == 4_usize { - break 0; - } - let __c1 = __p1.read(); - let __c2 = __p2.read(); + let __c1 = __it1.next(); + let __c2 = __it2.next(); if __c1 != __c2 { - break (__c1 as i32) - (__c2 as i32); + break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); } - if __c1 == 0 { + if __c1.is_none() { break 0; } - __p1 += 1; - __p2 += 1; - __i += 1; } } > 0) ); @@ -406,70 +370,54 @@ pub fn test_strncmp_7() { let n: Value = Rc::new(RefCell::new(3_usize)); assert!( ({ - let mut __p1 = (*p.borrow()).clone(); - let mut __p2 = (*q.borrow()).clone(); - let mut __i: usize = 0; + let __n = (*n.borrow()); + let mut __it1 = (*p.borrow()).to_c_string_iterator().take(__n); + let mut __it2 = (*q.borrow()).to_c_string_iterator().take(__n); loop { - if __i == (*n.borrow()) { - break 0; - } - let __c1 = __p1.read(); - let __c2 = __p2.read(); + let __c1 = __it1.next(); + let __c2 = __it2.next(); if __c1 != __c2 { - break (__c1 as i32) - (__c2 as i32); + break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); } - if __c1 == 0 { + if __c1.is_none() { break 0; } - __p1 += 1; - __p2 += 1; - __i += 1; } } == 0) ); assert!( ({ - let mut __p1 = (*p.borrow()).clone(); - let mut __p2 = (*q.borrow()).clone(); - let mut __i: usize = 0; + let __n = (*n.borrow()).wrapping_add(1_usize); + let mut __it1 = (*p.borrow()).to_c_string_iterator().take(__n); + let mut __it2 = (*q.borrow()).to_c_string_iterator().take(__n); loop { - if __i == (*n.borrow()).wrapping_add(1_usize) { - break 0; - } - let __c1 = __p1.read(); - let __c2 = __p2.read(); + let __c1 = __it1.next(); + let __c2 = __it2.next(); if __c1 != __c2 { - break (__c1 as i32) - (__c2 as i32); + break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); } - if __c1 == 0 { + if __c1.is_none() { break 0; } - __p1 += 1; - __p2 += 1; - __i += 1; } } < 0) ); assert!( ({ - let mut __p1 = (buf.as_pointer() as Ptr).clone(); - let mut __p2 = (*p.borrow()).clone(); - let mut __i: usize = 0; + let __n = 6_usize; + let mut __it1 = (buf.as_pointer() as Ptr) + .to_c_string_iterator() + .take(__n); + let mut __it2 = (*p.borrow()).to_c_string_iterator().take(__n); loop { - if __i == 6_usize { - break 0; - } - let __c1 = __p1.read(); - let __c2 = __p2.read(); + let __c1 = __it1.next(); + let __c2 = __it2.next(); if __c1 != __c2 { - break (__c1 as i32) - (__c2 as i32); + break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); } - if __c1 == 0 { + if __c1.is_none() { break 0; } - __p1 += 1; - __p2 += 1; - __i += 1; } } == 0) ); @@ -540,17 +488,22 @@ pub fn test_memchr_8() { pub fn test_strrchr_9() { let s: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"hello world"))); let r: Value> = Rc::new(RefCell::new({ - let mut __p = (*s.borrow()).clone(); - let mut __found = Ptr::null(); - loop { - let __c = __p.read(); - if __c == (('l' as u8) as i32) as u8 { - __found = __p.clone(); - } - if __c == 0 { - break __found; + let __s = (*s.borrow()).clone(); + let __t = (('l' as u8) as i32) as u8; + match __s + .to_c_string_iterator() + .enumerate() + .filter(|__e| __e.1 == __t) + .last() + { + Some((__i, _)) => __s.offset(__i), + None => { + if __t == 0 { + __s.offset(__s.to_c_string_iterator().count()) + } else { + Ptr::null() + } } - __p += 1; } })); assert!(!((*r.borrow()).is_null())); @@ -561,17 +514,22 @@ pub fn test_strrchr_9() { }); assert!( ({ - let mut __p = (*s.borrow()).clone(); - let mut __found = Ptr::null(); - loop { - let __c = __p.read(); - if __c == (('z' as u8) as i32) as u8 { - __found = __p.clone(); - } - if __c == 0 { - break __found; + let __s = (*s.borrow()).clone(); + let __t = (('z' as u8) as i32) as u8; + match __s + .to_c_string_iterator() + .enumerate() + .filter(|__e| __e.1 == __t) + .last() + { + Some((__i, _)) => __s.offset(__i), + None => { + if __t == 0 { + __s.offset(__s.to_c_string_iterator().count()) + } else { + Ptr::null() + } } - __p += 1; } }) .is_null() @@ -584,17 +542,22 @@ pub fn test_strrchr_9() { ]))); assert!( ({ - let mut __p = (buf.as_pointer() as Ptr).clone(); - let mut __found = Ptr::null(); - loop { - let __c = __p.read(); - if __c == (('a' as u8) as i32) as u8 { - __found = __p.clone(); - } - if __c == 0 { - break __found; + let __s = (buf.as_pointer() as Ptr).clone(); + let __t = (('a' as u8) as i32) as u8; + match __s + .to_c_string_iterator() + .enumerate() + .filter(|__e| __e.1 == __t) + .last() + { + Some((__i, _)) => __s.offset(__i), + None => { + if __t == 0 { + __s.offset(__s.to_c_string_iterator().count()) + } else { + Ptr::null() + } } - __p += 1; } } == ((buf.as_pointer() as Ptr).offset(2))) ); @@ -606,19 +569,17 @@ pub fn test_strdup_10() { assert!(!((*d.borrow()).is_null())); assert!( ({ - let mut __p1 = (*d.borrow()).clone(); - let mut __p2 = Ptr::from_string_literal(b"hello").clone(); + let mut __it1 = (*d.borrow()).to_c_string_iterator(); + let mut __it2 = Ptr::from_string_literal(b"hello").to_c_string_iterator(); loop { - let __c1 = __p1.read(); - let __c2 = __p2.read(); + let __c1 = __it1.next(); + let __c2 = __it2.next(); if __c1 != __c2 { - break (__c1 as i32) - (__c2 as i32); + break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); } - if __c1 == 0 { + if __c1.is_none() { break 0; } - __p1 += 1; - __p2 += 1; } } == 0) ); @@ -636,19 +597,17 @@ pub fn test_strdup_10() { assert!(!((*d2.borrow()).is_null())); assert!( ({ - let mut __p1 = (*d2.borrow()).clone(); - let mut __p2 = (*p.borrow()).clone(); + let mut __it1 = (*d2.borrow()).to_c_string_iterator(); + let mut __it2 = (*p.borrow()).to_c_string_iterator(); loop { - let __c1 = __p1.read(); - let __c2 = __p2.read(); + let __c1 = __it1.next(); + let __c2 = __it2.next(); if __c1 != __c2 { - break (__c1 as i32) - (__c2 as i32); + break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); } - if __c1 == 0 { + if __c1.is_none() { break 0; } - __p1 += 1; - __p2 += 1; } } == 0) ); @@ -659,19 +618,17 @@ pub fn test_strdup_10() { assert!(!((*d3.borrow()).is_null())); assert!( ({ - let mut __p1 = (*d3.borrow()).clone(); - let mut __p2 = (buf.as_pointer() as Ptr).clone(); + let mut __it1 = (*d3.borrow()).to_c_string_iterator(); + let mut __it2 = (buf.as_pointer() as Ptr).to_c_string_iterator(); loop { - let __c1 = __p1.read(); - let __c2 = __p2.read(); + let __c1 = __it1.next(); + let __c2 = __it2.next(); if __c1 != __c2 { - break (__c1 as i32) - (__c2 as i32); + break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); } - if __c1 == 0 { + if __c1.is_none() { break 0; } - __p1 += 1; - __p2 += 1; } } == 0) ); @@ -680,254 +637,94 @@ pub fn test_strdup_10() { pub fn test_strcspn_11() { assert!( ({ - let mut __p = Ptr::from_string_literal(b"hello").clone(); - let mut __i: usize = 0; - loop { - let __c = __p.read(); - if __c == 0 { - break __i; - } - let mut __q = Ptr::from_string_literal(b"el").clone(); - let __hit = loop { - let __r = __q.read(); - if __r == 0 { - break false; - } - if __r == __c { - break true; - } - __q += 1; - }; - if __hit { - break __i; - } - __p += 1; - __i += 1; - } + let __set = Ptr::from_string_literal(b"el").clone(); + Ptr::from_string_literal(b"hello") + .to_c_string_iterator() + .take_while(|__c| !__set.to_c_string_iterator().any(|__r| __r == *__c)) + .count() } == 1_usize) ); assert!( ({ - let mut __p = Ptr::from_string_literal(b"abc").clone(); - let mut __i: usize = 0; - loop { - let __c = __p.read(); - if __c == 0 { - break __i; - } - let mut __q = Ptr::from_string_literal(b"xyz").clone(); - let __hit = loop { - let __r = __q.read(); - if __r == 0 { - break false; - } - if __r == __c { - break true; - } - __q += 1; - }; - if __hit { - break __i; - } - __p += 1; - __i += 1; - } + let __set = Ptr::from_string_literal(b"xyz").clone(); + Ptr::from_string_literal(b"abc") + .to_c_string_iterator() + .take_while(|__c| !__set.to_c_string_iterator().any(|__r| __r == *__c)) + .count() } == 3_usize) ); assert!( ({ - let mut __p = Ptr::from_string_literal(b"").clone(); - let mut __i: usize = 0; - loop { - let __c = __p.read(); - if __c == 0 { - break __i; - } - let mut __q = Ptr::from_string_literal(b"abc").clone(); - let __hit = loop { - let __r = __q.read(); - if __r == 0 { - break false; - } - if __r == __c { - break true; - } - __q += 1; - }; - if __hit { - break __i; - } - __p += 1; - __i += 1; - } + let __set = Ptr::from_string_literal(b"abc").clone(); + Ptr::from_string_literal(b"") + .to_c_string_iterator() + .take_while(|__c| !__set.to_c_string_iterator().any(|__r| __r == *__c)) + .count() } == 0_usize) ); let s: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"hello"))); let rej: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"el"))); assert!( ({ - let mut __p = (*s.borrow()).clone(); - let mut __i: usize = 0; - loop { - let __c = __p.read(); - if __c == 0 { - break __i; - } - let mut __q = (*rej.borrow()).clone(); - let __hit = loop { - let __r = __q.read(); - if __r == 0 { - break false; - } - if __r == __c { - break true; - } - __q += 1; - }; - if __hit { - break __i; - } - __p += 1; - __i += 1; - } + let __set = (*rej.borrow()).clone(); + (*s.borrow()) + .to_c_string_iterator() + .take_while(|__c| !__set.to_c_string_iterator().any(|__r| __r == *__c)) + .count() } == 1_usize) ); } pub fn test_strspn_12() { assert!( ({ - let mut __p = Ptr::from_string_literal(b"hello").clone(); - let mut __i: usize = 0; - loop { - let __c = __p.read(); - if __c == 0 { - break __i; - } - let mut __q = Ptr::from_string_literal(b"hel").clone(); - let __hit = loop { - let __r = __q.read(); - if __r == 0 { - break false; - } - if __r == __c { - break true; - } - __q += 1; - }; - if !__hit { - break __i; - } - __p += 1; - __i += 1; - } + let __set = Ptr::from_string_literal(b"hel").clone(); + Ptr::from_string_literal(b"hello") + .to_c_string_iterator() + .take_while(|__c| __set.to_c_string_iterator().any(|__r| __r == *__c)) + .count() } == 4_usize) ); assert!( ({ - let mut __p = Ptr::from_string_literal(b"abc").clone(); - let mut __i: usize = 0; - loop { - let __c = __p.read(); - if __c == 0 { - break __i; - } - let mut __q = Ptr::from_string_literal(b"xyz").clone(); - let __hit = loop { - let __r = __q.read(); - if __r == 0 { - break false; - } - if __r == __c { - break true; - } - __q += 1; - }; - if !__hit { - break __i; - } - __p += 1; - __i += 1; - } + let __set = Ptr::from_string_literal(b"xyz").clone(); + Ptr::from_string_literal(b"abc") + .to_c_string_iterator() + .take_while(|__c| __set.to_c_string_iterator().any(|__r| __r == *__c)) + .count() } == 0_usize) ); assert!( ({ - let mut __p = Ptr::from_string_literal(b"aaa").clone(); - let mut __i: usize = 0; - loop { - let __c = __p.read(); - if __c == 0 { - break __i; - } - let mut __q = Ptr::from_string_literal(b"a").clone(); - let __hit = loop { - let __r = __q.read(); - if __r == 0 { - break false; - } - if __r == __c { - break true; - } - __q += 1; - }; - if !__hit { - break __i; - } - __p += 1; - __i += 1; - } + let __set = Ptr::from_string_literal(b"a").clone(); + Ptr::from_string_literal(b"aaa") + .to_c_string_iterator() + .take_while(|__c| __set.to_c_string_iterator().any(|__r| __r == *__c)) + .count() } == 3_usize) ); let s: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"hello"))); let acc: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"hel"))); assert!( ({ - let mut __p = (*s.borrow()).clone(); - let mut __i: usize = 0; - loop { - let __c = __p.read(); - if __c == 0 { - break __i; - } - let mut __q = (*acc.borrow()).clone(); - let __hit = loop { - let __r = __q.read(); - if __r == 0 { - break false; - } - if __r == __c { - break true; - } - __q += 1; - }; - if !__hit { - break __i; - } - __p += 1; - __i += 1; - } + let __set = (*acc.borrow()).clone(); + (*s.borrow()) + .to_c_string_iterator() + .take_while(|__c| __set.to_c_string_iterator().any(|__r| __r == *__c)) + .count() } == 4_usize) ); } pub fn test_strstr_13() { let h: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"hello world"))); let r: Value> = Rc::new(RefCell::new({ + let __needle = Ptr::from_string_literal(b"world").clone(); let mut __p = (*h.borrow()).clone(); loop { - let mut __h = __p.clone(); - let mut __n = Ptr::from_string_literal(b"world").clone(); - let __matched = loop { - let __c = __n.read(); - if __c == 0 { - break true; - } - if __h.read() != __c { - break false; - } - __h += 1; - __n += 1; - }; - if __matched { + let mut __h = __p.to_c_string_iterator(); + if __needle + .to_c_string_iterator() + .all(|__c| __h.next() == Some(__c)) + { break __p; } if __p.read() == 0 { @@ -943,22 +740,14 @@ pub fn test_strstr_13() { }); assert!( ({ + let __needle = Ptr::from_string_literal(b"xyz").clone(); let mut __p = (*h.borrow()).clone(); loop { - let mut __h = __p.clone(); - let mut __n = Ptr::from_string_literal(b"xyz").clone(); - let __matched = loop { - let __c = __n.read(); - if __c == 0 { - break true; - } - if __h.read() != __c { - break false; - } - __h += 1; - __n += 1; - }; - if __matched { + let mut __h = __p.to_c_string_iterator(); + if __needle + .to_c_string_iterator() + .all(|__c| __h.next() == Some(__c)) + { break __p; } if __p.read() == 0 { @@ -979,22 +768,14 @@ pub fn test_strstr_13() { ]))); assert!( ({ + let __needle = Ptr::from_string_literal(b"ll").clone(); let mut __p = (buf.as_pointer() as Ptr).clone(); loop { - let mut __h = __p.clone(); - let mut __n = Ptr::from_string_literal(b"ll").clone(); - let __matched = loop { - let __c = __n.read(); - if __c == 0 { - break true; - } - if __h.read() != __c { - break false; - } - __h += 1; - __n += 1; - }; - if __matched { + let mut __h = __p.to_c_string_iterator(); + if __needle + .to_c_string_iterator() + .all(|__c| __h.next() == Some(__c)) + { break __p; } if __p.read() == 0 { @@ -1008,27 +789,14 @@ pub fn test_strstr_13() { pub fn test_strpbrk_14() { let s: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"hello world"))); let r: Value> = Rc::new(RefCell::new({ - let mut __p = (*s.borrow()).clone(); - loop { - let __c = __p.read(); - if __c == 0 { - break Ptr::null(); - } - let mut __q = Ptr::from_string_literal(b"wo").clone(); - let __hit = loop { - let __r = __q.read(); - if __r == 0 { - break false; - } - if __r == __c { - break true; - } - __q += 1; - }; - if __hit { - break __p; - } - __p += 1; + let __s = (*s.borrow()).clone(); + let __set = Ptr::from_string_literal(b"wo").clone(); + match __s + .to_c_string_iterator() + .position(|__c| __set.to_c_string_iterator().any(|__r| __r == __c)) + { + Some(__i) => __s.offset(__i), + None => Ptr::null(), } })); assert!(!((*r.borrow()).is_null())); @@ -1038,27 +806,14 @@ pub fn test_strpbrk_14() { }); assert!( ({ - let mut __p = (*s.borrow()).clone(); - loop { - let __c = __p.read(); - if __c == 0 { - break Ptr::null(); - } - let mut __q = Ptr::from_string_literal(b"xyz").clone(); - let __hit = loop { - let __r = __q.read(); - if __r == 0 { - break false; - } - if __r == __c { - break true; - } - __q += 1; - }; - if __hit { - break __p; - } - __p += 1; + let __s = (*s.borrow()).clone(); + let __set = Ptr::from_string_literal(b"xyz").clone(); + match __s + .to_c_string_iterator() + .position(|__c| __set.to_c_string_iterator().any(|__r| __r == __c)) + { + Some(__i) => __s.offset(__i), + None => Ptr::null(), } }) .is_null() @@ -1071,27 +826,14 @@ pub fn test_strpbrk_14() { ]))); assert!( ({ - let mut __p = (buf.as_pointer() as Ptr).clone(); - loop { - let __c = __p.read(); - if __c == 0 { - break Ptr::null(); - } - let mut __q = Ptr::from_string_literal(b"b").clone(); - let __hit = loop { - let __r = __q.read(); - if __r == 0 { - break false; - } - if __r == __c { - break true; - } - __q += 1; - }; - if __hit { - break __p; - } - __p += 1; + let __s = (buf.as_pointer() as Ptr).clone(); + let __set = Ptr::from_string_literal(b"b").clone(); + match __s + .to_c_string_iterator() + .position(|__c| __set.to_c_string_iterator().any(|__r| __r == __c)) + { + Some(__i) => __s.offset(__i), + None => Ptr::null(), } } == ((buf.as_pointer() as Ptr).offset(1))) ); @@ -1099,55 +841,61 @@ pub fn test_strpbrk_14() { pub fn test_strcasecmp_15() { assert!( ({ - let mut __p1 = Ptr::from_string_literal(b"HELLO").clone(); - let mut __p2 = Ptr::from_string_literal(b"hello").clone(); + let mut __it1 = Ptr::from_string_literal(b"HELLO") + .to_c_string_iterator() + .map(|__c| __c.to_ascii_lowercase()); + let mut __it2 = Ptr::from_string_literal(b"hello") + .to_c_string_iterator() + .map(|__c| __c.to_ascii_lowercase()); loop { - let __c1 = __p1.read().to_ascii_lowercase(); - let __c2 = __p2.read().to_ascii_lowercase(); + let __c1 = __it1.next(); + let __c2 = __it2.next(); if __c1 != __c2 { - break (__c1 as i32) - (__c2 as i32); + break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); } - if __c1 == 0 { + if __c1.is_none() { break 0; } - __p1 += 1; - __p2 += 1; } } == 0) ); assert!( ({ - let mut __p1 = Ptr::from_string_literal(b"abc").clone(); - let mut __p2 = Ptr::from_string_literal(b"abd").clone(); + let mut __it1 = Ptr::from_string_literal(b"abc") + .to_c_string_iterator() + .map(|__c| __c.to_ascii_lowercase()); + let mut __it2 = Ptr::from_string_literal(b"abd") + .to_c_string_iterator() + .map(|__c| __c.to_ascii_lowercase()); loop { - let __c1 = __p1.read().to_ascii_lowercase(); - let __c2 = __p2.read().to_ascii_lowercase(); + let __c1 = __it1.next(); + let __c2 = __it2.next(); if __c1 != __c2 { - break (__c1 as i32) - (__c2 as i32); + break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); } - if __c1 == 0 { + if __c1.is_none() { break 0; } - __p1 += 1; - __p2 += 1; } } < 0) ); assert!( ({ - let mut __p1 = Ptr::from_string_literal(b"abd").clone(); - let mut __p2 = Ptr::from_string_literal(b"abc").clone(); + let mut __it1 = Ptr::from_string_literal(b"abd") + .to_c_string_iterator() + .map(|__c| __c.to_ascii_lowercase()); + let mut __it2 = Ptr::from_string_literal(b"abc") + .to_c_string_iterator() + .map(|__c| __c.to_ascii_lowercase()); loop { - let __c1 = __p1.read().to_ascii_lowercase(); - let __c2 = __p2.read().to_ascii_lowercase(); + let __c1 = __it1.next(); + let __c2 = __it2.next(); if __c1 != __c2 { - break (__c1 as i32) - (__c2 as i32); + break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); } - if __c1 == 0 { + if __c1.is_none() { break 0; } - __p1 += 1; - __p2 += 1; } } > 0) ); @@ -1155,19 +903,21 @@ pub fn test_strcasecmp_15() { let q: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"foo"))); assert!( ({ - let mut __p1 = (*p.borrow()).clone(); - let mut __p2 = (*q.borrow()).clone(); + let mut __it1 = (*p.borrow()) + .to_c_string_iterator() + .map(|__c| __c.to_ascii_lowercase()); + let mut __it2 = (*q.borrow()) + .to_c_string_iterator() + .map(|__c| __c.to_ascii_lowercase()); loop { - let __c1 = __p1.read().to_ascii_lowercase(); - let __c2 = __p2.read().to_ascii_lowercase(); + let __c1 = __it1.next(); + let __c2 = __it2.next(); if __c1 != __c2 { - break (__c1 as i32) - (__c2 as i32); + break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); } - if __c1 == 0 { + if __c1.is_none() { break 0; } - __p1 += 1; - __p2 += 1; } } == 0) ); diff --git a/tests/unit/out/refcount/offsetof.rs b/tests/unit/out/refcount/offsetof.rs index f183b9c3..09a39164 100644 --- a/tests/unit/out/refcount/offsetof.rs +++ b/tests/unit/out/refcount/offsetof.rs @@ -102,16 +102,7 @@ fn main_0() -> i32 { assert!(((*(*v.borrow()).b.borrow()) == 305419896_u32)); let text: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"example-body"))); let len: Value = Rc::new(RefCell::new( - ({ - let mut __p = (*text.borrow()).clone(); - let mut __i: usize = 0; - while __p.read() != 0 { - __p += 1; - __i += 1; - } - __i - }) - .wrapping_add(1_usize), + ((*text.borrow()).to_c_string_iterator().count()).wrapping_add(1_usize), )); let total: Value = Rc::new(RefCell::new( ((2_usize as u64).wrapping_add(((*len.borrow()) as u64)) as usize), diff --git a/tests/unit/out/refcount/strdup.rs b/tests/unit/out/refcount/strdup.rs index f75d5a0f..33c1f344 100644 --- a/tests/unit/out/refcount/strdup.rs +++ b/tests/unit/out/refcount/strdup.rs @@ -40,19 +40,17 @@ fn main_0() -> i32 { assert!((((!((*d.borrow()).is_null())) as i32) != 0)); assert!( ((({ - let mut __p1 = (*d.borrow()).clone(); - let mut __p2 = Ptr::from_string_literal(b"hello").clone(); + let mut __it1 = (*d.borrow()).to_c_string_iterator(); + let mut __it2 = Ptr::from_string_literal(b"hello").to_c_string_iterator(); loop { - let __c1 = __p1.read(); - let __c2 = __p2.read(); + let __c1 = __it1.next(); + let __c2 = __it2.next(); if __c1 != __c2 { - break (__c1 as i32) - (__c2 as i32); + break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); } - if __c1 == 0 { + if __c1.is_none() { break 0; } - __p1 += 1; - __p2 += 1; } } == 0) as i32) != 0) @@ -71,19 +69,17 @@ fn main_0() -> i32 { assert!((((!((*d2.borrow()).is_null())) as i32) != 0)); assert!( ((({ - let mut __p1 = (*d2.borrow()).clone(); - let mut __p2 = (*p.borrow()).clone(); + let mut __it1 = (*d2.borrow()).to_c_string_iterator(); + let mut __it2 = (*p.borrow()).to_c_string_iterator(); loop { - let __c1 = __p1.read(); - let __c2 = __p2.read(); + let __c1 = __it1.next(); + let __c2 = __it2.next(); if __c1 != __c2 { - break (__c1 as i32) - (__c2 as i32); + break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); } - if __c1 == 0 { + if __c1.is_none() { break 0; } - __p1 += 1; - __p2 += 1; } } == 0) as i32) != 0) @@ -95,19 +91,17 @@ fn main_0() -> i32 { assert!((((!((*d3.borrow()).is_null())) as i32) != 0)); assert!( ((({ - let mut __p1 = (*d3.borrow()).clone(); - let mut __p2 = (buf.as_pointer() as Ptr).clone(); + let mut __it1 = (*d3.borrow()).to_c_string_iterator(); + let mut __it2 = (buf.as_pointer() as Ptr).to_c_string_iterator(); loop { - let __c1 = __p1.read(); - let __c2 = __p2.read(); + let __c1 = __it1.next(); + let __c2 = __it2.next(); if __c1 != __c2 { - break (__c1 as i32) - (__c2 as i32); + break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); } - if __c1 == 0 { + if __c1.is_none() { break 0; } - __p1 += 1; - __p2 += 1; } } == 0) as i32) != 0) @@ -118,19 +112,17 @@ fn main_0() -> i32 { assert!((((!((*d4.borrow()).is_null())) as i32) != 0)); assert!( ((({ - let mut __p1 = (*d4.borrow()).clone(); - let mut __p2 = (*p.borrow()).clone(); + let mut __it1 = (*d4.borrow()).to_c_string_iterator(); + let mut __it2 = (*p.borrow()).to_c_string_iterator(); loop { - let __c1 = __p1.read(); - let __c2 = __p2.read(); + let __c1 = __it1.next(); + let __c2 = __it2.next(); if __c1 != __c2 { - break (__c1 as i32) - (__c2 as i32); + break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); } - if __c1 == 0 { + if __c1.is_none() { break 0; } - __p1 += 1; - __p2 += 1; } } == 0) as i32) != 0) @@ -145,19 +137,18 @@ fn main_0() -> i32 { assert!((((!((*(*(*r.borrow()).upgrade().deref()).name.borrow()).is_null())) as i32) != 0)); assert!( ((({ - let mut __p1 = (*(*(*r.borrow()).upgrade().deref()).name.borrow()).clone(); - let mut __p2 = (*p.borrow()).clone(); + let mut __it1 = + (*(*(*r.borrow()).upgrade().deref()).name.borrow()).to_c_string_iterator(); + let mut __it2 = (*p.borrow()).to_c_string_iterator(); loop { - let __c1 = __p1.read(); - let __c2 = __p2.read(); + let __c1 = __it1.next(); + let __c2 = __it2.next(); if __c1 != __c2 { - break (__c1 as i32) - (__c2 as i32); + break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); } - if __c1 == 0 { + if __c1.is_none() { break 0; } - __p1 += 1; - __p2 += 1; } } == 0) as i32) != 0) diff --git a/tests/unit/out/refcount/string_h.rs b/tests/unit/out/refcount/string_h.rs index 8cf22c36..7b8801ff 100644 --- a/tests/unit/out/refcount/string_h.rs +++ b/tests/unit/out/refcount/string_h.rs @@ -172,32 +172,34 @@ pub fn test_memmove_3() { pub fn test_strchr_4() { let s: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"hello world"))); let r: Value> = Rc::new(RefCell::new({ - let mut __p = (*s.borrow()).reinterpret_cast::().clone(); - loop { - let __c = __p.read(); - if __c == ('w' as i32) as u8 { - break __p; - } - if __c == 0 { - break Ptr::null(); + let __s = (*s.borrow()).reinterpret_cast::().clone(); + let __t = ('w' as i32) as u8; + match __s.to_c_string_iterator().position(|__c| __c == __t) { + Some(__i) => __s.offset(__i), + None => { + if __t == 0 { + __s.offset(__s.to_c_string_iterator().count()) + } else { + Ptr::null() + } } - __p += 1; } })); assert!((((!((*r.borrow()).is_null())) as i32) != 0)); assert!(((((((*r.borrow()).read()) as i32) == ('w' as i32)) as i32) != 0)); assert!( (((({ - let mut __p = (*s.borrow()).reinterpret_cast::().clone(); - loop { - let __c = __p.read(); - if __c == ('z' as i32) as u8 { - break __p; - } - if __c == 0 { - break Ptr::null(); + let __s = (*s.borrow()).reinterpret_cast::().clone(); + let __t = ('z' as i32) as u8; + match __s.to_c_string_iterator().position(|__c| __c == __t) { + Some(__i) => __s.offset(__i), + None => { + if __t == 0 { + __s.offset(__s.to_c_string_iterator().count()) + } else { + Ptr::null() + } } - __p += 1; } }) .is_null()) as i32) @@ -206,158 +208,111 @@ pub fn test_strchr_4() { } pub fn test_strlen_5() { assert!( - ((({ - let mut __p = Ptr::from_string_literal(b"").clone(); - let mut __i: usize = 0; - while __p.read() != 0 { - __p += 1; - __i += 1; - } - __i - } == 0_usize) as i32) - != 0) + (((Ptr::from_string_literal(b"").to_c_string_iterator().count() == 0_usize) as i32) != 0) ); assert!( - ((({ - let mut __p = Ptr::from_string_literal(b"hello").clone(); - let mut __i: usize = 0; - while __p.read() != 0 { - __p += 1; - __i += 1; - } - __i - } == 5_usize) as i32) + (((Ptr::from_string_literal(b"hello") + .to_c_string_iterator() + .count() + == 5_usize) as i32) != 0) ); assert!( - ((({ - let mut __p = Ptr::from_string_literal(b"hello world").clone(); - let mut __i: usize = 0; - while __p.read() != 0 { - __p += 1; - __i += 1; - } - __i - } == 11_usize) as i32) + (((Ptr::from_string_literal(b"hello world") + .to_c_string_iterator() + .count() + == 11_usize) as i32) != 0) ); let buf: Value> = Rc::new(RefCell::new(Box::from(*b"one\0two\0"))); let first: Value> = Rc::new(RefCell::new((buf.as_pointer() as Ptr))); let second: Value> = Rc::new(RefCell::new( - ((buf.as_pointer() as Ptr).offset( - ({ - let mut __p = (*first.borrow()).clone(); - let mut __i: usize = 0; - while __p.read() != 0 { - __p += 1; - __i += 1; - } - __i - }) - .wrapping_add(1_usize), - )), + ((buf.as_pointer() as Ptr) + .offset(((*first.borrow()).to_c_string_iterator().count()).wrapping_add(1_usize))), )); assert!( ((({ - let mut __p1 = (*second.borrow()).clone(); - let mut __p2 = Ptr::from_string_literal(b"two").clone(); + let mut __it1 = (*second.borrow()).to_c_string_iterator(); + let mut __it2 = Ptr::from_string_literal(b"two").to_c_string_iterator(); loop { - let __c1 = __p1.read(); - let __c2 = __p2.read(); + let __c1 = __it1.next(); + let __c2 = __it2.next(); if __c1 != __c2 { - break (__c1 as i32) - (__c2 as i32); + break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); } - if __c1 == 0 { + if __c1.is_none() { break 0; } - __p1 += 1; - __p2 += 1; } } == 0) as i32) != 0) ); let big : Value > = Rc::new(RefCell::new(Box::from(*b"hi\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0") )) ; assert!( - ((({ - let mut __p = (big.as_pointer() as Ptr).clone(); - let mut __i: usize = 0; - while __p.read() != 0 { - __p += 1; - __i += 1; - } - __i - } == 2_usize) as i32) + ((((big.as_pointer() as Ptr::) + .to_c_string_iterator() + .count() + == 2_usize) as i32) != 0) ); (*big.borrow_mut())[(2) as usize] = (('x' as i32) as u8); (*big.borrow_mut())[(3) as usize] = (('\0' as i32) as u8); assert!( - ((({ - let mut __p = (big.as_pointer() as Ptr).clone(); - let mut __i: usize = 0; - while __p.read() != 0 { - __p += 1; - __i += 1; - } - __i - } == 3_usize) as i32) + ((((big.as_pointer() as Ptr::) + .to_c_string_iterator() + .count() + == 3_usize) as i32) != 0) ); } pub fn test_strcmp_6() { assert!( ((({ - let mut __p1 = Ptr::from_string_literal(b"abc").clone(); - let mut __p2 = Ptr::from_string_literal(b"abc").clone(); + let mut __it1 = Ptr::from_string_literal(b"abc").to_c_string_iterator(); + let mut __it2 = Ptr::from_string_literal(b"abc").to_c_string_iterator(); loop { - let __c1 = __p1.read(); - let __c2 = __p2.read(); + let __c1 = __it1.next(); + let __c2 = __it2.next(); if __c1 != __c2 { - break (__c1 as i32) - (__c2 as i32); + break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); } - if __c1 == 0 { + if __c1.is_none() { break 0; } - __p1 += 1; - __p2 += 1; } } == 0) as i32) != 0) ); assert!( ((({ - let mut __p1 = Ptr::from_string_literal(b"abc").clone(); - let mut __p2 = Ptr::from_string_literal(b"abd").clone(); + let mut __it1 = Ptr::from_string_literal(b"abc").to_c_string_iterator(); + let mut __it2 = Ptr::from_string_literal(b"abd").to_c_string_iterator(); loop { - let __c1 = __p1.read(); - let __c2 = __p2.read(); + let __c1 = __it1.next(); + let __c2 = __it2.next(); if __c1 != __c2 { - break (__c1 as i32) - (__c2 as i32); + break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); } - if __c1 == 0 { + if __c1.is_none() { break 0; } - __p1 += 1; - __p2 += 1; } } < 0) as i32) != 0) ); assert!( ((({ - let mut __p1 = Ptr::from_string_literal(b"abd").clone(); - let mut __p2 = Ptr::from_string_literal(b"abc").clone(); + let mut __it1 = Ptr::from_string_literal(b"abd").to_c_string_iterator(); + let mut __it2 = Ptr::from_string_literal(b"abc").to_c_string_iterator(); loop { - let __c1 = __p1.read(); - let __c2 = __p2.read(); + let __c1 = __it1.next(); + let __c2 = __it2.next(); if __c1 != __c2 { - break (__c1 as i32) - (__c2 as i32); + break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); } - if __c1 == 0 { + if __c1.is_none() { break 0; } - __p1 += 1; - __p2 += 1; } } > 0) as i32) != 0) @@ -372,57 +327,51 @@ pub fn test_strcmp_6() { ]))); assert!( ((({ - let mut __p1 = (*p.borrow()).clone(); - let mut __p2 = (*p.borrow()).clone(); + let mut __it1 = (*p.borrow()).to_c_string_iterator(); + let mut __it2 = (*p.borrow()).to_c_string_iterator(); loop { - let __c1 = __p1.read(); - let __c2 = __p2.read(); + let __c1 = __it1.next(); + let __c2 = __it2.next(); if __c1 != __c2 { - break (__c1 as i32) - (__c2 as i32); + break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); } - if __c1 == 0 { + if __c1.is_none() { break 0; } - __p1 += 1; - __p2 += 1; } } == 0) as i32) != 0) ); assert!( ((({ - let mut __p1 = (*p.borrow()).clone(); - let mut __p2 = (*q.borrow()).clone(); + let mut __it1 = (*p.borrow()).to_c_string_iterator(); + let mut __it2 = (*q.borrow()).to_c_string_iterator(); loop { - let __c1 = __p1.read(); - let __c2 = __p2.read(); + let __c1 = __it1.next(); + let __c2 = __it2.next(); if __c1 != __c2 { - break (__c1 as i32) - (__c2 as i32); + break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); } - if __c1 == 0 { + if __c1.is_none() { break 0; } - __p1 += 1; - __p2 += 1; } } < 0) as i32) != 0) ); assert!( ((({ - let mut __p1 = (buf.as_pointer() as Ptr).clone(); - let mut __p2 = (*p.borrow()).clone(); + let mut __it1 = (buf.as_pointer() as Ptr).to_c_string_iterator(); + let mut __it2 = (*p.borrow()).to_c_string_iterator(); loop { - let __c1 = __p1.read(); - let __c2 = __p2.read(); + let __c1 = __it1.next(); + let __c2 = __it2.next(); if __c1 != __c2 { - break (__c1 as i32) - (__c2 as i32); + break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); } - if __c1 == 0 { + if __c1.is_none() { break 0; } - __p1 += 1; - __p2 += 1; } } == 0) as i32) != 0) @@ -431,72 +380,66 @@ pub fn test_strcmp_6() { pub fn test_strncmp_7() { assert!( ((({ - let mut __p1 = Ptr::from_string_literal(b"abcdef").clone(); - let mut __p2 = Ptr::from_string_literal(b"abcxyz").clone(); - let mut __i: usize = 0; + let __n = 3_usize; + let mut __it1 = Ptr::from_string_literal(b"abcdef") + .to_c_string_iterator() + .take(__n); + let mut __it2 = Ptr::from_string_literal(b"abcxyz") + .to_c_string_iterator() + .take(__n); loop { - if __i == 3_usize { - break 0; - } - let __c1 = __p1.read(); - let __c2 = __p2.read(); + let __c1 = __it1.next(); + let __c2 = __it2.next(); if __c1 != __c2 { - break (__c1 as i32) - (__c2 as i32); + break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); } - if __c1 == 0 { + if __c1.is_none() { break 0; } - __p1 += 1; - __p2 += 1; - __i += 1; } } == 0) as i32) != 0) ); assert!( ((({ - let mut __p1 = Ptr::from_string_literal(b"abcdef").clone(); - let mut __p2 = Ptr::from_string_literal(b"abcxyz").clone(); - let mut __i: usize = 0; + let __n = 4_usize; + let mut __it1 = Ptr::from_string_literal(b"abcdef") + .to_c_string_iterator() + .take(__n); + let mut __it2 = Ptr::from_string_literal(b"abcxyz") + .to_c_string_iterator() + .take(__n); loop { - if __i == 4_usize { - break 0; - } - let __c1 = __p1.read(); - let __c2 = __p2.read(); + let __c1 = __it1.next(); + let __c2 = __it2.next(); if __c1 != __c2 { - break (__c1 as i32) - (__c2 as i32); + break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); } - if __c1 == 0 { + if __c1.is_none() { break 0; } - __p1 += 1; - __p2 += 1; - __i += 1; } } < 0) as i32) != 0) ); assert!( ((({ - let mut __p1 = Ptr::from_string_literal(b"abcxyz").clone(); - let mut __p2 = Ptr::from_string_literal(b"abcdef").clone(); - let mut __i: usize = 0; + let __n = 4_usize; + let mut __it1 = Ptr::from_string_literal(b"abcxyz") + .to_c_string_iterator() + .take(__n); + let mut __it2 = Ptr::from_string_literal(b"abcdef") + .to_c_string_iterator() + .take(__n); loop { - if __i == 4_usize { - break 0; - } - let __c1 = __p1.read(); - let __c2 = __p2.read(); + let __c1 = __it1.next(); + let __c2 = __it2.next(); if __c1 != __c2 { - break (__c1 as i32) - (__c2 as i32); + break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); } - if __c1 == 0 { + if __c1.is_none() { break 0; } - __p1 += 1; - __p2 += 1; - __i += 1; } } > 0) as i32) != 0) @@ -515,72 +458,56 @@ pub fn test_strncmp_7() { let n: Value = Rc::new(RefCell::new(3_usize)); assert!( ((({ - let mut __p1 = (*p.borrow()).clone(); - let mut __p2 = (*q.borrow()).clone(); - let mut __i: usize = 0; + let __n = (*n.borrow()); + let mut __it1 = (*p.borrow()).to_c_string_iterator().take(__n); + let mut __it2 = (*q.borrow()).to_c_string_iterator().take(__n); loop { - if __i == (*n.borrow()) { - break 0; - } - let __c1 = __p1.read(); - let __c2 = __p2.read(); + let __c1 = __it1.next(); + let __c2 = __it2.next(); if __c1 != __c2 { - break (__c1 as i32) - (__c2 as i32); + break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); } - if __c1 == 0 { + if __c1.is_none() { break 0; } - __p1 += 1; - __p2 += 1; - __i += 1; } } == 0) as i32) != 0) ); assert!( ((({ - let mut __p1 = (*p.borrow()).clone(); - let mut __p2 = (*q.borrow()).clone(); - let mut __i: usize = 0; + let __n = (*n.borrow()).wrapping_add(1_usize); + let mut __it1 = (*p.borrow()).to_c_string_iterator().take(__n); + let mut __it2 = (*q.borrow()).to_c_string_iterator().take(__n); loop { - if __i == (*n.borrow()).wrapping_add(1_usize) { - break 0; - } - let __c1 = __p1.read(); - let __c2 = __p2.read(); + let __c1 = __it1.next(); + let __c2 = __it2.next(); if __c1 != __c2 { - break (__c1 as i32) - (__c2 as i32); + break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); } - if __c1 == 0 { + if __c1.is_none() { break 0; } - __p1 += 1; - __p2 += 1; - __i += 1; } } < 0) as i32) != 0) ); assert!( ((({ - let mut __p1 = (buf.as_pointer() as Ptr).clone(); - let mut __p2 = (*p.borrow()).clone(); - let mut __i: usize = 0; + let __n = 6_usize; + let mut __it1 = (buf.as_pointer() as Ptr) + .to_c_string_iterator() + .take(__n); + let mut __it2 = (*p.borrow()).to_c_string_iterator().take(__n); loop { - if __i == 6_usize { - break 0; - } - let __c1 = __p1.read(); - let __c2 = __p2.read(); + let __c1 = __it1.next(); + let __c2 = __it2.next(); if __c1 != __c2 { - break (__c1 as i32) - (__c2 as i32); + break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); } - if __c1 == 0 { + if __c1.is_none() { break 0; } - __p1 += 1; - __p2 += 1; - __i += 1; } } == 0) as i32) != 0) @@ -659,17 +586,22 @@ pub fn test_memchr_8() { pub fn test_strrchr_9() { let s: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"hello world"))); let r: Value> = Rc::new(RefCell::new({ - let mut __p = (*s.borrow()).reinterpret_cast::().clone(); - let mut __found = Ptr::null(); - loop { - let __c = __p.read(); - if __c == ('l' as i32) as u8 { - __found = __p.clone(); - } - if __c == 0 { - break __found; + let __s = (*s.borrow()).reinterpret_cast::().clone(); + let __t = ('l' as i32) as u8; + match __s + .to_c_string_iterator() + .enumerate() + .filter(|__e| __e.1 == __t) + .last() + { + Some((__i, _)) => __s.offset(__i), + None => { + if __t == 0 { + __s.offset(__s.to_c_string_iterator().count()) + } else { + Ptr::null() + } } - __p += 1; } })); assert!((((!((*r.borrow()).is_null())) as i32) != 0)); @@ -683,17 +615,22 @@ pub fn test_strrchr_9() { ); assert!( (((({ - let mut __p = (*s.borrow()).reinterpret_cast::().clone(); - let mut __found = Ptr::null(); - loop { - let __c = __p.read(); - if __c == ('z' as i32) as u8 { - __found = __p.clone(); - } - if __c == 0 { - break __found; + let __s = (*s.borrow()).reinterpret_cast::().clone(); + let __t = ('z' as i32) as u8; + match __s + .to_c_string_iterator() + .enumerate() + .filter(|__e| __e.1 == __t) + .last() + { + Some((__i, _)) => __s.offset(__i), + None => { + if __t == 0 { + __s.offset(__s.to_c_string_iterator().count()) + } else { + Ptr::null() + } } - __p += 1; } }) .is_null()) as i32) @@ -707,17 +644,22 @@ pub fn test_strrchr_9() { ]))); assert!( ((({ - let mut __p = (buf.as_pointer() as Ptr).clone(); - let mut __found = Ptr::null(); - loop { - let __c = __p.read(); - if __c == ('a' as i32) as u8 { - __found = __p.clone(); - } - if __c == 0 { - break __found; + let __s = (buf.as_pointer() as Ptr).clone(); + let __t = ('a' as i32) as u8; + match __s + .to_c_string_iterator() + .enumerate() + .filter(|__e| __e.1 == __t) + .last() + { + Some((__i, _)) => __s.offset(__i), + None => { + if __t == 0 { + __s.offset(__s.to_c_string_iterator().count()) + } else { + Ptr::null() + } } - __p += 1; } } == ((buf.as_pointer() as Ptr).offset(2))) as i32) != 0) @@ -726,88 +668,31 @@ pub fn test_strrchr_9() { pub fn test_strcspn_10() { assert!( ((({ - let mut __p = Ptr::from_string_literal(b"hello").clone(); - let mut __i: usize = 0; - loop { - let __c = __p.read(); - if __c == 0 { - break __i; - } - let mut __q = Ptr::from_string_literal(b"el").clone(); - let __hit = loop { - let __r = __q.read(); - if __r == 0 { - break false; - } - if __r == __c { - break true; - } - __q += 1; - }; - if __hit { - break __i; - } - __p += 1; - __i += 1; - } + let __set = Ptr::from_string_literal(b"el").clone(); + Ptr::from_string_literal(b"hello") + .to_c_string_iterator() + .take_while(|__c| !__set.to_c_string_iterator().any(|__r| __r == *__c)) + .count() } == 1_usize) as i32) != 0) ); assert!( ((({ - let mut __p = Ptr::from_string_literal(b"abc").clone(); - let mut __i: usize = 0; - loop { - let __c = __p.read(); - if __c == 0 { - break __i; - } - let mut __q = Ptr::from_string_literal(b"xyz").clone(); - let __hit = loop { - let __r = __q.read(); - if __r == 0 { - break false; - } - if __r == __c { - break true; - } - __q += 1; - }; - if __hit { - break __i; - } - __p += 1; - __i += 1; - } + let __set = Ptr::from_string_literal(b"xyz").clone(); + Ptr::from_string_literal(b"abc") + .to_c_string_iterator() + .take_while(|__c| !__set.to_c_string_iterator().any(|__r| __r == *__c)) + .count() } == 3_usize) as i32) != 0) ); assert!( ((({ - let mut __p = Ptr::from_string_literal(b"").clone(); - let mut __i: usize = 0; - loop { - let __c = __p.read(); - if __c == 0 { - break __i; - } - let mut __q = Ptr::from_string_literal(b"abc").clone(); - let __hit = loop { - let __r = __q.read(); - if __r == 0 { - break false; - } - if __r == __c { - break true; - } - __q += 1; - }; - if __hit { - break __i; - } - __p += 1; - __i += 1; - } + let __set = Ptr::from_string_literal(b"abc").clone(); + Ptr::from_string_literal(b"") + .to_c_string_iterator() + .take_while(|__c| !__set.to_c_string_iterator().any(|__r| __r == *__c)) + .count() } == 0_usize) as i32) != 0) ); @@ -815,30 +700,11 @@ pub fn test_strcspn_10() { let rej: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"el"))); assert!( ((({ - let mut __p = (*s.borrow()).clone(); - let mut __i: usize = 0; - loop { - let __c = __p.read(); - if __c == 0 { - break __i; - } - let mut __q = (*rej.borrow()).clone(); - let __hit = loop { - let __r = __q.read(); - if __r == 0 { - break false; - } - if __r == __c { - break true; - } - __q += 1; - }; - if __hit { - break __i; - } - __p += 1; - __i += 1; - } + let __set = (*rej.borrow()).clone(); + (*s.borrow()) + .to_c_string_iterator() + .take_while(|__c| !__set.to_c_string_iterator().any(|__r| __r == *__c)) + .count() } == 1_usize) as i32) != 0) ); @@ -846,88 +712,31 @@ pub fn test_strcspn_10() { pub fn test_strspn_11() { assert!( ((({ - let mut __p = Ptr::from_string_literal(b"hello").clone(); - let mut __i: usize = 0; - loop { - let __c = __p.read(); - if __c == 0 { - break __i; - } - let mut __q = Ptr::from_string_literal(b"hel").clone(); - let __hit = loop { - let __r = __q.read(); - if __r == 0 { - break false; - } - if __r == __c { - break true; - } - __q += 1; - }; - if !__hit { - break __i; - } - __p += 1; - __i += 1; - } + let __set = Ptr::from_string_literal(b"hel").clone(); + Ptr::from_string_literal(b"hello") + .to_c_string_iterator() + .take_while(|__c| __set.to_c_string_iterator().any(|__r| __r == *__c)) + .count() } == 4_usize) as i32) != 0) ); assert!( ((({ - let mut __p = Ptr::from_string_literal(b"abc").clone(); - let mut __i: usize = 0; - loop { - let __c = __p.read(); - if __c == 0 { - break __i; - } - let mut __q = Ptr::from_string_literal(b"xyz").clone(); - let __hit = loop { - let __r = __q.read(); - if __r == 0 { - break false; - } - if __r == __c { - break true; - } - __q += 1; - }; - if !__hit { - break __i; - } - __p += 1; - __i += 1; - } + let __set = Ptr::from_string_literal(b"xyz").clone(); + Ptr::from_string_literal(b"abc") + .to_c_string_iterator() + .take_while(|__c| __set.to_c_string_iterator().any(|__r| __r == *__c)) + .count() } == 0_usize) as i32) != 0) ); assert!( ((({ - let mut __p = Ptr::from_string_literal(b"aaa").clone(); - let mut __i: usize = 0; - loop { - let __c = __p.read(); - if __c == 0 { - break __i; - } - let mut __q = Ptr::from_string_literal(b"a").clone(); - let __hit = loop { - let __r = __q.read(); - if __r == 0 { - break false; - } - if __r == __c { - break true; - } - __q += 1; - }; - if !__hit { - break __i; - } - __p += 1; - __i += 1; - } + let __set = Ptr::from_string_literal(b"a").clone(); + Ptr::from_string_literal(b"aaa") + .to_c_string_iterator() + .take_while(|__c| __set.to_c_string_iterator().any(|__r| __r == *__c)) + .count() } == 3_usize) as i32) != 0) ); @@ -935,30 +744,11 @@ pub fn test_strspn_11() { let acc: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"hel"))); assert!( ((({ - let mut __p = (*s.borrow()).clone(); - let mut __i: usize = 0; - loop { - let __c = __p.read(); - if __c == 0 { - break __i; - } - let mut __q = (*acc.borrow()).clone(); - let __hit = loop { - let __r = __q.read(); - if __r == 0 { - break false; - } - if __r == __c { - break true; - } - __q += 1; - }; - if !__hit { - break __i; - } - __p += 1; - __i += 1; - } + let __set = (*acc.borrow()).clone(); + (*s.borrow()) + .to_c_string_iterator() + .take_while(|__c| __set.to_c_string_iterator().any(|__r| __r == *__c)) + .count() } == 4_usize) as i32) != 0) ); @@ -966,22 +756,14 @@ pub fn test_strspn_11() { pub fn test_strstr_12() { let h: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"hello world"))); let r: Value> = Rc::new(RefCell::new({ + let __needle = Ptr::from_string_literal(b"world").clone(); let mut __p = (*h.borrow()).reinterpret_cast::().clone(); loop { - let mut __h = __p.clone(); - let mut __n = Ptr::from_string_literal(b"world").clone(); - let __matched = loop { - let __c = __n.read(); - if __c == 0 { - break true; - } - if __h.read() != __c { - break false; - } - __h += 1; - __n += 1; - }; - if __matched { + let mut __h = __p.to_c_string_iterator(); + if __needle + .to_c_string_iterator() + .all(|__c| __h.next() == Some(__c)) + { break __p; } if __p.read() == 0 { @@ -1000,22 +782,14 @@ pub fn test_strstr_12() { ); assert!( (((({ + let __needle = Ptr::from_string_literal(b"xyz").clone(); let mut __p = (*h.borrow()).reinterpret_cast::().clone(); loop { - let mut __h = __p.clone(); - let mut __n = Ptr::from_string_literal(b"xyz").clone(); - let __matched = loop { - let __c = __n.read(); - if __c == 0 { - break true; - } - if __h.read() != __c { - break false; - } - __h += 1; - __n += 1; - }; - if __matched { + let mut __h = __p.to_c_string_iterator(); + if __needle + .to_c_string_iterator() + .all(|__c| __h.next() == Some(__c)) + { break __p; } if __p.read() == 0 { @@ -1037,22 +811,14 @@ pub fn test_strstr_12() { ]))); assert!( ((({ + let __needle = Ptr::from_string_literal(b"ll").clone(); let mut __p = (buf.as_pointer() as Ptr).clone(); loop { - let mut __h = __p.clone(); - let mut __n = Ptr::from_string_literal(b"ll").clone(); - let __matched = loop { - let __c = __n.read(); - if __c == 0 { - break true; - } - if __h.read() != __c { - break false; - } - __h += 1; - __n += 1; - }; - if __matched { + let mut __h = __p.to_c_string_iterator(); + if __needle + .to_c_string_iterator() + .all(|__c| __h.next() == Some(__c)) + { break __p; } if __p.read() == 0 { @@ -1067,27 +833,14 @@ pub fn test_strstr_12() { pub fn test_strpbrk_13() { let s: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"hello world"))); let r: Value> = Rc::new(RefCell::new({ - let mut __p = (*s.borrow()).reinterpret_cast::().clone(); - loop { - let __c = __p.read(); - if __c == 0 { - break Ptr::null(); - } - let mut __q = Ptr::from_string_literal(b"wo").clone(); - let __hit = loop { - let __r = __q.read(); - if __r == 0 { - break false; - } - if __r == __c { - break true; - } - __q += 1; - }; - if __hit { - break __p; - } - __p += 1; + let __s = (*s.borrow()).reinterpret_cast::().clone(); + let __set = Ptr::from_string_literal(b"wo").clone(); + match __s + .to_c_string_iterator() + .position(|__c| __set.to_c_string_iterator().any(|__r| __r == __c)) + { + Some(__i) => __s.offset(__i), + None => Ptr::null(), } })); assert!((((!((*r.borrow()).is_null())) as i32) != 0)); @@ -1100,27 +853,14 @@ pub fn test_strpbrk_13() { ); assert!( (((({ - let mut __p = (*s.borrow()).reinterpret_cast::().clone(); - loop { - let __c = __p.read(); - if __c == 0 { - break Ptr::null(); - } - let mut __q = Ptr::from_string_literal(b"xyz").clone(); - let __hit = loop { - let __r = __q.read(); - if __r == 0 { - break false; - } - if __r == __c { - break true; - } - __q += 1; - }; - if __hit { - break __p; - } - __p += 1; + let __s = (*s.borrow()).reinterpret_cast::().clone(); + let __set = Ptr::from_string_literal(b"xyz").clone(); + match __s + .to_c_string_iterator() + .position(|__c| __set.to_c_string_iterator().any(|__r| __r == __c)) + { + Some(__i) => __s.offset(__i), + None => Ptr::null(), } }) .is_null()) as i32) @@ -1134,27 +874,14 @@ pub fn test_strpbrk_13() { ]))); assert!( ((({ - let mut __p = (buf.as_pointer() as Ptr).clone(); - loop { - let __c = __p.read(); - if __c == 0 { - break Ptr::null(); - } - let mut __q = Ptr::from_string_literal(b"b").clone(); - let __hit = loop { - let __r = __q.read(); - if __r == 0 { - break false; - } - if __r == __c { - break true; - } - __q += 1; - }; - if __hit { - break __p; - } - __p += 1; + let __s = (buf.as_pointer() as Ptr).clone(); + let __set = Ptr::from_string_literal(b"b").clone(); + match __s + .to_c_string_iterator() + .position(|__c| __set.to_c_string_iterator().any(|__r| __r == __c)) + { + Some(__i) => __s.offset(__i), + None => Ptr::null(), } } == ((buf.as_pointer() as Ptr).offset(1))) as i32) != 0) @@ -1163,57 +890,63 @@ pub fn test_strpbrk_13() { pub fn test_strcasecmp_14() { assert!( ((({ - let mut __p1 = Ptr::from_string_literal(b"HELLO").clone(); - let mut __p2 = Ptr::from_string_literal(b"hello").clone(); + let mut __it1 = Ptr::from_string_literal(b"HELLO") + .to_c_string_iterator() + .map(|__c| __c.to_ascii_lowercase()); + let mut __it2 = Ptr::from_string_literal(b"hello") + .to_c_string_iterator() + .map(|__c| __c.to_ascii_lowercase()); loop { - let __c1 = __p1.read().to_ascii_lowercase(); - let __c2 = __p2.read().to_ascii_lowercase(); + let __c1 = __it1.next(); + let __c2 = __it2.next(); if __c1 != __c2 { - break (__c1 as i32) - (__c2 as i32); + break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); } - if __c1 == 0 { + if __c1.is_none() { break 0; } - __p1 += 1; - __p2 += 1; } } == 0) as i32) != 0) ); assert!( ((({ - let mut __p1 = Ptr::from_string_literal(b"abc").clone(); - let mut __p2 = Ptr::from_string_literal(b"abd").clone(); + let mut __it1 = Ptr::from_string_literal(b"abc") + .to_c_string_iterator() + .map(|__c| __c.to_ascii_lowercase()); + let mut __it2 = Ptr::from_string_literal(b"abd") + .to_c_string_iterator() + .map(|__c| __c.to_ascii_lowercase()); loop { - let __c1 = __p1.read().to_ascii_lowercase(); - let __c2 = __p2.read().to_ascii_lowercase(); + let __c1 = __it1.next(); + let __c2 = __it2.next(); if __c1 != __c2 { - break (__c1 as i32) - (__c2 as i32); + break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); } - if __c1 == 0 { + if __c1.is_none() { break 0; } - __p1 += 1; - __p2 += 1; } } < 0) as i32) != 0) ); assert!( ((({ - let mut __p1 = Ptr::from_string_literal(b"abd").clone(); - let mut __p2 = Ptr::from_string_literal(b"abc").clone(); + let mut __it1 = Ptr::from_string_literal(b"abd") + .to_c_string_iterator() + .map(|__c| __c.to_ascii_lowercase()); + let mut __it2 = Ptr::from_string_literal(b"abc") + .to_c_string_iterator() + .map(|__c| __c.to_ascii_lowercase()); loop { - let __c1 = __p1.read().to_ascii_lowercase(); - let __c2 = __p2.read().to_ascii_lowercase(); + let __c1 = __it1.next(); + let __c2 = __it2.next(); if __c1 != __c2 { - break (__c1 as i32) - (__c2 as i32); + break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); } - if __c1 == 0 { + if __c1.is_none() { break 0; } - __p1 += 1; - __p2 += 1; } } > 0) as i32) != 0) @@ -1222,19 +955,21 @@ pub fn test_strcasecmp_14() { let q: Value> = Rc::new(RefCell::new(Ptr::from_string_literal(b"foo"))); assert!( ((({ - let mut __p1 = (*p.borrow()).clone(); - let mut __p2 = (*q.borrow()).clone(); + let mut __it1 = (*p.borrow()) + .to_c_string_iterator() + .map(|__c| __c.to_ascii_lowercase()); + let mut __it2 = (*q.borrow()) + .to_c_string_iterator() + .map(|__c| __c.to_ascii_lowercase()); loop { - let __c1 = __p1.read().to_ascii_lowercase(); - let __c2 = __p2.read().to_ascii_lowercase(); + let __c1 = __it1.next(); + let __c2 = __it2.next(); if __c1 != __c2 { - break (__c1 as i32) - (__c2 as i32); + break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); } - if __c1 == 0 { + if __c1.is_none() { break 0; } - __p1 += 1; - __p2 += 1; } } == 0) as i32) != 0) diff --git a/tests/unit/out/refcount/va_arg_printf.rs b/tests/unit/out/refcount/va_arg_printf.rs index 854f3e13..d9af9044 100644 --- a/tests/unit/out/refcount/va_arg_printf.rs +++ b/tests/unit/out/refcount/va_arg_printf.rs @@ -30,15 +30,7 @@ pub fn lenf_2(fmt: Ptr, __args: &[VaArg]) -> i32 { (*ap.borrow_mut()) = VaList::new(__args); let s: Value> = Rc::new(RefCell::new(((*ap.borrow_mut()).arg::>()).clone())); let result: Value = Rc::new(RefCell::new( - ({ - let mut __p = (*s.borrow()).clone(); - let mut __i: usize = 0; - while __p.read() != 0 { - __p += 1; - __i += 1; - } - __i - } as i32), + ((*s.borrow()).to_c_string_iterator().count() as i32), )); return (*result.borrow()); } @@ -53,16 +45,7 @@ fn main_0() -> i32 { Ptr::from_string_literal(b"hello %d %d"), &[ (10).into(), - ({ - let mut __p = (*dummy.borrow()).clone(); - let mut __i: usize = 0; - while __p.read() != 0 { - __p += 1; - __i += 1; - } - __i - }) - .into(), + ((*dummy.borrow()).to_c_string_iterator().count()).into(), ], ) }) == 15) as i32) From a011ca37ad55b6c0ed3e0c0e1a809e0241ff9c1e Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Thu, 16 Jul 2026 12:01:50 +0100 Subject: [PATCH 12/15] Add memmove semantics to Ptr::memcpy --- libcc2rs/src/rc.rs | 10 ++++++ rules/cstring/tgt_refcount.rs | 12 +------ tests/unit/memcpy_overlap.cpp | 17 ++++++++++ tests/unit/out/refcount/cstring.rs | 19 +++-------- tests/unit/out/refcount/memcpy_overlap.rs | 33 +++++++++++++++++++ tests/unit/out/refcount/string_h.rs | 19 +++-------- tests/unit/out/unsafe/memcpy_overlap.rs | 40 +++++++++++++++++++++++ 7 files changed, 111 insertions(+), 39 deletions(-) create mode 100644 tests/unit/memcpy_overlap.cpp create mode 100644 tests/unit/out/refcount/memcpy_overlap.rs create mode 100644 tests/unit/out/unsafe/memcpy_overlap.rs diff --git a/libcc2rs/src/rc.rs b/libcc2rs/src/rc.rs index 30bc5ef2..ab1c1d7a 100644 --- a/libcc2rs/src/rc.rs +++ b/libcc2rs/src/rc.rs @@ -952,6 +952,16 @@ thread_local! { impl Ptr { #[allow(clippy::explicit_counter_loop)] pub fn memcpy(&self, src: &Self, len: usize) { + if *self > *src { + let mut dst = self.offset(len); + let mut s = src.offset(len); + for _ in 0..len { + dst -= 1; + s -= 1; + dst.write(s.read()); + } + return; + } let mut dst = self.clone(); let mut i: usize = 0; for value in src { diff --git a/rules/cstring/tgt_refcount.rs b/rules/cstring/tgt_refcount.rs index 7de598f8..f8974181 100644 --- a/rules/cstring/tgt_refcount.rs +++ b/rules/cstring/tgt_refcount.rs @@ -18,17 +18,7 @@ fn f3(a0: AnyPtr, a1: AnyPtr, a2: usize) -> i32 { } fn f4(a0: AnyPtr, a1: AnyPtr, a2: usize) -> AnyPtr { - let mut __src = a1.reinterpret_cast::(); - let mut __tmp: Vec = Vec::with_capacity(a2); - for _ in 0..a2 { - __tmp.push(__src.read()); - __src += 1; - } - let mut __dst = a0.reinterpret_cast::(); - for __i in 0..a2 { - __dst.write(__tmp[__i]); - __dst += 1; - } + a0.memcpy(&a1, a2 as usize); a0.clone() } diff --git a/tests/unit/memcpy_overlap.cpp b/tests/unit/memcpy_overlap.cpp new file mode 100644 index 00000000..9dda439f --- /dev/null +++ b/tests/unit/memcpy_overlap.cpp @@ -0,0 +1,17 @@ +#include +#include + +int main(void) { + char buf[6] = {1, 2, 3, 4, 5, 6}; + + memmove(buf + 2, buf, 4); + + assert(buf[0] == 1); + assert(buf[1] == 2); + assert(buf[2] == 1); + assert(buf[3] == 2); + assert(buf[4] == 3); + assert(buf[5] == 4); + + return 0; +} diff --git a/tests/unit/out/refcount/cstring.rs b/tests/unit/out/refcount/cstring.rs index 0a0a626c..82bb4195 100644 --- a/tests/unit/out/refcount/cstring.rs +++ b/tests/unit/out/refcount/cstring.rs @@ -101,21 +101,12 @@ pub fn test_memmove_3() { ('\0' as u8), ]))); let r: Value = Rc::new(RefCell::new({ - let mut __src = ((buf.as_pointer() as Ptr) as Ptr) - .to_any() - .reinterpret_cast::(); - let mut __tmp: Vec = Vec::with_capacity(4_usize); - for _ in 0..4_usize { - __tmp.push(__src.read()); - __src += 1; - } - let mut __dst = ((buf.as_pointer() as Ptr).offset((1) as isize) as Ptr) + ((buf.as_pointer() as Ptr).offset((1) as isize) as Ptr) .to_any() - .reinterpret_cast::(); - for __i in 0..4_usize { - __dst.write(__tmp[__i]); - __dst += 1; - } + .memcpy( + &((buf.as_pointer() as Ptr) as Ptr).to_any(), + 4_usize as usize, + ); ((buf.as_pointer() as Ptr).offset((1) as isize) as Ptr) .to_any() .clone() diff --git a/tests/unit/out/refcount/memcpy_overlap.rs b/tests/unit/out/refcount/memcpy_overlap.rs new file mode 100644 index 00000000..821853c5 --- /dev/null +++ b/tests/unit/out/refcount/memcpy_overlap.rs @@ -0,0 +1,33 @@ +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() { + std::process::exit(main_0()); +} +fn main_0() -> i32 { + let buf: Value> = + Rc::new(RefCell::new(Box::new([1_u8, 2_u8, 3_u8, 4_u8, 5_u8, 6_u8]))); + { + ((buf.as_pointer() as Ptr).offset((2) as isize) as Ptr) + .to_any() + .memcpy( + &((buf.as_pointer() as Ptr) as Ptr).to_any(), + 4_usize as usize, + ); + ((buf.as_pointer() as Ptr).offset((2) as isize) as Ptr) + .to_any() + .clone() + }; + assert!((((*buf.borrow())[(0) as usize] as i32) == 1)); + assert!((((*buf.borrow())[(1) as usize] as i32) == 2)); + assert!((((*buf.borrow())[(2) as usize] as i32) == 1)); + assert!((((*buf.borrow())[(3) as usize] as i32) == 2)); + assert!((((*buf.borrow())[(4) as usize] as i32) == 3)); + assert!((((*buf.borrow())[(5) as usize] as i32) == 4)); + return 0; +} diff --git a/tests/unit/out/refcount/string_h.rs b/tests/unit/out/refcount/string_h.rs index 7b8801ff..363bda13 100644 --- a/tests/unit/out/refcount/string_h.rs +++ b/tests/unit/out/refcount/string_h.rs @@ -124,21 +124,12 @@ pub fn test_memmove_3() { (('\0' as i32) as u8), ]))); let r: Value = Rc::new(RefCell::new({ - let mut __src = ((buf.as_pointer() as Ptr) as Ptr) - .to_any() - .reinterpret_cast::(); - let mut __tmp: Vec = Vec::with_capacity(4_usize); - for _ in 0..4_usize { - __tmp.push(__src.read()); - __src += 1; - } - let mut __dst = ((buf.as_pointer() as Ptr).offset((1) as isize) as Ptr) + ((buf.as_pointer() as Ptr).offset((1) as isize) as Ptr) .to_any() - .reinterpret_cast::(); - for __i in 0..4_usize { - __dst.write(__tmp[__i]); - __dst += 1; - } + .memcpy( + &((buf.as_pointer() as Ptr) as Ptr).to_any(), + 4_usize as usize, + ); ((buf.as_pointer() as Ptr).offset((1) as isize) as Ptr) .to_any() .clone() diff --git a/tests/unit/out/unsafe/memcpy_overlap.rs b/tests/unit/out/unsafe/memcpy_overlap.rs new file mode 100644 index 00000000..6455ac0e --- /dev/null +++ b/tests/unit/out/unsafe/memcpy_overlap.rs @@ -0,0 +1,40 @@ +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 { + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + let mut buf: [libc::c_char; 6] = [ + (1 as libc::c_char), + (2 as libc::c_char), + (3 as libc::c_char), + (4 as libc::c_char), + (5 as libc::c_char), + (6 as libc::c_char), + ]; + { + if 4_usize != 0 { + ::std::ptr::copy_nonoverlapping( + (buf.as_mut_ptr() as *const libc::c_char as *const ::libc::c_void), + (buf.as_mut_ptr().offset((2) as isize) as *mut libc::c_char as *mut ::libc::c_void), + 4_usize as usize, + ) + } + (buf.as_mut_ptr().offset((2) as isize) as *mut libc::c_char as *mut ::libc::c_void) + }; + assert!(((buf[(0) as usize] as i32) == (1))); + assert!(((buf[(1) as usize] as i32) == (2))); + assert!(((buf[(2) as usize] as i32) == (1))); + assert!(((buf[(3) as usize] as i32) == (2))); + assert!(((buf[(4) as usize] as i32) == (3))); + assert!(((buf[(5) as usize] as i32) == (4))); + return 0; +} From 0eebdcaa150bed8e799e37f89769d34e2773e4e1 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Thu, 16 Jul 2026 22:12:25 +0100 Subject: [PATCH 13/15] Add rules for AF_INET and AF_INET6 macros --- rules/socket/src.c | 8 +++ rules/socket/tgt_unsafe.rs | 8 +++ tests/unit/out/refcount/inet_pton_ntop.rs | 64 +++++++++++------------ tests/unit/out/unsafe/inet_pton_ntop.rs | 16 +++--- 4 files changed, 54 insertions(+), 42 deletions(-) diff --git a/rules/socket/src.c b/rules/socket/src.c index ca290b1d..b80d881c 100644 --- a/rules/socket/src.c +++ b/rules/socket/src.c @@ -85,3 +85,11 @@ ssize_t f19(int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen) { return sendto(sockfd, buf, len, flags, dest_addr, addrlen); } + +int f20() { + return AF_INET; +} + +int f21() { + return AF_INET6; +} diff --git a/rules/socket/tgt_unsafe.rs b/rules/socket/tgt_unsafe.rs index f263d8c5..74f757c5 100644 --- a/rules/socket/tgt_unsafe.rs +++ b/rules/socket/tgt_unsafe.rs @@ -94,3 +94,11 @@ unsafe fn f19( ) -> isize { libc::sendto(a0, a1, a2, a3, a4, a5) } + +unsafe fn f20() -> i32 { + libc::AF_INET +} + +unsafe fn f21() -> i32 { + libc::AF_INET6 +} diff --git a/tests/unit/out/refcount/inet_pton_ntop.rs b/tests/unit/out/refcount/inet_pton_ntop.rs index 6df25d74..ba2432d0 100644 --- a/tests/unit/out/refcount/inet_pton_ntop.rs +++ b/tests/unit/out/refcount/inet_pton_ntop.rs @@ -14,7 +14,7 @@ fn main_0() -> i32 { (0..16).map(|_| ::default()).collect::>(), )); assert!( - (((if 2 == libc::AF_INET { + (((if libc::AF_INET == libc::AF_INET { match Ptr::from_string_literal(b"1.2.3.4") .to_rust_string() .parse::() @@ -32,7 +32,7 @@ fn main_0() -> i32 { } Err(_) => 0, } - } else if 2 == libc::AF_INET6 { + } else if libc::AF_INET == libc::AF_INET6 { match Ptr::from_string_literal(b"1.2.3.4") .to_rust_string() .parse::() @@ -66,7 +66,7 @@ fn main_0() -> i32 { != 0) ); assert!( - (((if 2 == libc::AF_INET { + (((if libc::AF_INET == libc::AF_INET { match Ptr::from_string_literal(b"999.1.1.1") .to_rust_string() .parse::() @@ -84,7 +84,7 @@ fn main_0() -> i32 { } Err(_) => 0, } - } else if 2 == libc::AF_INET6 { + } else if libc::AF_INET == libc::AF_INET6 { match Ptr::from_string_literal(b"999.1.1.1") .to_rust_string() .parse::() @@ -108,7 +108,7 @@ fn main_0() -> i32 { != 0) ); assert!( - (((if 2 == libc::AF_INET { + (((if libc::AF_INET == libc::AF_INET { match Ptr::from_string_literal(b"not an ip") .to_rust_string() .parse::() @@ -126,7 +126,7 @@ fn main_0() -> i32 { } Err(_) => 0, } - } else if 2 == libc::AF_INET6 { + } else if libc::AF_INET == libc::AF_INET6 { match Ptr::from_string_literal(b"not an ip") .to_rust_string() .parse::() @@ -150,7 +150,7 @@ fn main_0() -> i32 { != 0) ); assert!( - (((if 10 == libc::AF_INET { + (((if libc::AF_INET6 == libc::AF_INET { match Ptr::from_string_literal(b"::1") .to_rust_string() .parse::() @@ -168,7 +168,7 @@ fn main_0() -> i32 { } Err(_) => 0, } - } else if 10 == libc::AF_INET6 { + } else if libc::AF_INET6 == libc::AF_INET6 { match Ptr::from_string_literal(b"::1") .to_rust_string() .parse::() @@ -197,7 +197,7 @@ fn main_0() -> i32 { != 0) ); assert!( - (((if 10 == libc::AF_INET { + (((if libc::AF_INET6 == libc::AF_INET { match Ptr::from_string_literal(b"2001:db8::5") .to_rust_string() .parse::() @@ -215,7 +215,7 @@ fn main_0() -> i32 { } Err(_) => 0, } - } else if 10 == libc::AF_INET6 { + } else if libc::AF_INET6 == libc::AF_INET6 { match Ptr::from_string_literal(b"2001:db8::5") .to_rust_string() .parse::() @@ -251,8 +251,8 @@ fn main_0() -> i32 { let four: Value> = Rc::new(RefCell::new(Box::new([10_u8, 0_u8, 0_u8, 1_u8]))); assert!( ((({ - let mut __p1 = { - let __text = if 2 == libc::AF_INET { + let mut __it1 = { + let __text = if libc::AF_INET == libc::AF_INET { let mut __b = [0u8; 4]; for __i in 0..4 { __b[__i] = ((four.as_pointer() as Ptr) as Ptr) @@ -262,7 +262,7 @@ fn main_0() -> i32 { .read(); } Some(std::net::Ipv4Addr::from(__b).to_string()) - } else if 2 == libc::AF_INET6 { + } else if libc::AF_INET == libc::AF_INET6 { let mut __b = [0u8; 16]; for __i in 0..16 { __b[__i] = ((four.as_pointer() as Ptr) as Ptr) @@ -290,19 +290,17 @@ fn main_0() -> i32 { _ => Ptr::null(), } } - .clone(); - let mut __p2 = Ptr::from_string_literal(b"10.0.0.1").clone(); + .to_c_string_iterator(); + let mut __it2 = Ptr::from_string_literal(b"10.0.0.1").to_c_string_iterator(); loop { - let __c1 = __p1.read(); - let __c2 = __p2.read(); + let __c1 = __it1.next(); + let __c2 = __it2.next(); if __c1 != __c2 { - break (__c1 as i32) - (__c2 as i32); + break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); } - if __c1 == 0 { + if __c1.is_none() { break 0; } - __p1 += 1; - __p2 += 1; } } == 0) as i32) != 0) @@ -328,8 +326,8 @@ fn main_0() -> i32 { (*sixteen.borrow_mut())[(15) as usize] = 1_u8; assert!( ((({ - let mut __p1 = { - let __text = if 10 == libc::AF_INET { + let mut __it1 = { + let __text = if libc::AF_INET6 == libc::AF_INET { let mut __b = [0u8; 4]; for __i in 0..4 { __b[__i] = ((sixteen.as_pointer() as Ptr) as Ptr) @@ -339,7 +337,7 @@ fn main_0() -> i32 { .read(); } Some(std::net::Ipv4Addr::from(__b).to_string()) - } else if 10 == libc::AF_INET6 { + } else if libc::AF_INET6 == libc::AF_INET6 { let mut __b = [0u8; 16]; for __i in 0..16 { __b[__i] = ((sixteen.as_pointer() as Ptr) as Ptr) @@ -367,26 +365,24 @@ fn main_0() -> i32 { _ => Ptr::null(), } } - .clone(); - let mut __p2 = Ptr::from_string_literal(b"::1").clone(); + .to_c_string_iterator(); + let mut __it2 = Ptr::from_string_literal(b"::1").to_c_string_iterator(); loop { - let __c1 = __p1.read(); - let __c2 = __p2.read(); + let __c1 = __it1.next(); + let __c2 = __it2.next(); if __c1 != __c2 { - break (__c1 as i32) - (__c2 as i32); + break (__c1.unwrap_or(0) as i32) - (__c2.unwrap_or(0) as i32); } - if __c1 == 0 { + if __c1.is_none() { break 0; } - __p1 += 1; - __p2 += 1; } } == 0) as i32) != 0) ); assert!( (((({ - let __text = if 2 == libc::AF_INET { + let __text = if libc::AF_INET == libc::AF_INET { let mut __b = [0u8; 4]; for __i in 0..4 { __b[__i] = ((four.as_pointer() as Ptr) as Ptr) @@ -396,7 +392,7 @@ fn main_0() -> i32 { .read(); } Some(std::net::Ipv4Addr::from(__b).to_string()) - } else if 2 == libc::AF_INET6 { + } else if libc::AF_INET == libc::AF_INET6 { let mut __b = [0u8; 16]; for __i in 0..16 { __b[__i] = ((four.as_pointer() as Ptr) as Ptr) diff --git a/tests/unit/out/unsafe/inet_pton_ntop.rs b/tests/unit/out/unsafe/inet_pton_ntop.rs index 45b8908f..2f11f229 100644 --- a/tests/unit/out/unsafe/inet_pton_ntop.rs +++ b/tests/unit/out/unsafe/inet_pton_ntop.rs @@ -19,7 +19,7 @@ unsafe fn main_0() -> i32 { fn inet_pton(af: i32, src: *const libc::c_char, dst: *mut ::libc::c_void) -> i32; } inet_pton( - 2, + libc::AF_INET, (c"1.2.3.4".as_ptr().cast_mut()).cast_const(), (buf.as_mut_ptr() as *mut u8 as *mut ::libc::c_void), ) @@ -41,7 +41,7 @@ unsafe fn main_0() -> i32 { fn inet_pton(af: i32, src: *const libc::c_char, dst: *mut ::libc::c_void) -> i32; } inet_pton( - 2, + libc::AF_INET, (c"999.1.1.1".as_ptr().cast_mut()).cast_const(), (buf.as_mut_ptr() as *mut u8 as *mut ::libc::c_void), ) @@ -54,7 +54,7 @@ unsafe fn main_0() -> i32 { fn inet_pton(af: i32, src: *const libc::c_char, dst: *mut ::libc::c_void) -> i32; } inet_pton( - 2, + libc::AF_INET, (c"not an ip".as_ptr().cast_mut()).cast_const(), (buf.as_mut_ptr() as *mut u8 as *mut ::libc::c_void), ) @@ -67,7 +67,7 @@ unsafe fn main_0() -> i32 { fn inet_pton(af: i32, src: *const libc::c_char, dst: *mut ::libc::c_void) -> i32; } inet_pton( - 10, + libc::AF_INET6, (c"::1".as_ptr().cast_mut()).cast_const(), (buf.as_mut_ptr() as *mut u8 as *mut ::libc::c_void), ) @@ -85,7 +85,7 @@ unsafe fn main_0() -> i32 { fn inet_pton(af: i32, src: *const libc::c_char, dst: *mut ::libc::c_void) -> i32; } inet_pton( - 10, + libc::AF_INET6, (c"2001:db8::5".as_ptr().cast_mut()).cast_const(), (buf.as_mut_ptr() as *mut u8 as *mut ::libc::c_void), ) @@ -113,7 +113,7 @@ unsafe fn main_0() -> i32 { ) -> *const libc::c_char; } inet_ntop( - 2, + libc::AF_INET, (four.as_mut_ptr() as *const u8 as *const ::libc::c_void), text.as_mut_ptr(), (::std::mem::size_of::<[libc::c_char; 64]>() as u32), @@ -140,7 +140,7 @@ unsafe fn main_0() -> i32 { ) -> *const libc::c_char; } inet_ntop( - 10, + libc::AF_INET6, (sixteen.as_mut_ptr() as *const u8 as *const ::libc::c_void), text.as_mut_ptr(), (::std::mem::size_of::<[libc::c_char; 64]>() as u32), @@ -161,7 +161,7 @@ unsafe fn main_0() -> i32 { ) -> *const libc::c_char; } inet_ntop( - 2, + libc::AF_INET, (four.as_mut_ptr() as *const u8 as *const ::libc::c_void), text.as_mut_ptr(), 4_u32, From 61d74fba3552cdae086c61dc641b08a97d3282f5 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Sat, 18 Jul 2026 15:30:26 +0100 Subject: [PATCH 14/15] Set errno --- rules/arpa_inet/tgt_refcount.rs | 10 ++++++- tests/unit/out/refcount/inet_pton_ntop.rs | 32 ++++++++++++++++++++--- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/rules/arpa_inet/tgt_refcount.rs b/rules/arpa_inet/tgt_refcount.rs index 6f595551..05420a09 100644 --- a/rules/arpa_inet/tgt_refcount.rs +++ b/rules/arpa_inet/tgt_refcount.rs @@ -27,6 +27,7 @@ fn f5(a0: i32, a1: Ptr, a2: AnyPtr) -> i32 { Err(_) => 0, } } else { + libcc2rs::cpp2rust_errno().write(::libc::EAFNOSUPPORT); -1 } } @@ -55,6 +56,13 @@ fn f6(a0: i32, a1: AnyPtr, a2: Ptr, a3: u32) -> Ptr { a2.offset(__s.len()).write(0); a2.clone() } - _ => Ptr::null(), + Some(_) => { + libcc2rs::cpp2rust_errno().write(::libc::ENOSPC); + Ptr::null() + } + None => { + libcc2rs::cpp2rust_errno().write(::libc::EAFNOSUPPORT); + Ptr::null() + } } } diff --git a/tests/unit/out/refcount/inet_pton_ntop.rs b/tests/unit/out/refcount/inet_pton_ntop.rs index ba2432d0..2bc0676e 100644 --- a/tests/unit/out/refcount/inet_pton_ntop.rs +++ b/tests/unit/out/refcount/inet_pton_ntop.rs @@ -51,6 +51,7 @@ fn main_0() -> i32 { Err(_) => 0, } } else { + libcc2rs::cpp2rust_errno().write(::libc::EAFNOSUPPORT); -1 } == 1) as i32) != 0) @@ -103,6 +104,7 @@ fn main_0() -> i32 { Err(_) => 0, } } else { + libcc2rs::cpp2rust_errno().write(::libc::EAFNOSUPPORT); -1 } == 0) as i32) != 0) @@ -145,6 +147,7 @@ fn main_0() -> i32 { Err(_) => 0, } } else { + libcc2rs::cpp2rust_errno().write(::libc::EAFNOSUPPORT); -1 } == 0) as i32) != 0) @@ -187,6 +190,7 @@ fn main_0() -> i32 { Err(_) => 0, } } else { + libcc2rs::cpp2rust_errno().write(::libc::EAFNOSUPPORT); -1 } == 1) as i32) != 0) @@ -234,6 +238,7 @@ fn main_0() -> i32 { Err(_) => 0, } } else { + libcc2rs::cpp2rust_errno().write(::libc::EAFNOSUPPORT); -1 } == 1) as i32) != 0) @@ -287,7 +292,14 @@ fn main_0() -> i32 { (text.as_pointer() as Ptr).offset(__s.len()).write(0); (text.as_pointer() as Ptr).clone() } - _ => Ptr::null(), + Some(_) => { + libcc2rs::cpp2rust_errno().write(::libc::ENOSPC); + Ptr::null() + } + None => { + libcc2rs::cpp2rust_errno().write(::libc::EAFNOSUPPORT); + Ptr::null() + } } } .to_c_string_iterator(); @@ -362,7 +374,14 @@ fn main_0() -> i32 { (text.as_pointer() as Ptr).offset(__s.len()).write(0); (text.as_pointer() as Ptr).clone() } - _ => Ptr::null(), + Some(_) => { + libcc2rs::cpp2rust_errno().write(::libc::ENOSPC); + Ptr::null() + } + None => { + libcc2rs::cpp2rust_errno().write(::libc::EAFNOSUPPORT); + Ptr::null() + } } } .to_c_string_iterator(); @@ -415,7 +434,14 @@ fn main_0() -> i32 { (text.as_pointer() as Ptr).offset(__s.len()).write(0); (text.as_pointer() as Ptr).clone() } - _ => Ptr::null(), + Some(_) => { + libcc2rs::cpp2rust_errno().write(::libc::ENOSPC); + Ptr::null() + } + None => { + libcc2rs::cpp2rust_errno().write(::libc::EAFNOSUPPORT); + Ptr::null() + } } }) .is_null()) as i32) From f2b330bc66eda1519d81683bcb0053573b979571 Mon Sep 17 00:00:00 2001 From: Lucian Popescu Date: Sat, 18 Jul 2026 15:42:50 +0100 Subject: [PATCH 15/15] Use with_slice_mut instead of looping over bytes --- rules/arpa_inet/tgt_refcount.rs | 35 ++-- tests/unit/out/refcount/inet_pton_ntop.rs | 225 ++++++++-------------- 2 files changed, 94 insertions(+), 166 deletions(-) diff --git a/rules/arpa_inet/tgt_refcount.rs b/rules/arpa_inet/tgt_refcount.rs index 05420a09..90a0e52e 100644 --- a/rules/arpa_inet/tgt_refcount.rs +++ b/rules/arpa_inet/tgt_refcount.rs @@ -7,10 +7,8 @@ fn f5(a0: i32, a1: Ptr, a2: AnyPtr) -> i32 { if a0 == libc::AF_INET { match a1.to_rust_string().parse::() { Ok(__ip) => { - let __octets = __ip.octets(); - for __i in 0..4 { - a2.reinterpret_cast::().offset(__i).write(__octets[__i]); - } + a2.reinterpret_cast::() + .with_slice_mut(4, |__s| __s.copy_from_slice(&__ip.octets())); 1 } Err(_) => 0, @@ -18,10 +16,8 @@ fn f5(a0: i32, a1: Ptr, a2: AnyPtr) -> i32 { } else if a0 == libc::AF_INET6 { match a1.to_rust_string().parse::() { Ok(__ip) => { - let __octets = __ip.octets(); - for __i in 0..16 { - a2.reinterpret_cast::().offset(__i).write(__octets[__i]); - } + a2.reinterpret_cast::() + .with_slice_mut(16, |__s| __s.copy_from_slice(&__ip.octets())); 1 } Err(_) => 0, @@ -34,26 +30,25 @@ fn f5(a0: i32, a1: Ptr, a2: AnyPtr) -> i32 { fn f6(a0: i32, a1: AnyPtr, a2: Ptr, a3: u32) -> Ptr { let __text = if a0 == libc::AF_INET { - let mut __b = [0u8; 4]; - for __i in 0..4 { - __b[__i] = a1.reinterpret_cast::().offset(__i).read(); - } + let __b: [u8; 4] = a1 + .reinterpret_cast::() + .with_slice(4, |__s| __s.try_into().unwrap()); Some(std::net::Ipv4Addr::from(__b).to_string()) } else if a0 == libc::AF_INET6 { - let mut __b = [0u8; 16]; - for __i in 0..16 { - __b[__i] = a1.reinterpret_cast::().offset(__i).read(); - } + let __b: [u8; 16] = a1 + .reinterpret_cast::() + .with_slice(16, |__s| __s.try_into().unwrap()); Some(std::net::Ipv6Addr::from(__b).to_string()) } else { None }; match __text { Some(__s) if (__s.len() as u32) < a3 => { - for __i in 0..__s.len() { - a2.offset(__i).write(__s.as_bytes()[__i]); - } - a2.offset(__s.len()).write(0); + let __n = __s.len(); + a2.with_slice_mut(__n + 1, |__sl| { + __sl[..__n].copy_from_slice(__s.as_bytes()); + __sl[__n] = 0; + }); a2.clone() } Some(_) => { diff --git a/tests/unit/out/refcount/inet_pton_ntop.rs b/tests/unit/out/refcount/inet_pton_ntop.rs index 2bc0676e..2394ba92 100644 --- a/tests/unit/out/refcount/inet_pton_ntop.rs +++ b/tests/unit/out/refcount/inet_pton_ntop.rs @@ -20,14 +20,10 @@ fn main_0() -> i32 { .parse::() { Ok(__ip) => { - let __octets = __ip.octets(); - for __i in 0..4 { - ((buf.as_pointer() as Ptr) as Ptr) - .to_any() - .reinterpret_cast::() - .offset(__i) - .write(__octets[__i]); - } + ((buf.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .with_slice_mut(4, |__s| __s.copy_from_slice(&__ip.octets())); 1 } Err(_) => 0, @@ -38,14 +34,10 @@ fn main_0() -> i32 { .parse::() { Ok(__ip) => { - let __octets = __ip.octets(); - for __i in 0..16 { - ((buf.as_pointer() as Ptr) as Ptr) - .to_any() - .reinterpret_cast::() - .offset(__i) - .write(__octets[__i]); - } + ((buf.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .with_slice_mut(16, |__s| __s.copy_from_slice(&__ip.octets())); 1 } Err(_) => 0, @@ -73,14 +65,10 @@ fn main_0() -> i32 { .parse::() { Ok(__ip) => { - let __octets = __ip.octets(); - for __i in 0..4 { - ((buf.as_pointer() as Ptr) as Ptr) - .to_any() - .reinterpret_cast::() - .offset(__i) - .write(__octets[__i]); - } + ((buf.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .with_slice_mut(4, |__s| __s.copy_from_slice(&__ip.octets())); 1 } Err(_) => 0, @@ -91,14 +79,10 @@ fn main_0() -> i32 { .parse::() { Ok(__ip) => { - let __octets = __ip.octets(); - for __i in 0..16 { - ((buf.as_pointer() as Ptr) as Ptr) - .to_any() - .reinterpret_cast::() - .offset(__i) - .write(__octets[__i]); - } + ((buf.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .with_slice_mut(16, |__s| __s.copy_from_slice(&__ip.octets())); 1 } Err(_) => 0, @@ -116,14 +100,10 @@ fn main_0() -> i32 { .parse::() { Ok(__ip) => { - let __octets = __ip.octets(); - for __i in 0..4 { - ((buf.as_pointer() as Ptr) as Ptr) - .to_any() - .reinterpret_cast::() - .offset(__i) - .write(__octets[__i]); - } + ((buf.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .with_slice_mut(4, |__s| __s.copy_from_slice(&__ip.octets())); 1 } Err(_) => 0, @@ -134,14 +114,10 @@ fn main_0() -> i32 { .parse::() { Ok(__ip) => { - let __octets = __ip.octets(); - for __i in 0..16 { - ((buf.as_pointer() as Ptr) as Ptr) - .to_any() - .reinterpret_cast::() - .offset(__i) - .write(__octets[__i]); - } + ((buf.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .with_slice_mut(16, |__s| __s.copy_from_slice(&__ip.octets())); 1 } Err(_) => 0, @@ -159,14 +135,10 @@ fn main_0() -> i32 { .parse::() { Ok(__ip) => { - let __octets = __ip.octets(); - for __i in 0..4 { - ((buf.as_pointer() as Ptr) as Ptr) - .to_any() - .reinterpret_cast::() - .offset(__i) - .write(__octets[__i]); - } + ((buf.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .with_slice_mut(4, |__s| __s.copy_from_slice(&__ip.octets())); 1 } Err(_) => 0, @@ -177,14 +149,10 @@ fn main_0() -> i32 { .parse::() { Ok(__ip) => { - let __octets = __ip.octets(); - for __i in 0..16 { - ((buf.as_pointer() as Ptr) as Ptr) - .to_any() - .reinterpret_cast::() - .offset(__i) - .write(__octets[__i]); - } + ((buf.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .with_slice_mut(16, |__s| __s.copy_from_slice(&__ip.octets())); 1 } Err(_) => 0, @@ -207,14 +175,10 @@ fn main_0() -> i32 { .parse::() { Ok(__ip) => { - let __octets = __ip.octets(); - for __i in 0..4 { - ((buf.as_pointer() as Ptr) as Ptr) - .to_any() - .reinterpret_cast::() - .offset(__i) - .write(__octets[__i]); - } + ((buf.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .with_slice_mut(4, |__s| __s.copy_from_slice(&__ip.octets())); 1 } Err(_) => 0, @@ -225,14 +189,10 @@ fn main_0() -> i32 { .parse::() { Ok(__ip) => { - let __octets = __ip.octets(); - for __i in 0..16 { - ((buf.as_pointer() as Ptr) as Ptr) - .to_any() - .reinterpret_cast::() - .offset(__i) - .write(__octets[__i]); - } + ((buf.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .with_slice_mut(16, |__s| __s.copy_from_slice(&__ip.octets())); 1 } Err(_) => 0, @@ -258,24 +218,16 @@ fn main_0() -> i32 { ((({ let mut __it1 = { let __text = if libc::AF_INET == libc::AF_INET { - let mut __b = [0u8; 4]; - for __i in 0..4 { - __b[__i] = ((four.as_pointer() as Ptr) as Ptr) - .to_any() - .reinterpret_cast::() - .offset(__i) - .read(); - } + let __b: [u8; 4] = ((four.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .with_slice(4, |__s| __s.try_into().unwrap()); Some(std::net::Ipv4Addr::from(__b).to_string()) } else if libc::AF_INET == libc::AF_INET6 { - let mut __b = [0u8; 16]; - for __i in 0..16 { - __b[__i] = ((four.as_pointer() as Ptr) as Ptr) - .to_any() - .reinterpret_cast::() - .offset(__i) - .read(); - } + let __b: [u8; 16] = ((four.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .with_slice(16, |__s| __s.try_into().unwrap()); Some(std::net::Ipv6Addr::from(__b).to_string()) } else { None @@ -284,12 +236,11 @@ fn main_0() -> i32 { Some(__s) if (__s.len() as u32) < (::std::mem::size_of::<[u8; 64]>() as u32) => { - for __i in 0..__s.len() { - (text.as_pointer() as Ptr) - .offset(__i) - .write(__s.as_bytes()[__i]); - } - (text.as_pointer() as Ptr).offset(__s.len()).write(0); + let __n = __s.len(); + (text.as_pointer() as Ptr).with_slice_mut(__n + 1, |__sl| { + __sl[..__n].copy_from_slice(__s.as_bytes()); + __sl[__n] = 0; + }); (text.as_pointer() as Ptr).clone() } Some(_) => { @@ -340,24 +291,16 @@ fn main_0() -> i32 { ((({ let mut __it1 = { let __text = if libc::AF_INET6 == libc::AF_INET { - let mut __b = [0u8; 4]; - for __i in 0..4 { - __b[__i] = ((sixteen.as_pointer() as Ptr) as Ptr) - .to_any() - .reinterpret_cast::() - .offset(__i) - .read(); - } + let __b: [u8; 4] = ((sixteen.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .with_slice(4, |__s| __s.try_into().unwrap()); Some(std::net::Ipv4Addr::from(__b).to_string()) } else if libc::AF_INET6 == libc::AF_INET6 { - let mut __b = [0u8; 16]; - for __i in 0..16 { - __b[__i] = ((sixteen.as_pointer() as Ptr) as Ptr) - .to_any() - .reinterpret_cast::() - .offset(__i) - .read(); - } + let __b: [u8; 16] = ((sixteen.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .with_slice(16, |__s| __s.try_into().unwrap()); Some(std::net::Ipv6Addr::from(__b).to_string()) } else { None @@ -366,12 +309,11 @@ fn main_0() -> i32 { Some(__s) if (__s.len() as u32) < (::std::mem::size_of::<[u8; 64]>() as u32) => { - for __i in 0..__s.len() { - (text.as_pointer() as Ptr) - .offset(__i) - .write(__s.as_bytes()[__i]); - } - (text.as_pointer() as Ptr).offset(__s.len()).write(0); + let __n = __s.len(); + (text.as_pointer() as Ptr).with_slice_mut(__n + 1, |__sl| { + __sl[..__n].copy_from_slice(__s.as_bytes()); + __sl[__n] = 0; + }); (text.as_pointer() as Ptr).clone() } Some(_) => { @@ -402,36 +344,27 @@ fn main_0() -> i32 { assert!( (((({ let __text = if libc::AF_INET == libc::AF_INET { - let mut __b = [0u8; 4]; - for __i in 0..4 { - __b[__i] = ((four.as_pointer() as Ptr) as Ptr) - .to_any() - .reinterpret_cast::() - .offset(__i) - .read(); - } + let __b: [u8; 4] = ((four.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .with_slice(4, |__s| __s.try_into().unwrap()); Some(std::net::Ipv4Addr::from(__b).to_string()) } else if libc::AF_INET == libc::AF_INET6 { - let mut __b = [0u8; 16]; - for __i in 0..16 { - __b[__i] = ((four.as_pointer() as Ptr) as Ptr) - .to_any() - .reinterpret_cast::() - .offset(__i) - .read(); - } + let __b: [u8; 16] = ((four.as_pointer() as Ptr) as Ptr) + .to_any() + .reinterpret_cast::() + .with_slice(16, |__s| __s.try_into().unwrap()); Some(std::net::Ipv6Addr::from(__b).to_string()) } else { None }; match __text { Some(__s) if (__s.len() as u32) < 4_u32 => { - for __i in 0..__s.len() { - (text.as_pointer() as Ptr) - .offset(__i) - .write(__s.as_bytes()[__i]); - } - (text.as_pointer() as Ptr).offset(__s.len()).write(0); + let __n = __s.len(); + (text.as_pointer() as Ptr).with_slice_mut(__n + 1, |__sl| { + __sl[..__n].copy_from_slice(__s.as_bytes()); + __sl[__n] = 0; + }); (text.as_pointer() as Ptr).clone() } Some(_) => {