avoid potential out-of-bounds array access (undefined behaviour) + add c++ C-array size implementation

This commit is contained in:
Artem Pavlenko 2019-11-01 13:06:21 +00:00
parent 0842d0af6f
commit dec6bc0950

View file

@ -21,8 +21,13 @@
#pragma GCC diagnostic pop
#include <mapnik/text/scrptrun.hpp>
#include <cstddef>
#define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0]))
template <class T, std::size_t N>
constexpr std::size_t ARRAY_SIZE(const T (&array)[N]) noexcept
{
return N;
}
const char ScriptRun::fgClassID=0;
@ -156,7 +161,8 @@ UBool ScriptRun::next()
// characters above it on the stack will be poped.
if (pairIndex >= 0) {
if ((pairIndex & 1) == 0) {
parenStack[++parenSP].pairIndex = pairIndex;
parenSP = (++parenSP) % ARRAY_SIZE(parenStack); // avoid out-of-bounds access
parenStack[parenSP].pairIndex = pairIndex;
parenStack[parenSP].scriptCode = scriptCode;
} else if (parenSP >= 0) {
int32_t pi = pairIndex & ~1;