don't crash when calling EnumNameXXX on sparse enum (#4982)

Make an out-of-bounds check for enum values before using them to index the
names array.  For consistency with non-sparse enums an empty string is
returned.

Fixes #4821
This commit is contained in:
Frank Benkstein
2018-10-08 23:37:35 +02:00
committed by Wouter van Oortmerssen
parent a4f9d1bfcc
commit 99fe1dc80f
6 changed files with 19 additions and 0 deletions

View File

@@ -991,6 +991,10 @@ class CppGenerator : public BaseGenerator {
code_ += "inline const char *EnumName{{ENUM_NAME}}({{ENUM_NAME}} e) {";
code_ += " if (e < " + GetEnumValUse(enum_def, *enum_def.vals.vec.front()) +
" || e > " + GetEnumValUse(enum_def, *enum_def.vals.vec.back()) +
") return \"\";";
code_ += " const size_t index = static_cast<int>(e)\\";
if (enum_def.vals.vec.front()->value) {
auto vals = GetEnumValUse(enum_def, *enum_def.vals.vec.front());