From: Yurii Chalov -X (ychalov - SOFTSERVE INC at Cisco) Date: Wed, 3 Jul 2024 11:46:33 +0000 (+0000) Subject: Pull request #4364: parser: do not skip symbols while expanding variables X-Git-Tag: 3.3.1.0~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=467ab2e74645cb41f1f2b0f88241ce64f5d9caca;p=thirdparty%2Fsnort3.git Pull request #4364: parser: do not skip symbols while expanding variables Merge in SNORT/snort3 from ~YCHALOV/snort3:expand_variable_fix to master Squashed commit of the following: commit 6950e149cd99270d4a9c467aa102beb3944b257e Author: Yurii Chalov Date: Mon Jun 24 13:03:29 2024 +0200 parser: do not skip symbols while expanding variables --- diff --git a/src/parser/vars.cc b/src/parser/vars.cc index 829b8715f..f0997b702 100644 --- a/src/parser/vars.cc +++ b/src/parser/vars.cc @@ -435,7 +435,8 @@ const std::string ExpandVars(const std::string& input_str) if (input_str.find('$') == std::string::npos) return(input_str); - for (auto i = input_str.begin(); i < input_str.end(); i++) + auto i = input_str.begin(); + while (i < input_str.end()) { const char c = *i; if (c == '"') @@ -444,69 +445,72 @@ const std::string ExpandVars(const std::string& input_str) quote_toggle = !quote_toggle; } - if (c == '$' && !quote_toggle) + if (c != '$' or quote_toggle) { - auto begin = (i+1); - auto end = begin; - bool name_only = *begin != '('; - if (!name_only) - begin++; - - while (*end != '\0' && ( - ( name_only && (isalnum(*end) || *end == '_') ) || - ( !name_only && *end != ')' ) ) ) { - end++; - } + output << c; + i++; + continue; + } - std::string var_name(begin, end); - std::string var_aux; + auto begin = (i+1); + auto end = begin; + bool name_only = *begin != '('; + if (!name_only) + begin++; - i = end; + while (*end != '\0' and ( + ( name_only and (isalnum(*end) or *end == '_') ) or + ( !name_only and *end != ')' ) ) ) { + end++; + } - char var_modifier = ' '; + std::string var_name(begin, end); + std::string var_aux; - size_t p = var_name.find(':'); + i = end; - if (p != std::string::npos) - { - if (var_name.size() - p >= 2) - { - var_modifier = var_name[p+1]; - var_aux = var_name.substr(p+2); - } - var_name.resize(p); - } + char var_modifier = ' '; - std::string var_contents = VarSearch(var_name); + size_t p = var_name.find(':'); - switch (var_modifier) + if (p != std::string::npos) + { + if (var_name.size() - p >= 2) { - case '-': - if (var_contents.empty()) - var_contents = var_aux; - break; - - case '?': - if (var_contents.empty()) - { - if (!var_aux.empty()) - ParseAbort("%s", var_aux.c_str()); - else - ParseAbort("undefined variable '%s'.", var_name.c_str()); - } - break; + var_modifier = var_name[p+1]; + var_aux = var_name.substr(p+2); } + var_name.resize(p); + } - // If variable not defined now, we're toast - if (var_contents.empty()) - ParseAbort("undefined variable name: %s.", var_name.c_str()); + std::string var_contents = VarSearch(var_name); - output << var_contents; - } - else + switch (var_modifier) { - output << c; + case '-': + if (var_contents.empty()) + var_contents = var_aux; + break; + + case '?': + if (var_contents.empty()) + { + if (!var_aux.empty()) + ParseAbort("%s", var_aux.c_str()); + else + ParseAbort("undefined variable '%s'.", var_name.c_str()); + } + break; } + + // If variable not defined now, we're toast + if (var_contents.empty()) + ParseAbort("undefined variable name: %s.", var_name.c_str()); + + output << var_contents; + + if (!name_only) + i++; } return output.str();