Skip to content

Commit

Permalink
Add enum value negative check
Browse files Browse the repository at this point in the history
There was one already at parse time, this adds a check later so that
cases like overflows or internal enums with negative values get caught.
  • Loading branch information
evantypanski committed Sep 17, 2024
1 parent d3dd8a1 commit 08348cd
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Type.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1542,6 +1542,12 @@ void EnumType::CheckAndAddName(const string& module_name, const char* name, zeek
return;
}

if ( val < 0 ) {
reporter->Error("enumerator value cannot be negative");
SetError();
return;
}

auto fullname = detail::make_full_var_name(module_name.c_str(), name);
auto id = id::find(fullname);

Expand Down
3 changes: 3 additions & 0 deletions testing/btest/Baseline/language.enum-negative/output
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
error in <...>/enum-negative.zeek, line 5: enumerator is not a count constant
error in <...>/enum-negative.zeek, line 6: enumerator value cannot be negative
7 changes: 7 additions & 0 deletions testing/btest/language/enum-negative.zeek
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# @TEST-EXEC-FAIL: zeek -b %INPUT >output 2>&1
# @TEST-EXEC: TEST_DIFF_CANONIFIER="$SCRIPTS/diff-remove-abspath" btest-diff output

type my_enum: enum {
explicitly_negative = -1,
overflow = 9223372036854775808,
};

0 comments on commit 08348cd

Please sign in to comment.