Skip to content

Commit 5cac927

Browse files
committed
Add UB test for int to enum cast
1 parent f56a035 commit 5cac927

3 files changed

Lines changed: 85 additions & 0 deletions

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// panic
2+
3+
enum Color { RED, GREEN, BLUE };
4+
5+
int main() {
6+
int n = 3;
7+
Color c = (Color)n;
8+
return c == BLUE ? 0 : 1;
9+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
#[derive(Clone, Copy, PartialEq, Debug, Default)]
10+
enum Color {
11+
#[default]
12+
RED = 0,
13+
GREEN = 1,
14+
BLUE = 2,
15+
}
16+
impl From<i32> for Color {
17+
fn from(n: i32) -> Color {
18+
match n {
19+
0 => Color::RED,
20+
1 => Color::GREEN,
21+
2 => Color::BLUE,
22+
_ => panic!("invalid Color value: {}", n),
23+
}
24+
}
25+
}
26+
pub fn main() {
27+
std::process::exit(main_0());
28+
}
29+
fn main_0() -> i32 {
30+
let n: Value<i32> = Rc::new(RefCell::new(3));
31+
let c: Value<Color> = Rc::new(RefCell::new(Color::from((*n.borrow()) as i32)));
32+
return if (((*c.borrow()) as i32) == (Color::BLUE as i32)) {
33+
0
34+
} else {
35+
1
36+
};
37+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
#[derive(Clone, Copy, PartialEq, Debug, Default)]
10+
enum Color {
11+
#[default]
12+
RED = 0,
13+
GREEN = 1,
14+
BLUE = 2,
15+
}
16+
impl From<i32> for Color {
17+
fn from(n: i32) -> Color {
18+
match n {
19+
0 => Color::RED,
20+
1 => Color::GREEN,
21+
2 => Color::BLUE,
22+
_ => panic!("invalid Color value: {}", n),
23+
}
24+
}
25+
}
26+
pub fn main() {
27+
unsafe {
28+
std::process::exit(main_0() as i32);
29+
}
30+
}
31+
unsafe fn main_0() -> i32 {
32+
let mut n: i32 = 3;
33+
let mut c: Color = Color::from(n as i32);
34+
return if ((c as i32) == (Color::BLUE as i32)) {
35+
0
36+
} else {
37+
1
38+
};
39+
}

0 commit comments

Comments
 (0)