Re: strdup or str_dup?

From: Sean Daley (sdaley@bbnplanet.com)
Date: 03/09/99


At 09:19 PM 03/09/1999 +0100, you wrote:
>At 06:56 PM 3/8/99 -0800, you wrote:
>>Just curious, but where in this do you see a check for NULL?
>>
>>/* Create a duplicate of a string */
>>char *str_dup(const char *source)
>>{
>>  char *new_z;
>>
>>  CREATE(new_z, char, strlen(source) + 1);
>>  return (strcpy(new_z, source));
>>}
>>
>>#define CREATE(result, type, number)  do {\
>> if ((number) * sizeof(type) <= 0) \
>>  log("SYSERR: Zero bytes or less requested at %s:%d.", __FILE__, __LINE__);
>>\
>> if (!((result) = (type *) calloc ((number), sizeof(type)))) \
>>  { perror("SYSERR: malloc failure"); abort(); } } while(0)
>>
>>I've sent a NULL here and every time it crashes.
>
>There's a NULL check in bpl15
>
Not to beat a dead horse, but I wouldn't completely consider what's
in bpl15 a NULL check.  Some sanity checking was added to the CREATE
macro it looks like, but the str_dup function wasn't touched.  It
will still crash on some/"many?" platforms if you pass a null string to
it, even before it gets to the CREATE macro.  That strlen(source)
will die if source is NULL.  This was all tested on solaris using
gcc.  Don't know what other machines will do.  Are there any
platforms that consider strlen(NULL) == 0?  I hope not, but if
so, CREATE will work since 0+1 = 1, and the sanity check will be
passed, and your strcpy will do nasty things.

I'll have to agree with Daniel on this one though.  Let the mud crash.
You'll have more incentive to fix whatever is causing the problem if
your players are constantly asking you why it keeps crashing ;)

Sean

I believe this horse is dead.


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



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