Skip to content

Commit 10b5d1a

Browse files
authored
Add if_nametoindex safe rule (#270)
1 parent a3fba06 commit 10b5d1a

5 files changed

Lines changed: 56 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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <assert.h>
22
#include <ifaddrs.h>
3+
#include <net/if.h>
34
#include <netinet/in.h>
45
#include <string.h>
56
#include <sys/socket.h>
@@ -28,9 +29,12 @@ int main(void) {
2829
struct sockaddr_in *mask = (struct sockaddr_in *)ifa->ifa_netmask;
2930
unsigned char mask_be[4] = {255, 0, 0, 0};
3031
assert(memcmp(&mask->sin_addr, mask_be, 4) == 0);
32+
assert(if_nametoindex(ifa->ifa_name) > 0);
3133
}
3234
}
3335
assert(found_loopback);
3436
freeifaddrs(list);
37+
38+
assert(if_nametoindex("cpp2rust_no_such_if") == 0);
3539
return 0;
3640
}

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

Lines changed: 28 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,19 @@ fn main_0() -> i32 {
131145
__cur = __next;
132146
}
133147
};
148+
assert!(
149+
(((match nix::net::if_::if_nametoindex(
150+
Ptr::from_string_literal(b"cpp2rust_no_such_if")
151+
.to_rust_string()
152+
.as_str()
153+
) {
154+
Ok(__i) => __i,
155+
Err(__e) => {
156+
libcc2rs::cpp2rust_errno().write(__e as i32);
157+
0
158+
}
159+
} == 0_u32) as i32)
160+
!= 0)
161+
);
134162
return 0;
135163
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,18 @@ 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+
assert!(
91+
((((libc::if_nametoindex((c"cpp2rust_no_such_if".as_ptr().cast_mut()).cast_const()))
92+
== (0_u32)) as i32)
93+
!= 0)
94+
);
8795
return 0;
8896
}

0 commit comments

Comments
 (0)