Subject: [PATCH] dfa: avoid undefined behavior of "1 << 31"

From: Jim Meyering <[email-masked]>
Date: Mon, 18 Nov 2013 17:54:56 -0800

FYI:
From 6e618d5f57916d53f70d77186feea5b265751ab1 Mon Sep 17 00:00:00 2001
From: Jim Meyering <[email-masked]>
Date: Mon, 18 Nov 2013 17:53:33 -0800
Subject: [PATCH] dfa: avoid undefined behavior of "1 << 31"

* src/dfa.c (tstbit, setbit, clrbit, dfastate): Don't shift "1"
(aka (int)1) left by 31 bits.  Instead, use "1U" as the operand,
to avoid undefined behavior.  Spotted by gcc's new -fsanitize=undefined.
---
 src/dfa.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/src/dfa.c b/src/dfa.c
index 92c410e..1fae558 100644
--- a/src/dfa.c
+++ b/src/dfa.c
@@ -550,19 +550,19 @@ prtok (token t)
 static int
 tstbit (unsigned int b, charclass const c)
 {
-  return c[b / INTBITS] & 1 << b % INTBITS;
+  return c[b / INTBITS] & 1U << b % INTBITS;
 }

 static void
 setbit (unsigned int b, charclass c)
 {
-  c[b / INTBITS] |= 1 << b % INTBITS;
+  c[b / INTBITS] |= 1U << b % INTBITS;
 }

 static void
 clrbit (unsigned int b, charclass c)
 {
-  c[b / INTBITS] &= ~(1 << b % INTBITS);
+  c[b / INTBITS] &= ~(1U << b % INTBITS);
 }

 static void
@@ -2738,7 +2738,7 @@ dfastate (state_num s, struct dfa *d, state_num trans[])
       /* 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;

--
1.8.4.rc0.11.g35f5eaa