Improve sbi_console
v5 cover Improve sbi_console
v5 changes:
- PATCH12: fix printf("%%") missing USED_TBUF;
- add PATCH14, Fix invalid directive print
v4 changes:
- PATCH11: Simplify printc
- add PATCH13, 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 (14):
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: Fix invalid directive print
lib/sbi/sbi_console.c | 421 ++++++++++++++++++++++-------------------- 1 file changed, 218 insertions(+), 203 deletions(-)
Signature · 1 line
2.39.2
PATCH v5 1/14 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
@@ -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
PATCH v5 1/14 lib: sbi: Fix missing contine in print
Quoted 2 lines · Xiang W
Missing continue after print %lu %ld %lx ...
What kind of commit message is this? What?s the exact bug?
Jess
Quoted 20 lines
Signed-off-by Xiang W <[email protected]>---
lib/sbi/sbi_console.c| 1 +1 file changed, 1 insertion(+)lib/sbi/sbi_console.c +1
-0@@ -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
PATCH v5 1/14 lib: sbi: Fix missing contine in print
? 2023-06-09???? 05:16 +0100?Jessica Clarke???
Quoted 5 lines · Xiang W
Quoted 2 lines
Missing continue after print %lu %ld %lx ...
What kind of commit message is this? What?s the exact bug?
Thanks! There are no bugs here, just a different style of code than the rest. I will drop this.
Xiang
Quoted 23 lines
Jess
Quoted 20 lines
Signed-off-by Xiang W <[email protected]>---
lib/sbi/sbi_console.c| 1 +1 file changed, 1 insertion(+)lib/sbi/sbi_console.c +1
-0@@ -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
PATCH v5 2/14 lib: sbi: Fix how print gets flags
In reply to Xiang WThe 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
@@ -236,6 +236,7 @@ static int printi(char **out, u32 *out_len, long long i, int b, int sg,static int 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
PATCH v5 3/14 lib: sbi: print not fill '0' when left-aligned
In reply to Xiang WLeft 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
@@ -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
PATCH v5 4/14 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
@@ -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
PATCH v5 5/14 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
@@ -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
PATCH v5 6/14 lib: sbi: implifying the parameters of printi
In reply to Xiang WThe 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
@@ -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
PATCH v5 7/14 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
@@ -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
PATCH v5 8/14 lib: sbi: Fix printi
In reply to Xiang WFix two bug:
Quoted 2 lines
printf("%#08x", 0x123); /* print 0000x123 */printf("%#x", 0); /* print0x0*/
--- lib/sbi/sbi_console.c | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-)
lib/sbi/sbi_console.c +23 -13
@@ -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
PATCH v5 9/14 lib: sbi: Simplify prints
In reply to Xiang WWhen 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
@@ -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
PATCH v5 10/14 lib: sbi: sbi_console.c code re-layout
In reply to Xiang WMove 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
@@ -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
PATCH v5 11/14 lib: sbi: Add print_info for sbi_console.c
In reply to Xiang WCreate a structure print_info for print, this structure simplifies parameter passing.
--- lib/sbi/sbi_console.c | 235 +++++++++++++++++++++--------------------- 1 file changed, 120 insertions(+), 115 deletions(-)
lib/sbi/sbi_console.c +120 -115
@@ -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; -if (out_len && *out_len >0 ) - --(*out_len); +/* The buffer is long enough to add new characters */ +if (info->len - info->pos >1 ) + goto append; +return ; + +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; - } -for (; width >0 ; --width) { -printc (out, out_len,' ' ); - ++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' :' ' ); } - -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 char **out,u32 *out_len,const char *format,va_list args) +static void 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,19 +261,15 @@ 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 =='%' ) { ++format;if (*format =='\0' )
@@ -279,21 +277,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 +304,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->width =0 ; + info->wcnt =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 =NULL , 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 =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 =NULL ,NULL , 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 =NULL ,NULL , 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); -NULL ,NULL , format, args); +va_end (args);spin_unlock (&console_out_lock);
Signature · 1 line
2.39.2
PATCH v5 12/14 lib: sbi: Fix timing of clearing tbuf
In reply to Xiang WA 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 | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-)
lib/sbi/sbi_console.c +10 -5
@@ -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,10 +273,9 @@ 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 ; - } + info->flags =0 ; +if (use_tbuf) + info->flags |=USED_TBUF ;if (*format =='%' ) { ++format;if (*format =='\0' )
@@ -277,7 +283,6 @@ static void print(struct print_info *info, const char *format, va_list args)if (*format =='%' ) goto literal;/* Get flags */ - info->flags =0 ; flags_done = false;while (!flags_done) {switch (*format) {
Signature · 1 line
2.39.2
PATCH v5 13/14 lib: sbi: Fix missing '\0' when buffer szie equal 1
In reply to Xiang WFix 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
@@ -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
PATCH v5 14/14 lib: sbi: Fix invalid directive print
In reply to Xiang Wbefore fix:
Quoted 1 line
printf("%1234"); /* print noting */
after fix:
Quoted 1 line
printf("%1234"); /* print %1234 */
--- lib/sbi/sbi_console.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
lib/sbi/sbi_console.c +7 -3
@@ -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 )
Signature · 1 line
2.39.2
PATCH v5 14/14 lib: sbi: Fix invalid directive print
Quoted 7 lines · Xiang W
before fix:
Quoted 1 line
printf("%1234"); /* print noting */after fix:
Quoted 1 line
printf("%1234"); /* print %1234 */Signed-off-by Xiang W <[email protected]>
Who cares what it does? It?s UB. Going out of your way to make it do a specific thing with extra code is a waste of time and obfuscation.
Turn on -Wformat -Werror=format and get rid of broken code.
Jess
Quoted 43 lines
---
lib/sbi/sbi_console.c| 10 +++++++---1 file changed, 7 insertions(+), 3 deletions(-)lib/sbi/sbi_console.c +8
-3@@ -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 )Signature · 1 line
2.39.2
PATCH v5 14/14 lib: sbi: Fix invalid directive print
? 2023-06-09???? 05:15 +0100?Jessica Clarke???
Quoted 5 lines · Xiang W
Quoted 4 lines
before fix:
Quoted 1 line
printf("%1234"); /* print noting */after fix:
Quoted 8 lines
Quoted 3 lines
Quoted 1 line
printf("%1234"); /* print %1234 */Signed-off-by Xiang W <[email protected]>Who cares what it does? It?s UB. Going out of your way to make it do a specific thing with extra code is a waste of time and obfuscation.
Turn on -Wformat -Werror=format and get rid of broken code.
Yes the compiler's error detection does catch problems early. But our sbi_printf implementation is not complete. For example, in the following case:
sbi_printf("... %f...") .
And this patch has very little impact on the code size befor
size build/lib/sbi/sbi_console.o
text data bss dec hex filename 3203 0 272 3475 d93 build/lib/sbi/sbi_console.o
after
size build/lib/sbi/sbi_console.o
text data bss dec hex filename 3205 0 272 3477 d95 build/lib/sbi/sbi_console.o
Xiang
Quoted 46 lines
Jess
Quoted 43 lines
---
lib/sbi/sbi_console.c| 10 +++++++---1 file changed, 7 insertions(+), 3 deletions(-)lib/sbi/sbi_console.c +8
-3@@ -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 )Signature · 1 line
2.39.2
PATCH v5 14/14 lib: sbi: Fix invalid directive print
Quoted 11 lines · Xiang W
? 2023-06-09???? 05:15 +0100?Jessica Clarke???
Quoted 9 lines · Xiang W
Quoted 7 lines
before fix:
Quoted 1 line
printf("%1234"); /* print noting */after fix:
Quoted 1 line
printf("%1234"); /* print %1234 */Signed-off-by Xiang W <[email protected]>
Quoted 7 lines
Quoted 4 lines
Who cares what it does? It?s UB. Going out of your way to make it do a specific thing with extra code is a waste of time and obfuscation.
Turn on -Wformat -Werror=format and get rid of broken code.
Yes the compiler's error detection does catch problems early. But our
sbi_printfimplementation is not complete. For example, in the following case:
sbi_printf("... %f...") .
case ?f': panic("f not implemented");?
Quoted 1 line
And this patch has very little impact on the code size
I wasn?t talking about the compiled code, I meant the source.
Jess
Quoted 58 lines
befor
sizebuild/lib/sbi/sbi_console.otext data bss dec hex filename 3203 0 272 3475 d93 build/lib/sbi/sbi_console.oafter
sizebuild/lib/sbi/sbi_console.otext data bss dec hex filename 3205 0 272 3477 d95 build/lib/sbi/sbi_console.oXiang
Quoted 46 lines
Jess
Quoted 43 lines
---
lib/sbi/sbi_console.c| 10 +++++++---1 file changed, 7 insertions(+), 3 deletions(-)lib/sbi/sbi_console.c +8
-3@@ -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 )Signature · 1 line
2.39.2
PATCH v5 14/14 lib: sbi: Fix invalid directive print
? 2023-06-09???? 08:04 +0100?Jessica Clarke???
Quoted 15 lines · Xiang W
Quoted 14 lines
? 2023-06-09???? 05:15 +0100?Jessica Clarke???
Quoted 12 lines · Xiang W
Quoted 7 lines
before fix:
Quoted 1 line
printf("%1234"); /* print noting */after fix:
Quoted 1 line
printf("%1234"); /* print %1234 */Signed-off-by Xiang W <[email protected]>Who cares what it does? It?s UB. Going out of your way to make it do a specific thing with extra code is a waste of time and obfuscation.
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
Turn on -Wformat -Werror=format and get rid of broken code.
Yes the compiler's error detection does catch problems early. But our
sbi_printfimplementation is not complete. For example, in the following case:
sbi_printf("... %f...") .case ?f':
panic("f not implemented");?
sbi_panic cannot be called directly here, it will cause a deadlock. Also such an error message is not convenient for error location. Maybe we can drop this patch.
Xiang
Quoted 67 lines
Quoted 2 lines
And this patch has very little impact on the code size
I wasn?t talking about the compiled code, I meant the source.
Jess
Quoted 58 lines
befor
sizebuild/lib/sbi/sbi_console.o
? text?? data??? bss??? dec??? hex filename
? 3203????? 0??? 272?? 3475??? d93build/lib/sbi/sbi_console.oafter
sizebuild/lib/sbi/sbi_console.o
? text?? data??? bss??? dec??? hex filename
? 3205????? 0??? 272?? 3477??? d95build/lib/sbi/sbi_console.oXiang
Quoted 46 lines
Jess
Quoted 43 lines
---
lib/sbi/sbi_console.c| 10 +++++++---1 file changed, 7 insertions(+), 3 deletions(-)lib/sbi/sbi_console.c +8
-3@@ -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 )Signature · 1 line
2.39.2