LWN.net Logo

Five Pitfalls of Linux Sockets Programming (developerWorks)

Five Pitfalls of Linux Sockets Programming (developerWorks)

Posted Sep 21, 2005 14:51 UTC (Wed) by hppnq (guest, #14462)
In reply to: Five Pitfalls of Linux Sockets Programming (developerWorks) by RobSeace
Parent article: Five Pitfalls of Linux Sockets Programming (developerWorks)

I meant that there is a distinction between the actual length of the data, and the value you pass on to send(2) as the requested number of bytes to send, e.g., send(fd, NULL, 1024, 0).


(Log in to post comments)

Five Pitfalls of Linux Sockets Programming (developerWorks)

Posted Sep 21, 2005 15:09 UTC (Wed) by RobSeace (subscriber, #4435) [Link]

That example I would expect to return -1 with an errno of EFAULT, since
the NULL pointer is invalid... Alternatively, it might simply crash the
app with SIGSEGV... But, I certainly would never expect it to return 0...
Really, there is no other notion of "actual length of the data" than the
length you pass in to send()/write()... It has no other way of knowing
any other length than what you tell it... (Short of running off the end of
your valid address space, causing either EFAULT or SIGSEGV...)

Five Pitfalls of Linux Sockets Programming (developerWorks)

Posted Sep 21, 2005 15:29 UTC (Wed) by hppnq (guest, #14462) [Link]

EFAULT seems appropriate, yes; SIGSEGV would be disappointing.

Though, looking up a few send(2) manpages I cannot really conclude that NULL would be an invalid address (I consulted a Solaris manpage before my first comment * bangs head on table * and apparently Solaris doesn't care about EFAULT at all ;-).

But it's trivial to find out of course what Linux does in this case, I'll let you know unless you beat me to it. ;-)

Five Pitfalls of Linux Sockets Programming (developerWorks)

Posted Sep 21, 2005 16:24 UTC (Wed) by RobSeace (subscriber, #4435) [Link]

It does indeed fail with EFAULT on both Linux (2.4.x, at least) and QNX4.2x
(the only other system I have access to, at the moment)... On any system
where it didn't fail so, I'd expect the seg-fault... It's the only other
rational choice...

Five Pitfalls of Linux Sockets Programming (developerWorks)

Posted Sep 21, 2005 17:12 UTC (Wed) by hppnq (guest, #14462) [Link]

Alright, common sense rules. ;-) Cheers Rob!

Copyright © 2013, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds