id,summary,reporter,owner,description,type,status,priority,milestone,component,version,resolution,keywords,cc
17,recv and recvBufFrom in network should accept 0 for a length when currently it throws an exception,anonymous,,"See the email below for context, Johan is fixing network-bytestring, which suffers from the same thing currently.

--------------------------------------------------------------------------------
Matthew Elder <matt@mattelder.org>  Thu, Jul 16, 2009 at 1:33 PM  
To: Johan Tibell <johan.tibell@gmail.com>  
Johan,
 
I am coming upon a behavior with recv that I find a bit strange. When you pass in a length of 0 as the second argument, it raises an exception. Here is the relevant code excerpt from network-bytestring-0.1.2.1:
 
recv :: Socket         -- ^ Connected socket
     -> Int            -- ^ Maximum number of bytes to receive
     -> IO ByteString  -- ^ Data received
recv (MkSocket s _ _ _ _) nbytes
    | nbytes <= 0 = ioError (mkInvalidRecvArgError ""Network.Socket.ByteString.recv"")
    | otherwise   = createAndTrim nbytes $ recvInner s nbytes

Now, 0 is definitely an unuseful value for this function, but it is definitely not an invalid one which justifies raising an exception. When I pass 0 for the length I am simply saying ""receive nothing"". I believe it should be changed like so:
 
recv :: Socket         -- ^ Connected socket
     -> Int            -- ^ Maximum number of bytes to receive
     -> IO ByteString  -- ^ Data received
recv (MkSocket s _ _ _ _) nbytes
    | nbytes < 0  = ioError (mkInvalidRecvArgError ""Network.Socket.ByteString.recv"")
    | nbytes == 0 = return empty
    | otherwise   = createAndTrim nbytes $ recvInner s nbytes

When I tried recvFrom it has the same semantics (not allowing 0). recvFrom uses a function recvBufFrom from Network.Socket. Now if my point of view that 0 should not be an error case is correct, then this means both network and network-bytestring are wrong. 
 
What do you think? Am I wrong? Is this how recv is supposed to behave on a posix socket?
-- 
Need somewhere to put your code? http://patch-tag.com
Want to build a webapp? http://happstack.com
 
 

--------------------------------------------------------------------------------
Johan Tibell <johan.tibell@gmail.com>  Fri, Jul 17, 2009 at 7:42 AM  
To: Matthew Elder <matt@mattelder.org>  
Hi,

What I turned up after some googling and looking at the Linux sources seems to support that a zero length shouldn't be an error. I'll fix that when I get home. However, I do think the syscall to recv should be made even if the length is zero. The reasoning is that a user might want this behavior to e.g. force EOF on the socket.


recv :: Socket         -- ^ Connected socket
     -> Int            -- ^ Maximum number of bytes to receive
     -> IO ByteString  -- ^ Data received
recv (MkSocket s _ _ _ _) nbytes
    | nbytes < 0  = ioError (mkInvalidRecvArgError ""Network.Socket.ByteString.recv"")
    | otherwise   = createAndTrim nbytes $ recvInner s nbytes


I can fix network-bytestring quite easily but could you file a bug at http://trac.haskell.org/network ?

-- Johan
 
 
",defect,new,major,,network,,,,
