XMIN(size_t, ssize_t) ?

From: Adrian Chadd <adrian_at_creative.net.au>
Date: Fri, 16 May 2008 02:49:59 +0800

A strange bug in the store update and store client code, with me to
blame.

the store client code does this:

    new_callback(cbdata, nr, XMIN(sc->copy_size, sz));

Now, sc->copy_size is size_t (unsigned); sz is ssize_t (signed);
its passing 4096 bytes (copy_size) in when sz is -1. I'm guessing
some C type promotion/casting rules are kicking in here:

violet:~ adrian$ cat test.c
#include <stdio.h>
#include <sys/types.h>

#define XMIN(x,y) ((x)<(y)? (x) : (y))

main()
{
        size_t k = 4096;
        ssize_t l = -1;

        printf("min: %d\n", XMIN(k, l));
        printf("min: %d\n", XMIN(l, k));
}
violet:~ adrian$ cc test.c -o test
violet:~ adrian$ ./test
min: 4096
min: 4096

.. which explains the strange bugs that someone is seeing with Squid-2.HEAD
and the store update stuff.

So, how should it be done? Is there a reason why sc->copy_size is a size_t
and not an ssize_t ? I don't like the idea of just typecasting it to a ssize_t
for the comparison because for some reason it might one day be huge and this'd
trigger -another- hard to find bug.

Adrian

-- 
- Xenion - http://www.xenion.com.au/ - VPS Hosting - Commercial Squid Support -
- $25/pm entry-level VPSes w/ capped bandwidth charges available in WA -
Received on Thu May 15 2008 - 18:49:48 MDT

This archive was generated by hypermail 2.2.0 : Tue Aug 05 2008 - 01:06:35 MDT