#28 xcalloc 0 elements fail

Dan Nguyen

xcalloc wraps around a call to calloc, and then checks
the pointer returned. However if calloc is called with
either elements or element size set to 0. The return
value is implementation defined.


"If either nelem or elsize is 0, then either a null pointer
or a unique pointer value that can be successfully
passed to free() shall be returned. "

On some platforms, the tests will fail because it thinks
the calloc of 0 elements returned NULL, which is not an
error condition.

The only portable way to detect the error condition is to
check the errno value to see if it ENOMEM.


    • status: open --> pending
  • Logged In: YES

    What version of bogofilter do you refer to?

    The CVS version (2002-12-06) looks fine to me, even though it doesn't check ENOMEM, it won't fall prey to NULL == calloc(0,x) or NULL == calloc(x,0). It's ugly, but that's the way autoconf recommends.

    6 void
    7 *xcalloc(size_t nmemb, size_t size){
    8 void *x;
    9 x = calloc(nmemb, size);
    10 if (x == NULL && (nmemb == 0 || size == 0))
    11 x = calloc(1, 1);
    12 if (x == NULL) {
    13 xmem_error("xcalloc");
    14 }
    15 return x;
    16 }

    Also available from: http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/bogofilter/bogofilter/xcalloc.c?rev=1.2&content-type=text/vnd.viewcvs-markup

  • Dan Nguyen
    Dan Nguyen

    Logged In: YES

    This is This should fix the problem thanks.

  • Dan Nguyen
    Dan Nguyen

    • status: pending --> closed
    • status: closed --> closed-out-of-date
  • Logged In: YES

    Yes, still had the non-portable xcalloc code that falsely aborted the program when xcalloc(0,0) returned NULL.

    Please, before reporting the next problem if you have a detailed analysis, look into the CVS repository (you can browse it online) if the latest version fixes the problem. If you cannot spare the time or don't know the exact location of the defect, then please remember to include the bogofilter version number in your report.

    JFTR: bogofilter -V will print it.