[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [PATCH 2/2] Fix signed integer overflow in hash_string
From: |
Jose E. Marchesi |
Subject: |
Re: [PATCH 2/2] Fix signed integer overflow in hash_string |
Date: |
Fri, 13 Dec 2019 01:21:38 +0100 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/26.1 (gnu/linux) |
From: Dan Čermák <address@hidden>
The product (hash * 613) exhibits a signed integer overflow, as 613 is a
signed
integer and hash gets promoted to a signed value
=> The result of this multiplication is now dependent on undefined behavior.
We eliminate the problem by explicitly casting 613 to size_t (unfortunately
there is no standard suffix for size_t like UL for unsigned long
(yet)).
Nice catch.
Applied, thanks!
---
src/pkl-env.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/pkl-env.c b/src/pkl-env.c
index 83d7b98..334168e 100644
--- a/src/pkl-env.c
+++ b/src/pkl-env.c
@@ -60,7 +60,7 @@ hash_string (const char *name)
len = strlen (name);
hash = len;
for (i = 0; i < len; i++)
- hash = ((hash * 613) + (unsigned)(name[i]));
+ hash = ((hash * (size_t)613) + (unsigned)(name[i]));
#define HASHBITS 30
hash &= (1 << HASHBITS) - 1;