Skip to content

Commit 60340ac

Browse files
vglavnyyaardappel
authored andcommitted
Fix the proto-enum leaking issue (google#5286)
* Detect leak with sanitizer * Fix proto-enum leak issue
1 parent 2bd4a27 commit 60340ac

File tree

4 files changed

+13
-2
lines changed

4 files changed

+13
-2
lines changed

src/idl_parser.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2035,10 +2035,18 @@ CheckedError Parser::ParseProtoDecl() {
20352035

20362036
// Temp: remove any duplicates, as .fbs files can't handle them.
20372037
for (auto it = v.begin(); it != v.end();) {
2038-
if (it != v.begin() && it[0]->value == it[-1]->value)
2038+
if (it != v.begin() && it[0]->value == it[-1]->value) {
2039+
auto ref = it[-1];
2040+
auto ev = it[0];
2041+
for (auto dit = enum_def->vals.dict.begin();
2042+
dit != enum_def->vals.dict.end(); ++dit) {
2043+
if (dit->second == ev) dit->second = ref; // reassign
2044+
}
2045+
delete ev; // delete enum value
20392046
it = v.erase(it);
2040-
else
2047+
} else {
20412048
++it;
2049+
}
20422050
}
20432051
} else if (IsIdent("syntax")) { // Skip these.
20442052
NEXT();

tests/prototest/test.golden

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ namespace proto.test;
44

55
/// Enum doc comment.
66
enum ProtoEnum : int {
7+
NUL = 0,
78
FOO = 1,
89
/// Enum 2nd value doc comment misaligned.
910
BAR = 5,

tests/prototest/test.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package proto.test;
88
/// Enum doc comment.
99
enum ProtoEnum {
1010
option allow_alias = true;
11+
NUL = 0;
1112
FOO = 1;
1213
/// Enum 2nd value doc comment misaligned.
1314
BAR = 5;

tests/prototest/test_union.golden

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ namespace proto.test;
44

55
/// Enum doc comment.
66
enum ProtoEnum : int {
7+
NUL = 0,
78
FOO = 1,
89
/// Enum 2nd value doc comment misaligned.
910
BAR = 5,

0 commit comments

Comments
 (0)