-
Notifications
You must be signed in to change notification settings - Fork 1.1k
libc should let you manage errno #47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
There's also some discussion at rust-lang/rust#18642 |
+1 for I believe there are several C APIs for which checking |
I'd like us to fix this too. Another use case is writing libraries in Rust that expose a C ABI interface using |
In nix, we have an |
This library's scope is now defined by an RFC so if these sorts of function as such exist in libc on various platforms then we can certainly bind them, otherwise it'll be up to an external crate to add extra "rustic handling" for these functions. Currently we don't necessarily keep an issue for all APIs we'd like to implement, so I'm going to close this, but PRs are always welcome! |
I assume you didn't mean a "C function" for which POSIX states
(emphasis mine.) So assignment to the lvalue So the topic is not about "rustic handling." It's about whether you can handle it or not with the libc crate. And again, because the original C interface based on a "lvalue" doesn't readily map to Rust, it is definitely worth having a discussion on the interface here. You shouldn't simply close this ticket. I'd even say it is Do you think it makes more sense to define a Rust macro which closely mimics the typical extern {
fn malloc(n: usize) -> *mut i8; // just for an experiment
pub fn __errno_location() -> *mut i32; // implementation detail, but it has to be pub
}
macro_rules! errno {
() => (
*__errno_location()
)
}
fn main() {
println!("{:?}", unsafe { malloc(1 << 63) }); // sets errno
println!("{}", unsafe { errno!() });
unsafe { errno!() = 0 };
println!("{}", unsafe { errno!() });
} |
Strongly agree that any library whose scope includes access to standard C library functions should include a way to set
and C99:
don't merely permit modifying errno, they specify that applications and user-created libraried should do it under certain situations. Furthermore, neither of them permits referencing In every implementation I know of, So, given that the scope of libc in RFC1291 includes "C macros" and "C statics", and errno is usually one or the other of those, and the specified interface to errno allows and in some cases requires assigning to errno, I would argue that our binding to errno is objectively incomplete and this is a needed improvement. |
Can crate libc even implement setting errno properly without compiling a C shim for it? Without shim it requires all the platforms to provide something like |
|
|
Without these functions, Rust programs cannot inspect errors returned by functions defined in `libc`. From POSIX [0]: > An application that needs to examine the value of errno to determine > the error should set it to 0 before a function call, then inspect it > before a subsequent function call. From the C standard [1]: > Thus, a program that uses errno for error checking should set it to > zero before a library function call, then inspect it before a > subsequent library function call. While most `libc` functions return a sentinel value to indicate an error, not all do. For example, POSIC `strtol` has an error condition that can only be discovered by setting and reading `errno` [2]: > If the value of base is not supported, 0 shall be returned and errno > shall be set to [EINVAL]. Refs rust-lang/libc#47 [0] http://pubs.opengroup.org/onlinepubs/9699919799/functions/errno.html#tag_16_110_07 [1] §7.5, footnote 202 of ISO/IEC 9899:2011, WG14 draft version N1570 (http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf) [2] http://pubs.opengroup.org/onlinepubs/9699919799/functions/strtol.html#tag_16_590_04
Without these functions, Rust programs cannot inspect errors returned by functions defined in `libc`. From POSIX [0]: > An application that needs to examine the value of errno to determine > the error should set it to 0 before a function call, then inspect it > before a subsequent function call. From the C standard [1]: > Thus, a program that uses errno for error checking should set it to > zero before a library function call, then inspect it before a > subsequent library function call. While most `libc` functions return a sentinel value to indicate an error, not all do. For example, POSIC `strtol` has an error condition that can only be discovered by setting and reading `errno` [2]: > If the value of base is not supported, 0 shall be returned and errno > shall be set to [EINVAL]. Refs rust-lang/libc#47 [0] http://pubs.opengroup.org/onlinepubs/9699919799/functions/errno.html#tag_16_110_07 [1] §7.5, footnote 202 of ISO/IEC 9899:2011, WG14 draft version N1570 (http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf) [2] http://pubs.opengroup.org/onlinepubs/9699919799/functions/strtol.html#tag_16_590_04
Since the issue is that libc scope as defined in RFC 1291 doesn't include anything about |
Thanks @kamalmarhubi! I think that's the best next step here. |
Is this still valid or can we move https://github.com/lambda-fairy/rust-errno here? |
Originally filed as rust-lang/rust#17714
The text was updated successfully, but these errors were encountered: