[PATCH v2] Add reallocarray function.
Dennis Wölfing
denniswoelfing@gmx.de
Mon Apr 17 14:40:00 GMT 2017
On 17.04.2017 16:32, Florian Weimer wrote:
> * Dennis Wölfing:
>
>> +static inline bool
>> +check_mul_overflow (size_t l, size_t r, INTERNAL_SIZE_T *result)
>> +{
>> +#if __GNUC__ >= 5
>> + return __builtin_mul_overflow (l, r, result);
>
> This is not correct for size_t != INTERNAL_SIZE_T. Either change the
> type of result to size_t * (so that the code fails to compile if the
> types differ, something which we have not done for a long time), or
> add another check against (INTERNAL_SIZE_T) -1.
Are you sure that this is not correct? The GCC manual says about the
overflow builtins:
> These built-in functions promote the first two operands into infinite
> precision signed type and perform addition on those promoted operands.
> The result is then cast to the type the third pointer argument points
> to and stored there. If the stored result is equal to the infinite
> precision result, the built-in functions return false, otherwise they
> return true. As the addition is performed in infinite signed
> precision, these built-in functions have fully defined behavior for
> all argument values.
So the result should be correct even if the types are different.
More information about the Libc-alpha
mailing list