Skip to contentSkip to conversation

Improve sbi_console

PATCH Superseded Xiang W 1 participant 15 messages Jun 8, 2023

v4 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

Xiang W

author
#0 lore raw

v4 cover Improve sbi_console

v4 changes:
- PATCH10: Simplify printc
- add PATCH11, handle special case

v3 changes:
- PATCH11: Simplify get info->type
- PATCH12: Change the way to keep USED_TBUF by and to reset it by use_tbuf

v2 changes:
- add patch 10-12

Xiang W (13):
lib: sbi: Fix missing contine in print
lib: sbi: Fix how print gets flags
lib: sbi: print not fill '0' when left-aligned
lib: sbi: Add '+' flags for print
lib: sbi: Add ' ' '\'' flags for print
lib: sbi: implifying the parameters of printi
lib: sbi: print add 'o' type
lib: sbi: Fix printi
lib: sbi: Simplify prints
lib: sbi: sbi_console.c code re-layout
lib: sbi: Add print_info for sbi_console.c
lib: sbi: Fix timing of clearing tbuf
lib: sbi: Fix missing '\0' when buffer szie equal 1

 lib/sbi/sbi_console.c | 413 ++++++++++++++++++++++--------------------
 1 file changed, 212 insertions(+), 201 deletions(-)
Signature · 1 line
2.39.2
author
#1 lore raw

PATCH v4 1/13 lib: sbi: Fix missing contine in print

Missing continue after print %lu %ld %lx ...

---
 lib/sbi/sbi_console.c | 1 +
 1 file changed, 1 insertion(+)
lib/sbi/sbi_console.c +1 -0
index 168dffd..1f0727c 100644
@@ -370,6 +370,7 @@ static int print(char **out, u32 *out_len, const char *format, va_list args)
 						     va_arg(args, long), 10, 1,
 						     width, flags, '0');
 				}
+				continue;
 			}
 			if (*format == 'c') {
 				/* char are converted to int then pushed on the stack */
Signature · 1 line
2.39.2
author
#2 lore raw

PATCH v4 2/13 lib: sbi: Fix how print gets flags

In reply to Xiang W

The flags for print should be able to appear in any order. The previous code required the order to be fixed.

---
 lib/sbi/sbi_console.c | 33 +++++++++++++++++++++------------
 1 file changed, 21 insertions(+), 12 deletions(-)
lib/sbi/sbi_console.c +21 -12
index 1f0727c..97ee328 100644
@@ -236,6 +236,7 @@ static int printi(char **out, u32 *out_len, long long i, int b, int sg,
 
 static int print(char **out, u32 *out_len, const char *format, va_list args)
 {
+	bool flags_done;
 	int width, flags, pc = 0;
 	char scr[2], *tout;
 	bool use_tbuf = (!out) ? true : false;
@@ -262,25 +263,33 @@ static int print(char **out, u32 *out_len, const char *format, va_list args)
 
 		if (*format == '%') {
 			++format;
-			width = flags = 0;
 			if (*format == '\0')
 				break;
 			if (*format == '%')
 				goto literal;
 			/* Get flags */
-			if (*format == '-') {
-				++format;
-				flags = PAD_RIGHT;
-			}
-			if (*format == '#') {
-				++format;
-				flags |= PAD_ALTERNATE;
-			}
-			while (*format == '0') {
-				++format;
-				flags |= PAD_ZERO;
+			flags = 0;
+			flags_done = false;
+			while (!flags_done) {
+				switch (*format) {
+				case '-':
+					flags |= PAD_RIGHT;
+					break;
+				case '#':
+					flags |= PAD_ALTERNATE;
+					break;
+				case '0':
+					flags |= PAD_ZERO;
+					break;
+				default:
+					flags_done = true;
+					break;
+				}
+				if (!flags_done)
+					++format;
 			}
 			/* Get width */
+			width = 0;
 			for (; *format >= '0' && *format <= '9'; ++format) {
 				width *= 10;
 				width += *format - '0';
Signature · 1 line
2.39.2
author
#3 lore raw

PATCH v4 3/13 lib: sbi: print not fill '0' when left-aligned

In reply to Xiang W

Left alignment and padding '0' should not exist at the same time, this patch is not padded with 0 when left aligned.

---
 lib/sbi/sbi_console.c | 2 ++
 1 file changed, 2 insertions(+)
lib/sbi/sbi_console.c +2 -0
index 97ee328..b18f9c5 100644
@@ -288,6 +288,8 @@ static int print(char **out, u32 *out_len, const char *format, va_list args)
 				if (!flags_done)
 					++format;
 			}
+			if (flags & PAD_RIGHT)
+				flags &= ~PAD_ZERO;
 			/* Get width */
 			width = 0;
 			for (; *format >= '0' && *format <= '9'; ++format) {
Signature · 1 line
2.39.2
author
#4 lore raw

PATCH v4 4/13 lib: sbi: Add '+' flags for print

In reply to Xiang W
---
 lib/sbi/sbi_console.c | 25 ++++++++++++++++---------
 1 file changed, 16 insertions(+), 9 deletions(-)
lib/sbi/sbi_console.c +16 -9
index b18f9c5..a616316 100644
@@ -120,6 +120,7 @@ unsigned long sbi_ngets(char *str, unsigned long len)
 #define PAD_RIGHT 1
 #define PAD_ZERO 2
 #define PAD_ALTERNATE 4
+#define PAD_SIGN 8
 #define PRINT_BUF_LEN 64
 
 #define va_start(v, l) __builtin_va_start((v), l)
@@ -186,15 +187,18 @@ static int prints(char **out, u32 *out_len, const char *string, int width,
 static int printi(char **out, u32 *out_len, long long i, int b, int sg,
 		  int width, int flags, int letbase)
 {
-	char print_buf[PRINT_BUF_LEN];
-	char *s;
-	int neg = 0, pc = 0;
+	char *s, sign, print_buf[PRINT_BUF_LEN];
+	int pc = 0;
 	u64 t;
 	unsigned long long u = i;
 
-	if (sg && b == 10 && i < 0) {
-		neg = 1;
-		u   = -i;
+	if (sg && b == 10) {
+		if ((flags & PAD_SIGN) && i > 0)
+			sign = '+';
+		if (i < 0) {
+			sign = '-';
+			u = -i;
+		}
 	}
 
 	s  = print_buf + PRINT_BUF_LEN - 1;
@@ -221,13 +225,13 @@ static int printi(char **out, u32 *out_len, long long i, int b, int sg,
 		*--s = '0';
 	}
 
-	if (neg) {
+	if (sign) {
 		if (width && (flags & PAD_ZERO)) {
-			printc(out, out_len, '-');
+			printc(out, out_len, sign);
 			++pc;
 			--width;
 		} else {
-			*--s = '-';
+			*--s = sign;
 		}
 	}
 
@@ -275,6 +279,9 @@ static int print(char **out, u32 *out_len, const char *format, va_list args)
 				case '-':
 					flags |= PAD_RIGHT;
 					break;
+				case '+':
+					flags |= PAD_SIGN;
+					break;
 				case '#':
 					flags |= PAD_ALTERNATE;
 					break;
Signature · 1 line
2.39.2
author
#5 lore raw

PATCH v4 5/13 lib: sbi: Add ' ' '\'' flags for print

In reply to Xiang W
---
 lib/sbi/sbi_console.c | 4 ++++
 1 file changed, 4 insertions(+)
lib/sbi/sbi_console.c +4 -0
index a616316..7f32f81 100644
@@ -288,6 +288,10 @@ static int print(char **out, u32 *out_len, const char *format, va_list args)
 				case '0':
 					flags |= PAD_ZERO;
 					break;
+				case ' ':
+				case '\'':
+					/* Ignored flags, do nothing */
+					break;
 				default:
 					flags_done = true;
 					break;
Signature · 1 line
2.39.2
author
#6 lore raw

PATCH v4 6/13 lib: sbi: implifying the parameters of printi

In reply to Xiang W

The information of sg/b/letbase can be obtained by the type character, simplifying the parameter by passing the type directly.

---
 lib/sbi/sbi_console.c | 126 ++++++++++++++++--------------------------
 1 file changed, 49 insertions(+), 77 deletions(-)
lib/sbi/sbi_console.c +49 -77
index 7f32f81..a9a2ed6 100644
@@ -184,15 +184,26 @@ static int prints(char **out, u32 *out_len, const char *string, int width,
 	return pc;
 }
 
-static int printi(char **out, u32 *out_len, long long i, int b, int sg,
-		  int width, int flags, int letbase)
+static int printi(char **out, u32 *out_len, long long i,
+		  int width, int flags, int type)
 {
-	char *s, sign, print_buf[PRINT_BUF_LEN];
 	int pc = 0;
-	u64 t;
-	unsigned long long u = i;
+	char *s, sign, letbase, print_buf[PRINT_BUF_LEN];
+	unsigned long long u, b, t;
+
+	b = 10;
+	letbase = 'a';
+	if (type == 'o')
+		b = 8;
+	else if (type == 'x' || type == 'X' || type == 'p' || type == 'P') {
+		b = 16;
+		letbase &= ~0x20;
+		letbase |= type & 0x20;
+	}
 
-	if (sg && b == 10) {
+	u = i;
+	sign = 0;
+	if (type == 'i' || type == 'd') {
 		if ((flags & PAD_SIGN) && i > 0)
 			sign = '+';
 		if (i < 0) {
@@ -242,9 +253,8 @@ static int print(char **out, u32 *out_len, const char *format, va_list args)
 {
 	bool flags_done;
 	int width, flags, pc = 0;
-	char scr[2], *tout;
+	char type, scr[2], *tout;
 	bool use_tbuf = (!out) ? true : false;
-	unsigned long long tmp;
 
 	/*
 	 * The console_tbuf is protected by console_out_lock and
@@ -315,83 +325,45 @@ static int print(char **out, u32 *out_len, const char *format, va_list args)
 			}
 			if ((*format == 'd') || (*format == 'i')) {
 				pc += printi(out, out_len, va_arg(args, int),
-					     10, 1, width, flags, '0');
-				continue;
-			}
-			if (*format == 'x') {
-				pc += printi(out, out_len,
-					     va_arg(args, unsigned int), 16, 0,
-					     width, flags, 'a');
-				continue;
-			}
-			if (*format == 'X') {
-				pc += printi(out, out_len,
-					     va_arg(args, unsigned int), 16, 0,
-					     width, flags, 'A');
+					     width, flags, *format);
 				continue;
 			}
-			if (*format == 'u') {
-				pc += printi(out, out_len,
-					     va_arg(args, unsigned int), 10, 0,
-					     width, flags, 'a');
+			if ((*format == 'u') || (*format == 'x') || (*format == 'X')) {
+				pc += printi(out, out_len, va_arg(args, unsigned int),
+					     width, flags, *format);
 				continue;
 			}
-			if (*format == 'p') {
-				pc += printi(out, out_len,
-					     va_arg(args, unsigned long), 16, 0,
-					     width, flags, 'a');
+			if ((*format == 'p') || (*format == 'P')) {
+				pc += printi(out, out_len, (uintptr_t)va_arg(args, void*),
+					     width, flags, *format);
 				continue;
 			}
-			if (*format == 'P') {
-				pc += printi(out, out_len,
-					     va_arg(args, unsigned long), 16, 0,
-					     width, flags, 'A');
-				continue;
-			}
-			if (*format == 'l' && *(format + 1) == 'l') {
-				tmp = va_arg(args, unsigned long long);
-				if (*(format + 2) == 'u') {
-					format += 2;
-					pc += printi(out, out_len, tmp, 10, 0,
-						     width, flags, 'a');
-				} else if (*(format + 2) == 'x') {
-					format += 2;
-					pc += printi(out, out_len, tmp, 16, 0,
-						     width, flags, 'a');
-				} else if (*(format + 2) == 'X') {
-					format += 2;
-					pc += printi(out, out_len, tmp, 16, 0,
-						     width, flags, 'A');
-				} else {
-					format += 1;
-					pc += printi(out, out_len, tmp, 10, 1,
-						     width, flags, '0');
+			if (*format == 'l') {
+				type = 'i';
+				if (format[1] == 'l') {
+					++format;
+					if ((format[1] == 'u')
+							|| (format[1] == 'd') || (format[1] == 'i')
+							|| (format[1] == 'x') || (format[1] == 'X')) {
+						++format;
+						type = *format;
+					}
+					pc += printi(out, out_len, va_arg(args, long long),
+						width, flags, type);
+					continue;
 				}
-				continue;
-			} else if (*format == 'l') {
-				if (*(format + 1) == 'u') {
-					format += 1;
-					pc += printi(
-						out, out_len,
-						va_arg(args, unsigned long), 10,
-						0, width, flags, 'a');
-				} else if (*(format + 1) == 'x') {
-					format += 1;
-					pc += printi(
-						out, out_len,
-						va_arg(args, unsigned long), 16,
-						0, width, flags, 'a');
-				} else if (*(format + 1) == 'X') {
-					format += 1;
-					pc += printi(
-						out, out_len,
-						va_arg(args, unsigned long), 16,
-						0, width, flags, 'A');
-				} else {
-					pc += printi(out, out_len,
-						     va_arg(args, long), 10, 1,
-						     width, flags, '0');
+				if ((format[1] == 'u')
+						|| (format[1] == 'd') || (format[1] == 'i')
+						|| (format[1] == 'x') || (format[1] == 'X')) {
+					++format;
+					type = *format;
 				}
+				if ((type == 'd') || (type == 'i'))
+					pc += printi(out, out_len, va_arg(args, long),
+					     width, flags, type);
+				else
+					pc += printi(out, out_len, va_arg(args, unsigned long),
+					     width, flags, type);
 				continue;
 			}
 			if (*format == 'c') {
Signature · 1 line
2.39.2
author
#7 lore raw

PATCH v4 7/13 lib: sbi: print add 'o' type

In reply to Xiang W
---
 lib/sbi/sbi_console.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
lib/sbi/sbi_console.c +4 -3
index a9a2ed6..047c51e 100644
@@ -328,7 +328,8 @@ static int print(char **out, u32 *out_len, const char *format, va_list args)
 					     width, flags, *format);
 				continue;
 			}
-			if ((*format == 'u') || (*format == 'x') || (*format == 'X')) {
+			if ((*format == 'u') || (*format == 'o')
+					 || (*format == 'x') || (*format == 'X')) {
 				pc += printi(out, out_len, va_arg(args, unsigned int),
 					     width, flags, *format);
 				continue;
@@ -342,7 +343,7 @@ static int print(char **out, u32 *out_len, const char *format, va_list args)
 				type = 'i';
 				if (format[1] == 'l') {
 					++format;
-					if ((format[1] == 'u')
+					if ((format[1] == 'u') || (format[1] == 'o')
 							|| (format[1] == 'd') || (format[1] == 'i')
 							|| (format[1] == 'x') || (format[1] == 'X')) {
 						++format;
@@ -352,7 +353,7 @@ static int print(char **out, u32 *out_len, const char *format, va_list args)
 						width, flags, type);
 					continue;
 				}
-				if ((format[1] == 'u')
+				if ((format[1] == 'u') || (format[1] == 'o')
 						|| (format[1] == 'd') || (format[1] == 'i')
 						|| (format[1] == 'x') || (format[1] == 'X')) {
 					++format;
Signature · 1 line
2.39.2
author
#8 lore raw

PATCH v4 8/13 lib: sbi: Fix printi

In reply to Xiang W

Fix two bug:

Quoted 2 lines

printf("%#08x", 0x123); /* print 0000x123 */
printf("%#x", 0); /* print 0x0 */

---
 lib/sbi/sbi_console.c | 36 +++++++++++++++++++++++-------------
 1 file changed, 23 insertions(+), 13 deletions(-)
lib/sbi/sbi_console.c +23 -13
index 047c51e..6f714f0 100644
@@ -227,23 +227,33 @@ static int printi(char **out, u32 *out_len, long long i,
 		}
 	}
 
-	if (flags & PAD_ALTERNATE) {
-		if ((b == 16) && (letbase == 'A')) {
-			*--s = 'X';
-		} else if ((b == 16) && (letbase == 'a')) {
-			*--s = 'x';
-		}
-		*--s = '0';
-	}
-
-	if (sign) {
-		if (width && (flags & PAD_ZERO)) {
+	if (flags & PAD_ZERO) {
+		if (sign) {
 			printc(out, out_len, sign);
 			++pc;
 			--width;
-		} else {
-			*--s = sign;
 		}
+		if (i && (flags & PAD_ALTERNATE)) {
+			if (b == 16 || b == 8) {
+				printc(out, out_len, '0');
+				++pc;
+				--width;
+			}
+			if (b == 16) {
+				printc(out, out_len, 'x' - 'a' + letbase);
+				++pc;
+				--width;
+			}
+		}
+	} else {
+		if (i && (flags & PAD_ALTERNATE)) {
+			if (b == 16)
+				*--s = 'x' - 'a' + letbase;
+			if (b == 16 || b == 8)
+				*--s = '0';
+		}
+		if (sign)
+			*--s = sign;
 	}
 
 	return pc + prints(out, out_len, s, width, flags);
Signature · 1 line
2.39.2
author
#9 lore raw

PATCH v4 9/13 lib: sbi: Simplify prints

In reply to Xiang W

When doing width = width - strlen(string) in prints there is no need to consider the case that witdh may be less than 0. This is because the code to do filling needs to be executed under the condition that width > 0.

---
 lib/sbi/sbi_console.c | 21 ++++-----------------
 1 file changed, 4 insertions(+), 17 deletions(-)
lib/sbi/sbi_console.c +4 -17
index 6f714f0..824ff6e 100644
@@ -151,24 +151,11 @@ static void printc(char **out, u32 *out_len, char ch)
 static int prints(char **out, u32 *out_len, const char *string, int width,
 		  int flags)
 {
-	int pc	     = 0;
-	char padchar = ' ';
-
-	if (width > 0) {
-		int len = 0;
-		const char *ptr;
-		for (ptr = string; *ptr; ++ptr)
-			++len;
-		if (len >= width)
-			width = 0;
-		else
-			width -= len;
-		if (flags & PAD_ZERO)
-			padchar = '0';
-	}
+	int pc = 0;
+	width -= sbi_strlen(string);
 	if (!(flags & PAD_RIGHT)) {
 		for (; width > 0; --width) {
-			printc(out, out_len, padchar);
+			printc(out, out_len, flags & PAD_ZERO ? '0' : ' ');
 			++pc;
 		}
 	}
@@ -177,7 +164,7 @@ static int prints(char **out, u32 *out_len, const char *string, int width,
 		++pc;
 	}
 	for (; width > 0; --width) {
-		printc(out, out_len, padchar);
+		printc(out, out_len, ' ');
 		++pc;
 	}
 
Signature · 1 line
2.39.2
author
#10 lore raw

PATCH v4 10/13 lib: sbi: sbi_console.c code re-layout

In reply to Xiang W

Move the type definitions and macros in sbi_console.c to the begin of the file.

---
 lib/sbi/sbi_console.c | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)
lib/sbi/sbi_console.c +11 -11
index 824ff6e..af93f51 100644
@@ -14,8 +14,19 @@
 #include <sbi/sbi_scratch.h>
 #include <sbi/sbi_string.h>
 
+#define PAD_RIGHT 1
+#define PAD_ZERO 2
+#define PAD_ALTERNATE 4
+#define PAD_SIGN 8
+#define PRINT_BUF_LEN 64
 #define CONSOLE_TBUF_MAX 256
 
+#define va_start(v, l) __builtin_va_start((v), l)
+#define va_end __builtin_va_end
+#define va_arg __builtin_va_arg
+
+typedef __builtin_va_list va_list;
+
 static const struct sbi_console_device *console_dev = NULL;
 static char console_tbuf[CONSOLE_TBUF_MAX];
 static u32 console_tbuf_len;
@@ -117,17 +128,6 @@ unsigned long sbi_ngets(char *str, unsigned long len)
 	return i;
 }
 
-#define PAD_RIGHT 1
-#define PAD_ZERO 2
-#define PAD_ALTERNATE 4
-#define PAD_SIGN 8
-#define PRINT_BUF_LEN 64
-
-#define va_start(v, l) __builtin_va_start((v), l)
-#define va_end __builtin_va_end
-#define va_arg __builtin_va_arg
-typedef __builtin_va_list va_list;
-
 static void printc(char **out, u32 *out_len, char ch)
 {
 	if (!out) {
Signature · 1 line
2.39.2
author
#11 lore raw

PATCH v4 11/13 lib: sbi: Add print_info for sbi_console.c

In reply to Xiang W

Create a structure print_info for print, this structure simplifies parameter passing.

---
 lib/sbi/sbi_console.c | 232 ++++++++++++++++++++++--------------------
 1 file changed, 119 insertions(+), 113 deletions(-)
lib/sbi/sbi_console.c +119 -113
index af93f51..7f6ab14 100644
@@ -27,11 +27,22 @@
 
 typedef __builtin_va_list va_list;
 
+struct print_info {
+	char *out;
+	int len;
+	int pos;
+	int pc;
+	int flags;
+	int width;
+	int wcnt;
+	char type;
+};
+
 static const struct sbi_console_device *console_dev = NULL;
 static char console_tbuf[CONSOLE_TBUF_MAX];
-static u32 console_tbuf_len;
 static spinlock_t console_out_lock	       = SPIN_LOCK_INITIALIZER;
 
+
 bool sbi_isprintable(char c)
 {
 	if (((31 < c) && (c < 127)) || (c == '\f') || (c == '\r') ||
@@ -128,70 +139,63 @@ unsigned long sbi_ngets(char *str, unsigned long len)
 	return i;
 }
 
-static void printc(char **out, u32 *out_len, char ch)
+static void printc(struct print_info *info, char ch)
 {
-	if (!out) {
+	/* output to the console */
+	if (info->out == NULL) {
 		sbi_putc(ch);
+		info->pc++;
 		return;
 	}
 
-	/*
-	 * The *printf entry point functions have enforced that (*out) can
-	 * only be null when out_len is non-null and its value is zero.
-	 */
-	if (!out_len || *out_len > 1) {
-		*(*out)++ = ch;
-		**out = '\0';
-	}
+	/* unknown buffer length */
+	if (info->len == 0)
+		goto append;
+
+	/* The buffer is long enough to add new characters */
+	if (info->len - info->pos > 1)
+		goto append;
+	return;
 
-	if (out_len && *out_len > 0)
-		--(*out_len);
+append:
+	info->out[info->pos++] = ch;
+	info->out[info->pos] = '\0';
+	info->pc++;
 }
 
-static int prints(char **out, u32 *out_len, const char *string, int width,
-		  int flags)
+static void prints(struct print_info *info, const char *string)
 {
-	int pc = 0;
-	width -= sbi_strlen(string);
-	if (!(flags & PAD_RIGHT)) {
-		for (; width > 0; --width) {
-			printc(out, out_len, flags & PAD_ZERO ? '0' : ' ');
-			++pc;
-		}
-	}
-	for (; *string; ++string) {
-		printc(out, out_len, *string);
-		++pc;
+	info->wcnt += sbi_strlen(string);
+	if (!(info->flags & PAD_RIGHT)) {
+		for (; info->wcnt < info->width; info->wcnt++)
+			printc(info, info->flags & PAD_ZERO ? '0' : ' ');
 	}
-	for (; width > 0; --width) {
-		printc(out, out_len, ' ');
-		++pc;
-	}
-
-	return pc;
+	for (; *string; ++string)
+		printc(info, *string);
+	for (; info->wcnt < info->width; info->wcnt++)
+		printc(info, ' ');
 }
 
-static int printi(char **out, u32 *out_len, long long i,
-		  int width, int flags, int type)
+static void printi(struct print_info *info, long long i)
 {
-	int pc = 0;
 	char *s, sign, letbase, print_buf[PRINT_BUF_LEN];
 	unsigned long long u, b, t;
 
 	b = 10;
 	letbase = 'a';
-	if (type == 'o')
+	if (info->type == 'o')
 		b = 8;
-	else if (type == 'x' || type == 'X' || type == 'p' || type == 'P') {
+	else if (info->type == 'x' || info->type == 'X'
+			 || info->type == 'p' || info->type == 'P') {
 		b = 16;
 		letbase &= ~0x20;
-		letbase |= type & 0x20;
+		letbase |= info->type & 0x20;
 	}
 
 	u = i;
 	sign = 0;
-	if (type == 'i' || type == 'd') {
-		if ((flags & PAD_SIGN) && i > 0)
+	if (info->type == 'i' || info->type == 'd') {
+		if ((info->flags & PAD_SIGN) && i > 0)
 			sign = '+';
 		if (i < 0) {
 			sign = '-';
@@ -214,26 +218,23 @@ static int printi(char **out, u32 *out_len, long long i,
 		}
 	}
 
-	if (flags & PAD_ZERO) {
+	if (info->flags & PAD_ZERO) {
 		if (sign) {
-			printc(out, out_len, sign);
-			++pc;
-			--width;
+			printc(info, sign);
+			++info->wcnt;
 		}
-		if (i && (flags & PAD_ALTERNATE)) {
+		if (i && (info->flags & PAD_ALTERNATE)) {
 			if (b == 16 || b == 8) {
-				printc(out, out_len, '0');
-				++pc;
-				--width;
+				printc(info, '0');
+				++info->wcnt;
 			}
 			if (b == 16) {
-				printc(out, out_len, 'x' - 'a' + letbase);
-				++pc;
-				--width;
+				printc(info, 'x' - 'a' + letbase);
+				++info->wcnt;
 			}
 		}
 	} else {
-		if (i && (flags & PAD_ALTERNATE)) {
+		if (i && (info->flags & PAD_ALTERNATE)) {
 			if (b == 16)
 				*--s = 'x' - 'a' + letbase;
 			if (b == 16 || b == 8)
@@ -243,15 +244,16 @@ static int printi(char **out, u32 *out_len, long long i,
 			*--s = sign;
 	}
 
-	return pc + prints(out, out_len, s, width, flags);
+	prints(info, s);
 }
 
-static int print(char **out, u32 *out_len, const char *format, va_list args)
+static void print(struct print_info *info, const char *format, va_list args)
 {
 	bool flags_done;
-	int width, flags, pc = 0;
-	char type, scr[2], *tout;
-	bool use_tbuf = (!out) ? true : false;
+	char scr[2];
+	bool use_tbuf = (!info->out) ? true : false;
+	info->pos = 0;
+	info->pc = 0;
 
 	/*
 	 * The console_tbuf is protected by console_out_lock and
@@ -259,17 +261,14 @@ static int print(char **out, u32 *out_len, const char *format, va_list args)
 	 * when out == NULL.
 	 */
 	if (use_tbuf) {
-		console_tbuf_len = CONSOLE_TBUF_MAX;
-		tout = console_tbuf;
-		out = &tout;
-		out_len = &console_tbuf_len;
+		info->out = console_tbuf;
+		info->len = CONSOLE_TBUF_MAX;
 	}
 
 	for (; *format != 0; ++format) {
-		if (use_tbuf && !console_tbuf_len) {
-			nputs_all(console_tbuf, CONSOLE_TBUF_MAX);
-			console_tbuf_len = CONSOLE_TBUF_MAX;
-			tout = console_tbuf;
+		if (use_tbuf && (info->len - info->pos <= 1)) {
+			nputs_all(info->out, info->pos);
+			info->pos = 0;
 		}
 
 		if (*format == '%') {
@@ -279,21 +278,21 @@ static int print(char **out, u32 *out_len, const char *format, va_list args)
 			if (*format == '%')
 				goto literal;
 			/* Get flags */
-			flags = 0;
+			info->flags = 0;
 			flags_done = false;
 			while (!flags_done) {
 				switch (*format) {
 				case '-':
-					flags |= PAD_RIGHT;
+					info->flags |= PAD_RIGHT;
 					break;
 				case '+':
-					flags |= PAD_SIGN;
+					info->flags |= PAD_SIGN;
 					break;
 				case '#':
-					flags |= PAD_ALTERNATE;
+					info->flags |= PAD_ALTERNATE;
 					break;
 				case '0':
-					flags |= PAD_ZERO;
+					info->flags |= PAD_ZERO;
 					break;
 				case ' ':
 				case '\'':
@@ -306,153 +305,160 @@ static int print(char **out, u32 *out_len, const char *format, va_list args)
 				if (!flags_done)
 					++format;
 			}
-			if (flags & PAD_RIGHT)
-				flags &= ~PAD_ZERO;
+			if (info->flags & PAD_RIGHT)
+				info->flags &= ~PAD_ZERO;
 			/* Get width */
-			width = 0;
+			info->wcnt = 0;
+			info->width = 0;
 			for (; *format >= '0' && *format <= '9'; ++format) {
-				width *= 10;
-				width += *format - '0';
+				info->width *= 10;
+				info->width += *format - '0';
 			}
+			info->type = *format;
 			if (*format == 's') {
 				char *s = va_arg(args, char *);
-				pc += prints(out, out_len, s ? s : "(null)",
-					     width, flags);
+				prints(info, s ? s : "(null)");
 				continue;
 			}
 			if ((*format == 'd') || (*format == 'i')) {
-				pc += printi(out, out_len, va_arg(args, int),
-					     width, flags, *format);
+				printi(info, va_arg(args, int));
 				continue;
 			}
 			if ((*format == 'u') || (*format == 'o')
 					 || (*format == 'x') || (*format == 'X')) {
-				pc += printi(out, out_len, va_arg(args, unsigned int),
-					     width, flags, *format);
+				printi(info, va_arg(args, unsigned int));
 				continue;
 			}
 			if ((*format == 'p') || (*format == 'P')) {
-				pc += printi(out, out_len, (uintptr_t)va_arg(args, void*),
-					     width, flags, *format);
+				printi(info, (uintptr_t)va_arg(args, void*));
 				continue;
 			}
 			if (*format == 'l') {
-				type = 'i';
+				info->type = 'i';
 				if (format[1] == 'l') {
 					++format;
 					if ((format[1] == 'u') || (format[1] == 'o')
 							|| (format[1] == 'd') || (format[1] == 'i')
 							|| (format[1] == 'x') || (format[1] == 'X')) {
 						++format;
-						type = *format;
+						info->type = *format;
 					}
-					pc += printi(out, out_len, va_arg(args, long long),
-						width, flags, type);
+					printi(info, va_arg(args, long long));
 					continue;
 				}
 				if ((format[1] == 'u') || (format[1] == 'o')
 						|| (format[1] == 'd') || (format[1] == 'i')
 						|| (format[1] == 'x') || (format[1] == 'X')) {
 					++format;
-					type = *format;
+					info->type = *format;
 				}
-				if ((type == 'd') || (type == 'i'))
-					pc += printi(out, out_len, va_arg(args, long),
-					     width, flags, type);
+				if ((info->type == 'd') || (info->type == 'i'))
+					printi(info, va_arg(args, long));
 				else
-					pc += printi(out, out_len, va_arg(args, unsigned long),
-					     width, flags, type);
+					printi(info, va_arg(args, unsigned long));
 				continue;
 			}
 			if (*format == 'c') {
 				/* char are converted to int then pushed on the stack */
 				scr[0] = va_arg(args, int);
 				scr[1] = '\0';
-				pc += prints(out, out_len, scr, width, flags);
+				prints(info, scr);
 				continue;
 			}
 		} else {
 literal:
-			printc(out, out_len, *format);
-			++pc;
+			printc(info, *format);
 		}
 	}
 
-	if (use_tbuf && console_tbuf_len < CONSOLE_TBUF_MAX)
-		nputs_all(console_tbuf, CONSOLE_TBUF_MAX - console_tbuf_len);
-
-	return pc;
+	if (use_tbuf && info->pos > 0)
+		nputs_all(info->out, info->pos);
 }
 
 int sbi_sprintf(char *out, const char *format, ...)
 {
 	va_list args;
-	int retval;
+	struct print_info info;
+	info.out = out;
+	info.len = 0;
+
 
 	if (unlikely(!out))
 		sbi_panic("sbi_sprintf called with NULL output string\n");
 
 	va_start(args, format);
-	retval = print(&out, NULL, format, args);
+	print(&info, format, args);
 	va_end(args);
 
-	return retval;
+	return info.pc;
 }
 
 int sbi_snprintf(char *out, u32 out_sz, const char *format, ...)
 {
 	va_list args;
-	int retval;
+	struct print_info info;
+	info.out = out;
+	info.len = out_sz;
+
 
 	if (unlikely(!out && out_sz != 0))
 		sbi_panic("sbi_snprintf called with NULL output string and "
 			  "output size is not zero\n");
 
 	va_start(args, format);
-	retval = print(&out, &out_sz, format, args);
+	print(&info, format, args);
 	va_end(args);
 
-	return retval;
+	return info.pc;
 }
 
 int sbi_printf(const char *format, ...)
 {
 	va_list args;
-	int retval;
+	struct print_info info;
+	info.out = NULL;
+	info.len = 0;
+
 
 	spin_lock(&console_out_lock);
 	va_start(args, format);
-	retval = print(NULL, NULL, format, args);
+	print(&info, format, args);
 	va_end(args);
 	spin_unlock(&console_out_lock);
 
-	return retval;
+	return info.pc;
 }
 
 int sbi_dprintf(const char *format, ...)
 {
 	va_list args;
-	int retval = 0;
 	struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
+	struct print_info info;
+	info.out = NULL;
+	info.len = 0;
+
 
 	va_start(args, format);
 	if (scratch->options & SBI_SCRATCH_DEBUG_PRINTS) {
 		spin_lock(&console_out_lock);
-		retval = print(NULL, NULL, format, args);
+		print(&info, format, args);
 		spin_unlock(&console_out_lock);
 	}
 	va_end(args);
 
-	return retval;
+	return info.pc;
 }
 
 void sbi_panic(const char *format, ...)
 {
 	va_list args;
+	struct print_info info;
+	info.out = NULL;
+	info.len = 0;
 
 	spin_lock(&console_out_lock);
 	va_start(args, format);
-	print(NULL, NULL, format, args);
+	print(&info, format, args);
 	va_end(args);
 	spin_unlock(&console_out_lock);
 
Signature · 1 line
2.39.2
author
#12 lore raw

PATCH v4 12/13 lib: sbi: Fix timing of clearing tbuf

A single scan of the format char may add multiple characters to the tbuf, causing a buffer overflow. You should check if tbuf is full in printc so that it does not cause a buffer overflow.

---
 lib/sbi/sbi_console.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
lib/sbi/sbi_console.c +9 -5
index 7f6ab14..7d811e7 100644
@@ -18,6 +18,8 @@
 #define PAD_ZERO 2
 #define PAD_ALTERNATE 4
 #define PAD_SIGN 8
+#define USED_TBUF (1 << (8 * sizeof(int) - 1))
+
 #define PRINT_BUF_LEN 64
 #define CONSOLE_TBUF_MAX 256
 
@@ -161,6 +163,11 @@ append:
 	info->out[info->pos++] = ch;
 	info->out[info->pos] = '\0';
 	info->pc++;
+
+	if ((info->flags & USED_TBUF) && (info->len - info->pos <= 1)) {
+		nputs_all(info->out, info->pos);
+		info->pos = 0;
+	}
 }
 
 static void prints(struct print_info *info, const char *string)
@@ -266,11 +273,6 @@ static void print(struct print_info *info, const char *format, va_list args)
 	}
 
 	for (; *format != 0; ++format) {
-		if (use_tbuf && (info->len - info->pos <= 1)) {
-			nputs_all(info->out, info->pos);
-			info->pos = 0;
-		}
-
 		if (*format == '%') {
 			++format;
 			if (*format == '\0')
@@ -305,6 +307,8 @@ static void print(struct print_info *info, const char *format, va_list args)
 				if (!flags_done)
 					++format;
 			}
+			if (use_tbuf)
+				info->flags |= USED_TBUF;
 			if (info->flags & PAD_RIGHT)
 				info->flags &= ~PAD_ZERO;
 			/* Get width */
Signature · 1 line
2.39.2
author
#13 lore raw

PATCH v4 13/13 lib: sbi: Fix missing '\0' when buffer szie equal 1

In reply to Xiang W

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

---
 lib/sbi/sbi_console.c | 8 ++++++++
 1 file changed, 8 insertions(+)
lib/sbi/sbi_console.c +8 -0
index 7d811e7..81555f3 100644
@@ -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:
Signature · 1 line
2.39.2
author
#14 lore raw

v4 cover Improve sbi_console

In reply to Xiang W

? 2023-06-08???? 15:29 +0800?Xiang W???

Quoted 3 lines

v4 changes:
- PATCH10: Simplify printc
- add PATCH11, handle special case

v4 changes:
- PATCH11: Simplify printc
- add PATCH13, handle special case

Quoted 26 lines

v3 changes:
- PATCH11: Simplify get info->type
- PATCH12: Change the way to keep USED_TBUF by and to reset it by use_tbuf

v2 changes:
- add patch 10-12

Xiang W (13):
? lib: sbi: Fix missing contine in print
? lib: sbi: Fix how print gets flags
? lib: sbi: print not fill '0' when left-aligned ? lib: sbi: Add '+' flags for print
? lib: sbi: Add ' ' '\'' flags for print
? lib: sbi: implifying the parameters of printi ? lib: sbi: print add 'o' type
? lib: sbi: Fix printi
? lib: sbi: Simplify prints
? lib: sbi: sbi_console.c code re-layout
? lib: sbi: Add print_info for sbi_console.c ? lib: sbi: Fix timing of clearing tbuf
? lib: sbi: Fix missing '\0' when buffer szie equal 1

?lib/sbi/sbi_console.c | 413 ++++++++++++++++++++++-------------------- ?1 file changed, 212 insertions(+), 201 deletions(-)