Module Name:    src
Committed By:   kamil
Date:           Tue Jun 12 21:22:47 UTC 2018

Modified Files:
        src/external/gpl2/grep/dist/src: dfa.c

Log Message:
Prevent Undefined Behavior in shift of signed integer in grep(1)

There is an interface in grep: dfa.c with functions to test bit, set bit,
clear bit etc. They operate over the INT-wide mask (INTBITS). Use unsigned
shift in these interfaces to prevent UB.

Detected with MKSANITIZER with the undefined behavior sanitizer option.

Sponsored by <The NetBSD Foundation>


To generate a diff of this commit:
cvs rdiff -u -r1.2 -r1.3 src/external/gpl2/grep/dist/src/dfa.c

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

Modified files:

Index: src/external/gpl2/grep/dist/src/dfa.c
diff -u src/external/gpl2/grep/dist/src/dfa.c:1.2 src/external/gpl2/grep/dist/src/dfa.c:1.3
--- src/external/gpl2/grep/dist/src/dfa.c:1.2	Sun Jan 10 22:16:40 2016
+++ src/external/gpl2/grep/dist/src/dfa.c	Tue Jun 12 21:22:47 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: dfa.c,v 1.2 2016/01/10 22:16:40 christos Exp $	*/
+/*	$NetBSD: dfa.c,v 1.3 2018/06/12 21:22:47 kamil Exp $	*/
 
 /* dfa.c - deterministic extended regexp routines for GNU
    Copyright 1988, 1998, 2000 Free Software Foundation, Inc.
@@ -223,19 +223,19 @@ prtok (token t)
 static int
 tstbit (unsigned b, charclass c)
 {
-  return c[b / INTBITS] & 1 << b % INTBITS;
+  return c[b / INTBITS] & 1U << b % INTBITS;
 }
 
 static void
 setbit (unsigned b, charclass c)
 {
-  c[b / INTBITS] |= 1 << b % INTBITS;
+  c[b / INTBITS] |= 1U << b % INTBITS;
 }
 
 static void
 clrbit (unsigned b, charclass c)
 {
-  c[b / INTBITS] &= ~(1 << b % INTBITS);
+  c[b / INTBITS] &= ~(1U << b % INTBITS);
 }
 
 static void
@@ -2203,7 +2203,7 @@ dfastate (int s, struct dfa *d, int tran
       /* Set the transitions for each character in the current label. */
       for (j = 0; j < CHARCLASS_INTS; ++j)
 	for (k = 0; k < INTBITS; ++k)
-	  if (labels[i][j] & 1 << k)
+	  if (labels[i][j] & 1U << k)
 	    {
 	      int c = j * INTBITS + k;
 

Reply via email to