poke-devel
[Top][All Lists]
Advanced

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

[PATCH 2/2] Fix signed integer overflow in hash_string


From: Dan Čermák
Subject: [PATCH 2/2] Fix signed integer overflow in hash_string
Date: Fri, 13 Dec 2019 00:08:32 +0100

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)).
---
 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;
-- 
2.23.0




reply via email to

[Prev in Thread] Current Thread [Next in Thread]