NetBSD-Bugs archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: bin/49487: Import reallocarray(3) from OpenBSD



The following reply was made to PR bin/49487; it has been noted by GNATS.

From: "Kamil Rytarowski" <n54%gmx.com@localhost>
To: gnats-bugs%NetBSD.org@localhost
Cc: 
Subject: Re: bin/49487: Import reallocarray(3) from OpenBSD
Date: Sat, 27 Dec 2014 03:04:47 +0100

 Hello,
 
 1)
 Actually I the autoconf code can be done better.
 
 So stop using in configure.ac:
 AC_CHECK_DECLS(reallocarray,,, [
 #include <stdlib.h>
 ])
 
 and add reallocarray to AC_CHECK_FUNCS()
 
 Then go for #if HAVE_REALLOCARRAY
 
 2)
 Instead of having local copy of this function in reallocarray.c:
 +static inline int
 +mul_overflow(const size_t a, const size_t b, size_t * const c)
 +{
 +#if (__GNUC__ - 0) > 5
 +	return __builtin_mul_overflow(a, b, c);
 +#else
 +	/*
 +	 * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX
 +	 * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW
 +	 */
 +	const size_t mul_no_overflow = (size_t)1 << (sizeof(size_t) * 4);
 +
 +	if ((a >= mul_no_overflow || b >= mul_no_overflow)
 +	    && a > 0 && SIZE_MAX / a < b)
 +		return 1;
 +
 +	*c = a * b;
 +	return 0;
 +#endif
 +}
 
 maybe go for:
 int mul_overflow(intmax_t a, intmax_t b, intmax_t max, intmax_t *c);
 int umul_overflow(uintmax_t a, uintmax_t b, uintmax_t max, uintmax_t *c);
 
 Then we will not use __builtins from edge versions of GCC... but this building block will look cleaner and reusable, including in the implementation of calloc(3). Optimizing these overflow checks (with intrinsic compiler functions) isn't a big win, as the majority of time is anyway in the kernel with pages allocation.
 
 What do you think?
 


Home | Main Index | Thread Index | Old Index