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] | |
On 04/06/2015 10:01 AM, Florian Weimer wrote:
+ /* Avoid overflow check if both values are small. */
+ if ((nelem | size) >> (sizeof (size_t) * CHAR_BIT / 2) != 0)
+ {
+ if (nelem != 0 && size > SIZE_MAX / nelem)
+ {
+ /* Overflow. Discard the old buffer, but it must remain
+ valid to free. */
+ scratch_buffer_free (buffer);
+ scratch_buffer_init (buffer);
+ __set_errno (ENOMEM);
+ return false;
+ }
+ }
+
+ size_t new_length = nelem * size;
It's better to compute new_length first, before the overflow check, and then replace "size > SIZE_MAX / nelem" with "size != new_length / nelem". This will avoid the need for having SIZE_MAX in the executable, which should shrink the code a tad. Also, hardware integer division tends to run faster with smaller dividends, which would be the case if this change were made.
| Index Nav: | [Date Index] [Subject Index] [Author Index] [Thread Index] | |
|---|---|---|
| Message Nav: | [Date Prev] [Date Next] | [Thread Prev] [Thread Next] |