Skip to content

Commit a8040de

Browse files
committed
Add errno in the libcc2rs compat layer
1 parent 2da2141 commit a8040de

5 files changed

Lines changed: 27 additions & 15 deletions

File tree

libcc2rs/src/compat.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ unsafe extern "C" {
1111
#[cfg(target_os = "macos")]
1212
#[link_name = "malloc_size"]
1313
fn platform_malloc_size(ptr: *const c_void) -> usize;
14+
15+
#[cfg(target_os = "linux")]
16+
#[link_name = "__errno_location"]
17+
fn platform_errno_location() -> *mut i32;
18+
19+
#[cfg(target_os = "macos")]
20+
#[link_name = "__error"]
21+
fn platform_errno_location() -> *mut i32;
1422
}
1523

1624
/// # Safety
@@ -28,3 +36,7 @@ pub unsafe fn malloc_usable_size(ptr: *mut c_void) -> usize {
2836
unsafe { platform_malloc_size(ptr as *const c_void) }
2937
}
3038
}
39+
40+
pub unsafe fn cpp2rust_errno() -> *mut i32 {
41+
platform_errno_location()
42+
}

rules/errno/ir_unsafe.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"f1": {
33
"body": [
44
{
5-
"text": "libc::__errno_location()"
5+
"text": "libcc2rs::cpp2rust_errno()"
66
}
77
],
88
"return_type": {

rules/errno/src.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33

44
#include <errno.h>
55

6-
int *f1(void) { return __errno_location(); }
6+
int *f1(void) { return cpp2rust_errno(); }

rules/errno/tgt_unsafe.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
// Distributed under the MIT license that can be found in the LICENSE file.
33

44
unsafe fn f1() -> *mut i32 {
5-
libc::__errno_location()
5+
libcc2rs::cpp2rust_errno()
66
}

tests/unit/out/unsafe/errno.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,25 @@ use std::io::{Read, Seek, Write};
77
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
88
use std::rc::Rc;
99
pub unsafe fn test_errno_0() {
10-
(*libc::__errno_location()) = 0;
11-
assert!(((((*libc::__errno_location()) == (0)) as i32) != 0));
12-
(*libc::__errno_location()) = 42;
13-
assert!(((((*libc::__errno_location()) == (42)) as i32) != 0));
14-
let mut saved: i32 = (*libc::__errno_location());
10+
(*libcc2rs::cpp2rust_errno()) = 0;
11+
assert!(((((*libcc2rs::cpp2rust_errno()) == (0)) as i32) != 0));
12+
(*libcc2rs::cpp2rust_errno()) = 42;
13+
assert!(((((*libcc2rs::cpp2rust_errno()) == (42)) as i32) != 0));
14+
let mut saved: i32 = (*libcc2rs::cpp2rust_errno());
1515
assert!(((((saved) == (42)) as i32) != 0));
16-
(*libc::__errno_location()) = 0;
16+
(*libcc2rs::cpp2rust_errno()) = 0;
1717
}
1818
pub unsafe fn test_errno_preserved_across_strdup_1() {
19-
(*libc::__errno_location()) = 99;
19+
(*libcc2rs::cpp2rust_errno()) = 99;
2020
let mut d: *mut u8 =
2121
libc::strdup((b"hello\0".as_ptr().cast_mut()).cast_const() as *const i8) as *mut u8;
2222
assert!((((!((d).is_null())) as i32) != 0));
23-
assert!(((((*libc::__errno_location()) == (99)) as i32) != 0));
23+
assert!(((((*libcc2rs::cpp2rust_errno()) == (99)) as i32) != 0));
2424
libc::free((d as *mut u8 as *mut ::libc::c_void));
25-
(*libc::__errno_location()) = 0;
25+
(*libcc2rs::cpp2rust_errno()) = 0;
2626
}
2727
pub unsafe fn test_errno_from_fseek_2() {
28-
(*libc::__errno_location()) = 0;
28+
(*libcc2rs::cpp2rust_errno()) = 0;
2929
let mut r: i32 = if (match 0 {
3030
0 => (*libcc2rs::cin_unsafe()).seek(std::io::SeekFrom::Start(0_i64 as u64)),
3131
1 => (*libcc2rs::cin_unsafe()).seek(std::io::SeekFrom::Current(0_i64)),
@@ -39,8 +39,8 @@ pub unsafe fn test_errno_from_fseek_2() {
3939
-1
4040
};
4141
assert!(((((r) == (-1_i32)) as i32) != 0));
42-
assert!(((((*libc::__errno_location()) == (29)) as i32) != 0));
43-
(*libc::__errno_location()) = 0;
42+
assert!(((((*libcc2rs::cpp2rust_errno()) == (29)) as i32) != 0));
43+
(*libcc2rs::cpp2rust_errno()) = 0;
4444
}
4545
pub fn main() {
4646
unsafe {

0 commit comments

Comments
 (0)