Fix the proto-enum leaking issue (#5286)
authorVladimir Glavnyy <31897320+vglavnyy@users.noreply.github.com>
Thu, 11 Apr 2019 20:52:01 +0000 (03:52 +0700)
committerWouter van Oortmerssen <aardappel@gmail.com>
Thu, 11 Apr 2019 20:52:01 +0000 (13:52 -0700)
* Detect leak with sanitizer

* Fix proto-enum leak issue

src/idl_parser.cpp
tests/prototest/test.golden
tests/prototest/test.proto
tests/prototest/test_union.golden

index 70f6cab..159238a 100644 (file)
@@ -2035,10 +2035,18 @@ CheckedError Parser::ParseProtoDecl() {
 
     // Temp: remove any duplicates, as .fbs files can't handle them.
     for (auto it = v.begin(); it != v.end();) {
-      if (it != v.begin() && it[0]->value == it[-1]->value)
+      if (it != v.begin() && it[0]->value == it[-1]->value) {
+        auto ref = it[-1];
+        auto ev = it[0];
+        for (auto dit = enum_def->vals.dict.begin();
+             dit != enum_def->vals.dict.end(); ++dit) {
+          if (dit->second == ev) dit->second = ref;  // reassign
+        }
+        delete ev;  // delete enum value
         it = v.erase(it);
-      else
+      } else {
         ++it;
+      }
     }
   } else if (IsIdent("syntax")) {  // Skip these.
     NEXT();
index ad59295..cf861b9 100644 (file)
@@ -4,6 +4,7 @@ namespace proto.test;
 
 /// Enum doc comment.
 enum ProtoEnum : int {
+  NUL = 0,
   FOO = 1,
   /// Enum 2nd value doc comment misaligned.
   BAR = 5,
index df264a1..45ce6c0 100644 (file)
@@ -8,6 +8,7 @@ package proto.test;
 /// Enum doc comment.
 enum ProtoEnum {
   option allow_alias = true;
+  NUL = 0;
   FOO = 1;
   /// Enum 2nd value doc comment misaligned.
   BAR = 5;
index 33fa084..18270eb 100644 (file)
@@ -4,6 +4,7 @@ namespace proto.test;
 
 /// Enum doc comment.
 enum ProtoEnum : int {
+  NUL = 0,
   FOO = 1,
   /// Enum 2nd value doc comment misaligned.
   BAR = 5,