[CODE] CREATE() buglet

From: Dean Takemori (dean@UHHEPH.PHYS.HAWAII.EDU)
Date: 07/06/98


From the comp.lang.c FAQ <http://www.eskimo.com/~scs/C-faq/top.html>

>Question 11.26
>What should malloc(0) do? Return a null pointer or a pointer to 0 bytes?
>
> The ANSI/ISO Standard says that it may do either; the behavior is
>implementation-defined
>   References: ANSI Sec. 4.10.3
>   ISO Sec. 7.10.3
>   PCS Sec. 16.1 p. 386

A pointer to 0 bytes?  Yikes.  I was curious to see what happens
when you malloc(0) on Solaris.  Turns out Sun's library does
indeed return a non-NULL pointer.  Did some more playing around
and discovered that malloc(-100) also returns a non-NULL pointer,
which can even be used!  (It trashes random parts of memory.)

Interestingly malloc(0) causes a sigIOT on Digital Unix.

Which brings me to my point/question.  Should the CREATE/RECREATE
macros disallow this behavior?

#define CREATE(result, type, number)  do {\
  if ((number <= 0) || \
      !((result) = (type *)calloc((number),sizeof(type))))\
          { perror("malloc failure"); abort(); } } while(0)

#define RECREATE(result,type,number) do {\
  if ((number < 0) || \
      !((result)=(type*)realloc((result),sizeof(type)*(number))))\
          { perror("realloc failure"); abort(); } } while(0)

Is the overhead too high?  Should the configure script check
what malloc()ing non-positive values do on each system and
add the check to the macros if necessary?


     +------------------------------------------------------------+
     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     | http://democracy.queensu.ca/~fletcher/Circle/list-faq.html |
     +------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/15/00 PST