From 9d01bfaea3befaa0e12b0b310cadc1ed036ecfe3 Mon Sep 17 00:00:00 2001 From: Wouter van Oortmerssen Date: Wed, 10 May 2017 17:21:47 -0700 Subject: [PATCH] Added error message for union values out of range. Change-Id: I481afcde6a554d1cad519ff95acac7f38a7f4ee5 Tested: on Linux. --- src/idl_parser.cpp | 4 ++++ tests/test.cpp | 1 + 2 files changed, 5 insertions(+) diff --git a/src/idl_parser.cpp b/src/idl_parser.cpp index e20c27599..78072b9ba 100644 --- a/src/idl_parser.cpp +++ b/src/idl_parser.cpp @@ -1340,6 +1340,10 @@ CheckedError Parser::ParseEnum(bool is_union, EnumDef **dest) { enum_def.vals.vec[prevsize - 1]->value >= ev.value) return Error("enum values must be specified in ascending order"); } + if (is_union) { + if (ev.value < 0 || ev.value >= 256) + return Error("union enum value must fit in a ubyte"); + } if (opts.proto_mode && Is('[')) { NEXT(); // ignore attributes on enums. diff --git a/tests/test.cpp b/tests/test.cpp index cc632bb5d..6cce142ac 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -1010,6 +1010,7 @@ void ErrorTest() { TestError("enum X:float {}", "underlying"); TestError("enum X:byte { Y, Y }", "value already"); TestError("enum X:byte { Y=2, Z=1 }", "ascending"); + TestError("union X { Y = 256 }", "must fit"); TestError("enum X:byte (bit_flags) { Y=8 }", "bit flag out"); TestError("table X { Y:int; } table X {", "datatype already"); TestError("struct X (force_align: 7) { Y:int; }", "force_align");