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

Anup Patel anup at brainfault.org
Wed Jul 5 22:38:18 PDT 2023


On Wed, Jul 5, 2023 at 8:08 PM Xiang W <wxjstz at 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.

I would rather do something like below in printc(...)

diff --git a/lib/sbi/sbi_console.c b/lib/sbi/sbi_console.c
index 168dffd..e0ecb41 100644
--- a/lib/sbi/sbi_console.c
+++ b/lib/sbi/sbi_console.c
@@ -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

>
> Signed-off-by: Xiang W <wxjstz at 126.com>
> ---
>  lib/sbi/sbi_console.c | 4 ++++
>  1 file changed, 4 insertions(+)
>
> diff --git a/lib/sbi/sbi_console.c b/lib/sbi/sbi_console.c
> index 00feec8..0703d4a 100644
> --- a/lib/sbi/sbi_console.c
> +++ b/lib/sbi/sbi_console.c
> @@ -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
>



More information about the opensbi mailing list