Source-Changes-HG archive

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

[src/trunk]: src/lib/libc/stdlib Minor alterations to reallocarr(3)



details:   https://anonhg.NetBSD.org/src/rev/e9024995e17f
branches:  trunk
changeset: 340042:e9024995e17f
user:      kamil <kamil%NetBSD.org@localhost>
date:      Thu Aug 20 22:27:49 2015 +0000

description:
Minor alterations to reallocarr(3)

Add comment about division.
Mark error branches with __predict_false().
Reduce one branch with the OR trick.

diffstat:

 lib/libc/stdlib/reallocarr.c |  16 +++++++++++-----
 1 files changed, 11 insertions(+), 5 deletions(-)

diffs (42 lines):

diff -r 63ac4d61b1ea -r e9024995e17f lib/libc/stdlib/reallocarr.c
--- a/lib/libc/stdlib/reallocarr.c      Thu Aug 20 21:41:12 2015 +0000
+++ b/lib/libc/stdlib/reallocarr.c      Thu Aug 20 22:27:49 2015 +0000
@@ -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 <joerg%NetBSD.org@localhost>.
@@ -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 @@
                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;



Home | Main Index | Thread Index | Old Index