[esnacc-dev] cxx-lib/src: corrected usage of 'delete' causing uncleared memory.

Message ID 20190624125929.8927-1-aconole@bytheb.org
State New
Delegated to: Aaron Conole
Headers show

Commit Message

Aaron Conole June 24, 2019, 12:59 p.m.
From: Jan Sperber <jan.sperber@hytera.de>

From: Jan Sperber <jan.sperber@hytera.de>

Working with libcxxasn1.so showed memory leakage. After analyzing the
esnacc-ng sources we have seen several deletes have not been used
correctly (helper tool cppcheck) and found that asn-int.cpp didn't
deleted the allocated buffer at all. After further review I think there
is no need to keep that buffer in memory so I added the delete statement
at the end of the function

Reported-at: https://github.com/esnacc/esnacc-ng/pull/49
Fixes: f245889884a7 ("cxx/asn-int: fix integer encoding/decoding")
Fixes: dce1e2a48583 ("Starting with eSnacc 1.7 as a base")
Signed-off-by: Jan Sperber <jan.sperber@hytera.de>
Signed-off-by: Aaron Conole <aconole@bytheb.org>
---
 X-post from github.

 cxx-lib/src/asn-int.cpp |  1 +
 cxx-lib/src/tcl-if.cpp  | 22 +++++++++++-----------
 2 files changed, 12 insertions(+), 11 deletions(-)

Comments

Aaron Conole June 24, 2019, 1:03 p.m. | #1
Aaron Conole <aconole@bytheb.org> writes:

> From: Jan Sperber <jan.sperber@hytera.de>
>
> From: Jan Sperber <jan.sperber@hytera.de>
>
> Working with libcxxasn1.so showed memory leakage. After analyzing the
> esnacc-ng sources we have seen several deletes have not been used
> correctly (helper tool cppcheck) and found that asn-int.cpp didn't
> deleted the allocated buffer at all. After further review I think there
> is no need to keep that buffer in memory so I added the delete statement
> at the end of the function
>
> Reported-at: https://github.com/esnacc/esnacc-ng/pull/49
> Fixes: f245889884a7 ("cxx/asn-int: fix integer encoding/decoding")
> Fixes: dce1e2a48583 ("Starting with eSnacc 1.7 as a base")
> Signed-off-by: Jan Sperber <jan.sperber@hytera.de>
> Signed-off-by: Aaron Conole <aconole@bytheb.org>
> ---

This patch is mostly fine for me.  I plan to make the following changes
on apply:

s/delete[]/delete []/

Also, I plan to add a change to AUTHORS to add Jan.

Thanks for your contribution!

-Aaron

Patch

diff --git a/cxx-lib/src/asn-int.cpp b/cxx-lib/src/asn-int.cpp
index 25fbe3b..3d303ab 100644
--- a/cxx-lib/src/asn-int.cpp
+++ b/cxx-lib/src/asn-int.cpp
@@ -130,6 +130,7 @@  void AsnInt::BDecContent (const AsnBuf &b, AsnTag, AsnLen elmtLen,
     }
 
     storeDERInteger(bytes, elmtLen, !isNeg);
+    delete[] bytes;
 }
 
 AsnInt::AsnInt (const AsnInt &that)
diff --git a/cxx-lib/src/tcl-if.cpp b/cxx-lib/src/tcl-if.cpp
index 73c71e6..0161e67 100644
--- a/cxx-lib/src/tcl-if.cpp
+++ b/cxx-lib/src/tcl-if.cpp
@@ -157,7 +157,7 @@  int ASN1File::read (Tcl_Interp *interp, const char *rfn)
   if (::read (rfd, buf, filesize) != filesize)
   {
     Tcl_AppendResult (interp, "can't read \"", rfn, "\": ", Tcl_PosixError (interp), NULL);
-    delete buf;
+    delete[] buf;
     return TCL_ERROR;
   }
 
@@ -173,7 +173,7 @@  int ASN1File::read (Tcl_Interp *interp, const char *rfn)
     sprintf (eno, "%d", eval);
     Tcl_AppendResult (interp, "can't decode (error ", eno, ")", NULL);
     Tcl_SetErrorCode (interp, "SNACC", "DECODE", eno, NULL);
-    delete buf;
+    delete[] buf;
     return TCL_ERROR;
   }
   pdu->BDec (inputBuf, decodedLen, env);
@@ -181,7 +181,7 @@  int ASN1File::read (Tcl_Interp *interp, const char *rfn)
   {
     Tcl_AppendResult (interp, "can't decode, out of data", NULL);
     Tcl_SetErrorCode (interp, "SNACC", "DECODE", "EOBUF", NULL);
-    delete buf;
+    delete[] buf;
     return TCL_ERROR;
   }
 
@@ -192,7 +192,7 @@  cout << "DECODED:" << endl << *pdu << endl;
   if (decodedLen != filesize)
     sprintf (interp->result, "decoded %d of %d bytes", decodedLen, filesize);
 
-  delete buf;
+  delete[] buf;
   return TCL_OK;
 }
 
@@ -250,7 +250,7 @@  int ASN1File::write (Tcl_Interp *interp, const char *wfn)
     encodedLen = pdu->BEnc (outputBuf);
     if (!outputBuf.WriteError())
       break;
-    delete buf;
+    delete[] buf;
   }
 
   outputBuf.ResetInReadMode();
@@ -264,14 +264,14 @@  int ASN1File::write (Tcl_Interp *interp, const char *wfn)
     if (::write (wfd, hunk, hunklen) != hunklen)
     {
       Tcl_AppendResult (interp, "write error on \"", wfn, "\": ", Tcl_PosixError (interp), NULL);
-      delete hunk; // may affect errno
-      delete buf; // may affect errno
+      delete[] hunk; // may affect errno
+      delete[] buf; // may affect errno
       return TCL_ERROR;
     }
   }
 
-  delete hunk;
-  delete buf;
+  delete[] hunk;
+  delete[] buf;
 
   filesize = encodedLen;
   if (!wfn)
@@ -311,12 +311,12 @@  int import (Tcl_Interp *interp, int argc, char **argv)
   if (::read (fd, ibuf, filesize) != filesize)
   {
     Tcl_AppendResult (interp, "read error on \"", fn, "\": ", Tcl_PosixError (interp), NULL);
-    delete ibuf;
+    delete[] ibuf;
     return TCL_ERROR;
   }
 
   int result = debinify (interp, ibuf, filesize);
-  delete ibuf;
+  delete[] ibuf;
   return result;
 }