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

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


The following commit(s) were added to refs/heads/master by this push:
     new 0b1ba70ac5 asprintf: Fixed possible memory leak if print fails.
0b1ba70ac5 is described below

commit 0b1ba70ac583289b53b88ccb86f9a08da67be789
Author: Fotis Panagiotopoulos <f.pa...@amco.gr>
AuthorDate: Mon Mar 27 12:18:47 2023 +0300

    asprintf: Fixed possible memory leak if print fails.
---
 libs/libc/stdio/lib_vasprintf.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/libs/libc/stdio/lib_vasprintf.c b/libs/libc/stdio/lib_vasprintf.c
index fa93e2272c..5bba2d9d72 100644
--- a/libs/libc/stdio/lib_vasprintf.c
+++ b/libs/libc/stdio/lib_vasprintf.c
@@ -121,12 +121,16 @@ int vasprintf(FAR char **ptr, FAR const IPTR char *fmt, 
va_list ap)
   /* Return a pointer to the string to the caller.  NOTE: the memstream put()
    * method has already added the NUL terminator to the end of the string
    * (not included in the nput count).
-   *
-   * Hmmm.. looks like the memory would be stranded if lib_vsprintf()
-   * returned an error.  Does that ever happen?
    */
 
   DEBUGASSERT(nbytes < 0 || nbytes == nulloutstream.nput);
+
+  if (nbytes < 0)
+    {
+      lib_free(buf);
+      return ERROR;
+    }
+
   *ptr = buf;
   return nbytes;
 }

Reply via email to