[rtems-tools commit] Explanations.cc: Fix resource leaks

Joel Sherrill joel at rtems.org
Fri Jun 4 17:23:14 UTC 2021


Module:    rtems-tools
Branch:    master
Commit:    9f4887ccafa77d8c477e7a1d6995b6cbc3b20476
Changeset: http://git.rtems.org/rtems-tools/commit/?id=9f4887ccafa77d8c477e7a1d6995b6cbc3b20476

Author:    Ryan Long <ryan.long at oarcorp.com>
Date:      Mon May 17 13:06:51 2021 -0400

Explanations.cc: Fix resource leaks

CID 1399608: Resource leak in load().
CID 1399611: Resource leak in load().

Closes #4417

---

 tester/covoar/Explanations.cc | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)

diff --git a/tester/covoar/Explanations.cc b/tester/covoar/Explanations.cc
index 12bd809..d94cd2e 100644
--- a/tester/covoar/Explanations.cc
+++ b/tester/covoar/Explanations.cc
@@ -32,7 +32,7 @@ namespace Coverage {
     #define MAX_LINE_LENGTH 512
     FILE       *explain;
     char        *cStatus;
-    Explanation *e;
+    Explanation  e;
     int          line = 1;
 
     if (!explanations)
@@ -46,7 +46,6 @@ namespace Coverage {
     }
 
     while ( 1 ) {
-      e = new Explanation;
       // Read the starting line of this explanation and
       // skip blank lines between
       do {
@@ -65,12 +64,13 @@ namespace Coverage {
         what << "line " << line
              << "contains a duplicate explanation ("
              << inputBuffer << ")";
+        fclose( explain );
         throw rld::error( what, "Explanations::load" );
       }
 
       // Add the starting line and file
-      e->startingPoint = std::string(inputBuffer);
-      e->found = false;
+      e.startingPoint = std::string(inputBuffer);
+      e.found = false;
 
       // Get the classification
       cStatus = fgets( inputBuffer, MAX_LINE_LENGTH, explain );
@@ -78,10 +78,11 @@ namespace Coverage {
         std::ostringstream what;
         what << "line " << line
              << "out of sync at the classification";
+        fclose( explain );
         throw rld::error( what, "Explanations::load" );
       }
       inputBuffer[ strlen(inputBuffer) - 1] = '\0';
-      e->classification = inputBuffer;
+      e.classification = inputBuffer;
       line++;
 
       // Get the explanation
@@ -92,6 +93,7 @@ namespace Coverage {
           std::ostringstream what;
           what << "line " << line
                << "out of sync at the explanation";
+          fclose( explain );
           throw rld::error( what, "Explanations::load" );
         }
         inputBuffer[ strlen(inputBuffer) - 1] = '\0';
@@ -102,15 +104,17 @@ namespace Coverage {
           break;
         }
         // XXX only taking last line.  Needs to be a vector
-        e->explanation.push_back( inputBuffer );
+        e.explanation.push_back( inputBuffer );
       }
 
       // Add this to the set of Explanations
-      set[ e->startingPoint ] = *e;
+      set[ e.startingPoint ] = e;
     }
 done:
     ;
 
+    fclose( explain );
+
     #undef MAX_LINE_LENGTH
   }
 



More information about the vc mailing list