diff options
author | Jeremy Huddleston Sequoia <jeremyhu@apple.com> | 2022-11-26 21:48:45 -0800 |
---|---|---|
committer | Jeremy Huddleston Sequoia <jeremyhu@apple.com> | 2022-11-26 22:36:41 -0800 |
commit | 59a952e39f186bc42e12502317981cd07930d939 (patch) | |
tree | 91b1495616f49f3ec6072f597dcb98b54699fde5 | |
parent | 84eaf7f1bdd402661a83db7ad914eb7f8d5dc627 (diff) |
This avoids undefined behavior (left shift overflow in signed integer type)
atom.c:62:16: runtime error: left shift of 1324774199 by 3 places cannot be represented in type 'int'
Signed-off-by: Jeremy Huddleston Sequoia <jeremyhu@apple.com>
-rw-r--r-- | atom.c | 32 |
1 files changed, 15 insertions, 17 deletions
@@ -44,34 +44,32 @@ typedef struct _AtomList { static AtomListPtr *hashTable; -static int hashSize, hashUsed; -static int hashMask; -static int rehash; +static unsigned hashSize, hashUsed; +static unsigned hashMask; +static unsigned rehash; static AtomListPtr *reverseMap; static size_t reverseMapSize; static Atom lastAtom; -static int -Hash(const char *string, int len) +static unsigned +Hash(const char *string, unsigned len) { - int h; + unsigned h = 0; - h = 0; while (len--) h = (h << 3) ^ *string++; - if (h < 0) - return -h; + return h; } static int ResizeHashTable(void) { - int newHashSize; - int newHashMask; + unsigned newHashSize; + unsigned newHashMask; AtomListPtr *newHashTable; - int newRehash; + unsigned newRehash; if (hashSize == 0) newHashSize = 1024; @@ -88,9 +86,9 @@ ResizeHashTable(void) newRehash = (newHashMask - 2); for (int i = 0; i < hashSize; i++) { if (hashTable[i]) { - int h = (hashTable[i]->hash) & newHashMask; + unsigned h = (hashTable[i]->hash) & newHashMask; if (newHashTable[h]) { - int r = hashTable[i]->hash % newRehash | 1; + unsigned r = hashTable[i]->hash % newRehash | 1; do { h += r; if (h >= newHashSize) @@ -143,9 +141,9 @@ Atom MakeAtom(const char *string, unsigned len, int makeit) { AtomListPtr a; - int hash; - int h = 0; - int r; + unsigned hash; + unsigned h = 0; + unsigned r; hash = Hash(string, len); if (hashTable) { |