Module Name: src Committed By: kamil Date: Thu Aug 20 22:27:49 UTC 2015
Modified Files: src/lib/libc/stdlib: reallocarr.c Log Message: Minor alterations to reallocarr(3) Add comment about division. Mark error branches with __predict_false(). Reduce one branch with the OR trick. To generate a diff of this commit: cvs rdiff -u -r1.4 -r1.5 src/lib/libc/stdlib/reallocarr.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/lib/libc/stdlib/reallocarr.c diff -u src/lib/libc/stdlib/reallocarr.c:1.4 src/lib/libc/stdlib/reallocarr.c:1.5 --- src/lib/libc/stdlib/reallocarr.c:1.4 Thu Aug 20 20:08:04 2015 +++ src/lib/libc/stdlib/reallocarr.c Thu Aug 20 22:27:49 2015 @@ -1,4 +1,4 @@ -/* $NetBSD: reallocarr.c,v 1.4 2015/08/20 20:08:04 joerg Exp $ */ +/* $NetBSD: reallocarr.c,v 1.5 2015/08/20 22:27:49 kamil Exp $ */ /*- * Copyright (c) 2015 Joerg Sonnenberger <jo...@netbsd.org>. @@ -34,7 +34,7 @@ #endif #include <sys/cdefs.h> -__RCSID("$NetBSD: reallocarr.c,v 1.4 2015/08/20 20:08:04 joerg Exp $"); +__RCSID("$NetBSD: reallocarr.c,v 1.5 2015/08/20 22:27:49 kamil Exp $"); #include "namespace.h" #include <errno.h> @@ -70,14 +70,20 @@ reallocarr(void *ptr, size_t number, siz return 0; } - if ((number >= SQRT_SIZE_MAX || size >= SQRT_SIZE_MAX) && - number > SIZE_MAX / size) { + /* + * Try to avoid division here. + * + * It isn't possible to overflow during multiplication if neither + * operand uses any of the most significant half of the bits. + */ + if (__predict_false((number|size) >= SQRT_SIZE_MAX && + number > SIZE_MAX / size)) { errno = saved_errno; return EOVERFLOW; } nptr = realloc(optr, number * size); - if (nptr == NULL) { + if (__predict_false(nptr == NULL)) { result = errno; } else { result = 0;