Skip to content

Commit 9ef900f

Browse files
authored
Do not invoke memset with count of zero (#4205)
We need to apply similar exclusion rules as we already do for intrinsics that invoke `memcmp`. Resolves: #90 Resolves: #3772 By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.
1 parent 3d5d867 commit 9ef900f

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

kani-compiler/src/codegen_cprover_gotoc/codegen/intrinsic.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1730,15 +1730,20 @@ impl GotocCtx<'_> {
17301730

17311731
// Check that computing `count` in bytes would not overflow
17321732
let (count_bytes, overflow_check) = self.count_in_bytes(
1733-
count,
1733+
count.clone(),
17341734
pointee_type_stable(dst_typ).unwrap(),
17351735
Type::size_t(),
17361736
"write_bytes",
17371737
loc,
17381738
);
17391739

17401740
let memset_call = BuiltinFn::Memset.call(vec![dst, val, count_bytes], loc);
1741-
Stmt::block(vec![align_check, overflow_check, memset_call.as_stmt(loc)], loc)
1741+
Stmt::if_then_else(
1742+
count.clone().gt(Expr::int_constant(0, count.typ().clone())),
1743+
Stmt::block(vec![align_check, overflow_check, memset_call.as_stmt(loc)], loc),
1744+
None,
1745+
loc,
1746+
)
17421747
}
17431748

17441749
/// Computes (multiplies) the equivalent of a memory-related number (e.g., an offset) in bytes.

tests/kani/NondetVectors/fixme_main.rs renamed to tests/kani/NondetVectors/main.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,20 @@ fn main() {
1616
assert!(_buf2[idx] == elt);
1717
}
1818
}
19+
20+
#[kani::proof]
21+
fn minimal1() {
22+
let v: Vec<i8> = vec![kani::any(); 0];
23+
}
24+
25+
#[kani::proof]
26+
fn minimal2() {
27+
let v: Vec<i8> = vec![5; 0];
28+
}
29+
30+
#[kani::proof]
31+
fn vec3772() {
32+
let value: u8 = 1; /* set to zero and it passes */
33+
let count: u16 = kani::any();
34+
let vector: Vec<u8> = vec![value; count as usize];
35+
}

0 commit comments

Comments
 (0)