Skip to content

Commit 2b64599

Browse files
authored
Add rules for strerror_r and gettimeofday (#146)
Both strerror_r and gettimeofday have platform specific signatures. Since the IR is not indexed in git anymore, adding rules for them is easy using a combination of ifdef `__linux__/__APPLE__` + target_os `linux/macos`
1 parent ac4d76e commit 2b64599

4 files changed

Lines changed: 52 additions & 0 deletions

File tree

rules/cstring/src.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,15 @@ void *f24(const void *a0, int a1, size_t a2) { return memrchr(a0, a1, a2); }
4040
#endif
4141

4242
int f27(const char *a0, const char *a1) { return strcasecmp(a0, a1); }
43+
44+
#if defined(__linux__)
45+
char *f28(int errnum, char *buf, size_t buflen) {
46+
return strerror_r(errnum, buf, buflen);
47+
}
48+
#elif defined(__APPLE__)
49+
int f28(int errnum, char *buf, size_t buflen) {
50+
return strerror_r(errnum, buf, buflen);
51+
}
52+
#else
53+
#error "Unsupported platform for strerror_r"
54+
#endif

rules/cstring/tgt_unsafe.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,21 @@ unsafe fn f26(a0: *mut u8, a1: i32, a2: usize) -> *mut ::libc::c_void {
130130
unsafe fn f27(a0: *const u8, a1: *const u8) -> i32 {
131131
libc::strcasecmp(a0 as *const i8, a1 as *const i8)
132132
}
133+
134+
// From the man page:
135+
//
136+
// The GNU-specific strerror_r() returns a pointer to a string containing the error message. This
137+
// may be either a pointer to a string that the function stores in buf, or a pointer to some
138+
// (immutable) static string (in which case buf is unused)
139+
//
140+
// So it's not 100% correct to always return a1. But the Rust libc version only returns int.
141+
#[cfg(target_os = "linux")]
142+
unsafe fn f28(a0: i32, a1: *mut u8, a2: usize) -> *mut u8 {
143+
libc::strerror_r(a0, a1 as *mut i8, a2 as usize);
144+
a1
145+
}
146+
147+
#[cfg(target_os = "macos")]
148+
unsafe fn f28(a0: i32, a1: *mut u8, a2: usize) -> i32 {
149+
libc::strerror_r(a0, a1 as *mut i8, a2 as usize)
150+
}

rules/time/src.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,15 @@ size_t f6(char *s, size_t maxsize, const char *format, const struct tm *tp) {
2525
int f7(const char *file, const struct timeval tvp[2]) {
2626
return utimes(file, tvp);
2727
}
28+
29+
#if defined(__linux__)
30+
int f8(struct timeval *tv, struct timezone *tz) {
31+
return gettimeofday(tv, tz);
32+
}
33+
#elif defined(__APPLE__)
34+
int f8(struct timeval *tv, void *tz) {
35+
return gettimeofday(tv, tz);
36+
}
37+
#else
38+
#error "Unsupported platform for gettimeofday"
39+
#endif

rules/time/tgt_unsafe.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,13 @@ unsafe fn f6(a0: *mut u8, a1: u64, a2: *const u8, a3: *const ::libc::tm) -> u64
2424
unsafe fn f7(a0: *const u8, a1: *const ::libc::timeval) -> i32 {
2525
libc::utimes(a0 as *const i8, a1)
2626
}
27+
28+
#[cfg(target_os = "linux")]
29+
unsafe fn f8(a0: *mut libc::timeval, a1: *mut libc::timezone) -> i32 {
30+
libc::gettimeofday(a0, a1 as *mut libc::timezone)
31+
}
32+
33+
#[cfg(target_os = "macos")]
34+
unsafe fn f8(a0: *mut libc::timeval, a1: *mut libc::c_void) -> i32 {
35+
libc::gettimeofday(a0, a1)
36+
}

0 commit comments

Comments
 (0)