Fix asprintf bug

git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@3653 42af7a65-404d-4744-a932-0658087f49c3
This commit is contained in:
patacongo 2011-05-31 23:32:48 +00:00
parent 29a70ad011
commit c68ddb5a57
2 changed files with 15 additions and 3 deletions

8
TODO
View File

@ -1,4 +1,4 @@
NuttX TODO List (Last updated May 28, 2011) NuttX TODO List (Last updated May 31, 2011)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
nuttx/ nuttx/
@ -12,7 +12,7 @@ nuttx/
(5) Binary loaders (binfmt/) (5) Binary loaders (binfmt/)
(15) Network (net/, drivers/net) (15) Network (net/, drivers/net)
(2) USB (drivers/usbdev, drivers/usbhost) (2) USB (drivers/usbdev, drivers/usbhost)
(5) Libraries (lib/) (6) Libraries (lib/)
(13) File system/Generic drivers (fs/, drivers/) (13) File system/Generic drivers (fs/, drivers/)
(1) Graphics subystem (graphics/) (1) Graphics subystem (graphics/)
(1) Pascal add-on (pcode/) (1) Pascal add-on (pcode/)
@ -386,6 +386,10 @@ o Libraries (lib/)
Status: Open Status: Open
Priority: Low Priority: Low
Description: Not implemented: ferror() and clearerr()
Status: Open
Priority: Low
o File system / Generic drivers (fs/, drivers/) o File system / Generic drivers (fs/, drivers/)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

View File

@ -101,6 +101,8 @@ int asprintf (FAR char **ptr, const char *fmt, ...)
va_list ap; va_list ap;
int n; int n;
DEBUGASSERT(ptr && fmt);
/* First, use a nullstream to get the size of the buffer */ /* First, use a nullstream to get the size of the buffer */
lib_nulloutstream(&nulloutstream); lib_nulloutstream(&nulloutstream);
@ -121,13 +123,19 @@ int asprintf (FAR char **ptr, const char *fmt, ...)
lib_memoutstream((FAR struct lib_memoutstream_s *)&memoutstream, lib_memoutstream((FAR struct lib_memoutstream_s *)&memoutstream,
buf, nulloutstream.nput); buf, nulloutstream.nput);
/* Then let lib_vsprintf do it real thing */ /* Then let lib_vsprintf do it's real thing */
va_start(ap, fmt); va_start(ap, fmt);
n = lib_vsprintf((FAR struct lib_outstream_s *)&memoutstream.public, fmt, ap); n = lib_vsprintf((FAR struct lib_outstream_s *)&memoutstream.public, fmt, ap);
va_end(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); DEBUGASSERT(n < 0 || n == nulloutstream.nput);
buf[nulloutstream.nput] = '\0'; buf[nulloutstream.nput] = '\0';
*ptr = buf;
return n; return n;
} }