This is the mail archive of the libc-alpha@sourceware.org mailing list for the glibc project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [patch] Fix BZ 19165 -- overflow in fread / fwrite


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]