public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Stefan Liebler <stli@linux.ibm.com>
To: libc-alpha@sourceware.org
Cc: Stefan Liebler <stli@linux.ibm.com>
Subject: [PATCH] Avoid -Wstringop-overflow= warning in iconv module.
Date: Fri,  3 Jun 2022 14:52:51 +0200	[thread overview]
Message-ID: <20220603125251.3903158-1-stli@linux.ibm.com> (raw)

On s390x when compiling with GCC 12, I get this warning:
utf8-utf16-z9.c:
../iconv/loop.c: In function ‘__from_utf8_loop_etf3eh_single’:
../iconv/loop.c:445:22: error: writing 1 byte into a region of size 0 [-Werror=stringop-overflow=]
  445 |     bytebuf[inlen++] = *inptr++;
      |     ~~~~~~~~~~~~~~~~~^~~~~~~~~~
../iconv/loop.c:381:17: note: at offset 4 into destination object ‘bytebuf’ of size 4
  381 |   unsigned char bytebuf[MAX_NEEDED_INPUT];
      |                 ^~~~~~~
../iconv/loop.c:445:22: error: writing 1 byte into a region of size 0 [-Werror=stringop-overflow=]
  445 |     bytebuf[inlen++] = *inptr++;
      |     ~~~~~~~~~~~~~~~~~^~~~~~~~~~
../iconv/loop.c:381:17: note: at offset 5 into destination object ‘bytebuf’ of size 4
  381 |   unsigned char bytebuf[MAX_NEEDED_INPUT];
      |                 ^~~~~~~

This patch tells the compiler that inend is always behind inptr which
avoids the warning.  Note that the SINGLE function is only used to
implement the mb*towc*() or wc*tomb*() functions.  Those functions use
inptr and inend pointing to a variable on stack, compute the inend pointer
or explicitly check the arguments which always leads to inptr < inend.
---
 iconv/loop.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/iconv/loop.c b/iconv/loop.c
index f8727a637a..09ade3b765 100644
--- a/iconv/loop.c
+++ b/iconv/loop.c
@@ -435,11 +435,17 @@ SINGLE(LOOPFCT) (struct __gconv_step *step,
     return __GCONV_FULL_OUTPUT;
 
   /*  Now add characters from the normal input buffer.  */
-  if (inlen >= MAX_NEEDED_INPUT)
+  if (inlen >= MAX_NEEDED_INPUT || inptr >= inend)
     /* Avoid a -Wstringop-overflow= warning when this loop is
        unrolled.  The compiler cannot otherwise see that this is
        unreachable because it depends on (state->__count & 7) not
-       being too large after a previous conversion step.  */
+       being too large after a previous conversion step.
+       Starting with GCC 12, we also have mark the inptr >= inend
+       case as unreachable to omit the warning.  Note that this SINGLE
+       function is only used to implement the mb*towc*() or wc*tomb*()
+       functions.  Those functions use inptr and inend pointing to a
+       variable on stack, compute the inend pointer or explicitly check
+       the arguments which always leads to inptr < inend.  */
     __builtin_unreachable ();
   do
     bytebuf[inlen++] = *inptr++;
-- 
2.36.1


             reply	other threads:[~2022-06-03 12:53 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-03 12:52 Stefan Liebler [this message]
2022-06-13  6:31 ` Siddhesh Poyarekar
2022-06-14  9:10   ` Stefan Liebler

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=20220603125251.3903158-1-stli@linux.ibm.com \
    --to=stli@linux.ibm.com \
    --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).