Fix humongous bug with fdopendir on mac
https://opensource.apple.com/source/Libc/Libc-1244.1.7/include/dirent.h.auto.html ccall picks the wrong one, causes crashes and truncated d_name entries. Wrapping the function in a small C shim, as well as using the CAPI, fixes this.
This commit is contained in:
parent
06b5a46cf8
commit
74d686547e
@ -1,7 +1,7 @@
|
|||||||
#include "dirutils.h"
|
#include "dirutils.h"
|
||||||
|
|
||||||
unsigned int
|
unsigned int
|
||||||
__posixdir_d_type(struct dirent* d)
|
__posixdir_d_type(struct dirent* d)
|
||||||
{
|
{
|
||||||
return(d -> d_type);
|
return(d -> d_type);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,7 +7,9 @@
|
|||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
|
|
||||||
|
|
||||||
extern unsigned int
|
extern unsigned int
|
||||||
__posixdir_d_type(struct dirent* d)
|
__posixdir_d_type(struct dirent* d)
|
||||||
;
|
;
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -10,6 +10,7 @@
|
|||||||
-- Traversal and read operations on directories.
|
-- Traversal and read operations on directories.
|
||||||
|
|
||||||
|
|
||||||
|
{-# LANGUAGE CApiFFI #-}
|
||||||
{-# LANGUAGE CPP #-}
|
{-# LANGUAGE CPP #-}
|
||||||
{-# LANGUAGE ForeignFunctionInterface #-}
|
{-# LANGUAGE ForeignFunctionInterface #-}
|
||||||
{-# LANGUAGE OverloadedStrings #-}
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
@ -173,11 +174,15 @@ foreign import ccall unsafe "__hscore_d_name"
|
|||||||
foreign import ccall unsafe "__posixdir_d_type"
|
foreign import ccall unsafe "__posixdir_d_type"
|
||||||
c_type :: Ptr CDirent -> IO DirType
|
c_type :: Ptr CDirent -> IO DirType
|
||||||
|
|
||||||
foreign import ccall "realpath"
|
foreign import capi "stdlib.h realpath"
|
||||||
c_realpath :: CString -> CString -> IO CString
|
c_realpath :: CString -> CString -> IO CString
|
||||||
|
|
||||||
foreign import ccall unsafe "fdopendir"
|
-- Using normal 'ccall' here lead to memory bugs, crashes
|
||||||
c_fdopendir :: Posix.Fd -> IO (Ptr ())
|
-- and corrupted d_name entries. It appears there are two fdopendirs:
|
||||||
|
-- https://opensource.apple.com/source/Libc/Libc-1244.1.7/include/dirent.h.auto.html
|
||||||
|
-- The capi call picks the correct one.
|
||||||
|
foreign import capi unsafe "dirent.h fdopendir"
|
||||||
|
c_fdopendir :: Posix.Fd -> IO (Ptr CDir)
|
||||||
|
|
||||||
----------------------------------------------------------
|
----------------------------------------------------------
|
||||||
-- less dodgy but still lower-level
|
-- less dodgy but still lower-level
|
||||||
|
Loading…
Reference in New Issue
Block a user