Source-Changes-HG archive

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

[src/trunk]: src/external/gpl2/grep/dist/src Prevent Undefined Behavior in sh...



details:   https://anonhg.NetBSD.org/src/rev/4fefe40f0fe4
branches:  trunk
changeset: 319818:4fefe40f0fe4
user:      kamil <kamil%NetBSD.org@localhost>
date:      Tue Jun 12 21:22:47 2018 +0000

description:
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>

diffstat:

 external/gpl2/grep/dist/src/dfa.c |  10 +++++-----
 1 files changed, 5 insertions(+), 5 deletions(-)

diffs (41 lines):

diff -r 166abad952fe -r 4fefe40f0fe4 external/gpl2/grep/dist/src/dfa.c
--- a/external/gpl2/grep/dist/src/dfa.c Tue Jun 12 20:27:54 2018 +0000
+++ b/external/gpl2/grep/dist/src/dfa.c Tue Jun 12 21:22:47 2018 +0000
@@ -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 @@
 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 @@
       /* 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;
 



Home | Main Index | Thread Index | Old Index