[PATCH v5 13/14] lib: sbi: Fix missing '\0' when buffer szie equal 1

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


Fix special case: sbi_snprintf(out, 1, ...), The previous code will
not fill the buffer with any char.

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

diff --git a/lib/sbi/sbi_console.c b/lib/sbi/sbi_console.c
index 2eefee1..f7fd761 100644
--- a/lib/sbi/sbi_console.c
+++ b/lib/sbi/sbi_console.c
@@ -157,6 +157,14 @@ static void printc(struct print_info *info, char ch)
 	/* The buffer is long enough to add new characters */
 	if (info->len - info->pos > 1)
 		goto append;
+
+	/*
+	 * special case: sbi_snprintf(out, 1, ...);
+	 * This case never jumps to append, so add '\0' here
+	 */
+	if (info->pos == 0)
+		info->out[0] = '\0';
+
 	return;
 
 append:
-- 
2.39.2




More information about the opensbi mailing list