From 3df8435167b61f3fba22a74ec4041ba5565044dd Mon Sep 17 00:00:00 2001 From: Steve Lau Date: Mon, 3 Oct 2022 10:02:53 +0800 Subject: [PATCH] adopt the get[pw/gr]ent_r def used by FreeBSD --- src/unix/solarish/compat.rs | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/src/unix/solarish/compat.rs b/src/unix/solarish/compat.rs index c423e4f9bfc75..acdc20ede33ac 100644 --- a/src/unix/solarish/compat.rs +++ b/src/unix/solarish/compat.rs @@ -1,7 +1,6 @@ // Common functions that are unfortunately missing on illumos and // Solaris, but often needed by other crates. -use core::convert::TryInto; use unix::solarish::*; const PTEM: &[u8] = b"ptem\0"; @@ -177,13 +176,18 @@ pub unsafe fn getpwent_r( buflen: ::size_t, result: *mut *mut passwd, ) -> ::c_int { - let res = native_getpwent_r(pwd, buf, buflen.try_into().unwrap()); - if res.is_null() { - -1 + let old_errno = *::___errno(); + *::___errno() = 0; + *result = native_getpwent_r(pwd, buf, buflen.min(::c_int::MAX as ::size_t) as ::c_int); + + let ret = if (*result).is_null() { + *::___errno() } else { - *result = res; 0 - } + }; + *::___errno() = old_errno; + + ret } pub unsafe fn getgrent_r( @@ -192,11 +196,16 @@ pub unsafe fn getgrent_r( buflen: ::size_t, result: *mut *mut ::group, ) -> ::c_int { - let res = native_getgrent_r(grp, buf, buflen.try_into().unwrap()); - if res.is_null() { - -1 + let old_errno = *::___errno(); + *::___errno() = 0; + *result = native_getgrent_r(grp, buf, buflen.min(::c_int::MAX as ::size_t) as ::c_int); + + let ret = if (*result).is_null() { + *::___errno() } else { - *result = res; 0 - } + }; + *::___errno() = old_errno; + + ret }