Skip to content

Commit 5a7ee97

Browse files
Avoid overlapping borrows in repeated-character append (#580)
Repeated-character string append now reads the target length before borrowing the receiver for resizing. This avoids a checked-borrow panic for strings whose data pointer is used, including calls that ignore the result. - Save the post-pop length plus the append count in a local before resizing in [rules/string/tgt_refcount.rs](rules/string/tgt_refcount.rs). - Keep the trailing-zero update and the existing result clone. - Use the size type explicitly in the fixture's modulo expressions so its generated-source expectations do not depend on the standard library's type spelling for `sizeof`. Runtime borrow checking and the unsafe-model rule are unchanged. This repairs the receiver's own length read; it does not change append's return-reference semantics or operand evaluation. ## Testing [tests/unit/string_repeat_append.cpp](tests/unit/string_repeat_append.cpp) checks size, receiver bytes and the trailing zero over a bounded grid of initial lengths, counts and byte values, with and without shrinking first. It includes empty strings, zero counts, embedded NULs and growth, plus unboxed-local, reference and pointer receivers. The scoped lit runner passes four tests with no failures or skips, comparing native C++ with both output models and checking their generated-source expectations. The existing string fixture checks the surrounding string rules. Coverage is bounded; application frequency remains unmeasured. 4 recorded checks pass with this change. - Scoped string lit tests - Rules clippy - String rule and expected\-output rustfmt - Append fixture clang\-format [Case and evidence](<https://app.perfloop.ai/t/oss/case_rmkdg2esen>) --- Made by [Perfloop](https://app.perfloop.ai). Human sponsor: [Tomás Senart](https://github.com/tsenart). --------- Signed-off-by: Perfloop Agent <agent@perfloop.ai> Signed-off-by: Tomás Senart <ts@perfloop.ai>
1 parent e2b679d commit 5a7ee97

4 files changed

Lines changed: 392 additions & 1 deletion

File tree

‎rules/string/tgt_refcount.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@ fn f18(a0: Vec<i8>, a1: Ptr<i8>) -> bool {
122122
// TODO: This should return a0
123123
fn f21(a0: &mut Vec<i8>, a1: usize, a2: i8) -> Vec<i8> {
124124
a0.pop();
125-
a0.resize(a0.len() + (a1) as usize, a2);
125+
let __len = a0.len() + (a1) as usize;
126+
a0.resize(__len, a2);
126127
a0.push(0);
127128
a0.clone()
128129
}
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
extern crate libcc2rs;
2+
use libcc2rs::*;
3+
use std::cell::RefCell;
4+
use std::collections::BTreeMap;
5+
use std::io::prelude::*;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::AsFd;
8+
use std::rc::{Rc, Weak};
9+
pub fn append_to_reference_0(s: Ptr<Vec<i8>>) {
10+
{
11+
s.with_mut(|__v: &mut Vec<i8>| __v.pop());
12+
let __len = (*s.upgrade().deref()).len() + (1_usize) as usize;
13+
s.with_mut(|__v: &mut Vec<i8>| __v.resize(__len, ('r' as i8)));
14+
s.with_mut(|__v: &mut Vec<i8>| __v.push(0));
15+
(*s.upgrade().deref()).clone()
16+
};
17+
}
18+
pub fn main() {
19+
__cpp2rust_init_globals();
20+
std::process::exit(main_0());
21+
}
22+
fn main_0() -> i32 {
23+
let lengths: Value<Box<[usize]>> = Rc::new(RefCell::new(Box::new([
24+
0_usize, 1_usize, 2_usize, 3_usize, 7_usize, 8_usize, 15_usize, 16_usize, 31_usize,
25+
64_usize,
26+
])));
27+
let counts: Value<Box<[usize]>> = Rc::new(RefCell::new(Box::new([
28+
0_usize, 1_usize, 2_usize, 3_usize, 8_usize, 33_usize,
29+
])));
30+
let characters: Value<Box<[i8]>> = Rc::new(RefCell::new(Box::new([
31+
0_i8,
32+
1_i8,
33+
2_i8,
34+
7_i8,
35+
9_i8,
36+
10_i8,
37+
13_i8,
38+
31_i8,
39+
32_i8,
40+
42_i8,
41+
65_i8,
42+
90_i8,
43+
97_i8,
44+
100_i8,
45+
127_i8,
46+
(-128_i32 as i8),
47+
(-127_i32 as i8),
48+
(-64_i32 as i8),
49+
(-2_i32 as i8),
50+
(-1_i32 as i8),
51+
])));
52+
let mut initial: [i8; 5] = [
53+
('a' as i8),
54+
0_i8,
55+
('b' as i8),
56+
(-128_i32 as i8),
57+
(-1_i32 as i8),
58+
];
59+
let mut cases: usize = 0_usize;
60+
'loop_: for length in IterRange::new(
61+
(lengths.as_pointer() as Ptr<usize>),
62+
(lengths.as_pointer() as Ptr<usize>).offset((10_i64) as isize),
63+
) {
64+
let mut length: usize = (length.read());
65+
'loop_: for count in IterRange::new(
66+
(counts.as_pointer() as Ptr<usize>),
67+
(counts.as_pointer() as Ptr<usize>).offset((6_i64) as isize),
68+
) {
69+
let mut count: usize = (count.read());
70+
'loop_: for character in IterRange::new(
71+
(characters.as_pointer() as Ptr<i8>),
72+
(characters.as_pointer() as Ptr<i8>).offset((20_i64) as isize),
73+
) {
74+
let character: Value<i8> = Rc::new(RefCell::new((character.read())));
75+
let mut shrink: i32 = 0;
76+
'loop_: while (shrink < 2) {
77+
let s: Value<Vec<i8>> = Rc::new(RefCell::new(
78+
vec![('x' as i8); (length) as usize]
79+
.iter()
80+
.cloned()
81+
.chain(std::iter::once(0))
82+
.collect(),
83+
));
84+
let mut i: usize = 0_usize;
85+
'loop_: while (i < length) {
86+
let __rhs = initial[((i)
87+
.wrapping_rem((::std::mem::size_of::<[i8; 5]>() as usize)))
88+
as usize];
89+
(*s.borrow_mut())[i] = __rhs;
90+
i.prefix_inc();
91+
}
92+
if (shrink != 0) {
93+
(*s.borrow_mut()).shrink_to_fit();
94+
}
95+
{
96+
(*s.borrow_mut()).pop();
97+
let __len = (*s.borrow()).len() + (count) as usize;
98+
(*s.borrow_mut()).resize(__len, ({ (*character.borrow()) }));
99+
(*s.borrow_mut()).push(0);
100+
(*s.borrow()).clone()
101+
};
102+
assert!(({ (((*s.borrow()).len() - 1) == (length).wrapping_add(count)) }));
103+
let mut data: Ptr<i8> = s.decay();
104+
let mut i: usize = 0_usize;
105+
'loop_: while (i < (length).wrapping_add(count)) {
106+
assert!(
107+
({
108+
({ ((elem!(data, i).read()) as i32) } == {
109+
((if (i < length) {
110+
initial[((i).wrapping_rem(
111+
(::std::mem::size_of::<[i8; 5]>() as usize),
112+
)) as usize]
113+
} else {
114+
(*character.borrow())
115+
}) as i32)
116+
})
117+
})
118+
);
119+
i.prefix_inc();
120+
}
121+
assert!(({ (((elem!(data, ((*s.borrow()).len() - 1)).read()) as i32) == 0) }));
122+
cases.prefix_inc();
123+
shrink.prefix_inc();
124+
}
125+
}
126+
}
127+
}
128+
let mut unboxed: Vec<i8> = {
129+
let mut __bytes = Ptr::<i8>::from_string_literal(b"a").to_c_bytes();
130+
__bytes.push(0);
131+
__bytes
132+
};
133+
{
134+
unboxed.pop();
135+
let __len = unboxed.len() + (2_usize) as usize;
136+
unboxed.resize(__len, ('b' as i8));
137+
unboxed.push(0);
138+
unboxed.clone()
139+
};
140+
assert!(
141+
Ptr::<i8>::from_string_literal(b"abb")
142+
.with_c_str(|__s| unboxed[..unboxed.len().saturating_sub(1)] == *__s)
143+
);
144+
let indirect: Value<Vec<i8>> = Rc::new(RefCell::new({
145+
let mut __bytes = Ptr::<i8>::from_string_literal(b"c").to_c_bytes();
146+
__bytes.push(0);
147+
__bytes
148+
}));
149+
({ append_to_reference_0(indirect.as_pointer()) });
150+
assert!(
151+
({
152+
Ptr::<i8>::from_string_literal(b"cr").with_c_str(|__s| {
153+
(*indirect.borrow())[..(*indirect.borrow()).len().saturating_sub(1)] == *__s
154+
})
155+
})
156+
);
157+
let mut pointer: Ptr<Vec<i8>> = (indirect.as_pointer());
158+
{
159+
pointer.with_mut(|__v: &mut Vec<i8>| __v.pop());
160+
let __len = (*pointer.upgrade().deref()).len() + (0_usize) as usize;
161+
pointer.with_mut(|__v: &mut Vec<i8>| __v.resize(__len, ('\0' as i8)));
162+
pointer.with_mut(|__v: &mut Vec<i8>| __v.push(0));
163+
(*pointer.upgrade().deref()).clone()
164+
};
165+
assert!(
166+
({
167+
Ptr::<i8>::from_string_literal(b"cr").with_c_str(|__s| {
168+
(*indirect.borrow())[..(*indirect.borrow()).len().saturating_sub(1)] == *__s
169+
})
170+
})
171+
);
172+
println!("checked {} appends", cases);
173+
return 0;
174+
}
175+
pub fn __cpp2rust_init_globals() {}
Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
extern crate libc;
2+
use libc::*;
3+
extern crate libcc2rs;
4+
use libcc2rs::*;
5+
use std::collections::BTreeMap;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
8+
use std::rc::Rc;
9+
pub unsafe fn append_to_reference_0(s: *mut Vec<libc::c_char>) {
10+
(*s).splice(
11+
(*s).len() - 1..(*s).len() - 1,
12+
::std::vec::from_elem(('r' as libc::c_char), 1_usize as usize),
13+
);
14+
}
15+
pub fn main() {
16+
unsafe {
17+
__cpp2rust_init_globals();
18+
std::process::exit(main_0() as i32);
19+
}
20+
}
21+
unsafe fn main_0() -> i32 {
22+
let lengths: [usize; 10] = [
23+
0_usize, 1_usize, 2_usize, 3_usize, 7_usize, 8_usize, 15_usize, 16_usize, 31_usize,
24+
64_usize,
25+
];
26+
let counts: [usize; 6] = [0_usize, 1_usize, 2_usize, 3_usize, 8_usize, 33_usize];
27+
let characters: [libc::c_char; 20] = [
28+
(0 as libc::c_char),
29+
(1 as libc::c_char),
30+
(2 as libc::c_char),
31+
(7 as libc::c_char),
32+
(9 as libc::c_char),
33+
(10 as libc::c_char),
34+
(13 as libc::c_char),
35+
(31 as libc::c_char),
36+
(32 as libc::c_char),
37+
(42 as libc::c_char),
38+
(65 as libc::c_char),
39+
(90 as libc::c_char),
40+
(97 as libc::c_char),
41+
(100 as libc::c_char),
42+
(127 as libc::c_char),
43+
(-128_i32 as libc::c_char),
44+
(-127_i32 as libc::c_char),
45+
(-64_i32 as libc::c_char),
46+
(-2_i32 as libc::c_char),
47+
(-1_i32 as libc::c_char),
48+
];
49+
let initial: [libc::c_char; 5] = [
50+
('a' as libc::c_char),
51+
(0 as libc::c_char),
52+
('b' as libc::c_char),
53+
(-128_i32 as libc::c_char),
54+
(-1_i32 as libc::c_char),
55+
];
56+
let mut cases: usize = 0_usize;
57+
'loop_: for length in
58+
IterRange::new(lengths.as_ptr(), lengths.as_ptr().offset((10_i64) as isize))
59+
{
60+
let mut length: usize = (*length);
61+
'loop_: for count in
62+
IterRange::new(counts.as_ptr(), counts.as_ptr().offset((6_i64) as isize))
63+
{
64+
let mut count: usize = (*count);
65+
'loop_: for character in IterRange::new(
66+
characters.as_ptr(),
67+
characters.as_ptr().offset((20_i64) as isize),
68+
) {
69+
let mut character: libc::c_char = (*character);
70+
let mut shrink: i32 = 0;
71+
'loop_: while ((shrink) < (2)) {
72+
let mut s: Vec<libc::c_char> = vec![('x' as libc::c_char); (length) as usize]
73+
.iter()
74+
.cloned()
75+
.chain(std::iter::once(0))
76+
.collect();
77+
let mut i: usize = 0_usize;
78+
'loop_: while ((i) < (length)) {
79+
s[(i)] = initial[((i)
80+
.wrapping_rem((::std::mem::size_of::<[libc::c_char; 5]>() as usize)))];
81+
i.prefix_inc();
82+
}
83+
if (shrink != 0) {
84+
s.shrink_to_fit();
85+
}
86+
s.splice(
87+
s.len() - 1..s.len() - 1,
88+
::std::vec::from_elem(character, count as usize),
89+
);
90+
assert!(((s.len() - 1) == ((length).wrapping_add(count))));
91+
let mut data: *const libc::c_char = (s.as_mut_ptr()).cast_const();
92+
let mut i: usize = 0_usize;
93+
'loop_: while ((i) < ((length).wrapping_add(count))) {
94+
assert!(
95+
(((*data.offset((i) as isize)) as i32)
96+
== ((if ((i) < (length)) {
97+
initial[((i).wrapping_rem(
98+
(::std::mem::size_of::<[libc::c_char; 5]>() as usize),
99+
))]
100+
} else {
101+
character
102+
}) as i32))
103+
);
104+
i.prefix_inc();
105+
}
106+
assert!((((*data.offset((s.len() - 1) as isize)) as i32) == (0)));
107+
cases.prefix_inc();
108+
shrink.prefix_inc();
109+
}
110+
}
111+
}
112+
}
113+
let mut unboxed: Vec<libc::c_char> = std::slice::from_raw_parts(
114+
c"a".as_ptr(),
115+
(0..).take_while(|&i| *c"a".as_ptr().add(i) != 0).count() + 1,
116+
)
117+
.to_vec();
118+
unboxed.splice(
119+
unboxed.len() - 1..unboxed.len() - 1,
120+
::std::vec::from_elem(('b' as libc::c_char), 2_usize as usize),
121+
);
122+
assert!(
123+
unboxed == {
124+
std::slice::from_raw_parts(
125+
c"abb".as_ptr(),
126+
(0..).take_while(|&i| *c"abb".as_ptr().add(i) != 0).count() + 1,
127+
)
128+
.to_vec()
129+
}
130+
);
131+
let mut indirect: Vec<libc::c_char> = std::slice::from_raw_parts(
132+
c"c".as_ptr(),
133+
(0..).take_while(|&i| *c"c".as_ptr().add(i) != 0).count() + 1,
134+
)
135+
.to_vec();
136+
(unsafe { append_to_reference_0(&mut indirect) });
137+
assert!(
138+
indirect == {
139+
std::slice::from_raw_parts(
140+
c"cr".as_ptr(),
141+
(0..).take_while(|&i| *c"cr".as_ptr().add(i) != 0).count() + 1,
142+
)
143+
.to_vec()
144+
}
145+
);
146+
let mut pointer: *mut Vec<libc::c_char> = (&mut indirect as *mut Vec<libc::c_char>);
147+
(*pointer).splice(
148+
(*pointer).len() - 1..(*pointer).len() - 1,
149+
::std::vec::from_elem(('\0' as libc::c_char), 0_usize as usize),
150+
);
151+
assert!(
152+
indirect == {
153+
std::slice::from_raw_parts(
154+
c"cr".as_ptr(),
155+
(0..).take_while(|&i| *c"cr".as_ptr().add(i) != 0).count() + 1,
156+
)
157+
.to_vec()
158+
}
159+
);
160+
(unsafe {
161+
libc::printf(
162+
c"checked %zu appends\n".as_ptr() as *const libc::c_char,
163+
(cases),
164+
)
165+
});
166+
return 0;
167+
}
168+
pub unsafe fn __cpp2rust_init_globals() {}

0 commit comments

Comments
 (0)