mirror of
https://github.com/zeek/zeek.git
synced 2025-10-02 06:38:20 +00:00
binpac: Optimize negative string length check.
Strings with a constant &length expression can be checked for negative length values while generating the parser instead of in the parser itself (which likely just ends up being dead code).
This commit is contained in:
parent
13e14768da
commit
201b43f3be
1 changed files with 20 additions and 6 deletions
|
@ -280,12 +280,26 @@ void StringType::DoGenParseCode(Output* out_cc, Env* env,
|
|||
if ( ! anonymous_value_var() )
|
||||
{
|
||||
// Set the value variable
|
||||
out_cc->println("// check for negative sizes");
|
||||
out_cc->println("if ( %s < 0 )",
|
||||
str_size.c_str());
|
||||
out_cc->println(
|
||||
"throw binpac::ExceptionInvalidStringLength(\"%s\", %s);",
|
||||
Location(), str_size.c_str());
|
||||
|
||||
int len;
|
||||
|
||||
if ( type_ == ANYSTR && attr_length_expr_ &&
|
||||
attr_length_expr_->ConstFold(env, &len) )
|
||||
{
|
||||
// can check for a negative length now
|
||||
if ( len < 0 )
|
||||
throw Exception(this, "negative &length on string");
|
||||
}
|
||||
else
|
||||
{
|
||||
out_cc->println("// check for negative sizes");
|
||||
out_cc->println("if ( %s < 0 )",
|
||||
str_size.c_str());
|
||||
out_cc->println(
|
||||
"throw binpac::ExceptionInvalidStringLength(\"%s\", %s);",
|
||||
Location(), str_size.c_str());
|
||||
}
|
||||
|
||||
out_cc->println("%s.init(%s, %s);",
|
||||
env->LValue(value_var()),
|
||||
data.ptr_expr(),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue