diff options
author | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2011-06-02 23:45:31 +0000 |
---|---|---|
committer | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2011-06-02 23:45:31 +0000 |
commit | 80d5b281209708972a1e484f10f9b1cf74868ea4 (patch) | |
tree | 055ca499f7321151291d83afc6a5585c0bdde1ae /nuttx/lib/stdio | |
parent | 944bf89644559cd2ae021122451ff5d0a740cb85 (diff) | |
download | px4-nuttx-80d5b281209708972a1e484f10f9b1cf74868ea4.tar.gz px4-nuttx-80d5b281209708972a1e484f10f9b1cf74868ea4.tar.bz2 px4-nuttx-80d5b281209708972a1e484f10f9b1cf74868ea4.zip |
More FTP client debug fixes
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@3662 42af7a65-404d-4744-a932-0658087f49c3
Diffstat (limited to 'nuttx/lib/stdio')
-rw-r--r-- | nuttx/lib/stdio/lib_asprintf.c | 31 | ||||
-rw-r--r-- | nuttx/lib/stdio/lib_meminstream.c | 2 | ||||
-rw-r--r-- | nuttx/lib/stdio/lib_memoutstream.c | 9 |
3 files changed, 29 insertions, 13 deletions
diff --git a/nuttx/lib/stdio/lib_asprintf.c b/nuttx/lib/stdio/lib_asprintf.c index 42467e71d..343f6dd86 100644 --- a/nuttx/lib/stdio/lib_asprintf.c +++ b/nuttx/lib/stdio/lib_asprintf.c @@ -99,18 +99,22 @@ int asprintf (FAR char **ptr, const char *fmt, ...) struct lib_memoutstream_s memoutstream; FAR char *buf; va_list ap; - int n; + int nbytes; 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. The number + * of bytes returned may or may not include the null terminator. + */ lib_nulloutstream(&nulloutstream); va_start(ap, fmt); - n = lib_vsprintf((FAR struct lib_outstream_s *)&nulloutstream, fmt, ap); + nbytes = lib_vsprintf((FAR struct lib_outstream_s *)&nulloutstream, fmt, ap); va_end(ap); - /* Then allocate a buffer to hold that number of characters */ + /* Then allocate a buffer to hold that number of characters, adding one + * for the null terminator. + */ buf = (FAR char *)malloc(nulloutstream.nput + 1); if (!buf) @@ -118,24 +122,29 @@ int asprintf (FAR char **ptr, const char *fmt, ...) return ERROR; } - /* Initialize a memory stream to write into the allocated buffer */ + /* Initialize a memory stream to write into the allocated buffer. The + * memory stream will reserve one byte at the end of the buffer for the + * null terminator and will not report this in the number of output bytes. + */ lib_memoutstream((FAR struct lib_memoutstream_s *)&memoutstream, - buf, nulloutstream.nput); + buf, nulloutstream.nput + 1); /* 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); + nbytes = 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. + /* 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(n < 0 || n == nulloutstream.nput); - buf[nulloutstream.nput] = '\0'; + DEBUGASSERT(nbytes < 0 || nbytes == nulloutstream.nput); *ptr = buf; - return n; + return nbytes; } diff --git a/nuttx/lib/stdio/lib_meminstream.c b/nuttx/lib/stdio/lib_meminstream.c index 6e1db7c2c..839e56219 100644 --- a/nuttx/lib/stdio/lib_meminstream.c +++ b/nuttx/lib/stdio/lib_meminstream.c @@ -52,7 +52,7 @@ static int meminstream_getc(FAR struct lib_instream_s *this) FAR struct lib_meminstream_s *mthis = (FAR struct lib_meminstream_s *)this; int ret; - if (this && this->nget < mthis->buflen - 1) + if (this && this->nget < mthis->buflen) { ret = mthis->buffer[this->nget]; this->nget++; diff --git a/nuttx/lib/stdio/lib_memoutstream.c b/nuttx/lib/stdio/lib_memoutstream.c index d5a673b3a..dca8456e8 100644 --- a/nuttx/lib/stdio/lib_memoutstream.c +++ b/nuttx/lib/stdio/lib_memoutstream.c @@ -50,7 +50,13 @@ static void memoutstream_putc(FAR struct lib_outstream_s *this, int ch) { FAR struct lib_memoutstream_s *mthis = (FAR struct lib_memoutstream_s *)this; - if (this && this->nput < mthis->buflen - 1) + + /* If this will not overrun the buffer, then write the character to the + * buffer. Not that buflen was pre-decremented when the stream was + * created so it is okay to write past the end of the buflen by one. + */ + + if (this && this->nput < mthis->buflen) { mthis->buffer[this->nput] = ch; this->nput++; @@ -89,6 +95,7 @@ void lib_memoutstream(FAR struct lib_memoutstream_s *memoutstream, memoutstream->public.nput = 0; /* Will be buffer index */ memoutstream->buffer = bufstart; /* Start of buffer */ memoutstream->buflen = buflen - 1; /* Save space for null terminator */ + memoutstream->buffer[0] = '\0'; /* Start with an empty string */ } |