Skip to content

Commit ba065d8

Browse files
committed
Add test to reinterpret NULL as integer pointer in va_arg
1 parent 1bff7be commit ba065d8

3 files changed

Lines changed: 156 additions & 0 deletions

File tree

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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 first_nonnull_0(count: i32, args: &[VaArg]) -> i32 {
10+
let count: Value<i32> = Rc::new(RefCell::new(count));
11+
let ap: Value<VaList> = Rc::new(RefCell::new(VaList::default()));
12+
(*ap.borrow_mut()) = VaList::new(args);
13+
let result: Value<i32> = Rc::new(RefCell::new(-1_i32));
14+
let i: Value<i32> = Rc::new(RefCell::new(0));
15+
'loop_: while ((((*i.borrow()) < (*count.borrow())) as i32) != 0) {
16+
let p: Value<Ptr<i32>> =
17+
Rc::new(RefCell::new(((*ap.borrow_mut()).arg::<Ptr<i32>>()).clone()));
18+
if (((!((*p.borrow()).is_null())) as i32) != 0) {
19+
let __rhs = ((*p.borrow()).read());
20+
(*result.borrow_mut()) = __rhs;
21+
break;
22+
}
23+
(*i.borrow_mut()).postfix_inc();
24+
}
25+
return (*result.borrow());
26+
}
27+
pub fn main() {
28+
std::process::exit(main_0());
29+
}
30+
fn main_0() -> i32 {
31+
let x: Value<i32> = Rc::new(RefCell::new(42));
32+
assert!(
33+
(((({
34+
let _count: i32 = 2;
35+
first_nonnull_0(
36+
_count,
37+
&[(AnyPtr::default()).into(), (x.as_pointer()).into()],
38+
)
39+
}) == 42) as i32)
40+
!= 0)
41+
);
42+
assert!(
43+
(((({
44+
let _count: i32 = 3;
45+
first_nonnull_0(
46+
_count,
47+
&[
48+
(AnyPtr::default()).into(),
49+
(AnyPtr::default()).into(),
50+
(x.as_pointer()).into(),
51+
],
52+
)
53+
}) == 42) as i32)
54+
!= 0)
55+
);
56+
assert!(
57+
(((({
58+
let _count: i32 = 1;
59+
first_nonnull_0(_count, &[(AnyPtr::default()).into()])
60+
}) == -1_i32) as i32)
61+
!= 0)
62+
);
63+
return 0;
64+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 first_nonnull_0(mut count: i32, args: &[VaArg]) -> i32 {
10+
let mut ap: VaList = VaList::default();
11+
ap = VaList::new(args);
12+
let mut result: i32 = -1_i32;
13+
let mut i: i32 = 0;
14+
'loop_: while ((((i) < (count)) as i32) != 0) {
15+
let mut p: *mut i32 = ap.arg::<*mut i32>();
16+
if (((!((p).is_null())) as i32) != 0) {
17+
result = (*p);
18+
break;
19+
}
20+
i.postfix_inc();
21+
}
22+
return result;
23+
}
24+
pub fn main() {
25+
unsafe {
26+
std::process::exit(main_0() as i32);
27+
}
28+
}
29+
unsafe fn main_0() -> i32 {
30+
let mut x: i32 = 42;
31+
assert!(
32+
((((unsafe {
33+
let _count: i32 = 2;
34+
first_nonnull_0(
35+
_count,
36+
&[
37+
(0 as *mut ::libc::c_void).into(),
38+
(&mut x as *mut i32).into(),
39+
],
40+
)
41+
}) == (42)) as i32)
42+
!= 0)
43+
);
44+
assert!(
45+
((((unsafe {
46+
let _count: i32 = 3;
47+
first_nonnull_0(
48+
_count,
49+
&[
50+
(0 as *mut ::libc::c_void).into(),
51+
(0 as *mut ::libc::c_void).into(),
52+
(&mut x as *mut i32).into(),
53+
],
54+
)
55+
}) == (42)) as i32)
56+
!= 0)
57+
);
58+
assert!(
59+
((((unsafe {
60+
let _count: i32 = 1;
61+
first_nonnull_0(_count, &[(0 as *mut ::libc::c_void).into()])
62+
}) == (-1_i32)) as i32)
63+
!= 0)
64+
);
65+
return 0;
66+
}

tests/unit/va_arg_null_int_ptr.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <assert.h>
2+
#include <stdarg.h>
3+
#include <stddef.h>
4+
5+
int first_nonnull(int count, ...) {
6+
va_list ap;
7+
va_start(ap, count);
8+
int result = -1;
9+
for (int i = 0; i < count; i++) {
10+
int *p = va_arg(ap, int *);
11+
if (p != NULL) {
12+
result = *p;
13+
break;
14+
}
15+
}
16+
va_end(ap);
17+
return result;
18+
}
19+
20+
int main() {
21+
int x = 42;
22+
assert(first_nonnull(2, NULL, &x) == 42);
23+
assert(first_nonnull(3, NULL, NULL, &x) == 42);
24+
assert(first_nonnull(1, NULL) == -1);
25+
return 0;
26+
}

0 commit comments

Comments
 (0)