Skip to content

Commit bd41414

Browse files
committed
Add null pointer serialization/deserialization test
1 parent 32d9d9c commit bd41414

3 files changed

Lines changed: 124 additions & 0 deletions

File tree

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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 main() {
10+
std::process::exit(main_0());
11+
}
12+
fn main_0() -> i32 {
13+
pub struct anon_0 {
14+
__bytes: Value<Box<[u8]>>,
15+
}
16+
impl anon_0 {
17+
pub fn p(&self) -> Ptr<Ptr<i32>> {
18+
(self.__bytes.as_pointer() as Ptr<u8>).reinterpret_cast()
19+
}
20+
pub fn bits(&self) -> Ptr<u64> {
21+
(self.__bytes.as_pointer() as Ptr<u8>).reinterpret_cast()
22+
}
23+
}
24+
impl Clone for anon_0 {
25+
fn clone(&self) -> Self {
26+
anon_0 {
27+
__bytes: Rc::new(RefCell::new(self.__bytes.borrow().clone())),
28+
}
29+
}
30+
}
31+
impl Default for anon_0 {
32+
fn default() -> Self {
33+
anon_0 {
34+
__bytes: Rc::new(RefCell::new(Box::from([0u8; 8]))),
35+
}
36+
}
37+
}
38+
impl ByteRepr for anon_0 {
39+
fn byte_size() -> usize {
40+
8
41+
}
42+
fn to_bytes(&self, buf: &mut [u8]) {
43+
buf.copy_from_slice(&self.__bytes.borrow());
44+
}
45+
fn from_bytes(buf: &[u8]) -> Self {
46+
anon_0 {
47+
__bytes: Rc::new(RefCell::new(Box::from(buf))),
48+
}
49+
}
50+
};
51+
let u: Value<anon_0> = <Value<anon_0>>::default();
52+
(*u.borrow_mut()).bits().write(0_u64);
53+
assert!((((((*u.borrow()).p().read()).is_null()) as i32) != 0));
54+
let x: Value<i32> = Rc::new(RefCell::new(5));
55+
(*u.borrow_mut()).p().write((x.as_pointer()));
56+
assert!((((((*u.borrow()).bits().read()) != 0_u64) as i32) != 0));
57+
(*u.borrow_mut()).bits().write(0_u64);
58+
assert!((((((*u.borrow()).p().read()).is_null()) as i32) != 0));
59+
(*u.borrow_mut()).p().write(Ptr::<i32>::null());
60+
assert!((((((*u.borrow()).bits().read()) == 0_u64) as i32) != 0));
61+
return 0;
62+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 fn main() {
10+
unsafe {
11+
std::process::exit(main_0() as i32);
12+
}
13+
}
14+
unsafe fn main_0() -> i32 {
15+
#[repr(C)]
16+
#[derive(Copy, Clone)]
17+
pub union anon_0 {
18+
pub p: *mut i32,
19+
pub bits: u64,
20+
}
21+
impl Default for anon_0 {
22+
fn default() -> Self {
23+
unsafe { std::mem::zeroed() }
24+
}
25+
};
26+
let mut u: anon_0 = <anon_0>::default();
27+
u.bits = 0_u64;
28+
assert!(((((u.p).is_null()) as i32) != 0));
29+
let mut x: i32 = 5;
30+
u.p = (&mut x as *mut i32);
31+
assert!(((((u.bits) != (0_u64)) as i32) != 0));
32+
u.bits = 0_u64;
33+
assert!(((((u.p).is_null()) as i32) != 0));
34+
u.p = std::ptr::null_mut();
35+
assert!(((((u.bits) == (0_u64)) as i32) != 0));
36+
return 0;
37+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <assert.h>
2+
#include <stddef.h>
3+
#include <stdint.h>
4+
5+
int main(void) {
6+
union {
7+
int *p;
8+
uintptr_t bits;
9+
} u;
10+
11+
u.bits = 0;
12+
assert(u.p == NULL);
13+
14+
int x = 5;
15+
u.p = &x;
16+
assert(u.bits != 0);
17+
18+
u.bits = 0;
19+
assert(u.p == NULL);
20+
21+
u.p = NULL;
22+
assert(u.bits == 0);
23+
24+
return 0;
25+
}

0 commit comments

Comments
 (0)