diff options
32 files changed, 168 insertions, 50 deletions
diff --git a/apps/include/ftpc.h b/apps/include/ftpc.h index 79632ba1d..3f373dad8 100644 --- a/apps/include/ftpc.h +++ b/apps/include/ftpc.h @@ -66,7 +66,7 @@ #endif #ifndef CONFIG_FTP_MAXREPLY -# define CONFIG_FTP_MAXREPLY 21 +# define CONFIG_FTP_MAXREPLY 256 #endif #ifndef CONFIG_FTP_TMPDIR @@ -74,7 +74,7 @@ #endif #ifndef CONFIG_FTP_BUFSIZE -# define CONFIG_FTP_BUFSIZE 4096 +# define CONFIG_FTP_BUFSIZE 1024 #endif #ifndef CONFIG_FTP_MAXPATH diff --git a/apps/netutils/ftpc/ftpc_cdup.c b/apps/netutils/ftpc/ftpc_cdup.c index 323033390..a5f22bbc2 100644 --- a/apps/netutils/ftpc/ftpc_cdup.c +++ b/apps/netutils/ftpc/ftpc_cdup.c @@ -37,7 +37,7 @@ * Included Files ****************************************************************************/ -#include <nuttx/config.h> +#include "ftpc_config.h" #include <apps/ftpc.h> diff --git a/apps/netutils/ftpc/ftpc_chdir.c b/apps/netutils/ftpc/ftpc_chdir.c index ddf6452c4..0bc47ddf8 100644 --- a/apps/netutils/ftpc/ftpc_chdir.c +++ b/apps/netutils/ftpc/ftpc_chdir.c @@ -37,7 +37,7 @@ * Included Files ****************************************************************************/ -#include <nuttx/config.h> +#include "ftpc_config.h" #include <apps/ftpc.h> diff --git a/apps/netutils/ftpc/ftpc_chmod.c b/apps/netutils/ftpc/ftpc_chmod.c index 332df2b0c..f349edb09 100644 --- a/apps/netutils/ftpc/ftpc_chmod.c +++ b/apps/netutils/ftpc/ftpc_chmod.c @@ -37,7 +37,7 @@ * Included Files ****************************************************************************/ -#include <nuttx/config.h> +#include "ftpc_config.h" #include <debug.h> #include <apps/ftpc.h> diff --git a/apps/netutils/ftpc/ftpc_cmd.c b/apps/netutils/ftpc/ftpc_cmd.c index 277899433..ec89050ba 100644 --- a/apps/netutils/ftpc/ftpc_cmd.c +++ b/apps/netutils/ftpc/ftpc_cmd.c @@ -37,7 +37,7 @@ * Included Files ****************************************************************************/ -#include <nuttx/config.h> +#include "ftpc_config.h" #include <stdlib.h> #include <stdarg.h> @@ -149,7 +149,7 @@ int ftpc_cmd(struct ftpc_session_s *session, const char *cmd, ...) /* Send the command */ va_start(ap, cmd); - ret = ftpc_sockprintf(&session->cmd, cmd, ap); + ret = ftpc_sockvprintf(&session->cmd, cmd, ap); if (ret >= 0) { ret = ftpc_sockprintf(&session->cmd, "\r\n"); diff --git a/apps/netutils/ftpc/ftpc_config.h b/apps/netutils/ftpc/ftpc_config.h new file mode 100644 index 000000000..9dc90085c --- /dev/null +++ b/apps/netutils/ftpc/ftpc_config.h @@ -0,0 +1,72 @@ +/**************************************************************************** + * apps/netutils/ftpc/ftpc_config.h + * + * Copyright (C) 2011 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +#ifndef __APPS_NETUTILS_FTPC_FTPC_CONFIG_H +#define __APPS_NETUTILS_FTPC_FTPC_CONFIG_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ +/* This is a mindless little wrapper around include/nuttx/config.h. Every + * file in the ftpc directory includes this file at the very beginning of + * of the file (instead of include/nuttx/config.h). The only purpose of + * this file is to muck with some of the settings to support some debug + * features. + * + * The FPT client uses common networking debug macros (ndbg and nvdbg). + * This can be overwhelming if there is a lot of networking debug output + * as well. But by defining CONFIG_DEBUG_FTPC, this file will force + * networking debug ON only for the files within this directory. + */ + +#if !defined(CONFIG_DEBUG_NET) && defined(CONFIG_DEBUG_FTPC) +# define CONFIG_DEBUG_NET 1 +#endif + +/**************************************************************************** + * Public Types + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +#endif /* __APPS_NETUTILS_FTPC_FTPC_CONFIG_H */ diff --git a/apps/netutils/ftpc/ftpc_connect.c b/apps/netutils/ftpc/ftpc_connect.c index 2b4701d42..3c9c460aa 100644 --- a/apps/netutils/ftpc/ftpc_connect.c +++ b/apps/netutils/ftpc/ftpc_connect.c @@ -37,7 +37,7 @@ * Included Files ****************************************************************************/ -#include <nuttx/config.h> +#include "ftpc_config.h" #include <stdlib.h> #include <unistd.h> @@ -97,6 +97,7 @@ SESSION ftpc_connect(FAR struct ftpc_connect_s *server) /* Initialize the session structure */ + ftpc_reset(session); session->addr.s_addr = server->addr.s_addr; session->pid = getpid(); @@ -223,7 +224,7 @@ int ftpc_reconnect(FAR struct ftpc_session_s *session) tmp = inet_ntoa(addr.sin_addr); ndbg(" Remote address: %s:%d\n", tmp, ntohs(addr.sin_port)); tmp = inet_ntoa(session->cmd.laddr.sin_addr); - ndbg(" Local address: %s:%d\n", tmp, ntohs(session->cmd.laddr.sin_port)); + ndbg(" Local address: %s:%d\n", tmp, ntohs(session->cmd.laddr.sin_port)); #endif return OK; diff --git a/apps/netutils/ftpc/ftpc_disconnect.c b/apps/netutils/ftpc/ftpc_disconnect.c index e55c35e34..90c47ee1d 100644 --- a/apps/netutils/ftpc/ftpc_disconnect.c +++ b/apps/netutils/ftpc/ftpc_disconnect.c @@ -37,7 +37,7 @@ * Included Files ****************************************************************************/ -#include <nuttx/config.h> +#include "ftpc_config.h" #include <stdlib.h> diff --git a/apps/netutils/ftpc/ftpc_filesize.c b/apps/netutils/ftpc/ftpc_filesize.c index afd773e41..18561eb1d 100644 --- a/apps/netutils/ftpc/ftpc_filesize.c +++ b/apps/netutils/ftpc/ftpc_filesize.c @@ -37,7 +37,7 @@ * Included Files ****************************************************************************/ -#include <nuttx/config.h> +#include "ftpc_config.h" #include <stdint.h> #include <stdio.h> diff --git a/apps/netutils/ftpc/ftpc_filetime.c b/apps/netutils/ftpc/ftpc_filetime.c index 331e7e7b6..a9558f22d 100644 --- a/apps/netutils/ftpc/ftpc_filetime.c +++ b/apps/netutils/ftpc/ftpc_filetime.c @@ -37,7 +37,7 @@ * Included Files ****************************************************************************/ -#include <nuttx/config.h> +#include "ftpc_config.h" #include <string.h> #include <time.h> diff --git a/apps/netutils/ftpc/ftpc_getfile.c b/apps/netutils/ftpc/ftpc_getfile.c index 039643d0e..e9816e5b4 100644 --- a/apps/netutils/ftpc/ftpc_getfile.c +++ b/apps/netutils/ftpc/ftpc_getfile.c @@ -37,7 +37,7 @@ * Included Files ****************************************************************************/ -#include <nuttx/config.h> +#include "ftpc_config.h" #include <sys/stat.h> #include <stdlib.h> diff --git a/apps/netutils/ftpc/ftpc_getreply.c b/apps/netutils/ftpc/ftpc_getreply.c index 89dab6e81..50d392a5f 100644 --- a/apps/netutils/ftpc/ftpc_getreply.c +++ b/apps/netutils/ftpc/ftpc_getreply.c @@ -37,7 +37,7 @@ * Included Files ****************************************************************************/ -#include <nuttx/config.h> +#include "ftpc_config.h" #include <stdlib.h> #include <string.h> diff --git a/apps/netutils/ftpc/ftpc_help.c b/apps/netutils/ftpc/ftpc_help.c index 2501ca365..63e3132b8 100644 --- a/apps/netutils/ftpc/ftpc_help.c +++ b/apps/netutils/ftpc/ftpc_help.c @@ -37,7 +37,7 @@ * Included Files ****************************************************************************/ -#include <nuttx/config.h> +#include "ftpc_config.h" #include <apps/ftpc.h> diff --git a/apps/netutils/ftpc/ftpc_idle.c b/apps/netutils/ftpc/ftpc_idle.c index 945ca8577..2d049f8b4 100644 --- a/apps/netutils/ftpc/ftpc_idle.c +++ b/apps/netutils/ftpc/ftpc_idle.c @@ -37,7 +37,7 @@ * Included Files ****************************************************************************/ -#include <nuttx/config.h> +#include "ftpc_config.h" #include <debug.h> #include <apps/ftpc.h> diff --git a/apps/netutils/ftpc/ftpc_internal.h b/apps/netutils/ftpc/ftpc_internal.h index 4a7a3ca9b..5e177b36b 100644 --- a/apps/netutils/ftpc/ftpc_internal.h +++ b/apps/netutils/ftpc/ftpc_internal.h @@ -40,7 +40,7 @@ * Included Files ****************************************************************************/ -#include <nuttx/config.h> +#include "ftpc_config.h" #include <sys/types.h> #include <sys/socket.h> @@ -200,6 +200,8 @@ extern "C" { fgetc((s)->instream) #define ftpc_sockflush(s) \ fflush((s)->outstream) +#define ftpc_sockvprintf(s,f,ap) \ + vfprintf((s)->outstream,f,ap) /**************************************************************************** * Public Functions @@ -237,7 +239,7 @@ EXTERN void ftpc_sockcopy(FAR struct ftpc_socket_s *dest, /* Socket I/O helpers */ -EXTERN int ftpc_sockprintf(FAR struct ftpc_socket_s *sock, const char *str, ...); +EXTERN int ftpc_sockprintf(FAR struct ftpc_socket_s *sock, const char *fmt, ...); EXTERN void ftpc_timeout(int argc, uint32_t arg1, ...); /* Transfer helpers */ diff --git a/apps/netutils/ftpc/ftpc_listdir.c b/apps/netutils/ftpc/ftpc_listdir.c index 20653799c..4c36445ad 100644 --- a/apps/netutils/ftpc/ftpc_listdir.c +++ b/apps/netutils/ftpc/ftpc_listdir.c @@ -37,7 +37,7 @@ * Included Files ****************************************************************************/ -#include <nuttx/config.h> +#include "ftpc_config.h" #include <sys/stat.h> #include <stdlib.h> diff --git a/apps/netutils/ftpc/ftpc_login.c b/apps/netutils/ftpc/ftpc_login.c index 4fa8171da..83d2cde05 100644 --- a/apps/netutils/ftpc/ftpc_login.c +++ b/apps/netutils/ftpc/ftpc_login.c @@ -37,7 +37,7 @@ * Included Files ****************************************************************************/ -#include <nuttx/config.h> +#include "ftpc_config.h" #include <string.h> #include <errno.h> diff --git a/apps/netutils/ftpc/ftpc_mkdir.c b/apps/netutils/ftpc/ftpc_mkdir.c index 0662f2cbc..59684f07d 100644 --- a/apps/netutils/ftpc/ftpc_mkdir.c +++ b/apps/netutils/ftpc/ftpc_mkdir.c @@ -37,7 +37,7 @@ * Included Files ****************************************************************************/ -#include <nuttx/config.h> +#include "ftpc_config.h" #include <stdlib.h> #include <string.h> diff --git a/apps/netutils/ftpc/ftpc_noop.c b/apps/netutils/ftpc/ftpc_noop.c index 5e6874f02..48d9ccafd 100644 --- a/apps/netutils/ftpc/ftpc_noop.c +++ b/apps/netutils/ftpc/ftpc_noop.c @@ -37,7 +37,7 @@ * Included Files ****************************************************************************/ -#include <nuttx/config.h> +#include "ftpc_config.h" #include <apps/ftpc.h> diff --git a/apps/netutils/ftpc/ftpc_putfile.c b/apps/netutils/ftpc/ftpc_putfile.c index edee42e41..f0e85a0bc 100644 --- a/apps/netutils/ftpc/ftpc_putfile.c +++ b/apps/netutils/ftpc/ftpc_putfile.c @@ -37,7 +37,7 @@ * Included Files ****************************************************************************/ -#include <nuttx/config.h> +#include "ftpc_config.h" #include <sys/stat.h> #include <stdlib.h> diff --git a/apps/netutils/ftpc/ftpc_pwd.c b/apps/netutils/ftpc/ftpc_pwd.c index f6bde0e13..809002c9b 100644 --- a/apps/netutils/ftpc/ftpc_pwd.c +++ b/apps/netutils/ftpc/ftpc_pwd.c @@ -37,7 +37,7 @@ * Included Files ****************************************************************************/ -#include <nuttx/config.h> +#include "ftpc_config.h" #include <stdlib.h> #include <string.h> @@ -86,26 +86,50 @@ FAR char *ftpc_pwd(SESSION handle) FAR char *end; FAR char *pwd; FAR char *ptr; + int len; int ret; + /* Send the PWD command */ + ret = ftpc_cmd(session, "PWD"); + + /* Response is like: 257 "/home/gnutt" (from vsftpd). + * + * Extract the quoated path name into allocated memory. + */ + start = strchr(session->reply, '\"'); if (!start) { ndbg("Opening quote not found\n"); return NULL; } - start++; + end = strchr(start, '\"'); if (!end) { - ndbg("Clsoing quote not found\n"); + ndbg("Closing quote not found\n"); + return NULL; + } + + /* Allocate memory for the path name */ + + len = end - start; + pwd = (char *)malloc(len + 1); + if (!pwd) + { + ndbg("Failed to allocate string\n"); return NULL; } + + /* Copy the string into the allocated memory */ + + memcpy(pwd, start, len); + pwd[len] = '\0'; + + /* Remove any trailing slashes that the server may have added */ - pwd = (char *)malloc(end-start+1); - strncpy(pwd, start, end-start); ftpc_stripslash(pwd); /* Change DOS style directory separator ('\') to UNIX style ('/') */ diff --git a/apps/netutils/ftpc/ftpc_quit.c b/apps/netutils/ftpc/ftpc_quit.c index daae04e88..25ec8b16f 100644 --- a/apps/netutils/ftpc/ftpc_quit.c +++ b/apps/netutils/ftpc/ftpc_quit.c @@ -37,7 +37,7 @@ * Included Files ****************************************************************************/ -#include <nuttx/config.h> +#include "ftpc_config.h" #include <time.h> #include <apps/ftpc.h> diff --git a/apps/netutils/ftpc/ftpc_rename.c b/apps/netutils/ftpc/ftpc_rename.c index d14bdf3f3..f2355fdd5 100644 --- a/apps/netutils/ftpc/ftpc_rename.c +++ b/apps/netutils/ftpc/ftpc_rename.c @@ -37,7 +37,7 @@ * Included Files ****************************************************************************/ -#include <nuttx/config.h> +#include "ftpc_config.h" #include <stdlib.h> #include <string.h> diff --git a/apps/netutils/ftpc/ftpc_response.c b/apps/netutils/ftpc/ftpc_response.c index 3bf1ee54b..275b48123 100644 --- a/apps/netutils/ftpc/ftpc_response.c +++ b/apps/netutils/ftpc/ftpc_response.c @@ -37,7 +37,7 @@ * Included Files ****************************************************************************/ -#include <nuttx/config.h> +#include "ftpc_config.h" #include <string.h> #include <apps/ftpc.h> diff --git a/apps/netutils/ftpc/ftpc_rmdir.c b/apps/netutils/ftpc/ftpc_rmdir.c index bb86941ae..4a2c27f48 100644 --- a/apps/netutils/ftpc/ftpc_rmdir.c +++ b/apps/netutils/ftpc/ftpc_rmdir.c @@ -37,7 +37,7 @@ * Included Files ****************************************************************************/ -#include <nuttx/config.h> +#include "ftpc_config.h" #include <stdlib.h> #include <string.h> diff --git a/apps/netutils/ftpc/ftpc_socket.c b/apps/netutils/ftpc/ftpc_socket.c index 5747a7c66..129e6f565 100644 --- a/apps/netutils/ftpc/ftpc_socket.c +++ b/apps/netutils/ftpc/ftpc_socket.c @@ -332,13 +332,13 @@ int ftpc_socklisten(struct ftpc_socket_s *sock) * ****************************************************************************/ -int ftpc_sockprintf(struct ftpc_socket_s *sock, const char *str, ...) +int ftpc_sockprintf(struct ftpc_socket_s *sock, const char *fmt, ...) { va_list ap; int r; - va_start(ap, str); - r = vfprintf(sock->outstream, str, ap); + va_start(ap, fmt); + r = vfprintf(sock->outstream, fmt, ap); va_end(ap); return r; } diff --git a/apps/netutils/ftpc/ftpc_unlink.c b/apps/netutils/ftpc/ftpc_unlink.c index 2af5d4f29..cf6733eca 100644 --- a/apps/netutils/ftpc/ftpc_unlink.c +++ b/apps/netutils/ftpc/ftpc_unlink.c @@ -37,7 +37,7 @@ * Included Files ****************************************************************************/ -#include <nuttx/config.h> +#include "ftpc_config.h" #include <apps/ftpc.h> diff --git a/apps/netutils/ftpc/ftpc_utils.c b/apps/netutils/ftpc/ftpc_utils.c index 0b5251fe1..45994532e 100644 --- a/apps/netutils/ftpc/ftpc_utils.c +++ b/apps/netutils/ftpc/ftpc_utils.c @@ -37,7 +37,7 @@ * Included Files ****************************************************************************/ -#include <nuttx/config.h> +#include "ftpc_config.h" #include <stdlib.h> #include <string.h> diff --git a/nuttx/configs/olimex-lpc1766stk/ftpc/defconfig b/nuttx/configs/olimex-lpc1766stk/ftpc/defconfig index abbd6a63e..53301f165 100755 --- a/nuttx/configs/olimex-lpc1766stk/ftpc/defconfig +++ b/nuttx/configs/olimex-lpc1766stk/ftpc/defconfig @@ -322,12 +322,14 @@ CONFIG_HAVE_LIBM=n # thread. Default: CONFIG_IDLETHREAD_STACKSIZE. # CONFIG_SIG_SIGWORK - The signal number that will be used to wake-up # the worker thread. Default: 4 +# CONFIG_SCHED_WAITPID - Enable the waitpid() function # #CONFIG_APPS_DIR= CONFIG_DEBUG=n CONFIG_DEBUG_VERBOSE=n CONFIG_DEBUG_SYMBOLS=n CONFIG_DEBUG_NET=n +CONFIG_DEBUG_FTPC=y CONFIG_MM_REGIONS=2 CONFIG_ARCH_LOWPUTC=y CONFIG_RR_INTERVAL=200 @@ -352,6 +354,7 @@ CONFIG_SCHED_WORKPRIORITY=50 CONFIG_SCHED_WORKPERIOD=(50*1000) CONFIG_SCHED_WORKSTACKSIZE=1024 CONFIG_SIG_SIGWORK=4 +CONFIG_SCHED_WAITPID=y # # Settings for NXFLAT 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 */ } |