[PATCH v5 14/14] lib: sbi: Fix invalid directive print

Xiang W wxjstz at 126.com
Thu Jun 8 20:36:43 PDT 2023


before fix:
> printf("%1234"); /* print noting */
after fix:
> printf("%1234"); /* print %1234 */

Signed-off-by: Xiang W <wxjstz at 126.com>
---
 lib/sbi/sbi_console.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/lib/sbi/sbi_console.c b/lib/sbi/sbi_console.c
index f7fd761..a4ee1e5 100644
--- a/lib/sbi/sbi_console.c
+++ b/lib/sbi/sbi_console.c
@@ -266,6 +266,7 @@ static void print(struct print_info *info, const char *format, va_list args)
 {
 	bool flags_done;
 	char scr[2];
+	const char *format_bak;
 	bool use_tbuf = (!info->out) ? true : false;
 	info->pos = 0;
 	info->pc = 0;
@@ -285,6 +286,8 @@ static void print(struct print_info *info, const char *format, va_list args)
 		if (use_tbuf)
 			info->flags |= USED_TBUF;
 		if (*format == '%') {
+			/* need restore when the directive is invalid */
+			format_bak = format;
 			++format;
 			if (*format == '\0')
 				break;
@@ -377,10 +380,11 @@ static void print(struct print_info *info, const char *format, va_list args)
 				prints(info, scr);
 				continue;
 			}
-		} else {
-literal:
-			printc(info, *format);
+			/* invalid directive */
+			format = format_bak;
 		}
+literal:
+		printc(info, *format);
 	}
 
 	if (use_tbuf && info->pos > 0)
-- 
2.39.2




More information about the opensbi mailing list