Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

checking the length of ASCII string allowing trailing 0. #585

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Network/Socket/Types.hsc
Original file line number Diff line number Diff line change
Expand Up @@ -1156,15 +1156,15 @@ 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be good to change the error message to show (length pathC); currently it's equivalent since done with map, but maybe in the future we lift the constraint in the future that the path must not contain unicode chars (which is a pretty weird contraint because it should prevent anybody who uses non-ASCII chars in their home directory to place sockets there).

Otherwise, looks good!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. Please check it again.

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
Loading