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

bcall pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficserver.git


The following commit(s) were added to refs/heads/master by this push:
     new 088783635c Coverity 1545947: Resource leak in header_rewrite plugin 
(#11386)
088783635c is described below

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

    Coverity 1545947: Resource leak in header_rewrite plugin (#11386)
---
 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 784b2cf17b..d0a2752e59 100644
--- a/plugins/header_rewrite/header_rewrite.cc
+++ b/plugins/header_rewrite/header_rewrite.cc
@@ -144,10 +144,10 @@ RulesConfig::add_rule(RuleSet *rule)
 bool
 RulesConfig::parse_config(const std::string &fname, TSHttpHookID default_hook, 
char *from_url, char *to_url)
 {
-  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);
@@ -199,15 +199,15 @@ RulesConfig::parse_config(const std::string &fname, 
TSHttpHookID default_hook, c
     }
 
     // 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) {
@@ -215,7 +215,6 @@ RulesConfig::parse_config(const std::string &fname, 
TSHttpHookID default_hook, c
         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;
@@ -243,13 +242,14 @@ RulesConfig::parse_config(const std::string &fname, 
TSHttpHookID default_hook, c
       }
     } 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