public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/98753] New: -Wfree-nonheap-object on Bison generated code
@ 2021-01-19 20:18 foss at grueninger dot de
  2021-01-19 20:55 ` [Bug c++/98753] " msebor at gcc dot gnu.org
                   ` (15 more replies)
  0 siblings, 16 replies; 17+ messages in thread
From: foss at grueninger dot de @ 2021-01-19 20:18 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 98753
           Summary: -Wfree-nonheap-object on Bison generated code
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Keywords: diagnostic
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: foss at grueninger dot de
  Target Milestone: ---
             Build: 11.0.0 20210114 (experimental) [revision
                    08a4adcf2b6ded2fea97195c715757df61a23395] (SUSE Linux)

Created attachment 50005
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=50005&action=edit
C++ file generated by Bison

While compiling CMake with GCC 11.0, I spot some -Wfree-nonheap-object
warnings. These came from files generated by Bison. I regenerated the C++ files
with the latest GNU Bison release and the problem persisted. So I asked on the
Bison user list and one of the developers claims, that Bison is right.

I don't know whether it is a false positive or whether Bison has a bug.

1.
Attached is the C++ file generated by Bison (cmCommandArgumentParser.cxx).

2.
The warning I get from G++:

[ 54%] Building CXX object
Source/CMakeFiles/CMakeLib.dir/LexerParser/cmCommandArgumentParser.cxx.o
cmCommandArgumentParser.cxx: In function ‘int
cmCommandArgument_yyparse(yyscan_t)’:
cmCommandArgumentParser.cxx:1838:18: warning: ‘void free(void*)’ called on
unallocated object ‘yyssa’ [-Wfree-nonheap-object]
cmCommandArgumentParser.cxx:1203:16: note: declared here

3.
The explanation / reduced code from one of the Bison developers, why the code
is correct:

> GCC 11 is wrong here.  The full story (taken from your file) is:
>
>> // Create.
>>     yy_state_t yyssa[YYINITDEPTH];
>>     yy_state_t *yyss = yyssa;
>> [...]
>> // Grow.
>>       yystacksize *= 2;
>>      {
>>         yy_state_t *yyss1 = yyss;
>>         union yyalloc *yyptr =
>>           YY_CAST (union yyalloc *,
>>                    YYSTACK_ALLOC (YY_CAST (YYSIZE_T, YYSTACK_BYTES 
>> (yystacksize))));
>>         if (! yyptr)
>>           goto yyexhaustedlab;
>>         YYSTACK_RELOCATE (yyss_alloc, yyss);
>>         if (yyss1 != yyssa)
>>           YYSTACK_FREE (yyss1);
>>       }
>> [...]
>> // Clean up
>>   if (yyss != yyssa)
>>     YYSTACK_FREE (yyss);
>
> Or, in words, we use a stack-allocated stack until it's too small and
> then we use a heap-allocated stack, and in that case, and that case
> only, we free it at the end.  Said another way: we never ever call
> free(yyssa).
> 
> So GCC's warning is a false positive.

Source: https://lists.gnu.org/archive/html/help-bison/2021-01/msg00021.html

4.
My GCC version is:

> gcc-11 -v
Using built-in specs.
COLLECT_GCC=gcc-11
COLLECT_LTO_WRAPPER=/usr/lib64/gcc/x86_64-suse-linux/11/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-suse-linux
Configured with: ../configure --prefix=/usr --infodir=/usr/share/info
--mandir=/usr/share/man --libdir=/usr/lib64 --libexecdir=/usr/lib64
--enable-languages=c,c++,objc,fortran,obj-c++,ada,go,d
--enable-offload-targets=nvptx-none,amdgcn-amdhsa, --without-cuda-driver
--enable-checking=release --disable-werror
--with-gxx-include-dir=/usr/include/c++/11 --enable-ssp --disable-libssp
--disable-libvtv --enable-cet=auto --disable-libcc1 --disable-plugin
--with-bugurl=https://bugs.opensuse.org/ --with-pkgversion='SUSE Linux'
--with-slibdir=/lib64 --with-system-zlib --enable-libstdcxx-allocator=new
--disable-libstdcxx-pch --enable-libphobos
--enable-version-specific-runtime-libs --with-gcc-major-version-only
--enable-linker-build-id --enable-linux-futex --enable-gnu-indirect-function
--program-suffix=-11 --without-system-libunwind --enable-multilib
--with-arch-32=x86-64 --with-tune=generic --build=x86_64-suse-linux
--host=x86_64-suse-linux
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 11.0.0 20210114 (experimental) [revision
08a4adcf2b6ded2fea97195c715757df61a23395] (SUSE Linux)

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

end of thread, other threads:[~2024-01-02 16:08 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-19 20:18 [Bug c++/98753] New: -Wfree-nonheap-object on Bison generated code foss at grueninger dot de
2021-01-19 20:55 ` [Bug c++/98753] " msebor at gcc dot gnu.org
2021-01-20  5:22 ` akim.demaille at gmail dot com
2021-01-20  7:31 ` rguenth at gcc dot gnu.org
2021-01-20  7:40 ` foss at grueninger dot de
2021-01-20 16:48 ` msebor at gcc dot gnu.org
2021-01-20 17:00 ` foss at grueninger dot de
2021-01-20 17:34 ` [Bug middle-end/98753] -Wfree-nonheap-object on unreachable code with -O0 msebor at gcc dot gnu.org
2021-01-20 17:34 ` akim.demaille at gmail dot com
2021-01-20 17:40 ` akim.demaille at gmail dot com
2021-01-26 19:37 ` slyfox at gcc dot gnu.org
2021-01-29 11:29 ` boris at kolpackov dot net
2021-01-29 11:30 ` boris at kolpackov dot net
2021-01-29 11:30 ` boris at kolpackov dot net
2021-01-29 11:36 ` boris at kolpackov dot net
2021-01-29 16:48 ` msebor at gcc dot gnu.org
2024-01-02 16:08 ` stefan at bytereef dot 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).