[Toybox] [PATCH] lib.* - xread -> ssize_t

Rob Landley rob at landley.net
Wed Dec 28 11:09:36 PST 2011


I already fixed it here.  (Sorry for the delay: road trip to visit two
neices, two nephews, two grandparents, one sister, an aunt, and an
uncle.  I'd hoped to get some programming done on vacation.  Not sure
what I was thinking...)

On 12/26/2011 03:26 PM, Nathan McSween wrote:
> # HG changeset patch
> # User Nathan McSween <nwmcsween at gmail.com>
> # Date 1324779507 0
> # Node ID e57dc63cee8188de5f9ffef881f1923ce52e6816
> # Parent  a4dcbad4f92a3a1aeacf77be0885392a959626d7
> lib.* - xread -> ssize_t
> 
> diff -r a4dcbad4f92a -r e57dc63cee81 lib/lib.c
> --- a/lib/lib.c	Mon Dec 12 23:49:55 2011 -0600
> +++ b/lib/lib.c	Sun Dec 25 02:18:27 2011 +0000
> @@ -244,7 +244,7 @@
>  }
>  
>  // Die if there's an error other than EOF.
> -size_t xread(int fd, void *buf, size_t len)
> +ssize_t xread(int fd, void *buf, size_t len)
>  {
>  	len = read(fd, buf, len);
>  	if (len < 0) perror_exit("xread");

My fix didn't change the return type, it added a temporary ssize_t
variable to store the return value of read() into, instead of reusing len.

The function can never _return_ a negative value (because we'd call
perror_exit instead), so size_t is the correct return type.  But we need
to be able to _detect_ a negative value internally, meaning testing len
< 0 can never trigger if that's unsigned.  (You'd think the compiler
would warn about this...)

xwrite() is ok because it's just testing for inequality, which should be
the case if we get an error.  (Since -1 is just about 4 billion in a 32
bit wrapround, and you can't get a chunk of memory that big on 32 bit
linux; even with the 4g/4g split and the obsolete int80 syscall type,
your binary is nonzero size and has a stack and environment.)

Thanks for the bug report: I totally missed that. :)

Rob

 1325099376.0


More information about the Toybox mailing list