This is an automated email from the ASF dual-hosted git repository.

cmcfarlen pushed a commit to branch 10.0.x
in repository https://gitbox.apache.org/repos/asf/trafficserver.git

commit 59b83c366317fbc986b97aa556006013ddde5368
Author: Bryan Call <[email protected]>
AuthorDate: Wed May 22 14:38:07 2024 -0700

    Coverity 1545947: Resource leak in header_rewrite plugin (#11386)
    
    (cherry picked from commit 088783635c25b89616a2c7f7a50c0d57cea6d498)
---
 plugins/header_rewrite/header_rewrite.cc | 20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)

diff --git a/plugins/header_rewrite/header_rewrite.cc 
b/plugins/header_rewrite/header_rewrite.cc
index b2f095e12e..476bd57599 100644
--- a/plugins/header_rewrite/header_rewrite.cc
+++ b/plugins/header_rewrite/header_rewrite.cc
@@ -135,10 +135,10 @@ RulesConfig::add_rule(RuleSet *rule)
 bool
 RulesConfig::parse_config(const std::string &fname, TSHttpHookID default_hook)
 {
-  RuleSet      *rule = nullptr;
-  std::string   filename;
-  std::ifstream f;
-  int           lineno = 0;
+  std::unique_ptr<RuleSet> rule(nullptr);
+  std::string              filename;
+  std::ifstream            f;
+  int                      lineno = 0;
 
   if (0 == fname.size()) {
     TSError("[%s] no config filename provided", PLUGIN_NAME);
@@ -190,15 +190,15 @@ RulesConfig::parse_config(const std::string &fname, 
TSHttpHookID default_hook)
     }
 
     // If we are at the beginning of a new condition, save away the previous 
rule (but only if it has operators).
-    if (p.is_cond() && add_rule(rule)) {
-      rule = nullptr;
+    if (p.is_cond() && add_rule(rule.get())) {
+      rule.release();
     }
 
     TSHttpHookID hook    = default_hook;
     bool         is_hook = p.cond_is_hook(hook); // This updates the hook if 
explicitly set, if not leaves at default
 
     if (nullptr == rule) {
-      rule = new RuleSet();
+      rule = std::make_unique<RuleSet>();
       rule->set_hook(hook);
 
       if (is_hook) {
@@ -206,7 +206,6 @@ RulesConfig::parse_config(const std::string &fname, 
TSHttpHookID default_hook)
         if ((default_hook == TS_REMAP_PSEUDO_HOOK) &&
             ((TS_HTTP_READ_REQUEST_HDR_HOOK == hook) || 
(TS_HTTP_PRE_REMAP_HOOK == hook))) {
           TSError("[%s] you can not use cond %%{%s} in a remap rule", 
PLUGIN_NAME, p.get_op().c_str());
-          delete rule;
           return false;
         }
         continue;
@@ -234,13 +233,14 @@ RulesConfig::parse_config(const std::string &fname, 
TSHttpHookID default_hook)
       }
     } catch (std::runtime_error &e) {
       TSError("[%s] header_rewrite configuration exception: %s in file: %s", 
PLUGIN_NAME, e.what(), fname.c_str());
-      delete rule;
       return false;
     }
   }
 
   // Add the last rule (possibly the only rule)
-  add_rule(rule);
+  if (add_rule(rule.get())) {
+    rule.release();
+  }
 
   // Collect all resource IDs that we need
   for (int i = TS_HTTP_READ_REQUEST_HDR_HOOK; i < TS_HTTP_LAST_HOOK; ++i) {

Reply via email to