Change in simtrace2[master]: stdio: fix detection of malformated format strings

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

Kévin Redon gerrit-no-reply at lists.osmocom.org
Tue Aug 28 17:44:06 UTC 2018


Kévin Redon has uploaded this change for review. ( https://gerrit.osmocom.org/10671


Change subject: stdio: fix detection of malformated format strings
......................................................................

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
---
M firmware/libcommon/source/stdio.c
1 file changed, 21 insertions(+), 8 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/simtrace2 refs/changes/71/10671/1

diff --git a/firmware/libcommon/source/stdio.c b/firmware/libcommon/source/stdio.c
index 2bfaed7..78517de 100644
--- a/firmware/libcommon/source/stdio.c
+++ b/firmware/libcommon/source/stdio.c
@@ -401,6 +401,9 @@
 
 	va_start(ap, pFormat);
 	rc = vsnprintf(pString, length, pFormat, ap);
+	if (rc < 0) { // format string error
+		return rc;
+	}
 	va_end(ap);
 
 	return rc;
@@ -429,12 +432,17 @@
 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 +462,17 @@
 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

-- 
To view, visit https://gerrit.osmocom.org/10671
To unsubscribe, or for help writing mail filters, visit https://gerrit.osmocom.org/settings

Gerrit-Project: simtrace2
Gerrit-Branch: master
Gerrit-MessageType: newchange
Gerrit-Change-Id: I8506b05d56da55d1357a1234917adf341b46e1db
Gerrit-Change-Number: 10671
Gerrit-PatchSet: 1
Gerrit-Owner: Kévin Redon <kredon at sysmocom.de>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osmocom.org/pipermail/gerrit-log/attachments/20180828/4f999c7b/attachment.htm>


More information about the gerrit-log mailing list