aboutsummaryrefslogtreecommitdiffhomepage
path: root/System/Posix/User.hsc
diff options
context:
space:
mode:
authorGravatar Ian Lynagh <igloo@earth.li>2008-01-15 02:05:47 +0000
committerGravatar Ian Lynagh <igloo@earth.li>2008-01-15 02:05:47 +0000
commitd93a1e7b2bc85d34cfa92558096008c3ee385981 (patch)
tree172b6403ca377103c82a31b62345bf70503770f1 /System/Posix/User.hsc
parent832fb683ba730115d5947493d1551ddeacb13bac (diff)
Throw a proper exception if getUserEntryForName fails to find an entry
Fixes trac #2033.
Diffstat (limited to 'System/Posix/User.hsc')
-rw-r--r--System/Posix/User.hsc23
1 files changed, 14 insertions, 9 deletions
diff --git a/System/Posix/User.hsc b/System/Posix/User.hsc
index 7ec49b6..d3af7d6 100644
--- a/System/Posix/User.hsc
+++ b/System/Posix/User.hsc
@@ -55,7 +55,7 @@ import Control.Concurrent.MVar ( newMVar, withMVar )
#ifdef HAVE_GETPWENT
import Control.Exception
#endif
-#ifdef HAVE_GETGRNAM_R
+#if defined(HAVE_GETGRNAM_R) || defined(HAVE_GETPWNAM_R)
import Control.Monad
import System.IO.Error
#endif
@@ -314,17 +314,22 @@ getUserEntryForName :: String -> IO UserEntry
getUserEntryForName name = do
allocaBytes (#const sizeof(struct passwd)) $ \ppw ->
allocaBytes pwBufSize $ \pbuf ->
- alloca $ \ pppw ->
- withCString name $ \ pstr -> do
+ alloca $ \ pppw ->
+ withCString name $ \ pstr -> do
throwErrorIfNonZero_ "getUserEntryForName" $
- c_getpwnam_r pstr ppw pbuf (fromIntegral pwBufSize) pppw
- throwErrnoIfNull "getUserEntryForName" $
- peekElemOff pppw 0
- unpackUserEntry ppw
+ c_getpwnam_r pstr ppw pbuf (fromIntegral pwBufSize) pppw
+ r <- peekElemOff pppw 0
+ when (r == nullPtr) $
+ ioError $ flip ioeSetErrorString "no user name"
+ $ mkIOError doesNotExistErrorType
+ "getUserEntryForName"
+ Nothing
+ (Just name)
+ unpackUserEntry ppw
foreign import ccall unsafe "getpwnam_r"
- c_getpwnam_r :: CString -> Ptr CPasswd ->
- CString -> CSize -> Ptr (Ptr CPasswd) -> IO CInt
+ c_getpwnam_r :: CString -> Ptr CPasswd
+ -> CString -> CSize -> Ptr (Ptr CPasswd) -> IO CInt
#elif HAVE_GETPWNAM
getUserEntryForName name = do
withCString name $ \ pstr -> do