From: Sergei Trofimovich Date: Mon, 22 Oct 2018 21:58:34 +0000 (+0100) Subject: src/dfa/closure_posix.cc: fix signed shift overflow X-Git-Tag: 1.2~330^2~1 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=1b5a5f4449d834b1a13b84a5afa787ebe3251eed;p=re2c src/dfa/closure_posix.cc: fix signed shift overflow 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 --- diff --git a/re2c/src/dfa/closure_posix.cc b/re2c/src/dfa/closure_posix.cc index a4113761..c33e7302 100644 --- a/re2c/src/dfa/closure_posix.cc +++ b/re2c/src/dfa/closure_posix.cc @@ -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(longest); + uint32_t u_leftmost = static_cast(leftmost); + uint32_t u_result = pack_u32(u_longest, u_leftmost); + return static_cast(u_result); +} + } // namespace re2c