Skip to content

Commit

Permalink
Fix bitsize of cmsgHdrLen and msgCtrlLen on Posix
Browse files Browse the repository at this point in the history
These fields are size_t, so using CInt on LP64 only accesses 32 bits
of the total 64.  This is especially noticable on big endian, where
the lower 32 bits of the number are written to the upper 32 bits of
the field.
  • Loading branch information
zeldin committed Aug 22, 2022
1 parent 53519f0 commit 206fb15
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 10 deletions.
6 changes: 3 additions & 3 deletions Network/Socket/Posix/CmsgHdr.hsc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import Network.Socket.Posix.MsgHdr
import Network.Socket.Types

data CmsgHdr = CmsgHdr {
cmsgHdrLen :: !CInt
cmsgHdrLen :: !CSize
, cmsgHdrLevel :: !CInt
, cmsgHdrType :: !CInt
} deriving (Eq, Show)
Expand Down Expand Up @@ -96,7 +96,7 @@ foreign import ccall unsafe "cmsg_data"
c_cmsg_data :: Ptr CmsgHdr -> IO (Ptr Word8)

foreign import ccall unsafe "cmsg_space"
c_cmsg_space :: CInt -> CInt
c_cmsg_space :: CSize -> CSize

foreign import ccall unsafe "cmsg_len"
c_cmsg_len :: CInt -> CInt
c_cmsg_len :: CSize -> CSize
2 changes: 1 addition & 1 deletion Network/Socket/Posix/MsgHdr.hsc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ data MsgHdr sa = MsgHdr
, msgIov :: !(Ptr IOVec)
, msgIovLen :: !CSize
, msgCtrl :: !(Ptr Word8)
, msgCtrlLen :: !CInt
, msgCtrlLen :: !CSize
, msgFlags :: !CInt
}

Expand Down
4 changes: 2 additions & 2 deletions cbits/cmsg.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ unsigned char *cmsg_data(struct cmsghdr *cmsg) {
return (CMSG_DATA(cmsg));
}

int cmsg_space(int l) {
size_t cmsg_space(size_t l) {
return (CMSG_SPACE(l));
}

int cmsg_len(int l) {
size_t cmsg_len(size_t l) {
return (CMSG_LEN(l));
}
#endif /* _WIN32 */
8 changes: 4 additions & 4 deletions include/HsNet.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,11 @@ cmsg_nxthdr(struct msghdr *mhdr, struct cmsghdr *cmsg);
extern unsigned char *
cmsg_data(struct cmsghdr *cmsg);

extern int
cmsg_space(int l);
extern size_t
cmsg_space(size_t l);

extern int
cmsg_len(int l);
extern size_t
cmsg_len(size_t l);
#endif /* _WIN32 */

INLINE int
Expand Down

0 comments on commit 206fb15

Please sign in to comment.