From 1edd3b7a930f6e2f2f8656ab8f946a8cc61c9d6f Mon Sep 17 00:00:00 2001 From: Artem Pavlenko Date: Fri, 1 Nov 2019 16:15:05 +0000 Subject: [PATCH] use `& mask` for array bounds clipping (provided array size is 2^n) --- include/mapnik/text/scrptrun.hpp | 5 ++++- src/text/scrptrun.cpp | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/include/mapnik/text/scrptrun.hpp b/include/mapnik/text/scrptrun.hpp index c219a2a0d..7cee74197 100644 --- a/include/mapnik/text/scrptrun.hpp +++ b/include/mapnik/text/scrptrun.hpp @@ -24,6 +24,9 @@ #include #pragma GCC diagnostic pop +const unsigned int STACK_SIZE = 1 << 7; // 2^n +const unsigned int STACK_MASK = STACK_SIZE - 1; + struct ScriptRecord { UChar32 startChar = 0; @@ -85,7 +88,7 @@ private: int32_t scriptEnd; UScriptCode scriptCode; - ParenStackEntry parenStack[128]; + ParenStackEntry parenStack[STACK_SIZE]; int32_t parenSP; static int8_t highBit(int32_t value); diff --git a/src/text/scrptrun.cpp b/src/text/scrptrun.cpp index bc16cb811..c2985397d 100644 --- a/src/text/scrptrun.cpp +++ b/src/text/scrptrun.cpp @@ -161,7 +161,7 @@ UBool ScriptRun::next() // characters above it on the stack will be poped. if (pairIndex >= 0) { if ((pairIndex & 1) == 0) { - parenSP = (++parenSP) % ARRAY_SIZE(parenStack); // avoid out-of-bounds access + parenSP = (++parenSP) & STACK_MASK; // avoid out-of-bounds access parenStack[parenSP].pairIndex = pairIndex; parenStack[parenSP].scriptCode = scriptCode; } else if (parenSP >= 0) {