]> granicus.if.org Git - re2c/commitdiff
src/dfa/closure_posix.cc: fix signed shift overflow
authorSergei Trofimovich <slyfox@gentoo.org>
Mon, 22 Oct 2018 21:58:34 +0000 (22:58 +0100)
committerSergei Trofimovich <slyfox@gentoo.org>
Mon, 22 Oct 2018 21:58:34 +0000 (22:58 +0100)
signed shift overflow is not defined by C standard.
clang++ -fsanitize=undefined detects it as:

```
src/dfa/closure_posix.cc:207:32: runtime error: left shift of negative value -1
```

This change wraps bit shift arithmetics into unsigned types.

Signed-off-by: Sergei Trofimovich <slyfox@gentoo.org>
re2c/src/dfa/closure_posix.cc

index a4113761a5922758e4e51995908fb6d7cabf3574..c33e730207326521ffe5b55d5e1f0bd5813d4385 100644 (file)
@@ -201,10 +201,19 @@ void orders(determ_context_t &ctx)
 }
 
 
-int32_t pack(int32_t longest, int32_t leftmost)
+static uint32_t pack_u32(uint32_t longest, uint32_t leftmost)
 {
     // leftmost: higher 2 bits, longest: lower 30 bits
     return longest | (leftmost << 30);
 }
 
+static int32_t pack(int32_t longest, int32_t leftmost)
+{
+    // avoid signed overflows by using unsigned arithmetics
+    uint32_t u_longest = static_cast<uint32_t>(longest);
+    uint32_t u_leftmost = static_cast<uint32_t>(leftmost);
+    uint32_t u_result = pack_u32(u_longest, u_leftmost);
+    return static_cast<int32_t>(u_result);
+}
+
 } // namespace re2c