Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CAPLrYETqNm=OiLdTZ2EE66N411vXjNTjT+QjKBMy6-S2PX-gFw@mail.gmail.com>
Date: Mon, 19 May 2014 18:30:21 +0200
From: Daniel Cegiełka <daniel.cegielka@...il.com>
To: musl@...ts.openwall.com
Subject: Re: thoughts on reallocarray, explicit_bzero?

2014-05-19 18:16 GMT+02:00 Rich Felker <dalias@...c.org>:

>>  _Noreturn void abort (void);
>>  int atexit (void (*) (void));
>> diff -urN musl.orig/src/stdlib/reallocarray.c musl/src/stdlib/reallocarray.c
>> --- musl.orig/src/stdlib/reallocarray.c       Thu Jan  1 00:00:00 1970
>> +++ musl/src/stdlib/reallocarray.c    Thu May  8 09:06:30 2014
>> @@ -0,0 +1,17 @@
>> +#include <stdlib.h>
>> +#include <limits.h>
>> +#include <errno.h>
>> +
>> +/* this is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX
>> + * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW */
>> +#define MUL_NO_OVERFLOW      (1UL << (sizeof(size_t) * 4))
>> +
>> +void *reallocarray(void *optr, size_t nmemb, size_t size)
>> +{
>> +     if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) &&
>> +         nmemb > 0 && SSIZE_MAX / nmemb < size) {
>> +             errno = ENOMEM;
>> +             return NULL;
>> +     }
>> +     return realloc(optr, size * nmemb);
>> +}
>
> While it's a bit ugly, if your goal is efficiency, it makes a lot more
> sense to special-case 32-bit systems and do a 32x32 -> 64 multiply.
> This makes it so you don't need division code or any extra branches.
> And for 64-bit systems, either nmemb or size being >32bit would be a
> pathological corner case (and very slow already anyway), so your check
> is efficient.

It was a quick fix only from malloc.c from openbsd :) I use a lot of
programs from openbsd and I had problems with the compilation.

>
> Also, is there a reason you're using SSIZE_MAX? SIZE_MAX should work
> just as well here, but functionally it makes no difference.

yes, I should correct it:
http://www.openbsd.org/cgi-bin/cvsweb/src/lib/libc/stdlib/reallocarray.c?rev=1.1;content-type=text%2Fplain

Thanks,
Daniel

> Rich

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.