Skip to content

Commit

Permalink
Merge pull request #585 from kazu-yamamoto/sockaddr-unix-length-check
Browse files Browse the repository at this point in the history
checking the length of ASCII string allowing trailing 0.
  • Loading branch information
kazu-yamamoto authored Aug 27, 2024
2 parents 2d5ebe6 + 33b45f8 commit 8d9e69b
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions Network/Socket/Types.hsc
Original file line number Diff line number Diff line change
Expand Up @@ -1156,15 +1156,16 @@ unixPathMax = #const sizeof(((struct sockaddr_un *)NULL)->sun_path)
-- | Write the given 'SockAddr' to the given memory location.
pokeSockAddr :: Ptr a -> SockAddr -> IO ()
pokeSockAddr p sa@(SockAddrUnix path) = do
when (length path > unixPathMax) $ error
let pathC = map castCharToCChar path
len = length pathC
when (len >= unixPathMax) $ error
$ "pokeSockAddr: path is too long in SockAddrUnix " <> show path
<> ", length " <> show (length path) <> ", unixPathMax " <> show unixPathMax
<> ", length " <> show len <> ", unixPathMax " <> show unixPathMax
zeroMemory p $ fromIntegral $ sizeOfSockAddr sa
# if defined(HAVE_STRUCT_SOCKADDR_SA_LEN)
(#poke struct sockaddr_un, sun_len) p ((#const sizeof(struct sockaddr_un)) :: Word8)
# endif
(#poke struct sockaddr_un, sun_family) p ((#const AF_UNIX) :: CSaFamily)
let pathC = map castCharToCChar path
-- the buffer is already filled with nulls.
pokeArray ((#ptr struct sockaddr_un, sun_path) p) pathC
pokeSockAddr p (SockAddrInet port addr) = do
Expand Down

0 comments on commit 8d9e69b

Please sign in to comment.