Merge remote-tracking branch 'origin/topic/timw/reformat-cpp-code-in-bison-and-flex-files'

* origin/topic/timw/reformat-cpp-code-in-bison-and-flex-files:
  Reformat embedded C++ code in bison/flex files
This commit is contained in:
Tim Wojtulewicz 2025-03-04 09:33:54 -07:00
commit 8f0236448b
8 changed files with 949 additions and 1138 deletions

View file

@ -1,3 +1,7 @@
7.2.0-dev.257 | 2025-03-04 09:33:54 -0700
* Reformat embedded C++ code in bison/flex files (Tim Wojtulewicz, Corelight)
7.2.0-dev.255 | 2025-03-04 08:52:13 -0700 7.2.0-dev.255 | 2025-03-04 08:52:13 -0700
* Fix Coverity findings from recent IPTunnel dumping changes (Tim Wojtulewicz, Corelight) * Fix Coverity findings from recent IPTunnel dumping changes (Tim Wojtulewicz, Corelight)

View file

@ -1 +1 @@
7.2.0-dev.255 7.2.0-dev.257

File diff suppressed because it is too large Load diff

View file

@ -9,7 +9,6 @@
#include "zeek/EquivClass.h" #include "zeek/EquivClass.h"
#include "zeek/Reporter.h" #include "zeek/Reporter.h"
namespace zeek::detail { namespace zeek::detail {
constexpr int csize = 256; constexpr int csize = 256;
bool re_syntax_error = 0; bool re_syntax_error = 0;
@ -69,24 +68,20 @@ singleton : singleton '*'
{ {
if ( $3 > $5 || $3 < 0 ) if ( $3 > $5 || $3 < 0 )
zeek::detail::synerr("bad iteration values"); zeek::detail::synerr("bad iteration values");
else else {
{ if ( $3 == 0 ) {
if ( $3 == 0 ) if ( $5 == 0 ) {
{
if ( $5 == 0 )
{
$$ = new zeek::detail::NFA_Machine(new zeek::detail::EpsilonState()); $$ = new zeek::detail::NFA_Machine(new zeek::detail::EpsilonState());
Unref($1); Unref($1);
} }
else else {
{
$1->MakeRepl(1, $5); $1->MakeRepl(1, $5);
$1->MakeOptional(); $1->MakeOptional();
}
} }
}
else else
$1->MakeRepl($3, $5); $1->MakeRepl($3, $5);
} }
} }
| singleton '{' TOK_NUMBER ',' '}' | singleton '{' TOK_NUMBER ',' '}'
@ -105,11 +100,10 @@ singleton : singleton '*'
{ {
if ( $3 < 0 ) if ( $3 < 0 )
zeek::detail::synerr("iteration value must be positive"); zeek::detail::synerr("iteration value must be positive");
else if ( $3 == 0 ) else if ( $3 == 0 ) {
{
Unref($1); Unref($1);
$$ = new zeek::detail::NFA_Machine(new zeek::detail::EpsilonState()); $$ = new zeek::detail::NFA_Machine(new zeek::detail::EpsilonState());
} }
else else
$1->LinkCopies($3-1); $1->LinkCopies($3-1);
} }
@ -117,7 +111,7 @@ singleton : singleton '*'
| '.' | '.'
{ {
$$ = new zeek::detail::NFA_Machine(new zeek::detail::NFA_State( $$ = new zeek::detail::NFA_Machine(new zeek::detail::NFA_State(
zeek::detail::rem->AnyCCL(zeek::detail::re_single_line))); zeek::detail::rem->AnyCCL(zeek::detail::re_single_line)));
} }
| full_ccl | full_ccl
@ -146,12 +140,11 @@ singleton : singleton '*'
{ {
auto sym = $1; auto sym = $1;
if ( sym < 0 || ( sym >= NUM_SYM && sym != SYM_EPSILON ) ) if ( sym < 0 || ( sym >= NUM_SYM && sym != SYM_EPSILON ) ) {
{
zeek::reporter->Error("bad symbol %d (compiling pattern /%s/)", sym, zeek::reporter->Error("bad symbol %d (compiling pattern /%s/)", sym,
zeek::detail::RE_parse_input); zeek::detail::RE_parse_input);
return 1; return 1;
} }
$$ = new zeek::detail::NFA_Machine(new zeek::detail::NFA_State(sym, zeek::detail::rem->EC())); $$ = new zeek::detail::NFA_Machine(new zeek::detail::NFA_State(sym, zeek::detail::rem->EC()));
} }
@ -184,40 +177,33 @@ ccl : ccl TOK_CHAR '-' TOK_CHAR
if ( $2 > $4 ) if ( $2 > $4 )
zeek::detail::synerr("negative range in character class"); zeek::detail::synerr("negative range in character class");
else if ( zeek::detail::case_insensitive && else if ( zeek::detail::case_insensitive && (isalpha($2) || isalpha($4)) ) {
(isalpha($2) || isalpha($4)) ) if ( isalpha($2) && isalpha($4) && isupper($2) == isupper($4) ) {
{ // Compatible range, do both versions
if ( isalpha($2) && isalpha($4) &&
isupper($2) == isupper($4) )
{ // Compatible range, do both versions
int l2 = tolower($2); int l2 = tolower($2);
int l4 = tolower($4); int l4 = tolower($4);
for ( int i = l2; i<= l4; ++i ) for ( int i = l2; i<= l4; ++i ) {
{
$1->Add(i); $1->Add(i);
$1->Add(toupper(i)); $1->Add(toupper(i));
}
} }
}
else else
zeek::detail::synerr("ambiguous case-insensitive character class"); zeek::detail::synerr("ambiguous case-insensitive character class");
} }
else else {
{
for ( int i = $2; i <= $4; ++i ) for ( int i = $2; i <= $4; ++i )
$1->Add(i); $1->Add(i);
} }
} }
| ccl TOK_CHAR | ccl TOK_CHAR
{ {
if ( zeek::detail::case_insensitive && isalpha($2) ) if ( zeek::detail::case_insensitive && isalpha($2) ) {
{
$1->Add(zeek::detail::clower($2)); $1->Add(zeek::detail::clower($2));
$1->Add(zeek::detail::cupper($2)); $1->Add(zeek::detail::cupper($2));
} }
else else
$1->Add($2); $1->Add($2);
} }
@ -255,12 +241,12 @@ namespace zeek::detail {
int cupper(int sym) int cupper(int sym)
{ {
return (isascii(sym) && islower(sym)) ? toupper(sym) : sym; return (isascii(sym) && islower(sym)) ? toupper(sym) : sym;
} }
int clower(int sym) int clower(int sym)
{ {
return (isascii(sym) && isupper(sym)) ? tolower(sym) : sym; return (isascii(sym) && isupper(sym)) ? tolower(sym) : sym;
} }
void synerr(const char str[]) void synerr(const char str[])

View file

@ -69,15 +69,13 @@ CCL_EXPR ("[:"[[:alpha:]]+":]")
"["({FIRST_CCL_CHAR}|{CCL_EXPR})({CCL_CHAR}|{CCL_EXPR})* { "["({FIRST_CCL_CHAR}|{CCL_EXPR})({CCL_CHAR}|{CCL_EXPR})* {
zeek::detail::curr_ccl = zeek::detail::rem->LookupCCL(yytext); zeek::detail::curr_ccl = zeek::detail::rem->LookupCCL(yytext);
if ( zeek::detail::curr_ccl ) if ( zeek::detail::curr_ccl ) {
{
if ( yyinput() != ']' ) if ( yyinput() != ']' )
zeek::detail::synerr("bad character class"); zeek::detail::synerr("bad character class");
yylval.ccl_val = zeek::detail::curr_ccl; yylval.ccl_val = zeek::detail::curr_ccl;
return TOK_CCL; return TOK_CCL;
} }
else else {
{
zeek::detail::curr_ccl = new zeek::detail::CCL(); zeek::detail::curr_ccl = new zeek::detail::CCL();
zeek::detail::rem->InsertCCL(yytext, zeek::detail::curr_ccl); zeek::detail::rem->InsertCCL(yytext, zeek::detail::curr_ccl);
@ -87,7 +85,7 @@ CCL_EXPR ("[:"[[:alpha:]]+":]")
BEGIN(SC_FIRST_CCL); BEGIN(SC_FIRST_CCL);
return '['; return '[';
} }
} }
"{"{NAME}"}" { "{"{NAME}"}" {
@ -99,48 +97,43 @@ CCL_EXPR ("[:"[[:alpha:]]+":]")
if ( namedef.empty() ) if ( namedef.empty() )
zeek::detail::synerr("undefined definition"); zeek::detail::synerr("undefined definition");
else else {
{ // push back name surrounded by ()'s // push back name surrounded by ()'s
int len = namedef.size(); int len = namedef.size();
if ( namedef[0] == '^' || if ( namedef[0] == '^' || (len > 0 && namedef[len - 1] == '$') ) {
(len > 0 && namedef[len - 1] == '$') ) // don't use ()'s after all
{ // don't use ()'s after all
for ( int i = len - 1; i >= 0; --i ) for ( int i = len - 1; i >= 0; --i )
unput(namedef[i]); unput(namedef[i]);
if ( namedef[0] == '^' ) if ( namedef[0] == '^' )
yy_set_bol(1); yy_set_bol(1);
} }
else {
else
{
unput(')'); unput(')');
for ( int i = len - 1; i >= 0; --i ) for ( int i = len - 1; i >= 0; --i )
unput(namedef[i]); unput(namedef[i]);
unput('('); unput('(');
}
} }
} }
}
"(?i:" zeek::detail::case_insensitive = true; return TOK_CASE_INSENSITIVE; "(?i:" zeek::detail::case_insensitive = true; return TOK_CASE_INSENSITIVE;
"(?s:" zeek::detail::re_single_line = true; return TOK_SINGLE_LINE; "(?s:" zeek::detail::re_single_line = true; return TOK_SINGLE_LINE;
[a-zA-Z] { [a-zA-Z] {
if ( zeek::detail::case_insensitive ) if ( zeek::detail::case_insensitive ) {
{
char c = yytext[0]; // unput trashes yytext! char c = yytext[0]; // unput trashes yytext!
// Push back the character inside a CCL, // Push back the character inside a CCL,
// so the parser can then expand it. // so the parser can then expand it.
unput(']'); unput(']');
unput(c); unput(c);
unput('['); unput('[');
} }
else else {
{
yylval.int_val = yytext[0]; yylval.int_val = yytext[0];
return TOK_CHAR; return TOK_CHAR;
} }
} }
[|*+?.(){}] return yytext[0]; [|*+?.(){}] return yytext[0];
@ -238,13 +231,11 @@ CCL_EXPR ("[:"[[:alpha:]]+":]")
YY_BUFFER_STATE RE_buf; YY_BUFFER_STATE RE_buf;
void RE_set_input(const char* str) void RE_set_input(const char* str) {
{
zeek::detail::RE_parse_input = str; zeek::detail::RE_parse_input = str;
RE_buf = yy_scan_string(str); RE_buf = yy_scan_string(str);
} }
void RE_done_with_scan() void RE_done_with_scan() {
{
yy_delete_buffer(RE_buf); yy_delete_buffer(RE_buf);
} }

View file

@ -19,17 +19,17 @@ extern void end_PS();
zeek::detail::Rule* current_rule = nullptr; zeek::detail::Rule* current_rule = nullptr;
const char* current_rule_file = nullptr; const char* current_rule_file = nullptr;
static uint8_t ip4_mask_to_len(uint32_t mask) static uint8_t ip4_mask_to_len(uint32_t mask) {
{ if ( mask == 0xffffffff )
if ( mask == 0xffffffff ) return 32;
return 32;
uint32_t x = ~mask + 1; uint32_t x = ~mask + 1;
uint8_t len; uint8_t len;
for ( len = 0; len < 32 && (! (x & (1 << len))); ++len ); for ( len = 0; len < 32 && (! (x & (1 << len))); ++len )
;
return 32 - len; return 32 - len;
} }
%} %}
%token TOK_COMP %token TOK_COMP
@ -153,20 +153,19 @@ rule_attr:
{ {
int proto = 0; int proto = 0;
switch ( $3 ) { switch ( $3 ) {
case zeek::detail::RuleHdrTest::ICMP: proto = IPPROTO_ICMP; break; case zeek::detail::RuleHdrTest::ICMP: proto = IPPROTO_ICMP; break;
case zeek::detail::RuleHdrTest::ICMPv6: proto = IPPROTO_ICMPV6; break; case zeek::detail::RuleHdrTest::ICMPv6: proto = IPPROTO_ICMPV6; break;
// signature matching against outer packet headers of IP-in-IP // signature matching against outer packet headers of IP-in-IP
// tunneling not supported, so do a no-op there // tunneling not supported, so do a no-op there
case zeek::detail::RuleHdrTest::IP: proto = 0; break; case zeek::detail::RuleHdrTest::IP: proto = 0; break;
case zeek::detail::RuleHdrTest::IPv6: proto = 0; break; case zeek::detail::RuleHdrTest::IPv6: proto = 0; break;
case zeek::detail::RuleHdrTest::TCP: proto = IPPROTO_TCP; break; case zeek::detail::RuleHdrTest::TCP: proto = IPPROTO_TCP; break;
case zeek::detail::RuleHdrTest::UDP: proto = IPPROTO_UDP; break; case zeek::detail::RuleHdrTest::UDP: proto = IPPROTO_UDP; break;
default: default:
rules_error("internal_error: unknown protocol"); rules_error("internal_error: unknown protocol");
} }
if ( proto ) if ( proto ) {
{
auto* vallist = new zeek::detail::maskedvalue_list; auto* vallist = new zeek::detail::maskedvalue_list;
auto* val = new zeek::detail::MaskedValue(); auto* val = new zeek::detail::MaskedValue();
@ -179,7 +178,7 @@ rule_attr:
current_rule->AddHdrTest(new zeek::detail::RuleHdrTest( current_rule->AddHdrTest(new zeek::detail::RuleHdrTest(
zeek::detail::RuleHdrTest::NEXT, 0, 0, zeek::detail::RuleHdrTest::NEXT, 0, 0,
(zeek::detail::RuleHdrTest::Comp) $2, vallist)); (zeek::detail::RuleHdrTest::Comp) $2, vallist));
} }
} }
| TOK_IP_PROTO TOK_COMP value_list | TOK_IP_PROTO TOK_COMP value_list
@ -303,11 +302,10 @@ value_list:
| value_list ',' ranged_value | value_list ',' ranged_value
{ {
int numVals = $3->length(); int numVals = $3->length();
for ( int idx = 0; idx < numVals; idx++ ) for ( int idx = 0; idx < numVals; idx++ ) {
{
zeek::detail::MaskedValue* val = (*$3)[idx]; zeek::detail::MaskedValue* val = (*$3)[idx];
$1->push_back(val); $1->push_back(val);
} }
$$ = $1; $$ = $1;
} }
| value_list ',' TOK_IDENT | value_list ',' TOK_IDENT
@ -364,13 +362,12 @@ ranged_value:
TOK_INT '-' TOK_INT TOK_INT '-' TOK_INT
{ {
$$ = new zeek::detail::maskedvalue_list(); $$ = new zeek::detail::maskedvalue_list();
for ( int val = $1; val <= $3; val++ ) for ( int val = $1; val <= $3; val++ ) {
{
auto* masked = new zeek::detail::MaskedValue(); auto* masked = new zeek::detail::MaskedValue();
masked->val = val; masked->val = val;
masked->mask = 0xffffffff; masked->mask = 0xffffffff;
$$->push_back(masked); $$->push_back(masked);
} }
} }
; ;
@ -447,29 +444,20 @@ pattern:
%% %%
void rules_error(const char* msg) void rules_error(const char* msg) {
{ zeek::reporter->Error("Error in signature (%s:%d): %s", current_rule_file, rules_line_number + 1, msg);
zeek::reporter->Error("Error in signature (%s:%d): %s", zeek::detail::rule_matcher->SetParseError();
current_rule_file, rules_line_number+1, msg); }
zeek::detail::rule_matcher->SetParseError();
}
void rules_error(const char* msg, const char* addl) void rules_error(const char* msg, const char* addl) {
{ zeek::reporter->Error("Error in signature (%s:%d): %s (%s)", current_rule_file, rules_line_number + 1, msg, addl);
zeek::reporter->Error("Error in signature (%s:%d): %s (%s)", zeek::detail::rule_matcher->SetParseError();
current_rule_file, rules_line_number+1, msg, addl); }
zeek::detail::rule_matcher->SetParseError();
}
void rules_error(zeek::detail::Rule* r, const char* msg) void rules_error(zeek::detail::Rule* r, const char* msg) {
{ const zeek::detail::Location& l = r->GetLocation();
const zeek::detail::Location& l = r->GetLocation(); zeek::reporter->Error("Error in signature %s (%s:%d): %s", r->ID(), l.filename, l.first_line, msg);
zeek::reporter->Error("Error in signature %s (%s:%d): %s", zeek::detail::rule_matcher->SetParseError();
r->ID(), l.filename, l.first_line, msg); }
zeek::detail::rule_matcher->SetParseError();
}
int rules_wrap(void) int rules_wrap(void) { return 1; }
{
return 1;
}

View file

@ -204,17 +204,15 @@ finger { rules_lval.val = zeek::detail::Rule::FINGER; return TOK_PATTERN_TYPE; }
{RE} { {RE} {
auto len = strlen(yytext); auto len = strlen(yytext);
if ( yytext[len - 1] == 'i' ) if ( yytext[len - 1] == 'i' ) {
{
*(yytext + len - 2) = '\0'; *(yytext + len - 2) = '\0';
const char fmt[] = "(?i:%s)"; const char fmt[] = "(?i:%s)";
int n = len + strlen(fmt); int n = len + strlen(fmt);
char* s = new char[n + 5 /* slop */]; char* s = new char[n + 5 /* slop */];
snprintf(s, n + 5, fmt, yytext + 1); snprintf(s, n + 5, fmt, yytext + 1);
rules_lval.str = s; rules_lval.str = s;
} }
else else {
{
*(yytext + len - 1) = '\0'; *(yytext + len - 1) = '\0';
rules_lval.str = zeek::util::copy_string(yytext + 1); rules_lval.str = zeek::util::copy_string(yytext + 1);
} }
@ -227,31 +225,26 @@ finger { rules_lval.val = zeek::detail::Rule::FINGER; return TOK_PATTERN_TYPE; }
%% %%
// We're about to parse a Zeek policy-layer symbol. // We're about to parse a Zeek policy-layer symbol.
void begin_PS() void begin_PS() {
{ BEGIN(PS);
BEGIN(PS); }
}
void end_PS() void end_PS() {
{ BEGIN(INITIAL);
BEGIN(INITIAL); }
}
static YY_BUFFER_STATE rules_buffer; static YY_BUFFER_STATE rules_buffer;
void rules_set_input_from_buffer(const char* data, size_t size) void rules_set_input_from_buffer(const char* data, size_t size) {
{ rules_buffer = yy_scan_bytes(data, size); // this copies the data
rules_buffer = yy_scan_bytes(data, size); // this copies the data }
}
void rules_set_input_from_file(FILE* f) void rules_set_input_from_file(FILE* f) {
{ rules_buffer = yy_create_buffer(f, YY_BUF_SIZE);
rules_buffer = yy_create_buffer(f, YY_BUF_SIZE); }
}
void rules_parse_input() void rules_parse_input() {
{ yy_switch_to_buffer(rules_buffer);
yy_switch_to_buffer(rules_buffer); rules_parse();
rules_parse(); yy_delete_buffer(rules_buffer);
yy_delete_buffer(rules_buffer); }
}

1029
src/scan.l

File diff suppressed because it is too large Load diff