[LCP]sockets problem

Paul Gearon pag at PISoftware.com
Wed Jan 30 11:39:34 UTC 2002


On Wed, 30 Jan 2002, Greg Black wrote:

> Paul Gearon wrote lots of good stuff, but broke one rule here:

<snip>

> Although unlikely, it's possible that len will have a negative
> value after the read(), so it should not be used as an array
> index without checking.

Yeah, you're right <sheepish_grin/> but I'd already accepted that the
purpose of this code was just to "stuff around and learn", and hence I
wasn't being strict with checking return values.  I happily accept that
return codes for system calls should ALWAYS, ALWAYS be checked in real
code.  (I can be more flexible with non-system calls like printf).

The problem with accepting that you don't have to check everything is that
you still have to check *some* things.  It can be tough figuring out which
parts I can skip.  :-)

Just to cover myself I should point out that I also skipped the return
types for gethostbyname(), select(), accept(), close(), connect(),
socket(), and write().  Some of these are pretty safe (like close()) but
others have a high likelihood of failure with segfault consequences (like
gethostbyname()).

> The snippet in question should be more
> like this:
>
>     if ((len = read(sockno, buffer, 34)) < 0)
>         scream_and_die();
>     buffer[len] = 0;  /* make sure the string is null terminated! */

This actually jumps out at me because len==0 isn't being checked for.
:-)  It's fine for what this program is trying to do, but it really
highlights that TCP isn't being used in a connection-oriented manner.
UDP would be much more appropriate.

> I didn't really check the whole thing, because it was a pretty
> good discussion of the issues -- but this one jumped out at me.

Thanks for the positive comments.  I normally hate responding to queries
like this because there's always the possibility of looking the fool.
:-}

Regards,
Paul Gearon

Software Engineer                Telephone:   +61 7 3876 2188
Plugged In Software              Fax:         +61 7 3876 4899
http://www.PIsoftware.com        PGP Key available via finger

Catapultam habeo. Nisi pecuniam omnem mihi dabis, ad caput tuum saxum
immane mittam.
(Translation from latin: "I have a catapult. Give me all the money,
or I will fling an enormous rock at your head.")







More information about the linuxCprogramming mailing list