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

            Bug ID: 97069
           Summary: Zero valued #line directive results in excessively
                    large blocks of memory being allocated
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: gcov-profile
          Assignee: unassigned at gcc dot gnu.org
          Reporter: martin.ogden at arm dot com
                CC: marxin at gcc dot gnu.org
  Target Milestone: ---

Created attachment 49225
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=49225&action=edit
Associated main.i file

Adding a #line directive with a line number of 0 causes gcov to attempt to
allocate large (~2G) blocks of memory. To reproduce, consider main.c:

    int main()
    {
    }

    #line 0 "main.c"
    void zero_line_directive()
    {
    }

Compile and attempt to run gcov:

    $ gcc main.c -Wall -Werror --coverage
    $ ./a.out
    $ gcov main.c # gcov a-main.c for gcc-11
    terminate called after throwing an instance of 'std::bad_alloc'
    what():  std::bad_alloc

The bad allocation is made in gcc/gcc/gcov.c at line 1346:

    s->lines.resize(last_line + 1);

A line number of 0 causes `last_line` to be large (often 0x100000, sometimes
larger ~10^8).


System details
--------------

Fault occurs on Ubuntu 16.04.6 with with gcc-{5,7,8,9,11} and gcov-{5,7,8,9,11}
(not tested with v{<=4,6,10}).

    $ lsb_release -a
    ...
    Distributor ID: Ubuntu
    Description:    Ubuntu 16.04.6 LTS
    Release:        16.04
    Codename:       xenial

    $ gcc -v
    gcc version 11.0.0 20200902 (experimental) (GCC)

    $ gcc-9 -v
    gcc-9 (Ubuntu 9.3.0-10ubuntu2~16.04) 9.3.0

    $ gcc-8
    gcc-8 (Ubuntu 8.1.0-9ubuntu1~16.04.york1) 8.1.0

    $ gcc-7 -v
    gcc-7 (Ubuntu 7.3.0-23ubuntu2~16.04.york0) 7.3.0

    $ gcc-5 -v
    gcc-5 (Ubuntu 5.5.0-12ubuntu1~16.04) 5.5.0 20171010

It also occurs on macOS 10.15.6 with gcc-{7,8,9} (not tested with
v{<=5,6,10,11}).

Reply via email to