Skip to content

Commit a89ebad

Browse files
committed
Add if_nametoindex safe rule
1 parent a9c50e7 commit a89ebad

5 files changed

Lines changed: 63 additions & 0 deletions

File tree

‎rules/net_if/tgt_refcount.rs‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright (c) 2022-present INESC-ID.
2+
// Distributed under the MIT license that can be found in the LICENSE file.
3+
4+
use libcc2rs::*;
5+
6+
fn f1(a0: Ptr<u8>) -> u32 {
7+
match nix::net::if_::if_nametoindex(a0.to_rust_string().as_str()) {
8+
Ok(__i) => __i,
9+
Err(__e) => {
10+
libcc2rs::cpp2rust_errno().write(__e as i32);
11+
0
12+
}
13+
}
14+
}

‎rules/src/modules.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ pub mod map_tgt_refcount;
9494
pub mod map_tgt_unsafe;
9595
#[path = r#"../math/tgt_unsafe.rs"#]
9696
pub mod math_tgt_unsafe;
97+
#[path = r#"../net_if/tgt_refcount.rs"#]
98+
pub mod net_if_tgt_refcount;
9799
#[path = r#"../net_if/tgt_unsafe.rs"#]
98100
pub mod net_if_tgt_unsafe;
99101
#[path = r#"../netdb/tgt_refcount.rs"#]

‎tests/unit/ifaddrs.c‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include <assert.h>
2+
#include <errno.h>
23
#include <ifaddrs.h>
4+
#include <net/if.h>
35
#include <netinet/in.h>
46
#include <string.h>
57
#include <sys/socket.h>
@@ -28,9 +30,14 @@ int main(void) {
2830
struct sockaddr_in *mask = (struct sockaddr_in *)ifa->ifa_netmask;
2931
unsigned char mask_be[4] = {255, 0, 0, 0};
3032
assert(memcmp(&mask->sin_addr, mask_be, 4) == 0);
33+
assert(if_nametoindex(ifa->ifa_name) > 0);
3134
}
3235
}
3336
assert(found_loopback);
3437
freeifaddrs(list);
38+
39+
errno = 0;
40+
assert(if_nametoindex("cpp2rust_no_such_if") == 0);
41+
assert(errno == ENODEV);
3542
return 0;
3643
}

‎tests/unit/out/refcount/ifaddrs.rs‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,20 @@ fn main_0() -> i32 {
104104
== 0) as i32)
105105
!= 0)
106106
);
107+
assert!(
108+
(((match nix::net::if_::if_nametoindex(
109+
(*(*(*ifa.borrow()).upgrade().deref()).ifa_name.borrow())
110+
.to_rust_string()
111+
.as_str()
112+
) {
113+
Ok(__i) => __i,
114+
Err(__e) => {
115+
libcc2rs::cpp2rust_errno().write(__e as i32);
116+
0
117+
}
118+
} > 0_u32) as i32)
119+
!= 0)
120+
);
107121
}
108122
let __rhs = (*(*(*ifa.borrow()).upgrade().deref()).ifa_next.borrow()).clone();
109123
(*ifa.borrow_mut()) = __rhs;
@@ -131,5 +145,21 @@ fn main_0() -> i32 {
131145
__cur = __next;
132146
}
133147
};
148+
libcc2rs::cpp2rust_errno().write(0);
149+
assert!(
150+
(((match nix::net::if_::if_nametoindex(
151+
Ptr::from_string_literal(b"cpp2rust_no_such_if")
152+
.to_rust_string()
153+
.as_str()
154+
) {
155+
Ok(__i) => __i,
156+
Err(__e) => {
157+
libcc2rs::cpp2rust_errno().write(__e as i32);
158+
0
159+
}
160+
} == 0_u32) as i32)
161+
!= 0)
162+
);
163+
assert!(((((libcc2rs::cpp2rust_errno().read()) == 19) as i32) != 0));
134164
return 0;
135165
}

‎tests/unit/out/unsafe/ifaddrs.rs‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,20 @@ unsafe fn main_0() -> i32 {
7979
}) == (0)) as i32)
8080
!= 0)
8181
);
82+
assert!(
83+
((((libc::if_nametoindex(((*ifa).ifa_name).cast_const())) > (0_u32)) as i32) != 0)
84+
);
8285
}
8386
ifa = (*ifa).ifa_next;
8487
}
8588
assert!((found_loopback != 0));
8689
libc::freeifaddrs(list);
90+
(*libcc2rs::cpp2rust_errno_unsafe()) = 0;
91+
assert!(
92+
((((libc::if_nametoindex((c"cpp2rust_no_such_if".as_ptr().cast_mut()).cast_const()))
93+
== (0_u32)) as i32)
94+
!= 0)
95+
);
96+
assert!(((((*libcc2rs::cpp2rust_errno_unsafe()) == (19)) as i32) != 0));
8797
return 0;
8898
}

0 commit comments

Comments
 (0)