diff mbox series

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

Message ID 20230609033624.84865-1-wxjstz@126.com
State Superseded
Headers show
Series Improve sbi_console | expand

Commit Message

Xiang W June 9, 2023, 3:36 a.m. UTC
Fix special case: sbi_snprintf(out, 1, ...), The previous code will
not fill the buffer with any char.

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

Patch

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: