Skip to content

Commit 83231d0

Browse files
Hans Boehmivmai
authored andcommitted
Speedup calloc size overflow check by preventing division if small values
* malloc.c (GC_SQRT_SIZE_MAX): New macro. * malloc.c (calloc): Add fast initial size overflow check to avoid integer division for reasonably small values passed.
1 parent 6a93f8e commit 83231d0

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

malloc.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -381,9 +381,12 @@ void * malloc(size_t lb)
381381
# define GC_SIZE_MAX (~(size_t)0)
382382
#endif
383383

384+
#define GC_SQRT_SIZE_MAX ((1U << (WORDSZ / 2)) - 1)
385+
384386
void * calloc(size_t n, size_t lb)
385387
{
386-
if (lb && n > GC_SIZE_MAX / lb)
388+
if ((lb | n) > GC_SQRT_SIZE_MAX /* fast initial test */
389+
&& lb && n > GC_SIZE_MAX / lb)
387390
return NULL;
388391
# if defined(GC_LINUX_THREADS) /* && !defined(USE_PROC_FOR_LIBRARIES) */
389392
/* libpthread allocated some memory that is only pointed to by */

0 commit comments

Comments
 (0)