public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/100496] New: False positive with -Wmaybe-uninitialized
@ 2021-05-10  8:29 jochen447 at concept dot de
  2021-05-10 18:12 ` [Bug c/100496] " pinskia at gcc dot gnu.org
  2021-05-10 21:39 ` msebor at gcc dot gnu.org
  0 siblings, 2 replies; 3+ messages in thread
From: jochen447 at concept dot de @ 2021-05-10  8:29 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100496

            Bug ID: 100496
           Summary: False positive with -Wmaybe-uninitialized
           Product: gcc
           Version: 11.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jochen447 at concept dot de
  Target Milestone: ---

Created attachment 50784
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=50784&action=edit
minimal example code to trigger the false positive warning

I have a piece of C code that is 100% correct, compiles fine, but evokes a
warning with -Wmaybe-uninitialized:

$ gcc -c -Wmaybe-uninitialized uninit.c
uninit.c: In function ‘findEncoding’:
uninit.c:19:3: warning: ‘buf’ may be used uninitialized [-Wmaybe-uninitialized]
   19 |   XmlUtf8Convert(p, p + sizeof(buf) - 1);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
uninit.c:14:13: note: by argument 2 of type ‘const char * const’ to
‘XmlUtf8Convert’ declared here
   14 | extern void XmlUtf8Convert(char* p_start, const char* const p_end);
      |             ^~~~~~~~~~~~~~
uninit.c:17:8: note: ‘buf’ declared here
   17 |   char buf[128];
      |        ^~~


The compiler complains about the 2nd argument passed down to function
XmlUtf8Convert. It is indeed pointing to uninitialized memory (but so is the
first argument). But it cannot see that this pointer serves as a guard (pointer
arithmetic) - it is never dereferenced inside the function XmlUtf8Convert.

So the compiler's assumptions are wrong here.

More info:
==========

$ uname -a
Linux inspiron14l 5.11.18-300.fc34.x86_64 #1 SMP Mon May 3 15:10:32 UTC 2021
x86_64 x86_64 x86_64 GNU/Linux

$ gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/11/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap
--enable-languages=c,c++,fortran,objc,obj-c++,ada,go,d,lto --prefix=/usr
--mandir=/usr/share/man --infodir=/usr/share/info
--with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared
--enable-threads=posix --enable-checking=release --enable-multilib
--with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions
--enable-gnu-unique-object --enable-linker-build-id
--with-gcc-major-version-only --with-linker-hash-style=gnu --enable-plugin
--enable-initfini-array
--with-isl=/builddir/build/BUILD/gcc-11.1.1-20210428/obj-x86_64-redhat-linux/isl-install
--enable-offload-targets=nvptx-none --without-cuda-driver
--enable-gnu-indirect-function --enable-cet --with-tune=generic
--with-arch_32=i686 --build=x86_64-redhat-linux
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 11.1.1 20210428 (Red Hat 11.1.1-1) (GCC)

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug c/100496] False positive with -Wmaybe-uninitialized
  2021-05-10  8:29 [Bug c/100496] New: False positive with -Wmaybe-uninitialized jochen447 at concept dot de
@ 2021-05-10 18:12 ` pinskia at gcc dot gnu.org
  2021-05-10 21:39 ` msebor at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-05-10 18:12 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100496

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Depends on|                            |100417

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
I think this is similar to PR 100417.


Referenced Bugs:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100417
[Bug 100417] False positive -Wmaybe-uninitalized with malloc.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug c/100496] False positive with -Wmaybe-uninitialized
  2021-05-10  8:29 [Bug c/100496] New: False positive with -Wmaybe-uninitialized jochen447 at concept dot de
  2021-05-10 18:12 ` [Bug c/100496] " pinskia at gcc dot gnu.org
@ 2021-05-10 21:39 ` msebor at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: msebor at gcc dot gnu.org @ 2021-05-10 21:39 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100496

Martin Sebor <msebor at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |msebor at gcc dot gnu.org
         Resolution|---                         |DUPLICATE
             Status|UNCONFIRMED                 |RESOLVED

--- Comment #2 from Martin Sebor <msebor at gcc dot gnu.org> ---
Yes, this is the same as pr100417.  Until something better is available a
workaround is to set the element whose address is passed to the const pointer,
like so:

extern void XmlUtf8Convert(char* p_start, const char* const p_end);

void findEncoding() {
  char buf[128];
  char *p = buf;
  buf[sizeof buf - 1] = 0;   // avoid -Wmaybe-uninitialized below
  XmlUtf8Convert(p, p + sizeof(buf) - 1);
}

Another alternative is to add an optimizer barrier (asm volatile("": :
:"memory")).

*** This bug has been marked as a duplicate of bug 100417 ***

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-05-10 21:39 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-10  8:29 [Bug c/100496] New: False positive with -Wmaybe-uninitialized jochen447 at concept dot de
2021-05-10 18:12 ` [Bug c/100496] " pinskia at gcc dot gnu.org
2021-05-10 21:39 ` msebor at gcc dot gnu.org

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).