Skip to content

Commit dcdc925

Browse files
committed
Add rules for netdb
1 parent d488a77 commit dcdc925

4 files changed

Lines changed: 139 additions & 0 deletions

File tree

rules/netdb/ir_unsafe.json

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
{
2+
"f1": {
3+
"body": [
4+
{
5+
"text": "libc::getaddrinfo("
6+
},
7+
{
8+
"placeholder": {
9+
"arg": 0,
10+
"access": "read"
11+
}
12+
},
13+
{
14+
"text": " as *const i8, "
15+
},
16+
{
17+
"placeholder": {
18+
"arg": 1,
19+
"access": "read"
20+
}
21+
},
22+
{
23+
"text": " as *const i8, "
24+
},
25+
{
26+
"placeholder": {
27+
"arg": 2,
28+
"access": "read"
29+
}
30+
},
31+
{
32+
"text": ", "
33+
},
34+
{
35+
"placeholder": {
36+
"arg": 3,
37+
"access": "read"
38+
}
39+
},
40+
{
41+
"text": ")"
42+
}
43+
],
44+
"params": {
45+
"a0": {
46+
"type": "*const u8",
47+
"is_unsafe_pointer": true
48+
},
49+
"a1": {
50+
"type": "*const u8",
51+
"is_unsafe_pointer": true
52+
},
53+
"a2": {
54+
"type": "*const ::libc::addrinfo",
55+
"is_unsafe_pointer": true
56+
},
57+
"a3": {
58+
"type": "*mut *mut ::libc::addrinfo",
59+
"is_unsafe_pointer": true
60+
}
61+
},
62+
"return_type": {
63+
"type": "i32"
64+
}
65+
},
66+
"f2": {
67+
"body": [
68+
{
69+
"text": "libc::freeaddrinfo("
70+
},
71+
{
72+
"placeholder": {
73+
"arg": 0,
74+
"access": "read"
75+
}
76+
},
77+
{
78+
"text": ")"
79+
}
80+
],
81+
"params": {
82+
"a0": {
83+
"type": "*mut ::libc::addrinfo",
84+
"is_unsafe_pointer": true
85+
}
86+
}
87+
}
88+
}

rules/netdb/src.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright (c) 2022-present INESC-ID.
2+
// Distributed under the MIT license that can be found in the LICENSE file.
3+
4+
#include <netdb.h>
5+
6+
int f1(const char *node, const char *service, const struct addrinfo *hints,
7+
struct addrinfo **res) {
8+
return getaddrinfo(node, service, hints, res);
9+
}
10+
11+
void f2(struct addrinfo *res) { return freeaddrinfo(res); }

rules/netdb/tgt_unsafe.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright (c) 2022-present INESC-ID.
2+
// Distributed under the MIT license that can be found in the LICENSE file.
3+
4+
unsafe fn f1(
5+
a0: *const u8,
6+
a1: *const u8,
7+
a2: *const ::libc::addrinfo,
8+
a3: *mut *mut ::libc::addrinfo,
9+
) -> i32 {
10+
libc::getaddrinfo(a0 as *const i8, a1 as *const i8, a2, a3)
11+
}
12+
13+
unsafe fn f2(a0: *mut ::libc::addrinfo) {
14+
libc::freeaddrinfo(a0)
15+
}

tests/unit/netdb.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// no-compile: refcount
2+
#include <assert.h>
3+
#include <netdb.h>
4+
#include <stddef.h>
5+
#include <string.h>
6+
#include <sys/socket.h>
7+
8+
static void test_getaddrinfo(void) {
9+
struct addrinfo hints;
10+
memset(&hints, 0, sizeof(hints));
11+
hints.ai_family = AF_INET;
12+
hints.ai_socktype = SOCK_STREAM;
13+
hints.ai_flags = AI_NUMERICHOST;
14+
struct addrinfo *res = NULL;
15+
assert(getaddrinfo("127.0.0.1", "80", &hints, &res) == 0);
16+
assert(res != NULL);
17+
assert(res->ai_family == AF_INET);
18+
assert(res->ai_socktype == SOCK_STREAM);
19+
freeaddrinfo(res);
20+
}
21+
22+
int main(void) {
23+
test_getaddrinfo();
24+
return 0;
25+
}

0 commit comments

Comments
 (0)