Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20140519165523.GP507@brightrain.aerifal.cx>
Date: Mon, 19 May 2014 12:55:23 -0400
From: Rich Felker <dalias@...c.org>
To: musl@...ts.openwall.com
Subject: Re: thoughts on reallocarray, explicit_bzero?

On Mon, May 19, 2014 at 06:25:57PM +0200, Szabolcs Nagy wrote:
> i'd use a saturated multiplication, because malloc/realloc
> are not the only places where overflowing size calculations
> may cause problems and in such cases (size_t)-1 is just as
> good as a failure and it can be added to your code without
> portability issues
> 
> static size_t sizemul(size_t a, size_t b)
> {
> 	return b>1 && a>1 && a>-1/b ? -1 : a*b;
> }

On 32-bit this can easily be optimized to just one conditional instead
of three:

uint64_t tmp = (uint64_t)a * b;
return tmp>SIZE_MAX ? SIZE_MAX : tmp;

Of course that requires an ifdef, which is perhaps ugly.

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.