diff mbox series

[v9,11/11] lib: sbi: Fix missing '\0' when buffer szie equal 1

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

Commit Message

Xiang W July 9, 2023, 4:03 p.m. UTC
Fix special case: sbi_snprintf(out, out_len, ...) when out_len equal
1, The previous code will not fill the buffer with any char.

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

Comments

Anup Patel July 10, 2023, 3:57 a.m. UTC | #1
On Sun, Jul 9, 2023 at 9:34 PM Xiang W <wxjstz@126.com> wrote:
>
> Fix special case: sbi_snprintf(out, out_len, ...) when out_len equal
> 1, The previous code will not fill the buffer with any char.
>
> Signed-off-by: Xiang W <wxjstz@126.com>

Looks good to me.

Reviewed-by: Anup Patel <anup@brainfault.org>

Regards,
Anup

> ---
>  lib/sbi/sbi_console.c | 6 ++++++
>  1 file changed, 6 insertions(+)
>
> diff --git a/lib/sbi/sbi_console.c b/lib/sbi/sbi_console.c
> index 00feec8..ad04c8b 100644
> --- a/lib/sbi/sbi_console.c
> +++ b/lib/sbi/sbi_console.c
> @@ -271,6 +271,12 @@ 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) {
> +               if(!out_len || *out_len)
> +                       **out = '\0';
> +       }
> +
>         for (; *format != 0; ++format) {
>                 width = flags = 0;
>                 if (use_tbuf)
> --
> 2.40.1
>
diff mbox series

Patch

diff --git a/lib/sbi/sbi_console.c b/lib/sbi/sbi_console.c
index 00feec8..ad04c8b 100644
--- a/lib/sbi/sbi_console.c
+++ b/lib/sbi/sbi_console.c
@@ -271,6 +271,12 @@  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) {
+		if(!out_len || *out_len)
+			**out = '\0';
+	}
+
 	for (; *format != 0; ++format) {
 		width = flags = 0;
 		if (use_tbuf)