From 7130c3c2de1b2e7647584b400367fbaae75d93a4 Mon Sep 17 00:00:00 2001 From: patacongo Date: Tue, 31 May 2011 23:32:48 +0000 Subject: Fix asprintf bug git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@3653 42af7a65-404d-4744-a932-0658087f49c3 --- nuttx/TODO | 8 ++++++-- nuttx/lib/stdio/lib_asprintf.c | 10 +++++++++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/nuttx/TODO b/nuttx/TODO index 553c79ec2..475263727 100644 --- a/nuttx/TODO +++ b/nuttx/TODO @@ -1,4 +1,4 @@ -NuttX TODO List (Last updated May 28, 2011) +NuttX TODO List (Last updated May 31, 2011) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ nuttx/ @@ -12,7 +12,7 @@ nuttx/ (5) Binary loaders (binfmt/) (15) Network (net/, drivers/net) (2) USB (drivers/usbdev, drivers/usbhost) - (5) Libraries (lib/) + (6) Libraries (lib/) (13) File system/Generic drivers (fs/, drivers/) (1) Graphics subystem (graphics/) (1) Pascal add-on (pcode/) @@ -386,6 +386,10 @@ o Libraries (lib/) Status: Open Priority: Low + Description: Not implemented: ferror() and clearerr() + Status: Open + Priority: Low + o File system / Generic drivers (fs/, drivers/) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/nuttx/lib/stdio/lib_asprintf.c b/nuttx/lib/stdio/lib_asprintf.c index 2baffe1ac..42467e71d 100644 --- a/nuttx/lib/stdio/lib_asprintf.c +++ b/nuttx/lib/stdio/lib_asprintf.c @@ -101,6 +101,8 @@ int asprintf (FAR char **ptr, const char *fmt, ...) va_list ap; int n; + DEBUGASSERT(ptr && fmt); + /* First, use a nullstream to get the size of the buffer */ lib_nulloutstream(&nulloutstream); @@ -121,13 +123,19 @@ int asprintf (FAR char **ptr, const char *fmt, ...) lib_memoutstream((FAR struct lib_memoutstream_s *)&memoutstream, buf, nulloutstream.nput); - /* Then let lib_vsprintf do it real thing */ + /* Then let lib_vsprintf do it's real thing */ va_start(ap, fmt); n = lib_vsprintf((FAR struct lib_outstream_s *)&memoutstream.public, fmt, ap); va_end(ap); + /* Terminate the string and return a pointer to the string to the caller. + * Hmmm.. looks like the memory would be stranded if lib_vsprintf() returned + * an error. Does that ever happen? + */ + DEBUGASSERT(n < 0 || n == nulloutstream.nput); buf[nulloutstream.nput] = '\0'; + *ptr = buf; return n; } -- cgit v1.2.3