]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
Use the | trick to save a comparison in our calloc check.
authorNick Mathewson <nickm@torproject.org>
Sun, 2 Nov 2014 16:48:08 +0000 (11:48 -0500)
committerNick Mathewson <nickm@torproject.org>
Sun, 2 Nov 2014 16:54:42 +0000 (11:54 -0500)
src/common/util.c

index 006fd804b195a8a95389a1841eb254c95bc16e17..3d81f2b530cd749242ea5eec0fb933290dd1ac06 100644 (file)
@@ -206,8 +206,15 @@ tor_malloc_zero_(size_t size DMALLOC_PARAMS)
 static INLINE int
 size_mul_check(const size_t x, const size_t y)
 {
-  return ((x < SQRT_SIZE_MAX_P1 && y < SQRT_SIZE_MAX_P1) ||
-          y == 0 || x <= SIZE_MAX / y);
+  /* This first check is equivalent to
+     (x < SQRT_SIZE_MAX_P1 && y < SQRT_SIZE_MAX_P1)
+
+     Rationale: if either one of x or y is >= SQRT_SIZE_MAX_P1, then it
+     will have some bit set in its most significant half.
+   */
+  return ((x|y) < SQRT_SIZE_MAX_P1 ||
+          y == 0 ||
+          x <= SIZE_MAX / y);
 }
 
 /** Allocate a chunk of <b>nmemb</b>*<b>size</b> bytes of memory, fill