catch exceptions from getEffectiveUserID

This fixes a crash when using the linux_standalone build in termux on
android.

This commit was sponsored by Jeff Goeke-Smith on Patreon.
This commit is contained in:
Joey Hess 2018-04-24 20:10:10 -04:00
parent ec7262bb87
commit 526243d6f5
No known key found for this signature in database
GPG key ID: DB12DB0FF05F8F38

View file

@ -57,10 +57,13 @@ myUserGecos = eitherToMaybe <$> myVal [] userGecos
myVal :: [String] -> (UserEntry -> String) -> IO (Either String String)
myVal envvars extract = go envvars
where
#ifndef mingw32_HOST_OS
go [] = Right . extract <$> (getUserEntryForID =<< getEffectiveUserID)
#else
go [] = return $ either Left (Right . extract) $
Left ("environment not set: " ++ show envvars)
#endif
go [] = either (const $ envnotset) (Right . extract) <$> get
go (v:vs) = maybe (go vs) (return . Right) =<< getEnv v
#ifndef mingw32_HOST_OS
-- This may throw an exception if the system doesn't have a
-- passwd file etc; don't let it crash.
get = tryNonAsync $ getUserEntryForID =<< getEffectiveUserID
#else
get = return envnotset
#endif
envnotset = Left ("environment not set: " ++ show envvars)