]> git.ipfire.org Git - thirdparty/snort3.git/commitdiff
Pull request #4364: parser: do not skip symbols while expanding variables
authorYurii Chalov -X (ychalov - SOFTSERVE INC at Cisco) <ychalov@cisco.com>
Wed, 3 Jul 2024 11:46:33 +0000 (11:46 +0000)
committerOleksii Shumeiko -X (oshumeik - SOFTSERVE INC at Cisco) <oshumeik@cisco.com>
Wed, 3 Jul 2024 11:46:33 +0000 (11:46 +0000)
Merge in SNORT/snort3 from ~YCHALOV/snort3:expand_variable_fix to master

Squashed commit of the following:

commit 6950e149cd99270d4a9c467aa102beb3944b257e
Author: Yurii Chalov <ychalov@cisco.com>
Date:   Mon Jun 24 13:03:29 2024 +0200

    parser: do not skip symbols while expanding variables

src/parser/vars.cc

index 829b8715f464d76b624fc8e3b5c8d9492023dfda..f0997b702b83ed72346a5ddec26aefacaf29ac12 100644 (file)
@@ -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();