This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Re: [patch] Fix BZ 19165 -- overflow in fread / fwrite
- From: Paul Pluzhnikov <ppluzhnikov at google dot com>
- To: Paul Pluzhnikov <ppluzhnikov at google dot com>, Rich Felker <dalias at libc dot org>, GLIBC Devel <libc-alpha at sourceware dot org>, Alexander Cherepanov <ch3root at openwall dot com>, Florian Weimer <fweimer at redhat dot com>, "Joseph S. Myers" <joseph at codesourcery dot com>
- Date: Mon, 26 Oct 2015 22:11:26 -0700
- Subject: Re: [patch] Fix BZ 19165 -- overflow in fread / fwrite
- Authentication-results: sourceware.org; auth=none
- References: <CALoOobOpSFwNOqD2RbsSQ95+16=xWN=fTpDJZqgPGJPSXCDmEA at mail dot gmail dot com> <20151026200605 dot GI8645 at brightrain dot aerifal dot cx> <CALoOobPxCPN_Lwvc98CevgCJMwHa_9cURZsALsLeG+SPDSF+Xw at mail dot gmail dot com> <20151027042627 dot GA25140 at vapier dot lan>
On Mon, Oct 26, 2015 at 9:26 PM, Mike Frysinger <vapier@gentoo.org> wrote:
> seems like it'd be better:
> return __builtin_umull_overflow (a, b, &result) ? SIZE_MAX : result;
Thanks. I've fixed this in the patch that I will send next.
>
>> +#else
>> + const size_t mul_no_overflow = (size_t) 1 << 4 * sizeof (size_t);
>> + if ((a >= mul_no_overflow || b >= mul_no_overflow)
>> + && b > 1 && a > SIZE_MAX / b)
>
> should we add a __umull_overflow define to misc/sys/cdefs.h ?
> then we don't have to duplicate this logic everywhere.
In malloc/malloc.c the following trick is played to save another compare/branch:
#define HALF_INTERNAL_SIZE_T \
(((INTERNAL_SIZE_T) 1) << (8 * sizeof (INTERNAL_SIZE_T) / 2))
if (__builtin_expect ((n | elem_size) >= HALF_INTERNAL_SIZE_T, 0))
To be clear, you are proposing adding to misc/sys/cdefs.h something like:
static inline bool
__attribute__ ((__always_inline))
__umull_overflow (size_t a, size_t b)
{
#if __GNUC_PREREQ(5, 0)
size_t result;
return __builtin_umull_overflow (a, b, &result);
#else
const size_t half_size_t = (size_t) 1 << 4 * sizeof (size_t);
return __glibc_unlikely ((a|b) >= half_size_t) && b > 1 && a > SIZE_MAX / b;
#endif
}
or an equivalent macro?
(What would be the advantage of macro over inline function?)
Thanks.
--
Paul Pluzhnikov