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

wkaras 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 2b036da1a6 Quiet Coverity memory leak detection in stream_editor 
plugin. (#10490)
2b036da1a6 is described below

commit 2b036da1a612628fbd5b8559bf70882a92153dd5
Author: Walt Karas <[email protected]>
AuthorDate: Mon Sep 25 18:51:30 2023 -0400

    Quiet Coverity memory leak detection in stream_editor plugin. (#10490)
    
    CID 1508843
---
 .../experimental/stream_editor/stream_editor.cc    | 38 ++++++++++++----------
 1 file changed, 20 insertions(+), 18 deletions(-)

diff --git a/plugins/experimental/stream_editor/stream_editor.cc 
b/plugins/experimental/stream_editor/stream_editor.cc
index a5f1533c6e..21d5dd0518 100644
--- a/plugins/experimental/stream_editor/stream_editor.cc
+++ b/plugins/experimental/stream_editor/stream_editor.cc
@@ -99,6 +99,7 @@
 #include <string>
 #include <cstdio>
 #include <stdexcept>
+#include <memory>
 #include "ts/ts.h"
 
 struct edit_t;
@@ -554,15 +555,16 @@ public:
     }
   }
 };
-using ruleset_t = std::vector<rule_t>;
-using rule_p    = ruleset_t::const_iterator;
+using ruleset_t    = std::vector<rule_t>;
+using rule_p       = ruleset_t::const_iterator;
+using ruleset_up_t = std::unique_ptr<ruleset_t>;
 
-using contdata_t = struct contdata_t {
+struct contdata_t {
   TSCont cont             = nullptr;
   TSIOBuffer out_buf      = nullptr;
   TSIOBufferReader out_rd = nullptr;
   TSVIO out_vio           = nullptr;
-  ruleset_t rules;
+  ruleset_up_t rules;
   std::string contbuf;
   size_t contbuf_sz = 0;
   int64_t bytes_in  = 0;
@@ -623,7 +625,7 @@ process_block(contdata_t *contdata, TSIOBufferReader reader)
 
   editset_t edits;
 
-  for (const auto &rule : contdata->rules) {
+  for (const auto &rule : *(contdata->rules)) {
     rule.apply(buf, buflen, edits);
   }
 
@@ -767,8 +769,8 @@ static int
 streamedit_setup(TSCont contp, TSEvent event, void *edata)
 {
   TSHttpTxn txn        = static_cast<TSHttpTxn>(edata);
-  ruleset_t *rules_in  = static_cast<ruleset_t *>(TSContDataGet(contp));
   contdata_t *contdata = nullptr;
+  auto rules_in{static_cast<ruleset_t *>(TSContDataGet(contp))};
 
   assert((event == TS_EVENT_HTTP_READ_RESPONSE_HDR) || (event == 
TS_EVENT_HTTP_READ_REQUEST_HDR));
 
@@ -778,7 +780,7 @@ streamedit_setup(TSCont contp, TSEvent event, void *edata)
       if (contdata == nullptr) {
         contdata = new contdata_t();
       }
-      contdata->rules.push_back(r);
+      contdata->rules->push_back(r);
       contdata->set_cont_size(r.cont_size());
     }
   }
@@ -804,7 +806,7 @@ streamedit_setup(TSCont contp, TSEvent event, void *edata)
 }
 
 static void
-read_conf(const char *filename, ruleset_t *&in, ruleset_t *&out)
+read_conf(const char *filename, ruleset_up_t &in, ruleset_up_t &out)
 {
   char buf[MAX_CONFIG_LINE];
   FILE *file = fopen(filename, "r");
@@ -816,13 +818,13 @@ read_conf(const char *filename, ruleset_t *&in, ruleset_t 
*&out)
   while (fgets(buf, MAX_CONFIG_LINE, file) != nullptr) {
     try {
       if (!strncasecmp(buf, "[in]", 4)) {
-        if (in == nullptr) {
-          in = new ruleset_t();
+        if (!in) {
+          in.reset(new ruleset_t);
         }
         in->push_back(rule_t(buf));
       } else if (!strncasecmp(buf, "[out]", 5)) {
-        if (out == nullptr) {
-          out = new ruleset_t();
+        if (!out) {
+          out.reset(new ruleset_t);
         }
         out->push_back(rule_t(buf));
       }
@@ -838,8 +840,8 @@ TSPluginInit(int argc, const char *argv[])
 {
   TSPluginRegistrationInfo info;
   TSCont inputcont, outputcont;
-  ruleset_t *rewrites_in  = nullptr;
-  ruleset_t *rewrites_out = nullptr;
+  ruleset_up_t rewrites_in;
+  ruleset_up_t rewrites_out;
 
   info.plugin_name   = (char *)"stream-editor";
   info.vendor_name   = (char *)"Apache Software Foundation";
@@ -855,26 +857,26 @@ TSPluginInit(int argc, const char *argv[])
     read_conf(*++argv, rewrites_in, rewrites_out);
   }
 
-  if (rewrites_in != nullptr) {
+  if (rewrites_in) {
     Dbg(dbg_ctl, "initializing input filtering");
     inputcont = TSContCreate(streamedit_setup, nullptr);
     if (inputcont == nullptr) {
       TSError("[stream-editor] failed to initialize input filtering!");
     } else {
-      TSContDataSet(inputcont, rewrites_in);
+      TSContDataSet(inputcont, rewrites_in.release());
       TSHttpHookAdd(TS_HTTP_READ_REQUEST_HDR_HOOK, inputcont);
     }
   } else {
     Dbg(dbg_ctl, "no input filter rules, skipping filter");
   }
 
-  if (rewrites_out != nullptr) {
+  if (rewrites_out) {
     Dbg(dbg_ctl, "initializing output filtering");
     outputcont = TSContCreate(streamedit_setup, nullptr);
     if (outputcont == nullptr) {
       TSError("[stream-editor] failed to initialize output filtering!");
     } else {
-      TSContDataSet(outputcont, rewrites_out);
+      TSContDataSet(outputcont, rewrites_out.release());
       TSHttpHookAdd(TS_HTTP_READ_RESPONSE_HDR_HOOK, outputcont);
     }
   } else {

Reply via email to