Skip to content

Commit 8684336

Browse files
committed
Add rules for locale and netdb
1 parent dcdc925 commit 8684336

6 files changed

Lines changed: 143 additions & 0 deletions

File tree

rules/locale/ir_unsafe.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"f1": {
3+
"body": [
4+
{
5+
"text": "libc::setlocale("
6+
},
7+
{
8+
"placeholder": {
9+
"arg": 0,
10+
"access": "read"
11+
}
12+
},
13+
{
14+
"text": ", "
15+
},
16+
{
17+
"placeholder": {
18+
"arg": 1,
19+
"access": "read"
20+
}
21+
},
22+
{
23+
"text": " as *const i8) as *mut u8"
24+
}
25+
],
26+
"params": {
27+
"a0": {
28+
"type": "i32"
29+
},
30+
"a1": {
31+
"type": "*const u8",
32+
"is_unsafe_pointer": true
33+
}
34+
},
35+
"return_type": {
36+
"type": "*mut u8",
37+
"is_unsafe_pointer": true
38+
}
39+
}
40+
}

rules/locale/src.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Copyright (c) 2022-present INESC-ID.
2+
// Distributed under the MIT license that can be found in the LICENSE file.
3+
4+
#include <locale.h>
5+
6+
char *f1(int category, const char *locale) {
7+
return setlocale(category, locale);
8+
}

rules/locale/tgt_unsafe.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
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(a0: i32, a1: *const u8) -> *mut u8 {
5+
libc::setlocale(a0, a1 as *const i8) as *mut u8
6+
}

tests/unit/locale.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// no-compile: refcount
2+
#include <assert.h>
3+
#include <locale.h>
4+
#include <stddef.h>
5+
#include <string.h>
6+
7+
static void test_setlocale(void) {
8+
char *cur = setlocale(LC_ALL, NULL);
9+
assert(cur != NULL);
10+
assert(strcmp(cur, "C") == 0);
11+
}
12+
13+
int main(void) {
14+
test_setlocale();
15+
return 0;
16+
}

tests/unit/out/unsafe/locale.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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 unsafe fn test_setlocale_0() {
10+
let mut cur: *mut u8 = libc::setlocale(6, std::ptr::null() as *const i8) as *mut u8;
11+
assert!((((!((cur).is_null())) as i32) != 0));
12+
assert!(
13+
((((libc::strcmp(
14+
(cur).cast_const() as *const i8,
15+
(b"C\0".as_ptr().cast_mut()).cast_const() as *const i8
16+
)) == (0)) as i32)
17+
!= 0)
18+
);
19+
}
20+
pub fn main() {
21+
unsafe {
22+
std::process::exit(main_0() as i32);
23+
}
24+
}
25+
unsafe fn main_0() -> i32 {
26+
(unsafe { test_setlocale_0() });
27+
return 0;
28+
}

tests/unit/out/unsafe/netdb.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 unsafe fn test_getaddrinfo_0() {
10+
let mut hints: addrinfo = std::mem::zeroed::<addrinfo>();
11+
{
12+
let byte_0 =
13+
((&mut hints as *mut addrinfo) as *mut addrinfo as *mut ::libc::c_void) as *mut u8;
14+
for offset in 0..::std::mem::size_of::<addrinfo>() as u64 {
15+
*byte_0.offset(offset as isize) = 0 as u8;
16+
}
17+
((&mut hints as *mut addrinfo) as *mut addrinfo as *mut ::libc::c_void)
18+
};
19+
hints.ai_family = 2;
20+
hints.ai_socktype = libc::SOCK_STREAM;
21+
hints.ai_flags = 4;
22+
let mut res: *mut addrinfo = std::ptr::null_mut();
23+
assert!(
24+
((((libc::getaddrinfo(
25+
(b"127.0.0.1\0".as_ptr().cast_mut()).cast_const() as *const i8,
26+
(b"80\0".as_ptr().cast_mut()).cast_const() as *const i8,
27+
(&mut hints as *mut addrinfo).cast_const(),
28+
(&mut res as *mut *mut addrinfo)
29+
)) == (0)) as i32)
30+
!= 0)
31+
);
32+
assert!((((!((res).is_null())) as i32) != 0));
33+
assert!((((((*res).ai_family) == (2)) as i32) != 0));
34+
assert!((((((*res).ai_socktype) == (libc::SOCK_STREAM)) as i32) != 0));
35+
libc::freeaddrinfo(res);
36+
}
37+
pub fn main() {
38+
unsafe {
39+
std::process::exit(main_0() as i32);
40+
}
41+
}
42+
unsafe fn main_0() -> i32 {
43+
(unsafe { test_getaddrinfo_0() });
44+
return 0;
45+
}

0 commit comments

Comments
 (0)