From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/5136 Path: news.gmane.org!not-for-mail From: Rich Felker Newsgroups: gmane.linux.lib.musl.general Subject: Re: thoughts on reallocarray, explicit_bzero? Date: Mon, 19 May 2014 12:55:23 -0400 Message-ID: <20140519165523.GP507@brightrain.aerifal.cx> References: <20140519153130.GA519@muslin> <20140519162556.GY12324@port70.net> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: ger.gmane.org 1400518550 15750 80.91.229.3 (19 May 2014 16:55:50 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Mon, 19 May 2014 16:55:50 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-5141-gllmg-musl=m.gmane.org@lists.openwall.com Mon May 19 18:55:40 2014 Return-path: Envelope-to: gllmg-musl@plane.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by plane.gmane.org with smtp (Exim 4.69) (envelope-from ) id 1WmQqe-00028T-4D for gllmg-musl@plane.gmane.org; Mon, 19 May 2014 18:55:36 +0200 Original-Received: (qmail 11926 invoked by uid 550); 19 May 2014 16:55:35 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: Original-Received: (qmail 11918 invoked from network); 19 May 2014 16:55:35 -0000 Content-Disposition: inline In-Reply-To: <20140519162556.GY12324@port70.net> User-Agent: Mutt/1.5.21 (2010-09-15) Original-Sender: Rich Felker Xref: news.gmane.org gmane.linux.lib.musl.general:5136 Archived-At: 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