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; }