Improve sbi_console
v8 cover Improve sbi_console
v8 changes:
- PATCH10: Rename USED_TBUF to USE_TBUF, and change value to 16
- PATCH 1/8/9 Add Anup's SoB
Xiang W (11):
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: Fix printc
lib: sbi: Fix timing of clearing tbuf
lib: sbi: Fix missing '\0' when buffer szie equal 1
lib/sbi/sbi_console.c | 272 +++++++++++++++++++++--------------------- 1 file changed, 135 insertions(+), 137 deletions(-)
Signature · 1 line
2.40.1
PATCH v8 1/11 lib: sbi: Fix how print gets flags
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 | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-)
lib/sbi/sbi_console.c +19 -11
@@ -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;
@@ -268,17 +269,24 @@ static int print(char **out, u32 *out_len, const char *format, va_list args)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_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 */ for (; *format >='0' && *format <='9' ; ++format) {
Signature · 1 line
2.40.1
PATCH v8 2/11 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 skips padding.
--- 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 */ for (; *format >='0' && *format <='9' ; ++format) { width *=10 ;
Signature · 1 line
2.40.1
PATCH v8 3/11 lib: sbi: Add '+' flags for print
In reply to Xiang WAdds + flags for print, prefixing positive numbers with + when this flags is present
--- 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.40.1
PATCH v8 4/11 lib: sbi: Add ' ' '\'' flags for print
In reply to Xiang WThe space flag is used to add a space before positive numbers, and apostrophe is used to print the thousand separator. Add code to ignore these two flags
--- 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.40.1
PATCH v8 5/11 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 | 127 +++++++++++++++++------------------------- 1 file changed, 50 insertions(+), 77 deletions(-)
lib/sbi/sbi_console.c +50 -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
@@ -314,83 +324,46 @@ 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' ); + width, flags, *format);continue ; } -if (*format =='X' ) { - pc +=printi (out, out_len, - va_arg(args, unsigned int), 16, 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 =='u' ) { - pc +=printi (out, out_len, - va_arg(args, unsigned int), 10, 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 =='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' ) {/* char are converted to int then pushed on the stack */
Signature · 1 line
2.40.1
PATCH v8 6/11 lib: sbi: print add 'o' type
In reply to Xiang WAdd o type for print to print octal numbers
--- lib/sbi/sbi_console.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
lib/sbi/sbi_console.c +4 -3
@@ -327,7 +327,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 ;
@@ -341,7 +342,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;
@@ -351,7 +352,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.40.1
PATCH v8 6/11 lib: sbi: print add 'o' type
Hi Xiang,
Quoted 23 lines · Xiang W
Add o type for print to print octal numbers
Signed-off-by Xiang W <[email protected]>Reviewed-by Anup Patel <[email protected]>--- lib/sbi/sbi_console.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)lib/sbi/sbi_console.c +2
-1@@ -327,7 +327,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 ;
lib/sbi/sbi_console.c static int print(char **out, u32 *out_len, const char *format, va_list args) · line 347
Quoted 7 lines
lib/sbi/sbi_console.c +1
-1@@ -341,7 +342,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' )) {
With %ld format, I have seen than 'd' is printed as it is. Can you take care of that in this patch?
Regards
Himanshu
Quoted 10 lines
lib/sbi/sbi_console.c +1
-1++format;@@ -351,7 +352,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 · 2 lines
Thanks & Regards Himanshu
PATCH v8 6/11 lib: sbi: print add 'o' type
? 2023-07-06???? 16:47 +0530?Himanshu Chauhan???
Quoted 28 lines
Hi Xiang,
Quoted 25 lines · Xiang W
Add o type for print to print octal numbers
Signed-off-by Xiang W <[email protected]>Reviewed-by Anup Patel <[email protected]>---
?lib/sbi/sbi_console.c| 7 ++++---
? 1 file changed, 4 insertions(+), 3 deletions(-)lib/sbi/sbi_console.c +2
-1@@ -327,7 +327,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;@@ -341,7 +342,7 @@ static int print(char **out, u32 *out_len, const char *format, va_list args) ????????????????????????????????type = 'i'; ????????????????????????????????if (format[1] == 'l') {
Quoted 8 lines
lib/sbi/sbi_console.c static int print(char **out, u32 *out_len, const char *format, va_list args) · line 347
Quoted 5 lines
lib/sbi/sbi_console.c +1
-1????????????????????????????????????????++format; -???????????????????????????????????????if ((format[1] == 'u') +???????????????????????????????????????if ((format[1] == 'u') || (format[1] == 'o') ????????????????????????????????????????????????????????|| (format[1] == 'd') || (format[1] == 'i') ????????????????????????????????????????????????????????|| (format[1] == 'x') || (format[1] == 'X')) {With %ld format, I have seen than 'd' is printed as it is. Can you take care of that in this patch?
Your using mainline code will print out d as it is. The new code will not have this bug.
Regards,
Xiang W
Quoted 17 lines
Regards
Himanshu
Quoted 10 lines
lib/sbi/sbi_console.c +1
-1????????????????????????????????????????????????++format;@@ -351,7 +352,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;
PATCH v8 7/11 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.40.1
PATCH v8 8/11 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.40.1
PATCH v8 9/11 lib: sbi: Fix printc
In reply to Xiang WBecause *out needs to reserve a byte to hold '\0', no more characters should be added to the buffer when *out has one byte left, and the buffer size *out_len should not be modified. this patch prevents the correction of *out_len when *out_len is 1.
--- lib/sbi/sbi_console.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
lib/sbi/sbi_console.c +2 -3
@@ -142,10 +142,9 @@ static void printc(char **out, u32 *out_len, char ch)if (!out_len || *out_len >1 ) { *(*out)++ = ch; **out ='\0' ; +if (out_len) + --(*out_len); } - -if (out_len && *out_len >0 ) - --(*out_len); } static int prints(char **out, u32 *out_len, const char *string, int width,
Signature · 1 line
2.40.1
PATCH v8 10/11 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 | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-)
lib/sbi/sbi_console.c +19 -16
@@ -121,6 +121,7 @@ unsigned long sbi_ngets(char *str, unsigned long len)#define PAD_ZERO 2#define PAD_ALTERNATE 4#define PAD_SIGN 8 +#define USE_TBUF 16#define PRINT_BUF_LEN 64#define va_start (v, l) __builtin_va_start((v), l)
@@ -128,7 +129,7 @@ unsigned long sbi_ngets(char *str, unsigned long len)#define va_arg __builtin_va_argtypedef __builtin_va_list va_list ; -static void printc (char **out,u32 *out_len,char ch) +static void printc (char **out,u32 *out_len,char ch,int flags) {if (!out) {sbi_putc (ch);
@@ -142,8 +143,14 @@ static void printc(char **out, u32 *out_len, char ch)if (!out_len || *out_len >1 ) { *(*out)++ = ch; **out ='\0' ; -if (out_len) +if (out_len) { --(*out_len); +if ((flags &USE_TBUF ) && *out_len ==1 ) { +nputs_all (console_tbuf,CONSOLE_TBUF_MAX - *out_len); + *out = console_tbuf; + *out_len =CONSOLE_TBUF_MAX ; + } + } } }
@@ -154,16 +161,16 @@ static int prints(char **out, u32 *out_len, const char *string, int width, width -=sbi_strlen (string);if (!(flags &PAD_RIGHT )) {for (; width >0 ; --width) { -printc (out, out_len, flags &PAD_ZERO ?'0' :' ' ); +printc (out, out_len, flags &PAD_ZERO ?'0' :' ' , flags); ++pc; } }for (; *string; ++string) { -printc (out, out_len, *string); +printc (out, out_len, *string, flags); ++pc; }for (; width >0 ; --width) { -printc (out, out_len,' ' ); +printc (out, out_len,' ' , flags); ++pc; }
@@ -215,18 +222,18 @@ static int printi(char **out, u32 *out_len, long long i,if (flags &PAD_ZERO ) {if (sign) { -printc (out, out_len, sign); +printc (out, out_len, sign, flags); ++pc; --width; }if (i && (flags &PAD_ALTERNATE )) {if (b ==16 || b ==8 ) { -printc (out, out_len,'0' ); +printc (out, out_len,'0' , flags); ++pc; --width; }if (b ==16 ) { -printc (out, out_len,'x' -'a' + letbase); +printc (out, out_len,'x' -'a' + letbase, flags); ++pc; --width; }
@@ -265,15 +272,11 @@ static int print(char **out, u32 *out_len, const char *format, va_list args) }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; - } - + width = flags =0 ; +if (use_tbuf) + flags |=USE_TBUF ;if (*format =='%' ) { ++format; - width = flags =0 ;if (*format =='\0' )break ;if (*format =='%' )
@@ -371,7 +374,7 @@ static int print(char **out, u32 *out_len, const char *format, va_list args) } }else { literal: -printc (out, out_len, *format); +printc (out, out_len, *format, flags); ++pc; } }
Signature · 1 line
2.40.1
PATCH v8 10/11 lib: sbi: Fix timing of clearing tbuf
Quoted 6 lines · On Wed, Jul 5, 2023 at 8:08?PM Xiang W <[email protected]> wrote:
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.
Signed-off-by Xiang W <[email protected]>
Looks good to me.
Regards,
Anup
Quoted 114 lines
--- lib/sbi/sbi_console.c | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-)lib/sbi/sbi_console.c +19
-17@@ -121,6 +121,7 @@ unsigned long sbi_ngets(char *str, unsigned long len)#define PAD_ZERO 2#define PAD_ALTERNATE 4#define PAD_SIGN 8 +#define USE_TBUF 16#define PRINT_BUF_LEN 64#define va_start (v, l) __builtin_va_start((v), l)@@ -128,7 +129,7 @@ unsigned long sbi_ngets(char *str, unsigned long len)#define va_arg __builtin_va_argtypedef __builtin_va_list va_list ; -static void printc (char **out,u32 *out_len,char ch) +static void printc (char **out,u32 *out_len,char ch,int flags) {if (!out) {sbi_putc (ch);@@ -142,8 +143,14 @@ static void printc(char **out, u32 *out_len, char ch)if (!out_len || *out_len >1 ) { *(*out)++ = ch; **out ='\0' ; -if (out_len) +if (out_len) { --(*out_len); +if ((flags &USE_TBUF ) && *out_len ==1 ) { +nputs_all (console_tbuf,CONSOLE_TBUF_MAX - *out_len); + *out = console_tbuf; + *out_len =CONSOLE_TBUF_MAX ; + } + } } }@@ -154,16 +161,16 @@ static int prints(char **out, u32 *out_len, const char *string, int width, width -=sbi_strlen (string);if (!(flags &PAD_RIGHT )) {for (; width >0 ; --width) { -printc (out, out_len, flags &PAD_ZERO ?'0' :' ' ); +printc (out, out_len, flags &PAD_ZERO ?'0' :' ' , flags); ++pc; } }for (; *string; ++string) { -printc (out, out_len, *string); +printc (out, out_len, *string, flags); ++pc; }for (; width >0 ; --width) { -printc (out, out_len,' ' ); +printc (out, out_len,' ' , flags); ++pc; }@@ -215,18 +222,18 @@ static int printi(char **out, u32 *out_len, long long i,if (flags &PAD_ZERO ) {if (sign) { -printc (out, out_len, sign); +printc (out, out_len, sign, flags); ++pc; --width; }if (i && (flags &PAD_ALTERNATE )) {if (b ==16 || b ==8 ) { -printc (out, out_len,'0' ); +printc (out, out_len,'0' , flags); ++pc; --width; }if (b ==16 ) { -printc (out, out_len,'x' -'a' + letbase); +printc (out, out_len,'x' -'a' + letbase, flags); ++pc; --width; }@@ -265,15 +272,11 @@ static int print(char **out, u32 *out_len, const char *format, va_list args) }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; - } - + width = flags =0 ; +if (use_tbuf) + flags |=USE_TBUF ;if (*format =='%' ) { ++format; - width = flags =0 ;if (*format =='\0' )break ;if (*format =='%' )@@ -371,7 +374,7 @@ static int print(char **out, u32 *out_len, const char *format, va_list args) } }else { literal: -printc (out, out_len, *format); +printc (out, out_len, *format, flags); ++pc; } } --2.40.1
PATCH v8 11/11 lib: sbi: Fix missing '\0' when buffer szie equal 1
In reply to Xiang WFix special case: sbi_snprintf(out, out_len, ...) when out_len equal 1, The previous code will not fill the buffer with any char.
--- lib/sbi/sbi_console.c | 4 ++++ 1 file changed, 4 insertions(+)
lib/sbi/sbi_console.c +4 -0
@@ -271,6 +271,10 @@ static int print(char **out, u32 *out_len, const char *format, va_list args) out_len = &console_tbuf_len; } +/* handle special case: *out_len == 1*/ +if (out) + **out ='\0' ; +for (; *format !=0 ; ++format) { width = flags =0 ;if (use_tbuf)
Signature · 1 line
2.40.1
PATCH v8 11/11 lib: sbi: Fix missing '\0' when buffer szie equal 1
Quoted 3 lines · On Wed, Jul 5, 2023 at 8:08?PM Xiang W <[email protected]> wrote:
Fix special case:
sbi_snprintf(out, out_len, ...)whenout_lenequal 1, The previous code will not fill the buffer with any char.
I would rather do something like below in printc(...)
lib/sbi/sbi_console.c +2 -2
@@ -138,10 +138,10 @@ static void printc(char **out, u32 *out_len, char ch)* 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 ) { +if (!out_len || *out_len) *(*out)++ = ch; +if (!out_len || *out_len >1 ) **out ='\0' ; - }if (out_len && *out_len >0 ) --(*out_len);
Regards,
Anup
Quoted 24 lines
Signed-off-by Xiang W <[email protected]>--- lib/sbi/sbi_console.c | 4 ++++ 1 file changed, 4 insertions(+)lib/sbi/sbi_console.c +4
-1@@ -271,6 +271,10 @@ static int print(char **out, u32 *out_len, const char *format, va_list args) out_len = &console_tbuf_len; } +/* handle special case: *out_len == 1*/ +if (out) + **out ='\0' ; +for (; *format !=0 ; ++format) { width = flags =0 ;if (use_tbuf) --2.40.1
PATCH v8 11/11 lib: sbi: Fix missing '\0' when buffer szie equal 1
? 2023-07-06???? 11:08 +0530?Anup Patel???
Quoted 15 lines · On Wed, Jul 5, 2023 at 8:08?PM Xiang W <[email protected]> wrote:
Quoted 3 lines
Fix special case:
sbi_snprintf(out, out_len, ...)whenout_lenequal 1, The previous code will not fill the buffer with any char.I would rather do something like below in printc(...)
lib/sbi/sbi_console.c +0
-0@@ -138,10 +138,10 @@ static void printc(char **out, u32 *out_len, char ch)????? * The *printf entry point functions have enforced that (*out) can ????? * only be null when
out_lenis non-null and its value is zero. ????? */
Quoted 6 lines
-??? if (!out_len || *out_len > 1) { +??? if (!out_len || *out_len)???????? *(*out)++ = ch;
+??? if (!out_len ||*out_len> 1)
???????? **out = '\0';
-??? }
When *out_len is equal to 1, that is, when there is not enough buffer to drop the formatted string, a string without '\0' will be generated.
And when there is a problem with the format in sbi_snprintf, it may not entry printc at all. e.g:
sbi_printf(out,out_len,"%f",3.14)
Regards,
Xiang W
Quoted 31 lines
???? if (
out_len&&*out_len> 0)
???????? --(*out_len);Regards,
AnupQuoted 24 lines
Signed-off-by Xiang W <[email protected]>---
?lib/sbi/sbi_console.c | 4 ++++
?1 file changed, 4 insertions(+)lib/sbi/sbi_console.c +4
-1@@ -271,6 +271,10 @@ static int print(char **out, u32 *out_len, const char *format, va_list args) ??????????????? out_len = &console_tbuf_len; ??????? } +?????? /* handle special case: *out_len == 1*/ +?????? if (out) +?????????????? **out = '\0'; + ??????? for (; *format != 0; ++format) { ??????????????? width = flags = 0; ??????????????? if (use_tbuf) --2.40.1
PATCH v8 11/11 lib: sbi: Fix missing '\0' when buffer szie equal 1
Quoted 27 lines · On Thu, Jul 6, 2023 at 12:20?PM Xiang W <[email protected]> wrote:
? 2023-07-06???? 11:08 +0530?Anup Patel???
Quoted 12 lines · On Wed, Jul 5, 2023 at 8:08?PM Xiang W <[email protected]> wrote:
Quoted 3 lines
Fix special case:
sbi_snprintf(out, out_len, ...)whenout_lenequal 1, The previous code will not fill the buffer with any char.I would rather do something like below in printc(...)
lib/sbi/sbi_console.c +0
-0lib/sbi/sbi_console.c static void printc(char **out, u32 *out_len, char ch) · line 144
Quoted 9 lines
lib/sbi/sbi_console.c +2
-2@@ -138,10 +138,10 @@ static void printc(char **out, u32 *out_len, char ch)* 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 ) { +if (!out_len || *out_len) *(*out)++ = ch; +if (!out_len || *out_len >1 ) **out ='\0' ; - }When
*out_lenis equal to 1, that is, when there is not enough buffer to drop the formatted string, a string without '\0' will be generated.
Quoted 3 lines
And when there is a problem with the format in
sbi_snprintf, it may not entry printc at all. e.g:sbi_printf(out,out_len,"%f",3.14)
Fair enough but I still think check on out_len is also required.
if (out && out_len && *out_len)
**out = '\0';
Regards,
Anup
Quoted 36 lines
Regards,
Xiang WQuoted 31 lines
lib/sbi/sbi_console.c +0
-0if (out_len && *out_len >0 ) --(*out_len);Regards,
AnupQuoted 24 lines
Signed-off-by Xiang W <[email protected]>--- lib/sbi/sbi_console.c | 4 ++++ 1 file changed, 4 insertions(+)lib/sbi/sbi_console.c +4
-1@@ -271,6 +271,10 @@ static int print(char **out, u32 *out_len, const char *format, va_list args) out_len = &console_tbuf_len; } +/* handle special case: *out_len == 1*/ +if (out) + **out ='\0' ; +for (; *format !=0 ; ++format) { width = flags =0 ;if (use_tbuf) --2.40.1
PATCH v8 11/11 lib: sbi: Fix missing '\0' when buffer szie equal 1
? 2023-07-09???? 11:02 +0530?Anup Patel???
Quoted 28 lines · On Thu, Jul 6, 2023 at 12:20?PM Xiang W <[email protected]> wrote:
Quoted 27 lines
? 2023-07-06???? 11:08 +0530?Anup Patel???
Quoted 15 lines · On Wed, Jul 5, 2023 at 8:08?PM Xiang W <[email protected]> wrote:
Quoted 3 lines
Fix special case:
sbi_snprintf(out, out_len, ...)whenout_lenequal 1, The previous code will not fill the buffer with any char.I would rather do something like below in printc(...)
lib/sbi/sbi_console.c +0
-0@@ -138,10 +138,10 @@ static void printc(char **out, u32 *out_len, char ch)????? * The *printf entry point functions have enforced that (*out) can ????? * only be null when
out_lenis non-null and its value is zero. ????? */Quoted 6 lines
-??? if (!out_len || *out_len > 1) { +??? if (!out_len || *out_len)???????? *(*out)++ = ch;
+??? if (!out_len ||*out_len> 1)
???????? **out = '\0';
-??? }When
*out_lenis equal to 1, that is, when there is not enough buffer to drop the formatted string, a string without '\0' will be generated.
Quoted 8 lines
Quoted 3 lines
And when there is a problem with the format in
sbi_snprintf, it may not entry printc at all. e.g:
???????sbi_printf(out,out_len,"%f",3.14)Fair enough but I still think check on
out_lenis also required.if (out &&
out_len&&*out_len)
??? **out = '\0';
If we check out_len, it will result in possible loss of '\0' in the following cases:
sbi_sprintf(out, format, ...)
Regards,
Xiang W
Quoted 40 lines
Regards,
AnupQuoted 36 lines
Regards,
Xiang WQuoted 31 lines
???? if (
out_len&&*out_len> 0)
???????? --(*out_len);Regards,
AnupQuoted 24 lines
Signed-off-by Xiang W <[email protected]>---
?lib/sbi/sbi_console.c | 4 ++++
?1 file changed, 4 insertions(+)lib/sbi/sbi_console.c +4
-1@@ -271,6 +271,10 @@ static int print(char **out, u32 *out_len, const char *format, va_list args) ??????????????? out_len = &console_tbuf_len; ??????? } +?????? /* handle special case: *out_len == 1*/ +?????? if (out) +?????????????? **out = '\0'; + ??????? for (; *format != 0; ++format) { ??????????????? width = flags = 0; ??????????????? if (use_tbuf) --2.40.1
PATCH v8 11/11 lib: sbi: Fix missing '\0' when buffer szie equal 1
Quoted 34 lines · On Sun, Jul 9, 2023 at 11:47?AM Xiang W <[email protected]> wrote:
? 2023-07-09???? 11:02 +0530?Anup Patel???
Quoted 32 lines · On Thu, Jul 6, 2023 at 12:20?PM Xiang W <[email protected]> wrote:
Quoted 30 lines
? 2023-07-06???? 11:08 +0530?Anup Patel???
Quoted 12 lines · On Wed, Jul 5, 2023 at 8:08?PM Xiang W <[email protected]> wrote:
Quoted 3 lines
Fix special case:
sbi_snprintf(out, out_len, ...)whenout_lenequal 1, The previous code will not fill the buffer with any char.I would rather do something like below in printc(...)
lib/sbi/sbi_console.c +0
-0lib/sbi/sbi_console.c static void printc(char **out, u32 *out_len, char ch) · line 144
Quoted 9 lines
lib/sbi/sbi_console.c +2
-2@@ -138,10 +138,10 @@ static void printc(char **out, u32 *out_len, char ch)* 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 ) { +if (!out_len || *out_len) *(*out)++ = ch; +if (!out_len || *out_len >1 ) **out ='\0' ; - }When
*out_lenis equal to 1, that is, when there is not enough buffer to drop the formatted string, a string without '\0' will be generated.And when there is a problem with the format in
sbi_snprintf, it may not entry printc at all. e.g:sbi_printf(out,out_len,"%f",3.14)
Quoted 7 lines
Quoted 4 lines
Fair enough but I still think check on
out_lenis also required.if (out &&
out_len&&*out_len)**out = '\0';If we check
out_len, it will result in possible loss of '\0' in the following cases:sbi_sprintf(out, format, ...)
In which case, we should do something like below:
if (out) {
if (!out_len || *out_len)
**out = '\0';
}Regards,
Anup
Quoted 44 lines
Regards,
Xiang WQuoted 40 lines
Regards,
AnupQuoted 36 lines
Regards,
Xiang WQuoted 31 lines
lib/sbi/sbi_console.c +0
-0if (out_len && *out_len >0 ) --(*out_len);Regards,
AnupQuoted 24 lines
Signed-off-by Xiang W <[email protected]>--- lib/sbi/sbi_console.c | 4 ++++ 1 file changed, 4 insertions(+)lib/sbi/sbi_console.c +4
-1@@ -271,6 +271,10 @@ static int print(char **out, u32 *out_len, const char *format, va_list args) out_len = &console_tbuf_len; } +/* handle special case: *out_len == 1*/ +if (out) + **out ='\0' ; +for (; *format !=0 ; ++format) { width = flags =0 ;if (use_tbuf) --2.40.1