diff options
author | Kévin Redon <kredon@sysmocom.de> | 2018-08-28 19:27:34 +0200 |
---|---|---|
committer | Kévin Redon <kredon@sysmocom.de> | 2018-09-03 21:10:58 +0200 |
commit | c171112994117b172ef4394cbb4d85ab6326369b (patch) | |
tree | d12b04b97c88281021587b4557f8e65f09a0a248 | |
parent | 29200c62237594e9ce1d82e1f60298e9f0878e62 (diff) |
stdio: fix detection of malformated format strings
the error code returned by vsnprintf was ignored,
resulting in printing the string from a previous print.
Change-Id: I8506b05d56da55d1357a1234917adf341b46e1db
-rw-r--r-- | firmware/libcommon/source/stdio.c | 26 |
1 files changed, 18 insertions, 8 deletions
diff --git a/firmware/libcommon/source/stdio.c b/firmware/libcommon/source/stdio.c index 2bfaed7..a8612d1 100644 --- a/firmware/libcommon/source/stdio.c +++ b/firmware/libcommon/source/stdio.c @@ -429,12 +429,17 @@ signed int vsprintf(char *pString, const char *pFormat, va_list ap) signed int vfprintf(FILE *pStream, const char *pFormat, va_list ap) { char pStr[MAX_STRING_SIZE]; - char pError[] = "stdio.c: increase MAX_STRING_SIZE\n\r"; // Write formatted string in buffer - if (vsprintf(pStr, pFormat, ap) >= MAX_STRING_SIZE) { - - fputs(pError, stderr); + int rc = vsprintf(pStr, pFormat, ap); + if (rc < 0) { + fputs("format string error in ", stderr); + fputs(pFormat, stderr); + return rc; + } + if (rc >= MAX_STRING_SIZE) { + fputs("stdio.c: increase MAX_STRING_SIZE\n\r", stderr); + return rc; } // Display string @@ -454,12 +459,17 @@ signed int vfprintf(FILE *pStream, const char *pFormat, va_list ap) signed int vfprintf_sync(FILE *pStream, const char *pFormat, va_list ap) { char pStr[MAX_STRING_SIZE]; - char pError[] = "stdio.c: increase MAX_STRING_SIZE\n\r"; // Write formatted string in buffer - if (vsprintf(pStr, pFormat, ap) >= MAX_STRING_SIZE) { - - fputs_sync(pError, stderr); + int rc = vsprintf(pStr, pFormat, ap); + if (rc < 0) { + fputs_sync("format string error in ", stderr); + fputs_sync(pFormat, stderr); + return rc; + } + if (rc >= MAX_STRING_SIZE) { + fputs_sync("stdio.c: increase MAX_STRING_SIZE\n\r", stderr); + return rc; } // Display string |