public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Carlos O'Donell <carlos@redhat.com>
To: Adhemerval Zanella <adhemerval.zanella@linaro.org>,
	libc-alpha@sourceware.org
Subject: Re: [PATCH 1/4] locale: Use correct buffer size for utf8_sequence_error [BZ #19444]
Date: Mon, 9 Jan 2023 11:40:01 -0500	[thread overview]
Message-ID: <e7f7378f-d0b1-82c0-13c0-67318a2d16ad@redhat.com> (raw)
In-Reply-To: <20221229125802.2715435-2-adhemerval.zanella@linaro.org>

On 12/29/22 07:57, Adhemerval Zanella via Libc-alpha wrote:
> The buffer used by snprintf might not be large enough for all possible
> inputs, as indicated by gcc with -O1:
> 
> ../locale/programs/linereader.c: In function ‘utf8_sequence_error’:
> ../locale/programs/linereader.c:713:58: error: ‘%02x’ directive output
> may be truncated writing between 2 and 8 bytes into a region of size
> between 1 and 13 [-Werror=format-truncation=]
>   713 |     snprintf (buf, sizeof (buf), "0x%02x 0x%02x 0x%02x 0x%02x",
>       |                                                          ^~~~
> ../locale/programs/linereader.c:713:34: note: directive argument in the
> range [0, 2147483647]
>   713 |     snprintf (buf, sizeof (buf), "0x%02x 0x%02x 0x%02x 0x%02x",
>       |                                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> ../locale/programs/linereader.c:713:5: note: ‘snprintf’ output between
> 20 and 38 bytes into a destination of size 30
>   713 |     snprintf (buf, sizeof (buf), "0x%02x 0x%02x 0x%02x 0x%02x",
>       |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>   714 |               ch1, ch2, ch3, ch4);
>       |               ~~~~~~~~~~~~~~~~~~~
> 
> Checked on x86_64-linux-gnu.

LGTM. Took me a minute to work out the 38 bytes value though.

Reviewed-by: Carlos O'Donell <carlos@redhat.com>

> ---
>  locale/programs/linereader.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/locale/programs/linereader.c b/locale/programs/linereader.c
> index 0460074a0c..794f25a6e4 100644
> --- a/locale/programs/linereader.c
> +++ b/locale/programs/linereader.c
> @@ -701,7 +701,7 @@ static bool
>  utf8_sequence_error (struct linereader *lr, uint8_t ch1, int ch2, int ch3,
>  		     int ch4)
>  {
> -  char buf[30];
> +  char buf[38];

OK.

"0x7f 0x7fffffff 0x7fffffff 0x7fffffff\0" 
= 4 + 1 + 10 + 1 + 10 + 1 + 10 + 1 = 38 bytes including null terminator.

I expect that at -O2 that VRP can see the values propagated from utf8_decode
and determine the buffer will be smaller by a lot given the range checks.

The ch2, ch3, and ch4 values shall not exceed 0xff in all cases since they
are all distinct bytes in the UTF-8 sequence. I expect that the worst case
is actually "0xff 0xff 0xff 0xff\0" or 20 bytes, but if the checks in
utf8_decode are changed then the worst case could be 38 bytes.

>  
>    if (ch2 < 0)
>      snprintf (buf, sizeof (buf), "0x%02x", ch1);

-- 
Cheers,
Carlos.


  reply	other threads:[~2023-01-09 16:40 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-29 12:57 [PATCH 0/4] Fix remaining -Os/-O1 compile issues " Adhemerval Zanella
2022-12-29 12:57 ` [PATCH 1/4] locale: Use correct buffer size for utf8_sequence_error " Adhemerval Zanella
2023-01-09 16:40   ` Carlos O'Donell [this message]
2022-12-29 12:58 ` [PATCH 2/4] sunrpc: Suppress GCC -O1 warning on user2netname " Adhemerval Zanella
2023-01-09 17:09   ` Carlos O'Donell
2022-12-29 12:58 ` [PATCH 3/4] string: Suppress -Wmaybe-unitialized for wordcopy " Adhemerval Zanella
2023-01-10 22:47   ` Carlos O'Donell
2023-01-11 13:14     ` Adhemerval Zanella Netto
2023-01-11 19:33       ` Carlos O'Donell
2023-01-11 20:12       ` Carlos O'Donell
2022-12-29 12:58 ` [PATCH 4/4] math: Suppress -O0 warnings for soft-fp fsqrt " Adhemerval Zanella
2023-01-10 22:54   ` Carlos O'Donell

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=e7f7378f-d0b1-82c0-13c0-67318a2d16ad@redhat.com \
    --to=carlos@redhat.com \
    --cc=adhemerval.zanella@linaro.org \
    --cc=libc-alpha@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).