Module Name:    src
Committed By:   shm
Date:           Fri Sep  8 14:22:04 UTC 2023

Modified Files:
        src/usr.bin/mail: vars.c

Log Message:
Fixed undefined behaviour in hash()

Shift left on large int values was causing an undefined behaviour, fix it by
operating on unsigned int type instead. This patch changes behaviour of the
hash() slightly - if the computed hash is INT_MIN, the function previously
returned 0, but this case is negligible.


To generate a diff of this commit:
cvs rdiff -u -r1.18 -r1.19 src/usr.bin/mail/vars.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/mail/vars.c
diff -u src/usr.bin/mail/vars.c:1.18 src/usr.bin/mail/vars.c:1.19
--- src/usr.bin/mail/vars.c:1.18	Sat Oct 27 15:14:51 2007
+++ src/usr.bin/mail/vars.c	Fri Sep  8 14:22:04 2023
@@ -1,4 +1,4 @@
-/*	$NetBSD: vars.c,v 1.18 2007/10/27 15:14:51 christos Exp $	*/
+/*	$NetBSD: vars.c,v 1.19 2023/09/08 14:22:04 shm Exp $	*/
 
 /*
  * Copyright (c) 1980, 1993
@@ -34,7 +34,7 @@
 #if 0
 static char sccsid[] = "@(#)vars.c	8.1 (Berkeley) 6/6/93";
 #else
-__RCSID("$NetBSD: vars.c,v 1.18 2007/10/27 15:14:51 christos Exp $");
+__RCSID("$NetBSD: vars.c,v 1.19 2023/09/08 14:22:04 shm Exp $");
 #endif
 #endif /* not lint */
 
@@ -86,14 +86,12 @@ vcopy(const char str[])
 PUBLIC int
 hash(const char *name)
 {
-	int h = 0;
+	unsigned int h = 0;
 
 	while (*name) {
 		h <<= 2;
 		h += *name++;
 	}
-	if (h < 0 && (h = -h) < 0)
-		h = 0;
 	return h % HSHSIZE;
 }
 

Reply via email to