From 201b43f3bee9d9f5565467f2fa6fc78b9942df6d Mon Sep 17 00:00:00 2001 From: Jon Siwek Date: Wed, 25 Sep 2013 15:03:43 -0500 Subject: [PATCH] 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). --- tools/binpac/src/pac_strtype.cc | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/tools/binpac/src/pac_strtype.cc b/tools/binpac/src/pac_strtype.cc index ebb9df4803..2a80c005d7 100644 --- a/tools/binpac/src/pac_strtype.cc +++ b/tools/binpac/src/pac_strtype.cc @@ -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(),