From 412dd0df01cace25012ffc8d3a82a377e16022aa Mon Sep 17 00:00:00 2001 From: Kazu Yamamoto Date: Fri, 12 Jul 2024 16:31:00 +0900 Subject: [PATCH 1/2] checking the length of ASCII string allowing trailing 0. --- Network/Socket/Types.hsc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Network/Socket/Types.hsc b/Network/Socket/Types.hsc index 13fe3a8d..ef7d2b38 100644 --- a/Network/Socket/Types.hsc +++ b/Network/Socket/Types.hsc @@ -1156,7 +1156,8 @@ 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 + when (length pathC >= unixPathMax) $ error $ "pokeSockAddr: path is too long in SockAddrUnix " <> show path <> ", length " <> show (length path) <> ", unixPathMax " <> show unixPathMax zeroMemory p $ fromIntegral $ sizeOfSockAddr sa @@ -1164,7 +1165,6 @@ pokeSockAddr p sa@(SockAddrUnix path) = do (#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 From 33b45f8a036bc9843b24085babc95bb3a4e047c9 Mon Sep 17 00:00:00 2001 From: Kazu Yamamoto Date: Thu, 18 Jul 2024 08:31:33 +0900 Subject: [PATCH 2/2] fix according to nh2's comment --- Network/Socket/Types.hsc | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Network/Socket/Types.hsc b/Network/Socket/Types.hsc index ef7d2b38..fd4d8c7b 100644 --- a/Network/Socket/Types.hsc +++ b/Network/Socket/Types.hsc @@ -1157,9 +1157,10 @@ unixPathMax = #const sizeof(((struct sockaddr_un *)NULL)->sun_path) pokeSockAddr :: Ptr a -> SockAddr -> IO () pokeSockAddr p sa@(SockAddrUnix path) = do let pathC = map castCharToCChar path - when (length pathC >= unixPathMax) $ error + 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)