summaryrefslogtreecommitdiff
path: root/misc/hsearch_r.c
diff options
context:
space:
mode:
authorTulio Magno Quites Machado Filho <tuliom@linux.vnet.ibm.com>2016-07-11 14:16:01 -0300
committerTulio Magno Quites Machado Filho <tuliom@linux.vnet.ibm.com>2016-07-11 14:16:01 -0300
commitbde2a94b61d1cef444d7d4b4b9db70062c48cf5d (patch)
tree5393a8c666de2fb8995df1282909c09ed17d9351 /misc/hsearch_r.c
parent3f1ff80d9a0c60398e73d62c52f9a2f06af8d61d (diff)
parent66986dec455c2011085a04b72a5bd55d9f9c7d1c (diff)
downloadglibc-ibm/2.19/master.tar.gz
Merge branch 'release/2.19/master' into ibm/2.19/masteribm/2.19/master
Conflicts: NEWS
Diffstat (limited to 'misc/hsearch_r.c')
-rw-r--r--misc/hsearch_r.c30
1 files changed, 18 insertions, 12 deletions
diff --git a/misc/hsearch_r.c b/misc/hsearch_r.c
index 81c27d800c..af5521234c 100644
--- a/misc/hsearch_r.c
+++ b/misc/hsearch_r.c
@@ -19,7 +19,7 @@
#include <errno.h>
#include <malloc.h>
#include <string.h>
-
+#include <stdint.h>
#include <search.h>
/* [Aho,Sethi,Ullman] Compilers: Principles, Techniques and Tools, 1986
@@ -46,15 +46,12 @@ static int
isprime (unsigned int number)
{
/* no even number will be passed */
- unsigned int div = 3;
-
- while (div * div < number && number % div != 0)
- div += 2;
-
- return number % div != 0;
+ for (unsigned int div = 3; div <= number / div; div += 2)
+ if (number % div == 0)
+ return 0;
+ return 1;
}
-
/* Before using the hash table we must allocate memory for it.
Test for an existing table are done. We allocate one element
more as the found prime number says. This is done for more effective
@@ -81,10 +78,19 @@ hcreate_r (nel, htab)
use will not work. */
if (nel < 3)
nel = 3;
- /* Change nel to the first prime number not smaller as nel. */
- nel |= 1; /* make odd */
- while (!isprime (nel))
- nel += 2;
+
+ /* Change nel to the first prime number in the range [nel, UINT_MAX - 2],
+ The '- 2' means 'nel += 2' cannot overflow. */
+ for (nel |= 1; ; nel += 2)
+ {
+ if (UINT_MAX - 2 < nel)
+ {
+ __set_errno (ENOMEM);
+ return 0;
+ }
+ if (isprime (nel))
+ break;
+ }
htab->size = nel;
htab->filled = 0;