xcoffread.c: Fix -Werror=dangling-pointer= issue with main_subfile.
Commit Message
GCC 13 points out that main_subfile has local function scope, but a
pointer to it is assigned to the global inclTable array subfile
element field:
In function ‘void process_linenos(CORE_ADDR, CORE_ADDR)’,
inlined from ‘void aix_process_linenos(objfile*)’ at xcoffread.c:727:19,
inlined from ‘void aix_process_linenos(objfile*)’ at xcoffread.c:720:1:
xcoffread.c:629:37: error: storing the address of local variable ‘main_subfile’ in ‘*inclTable.19_45 + _28._inclTable::subfile’ [-Werror=dangling-pointer=]
629 | inclTable[ii].subfile = &main_subfile;
| ~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~
xcoffread.c: In function ‘void aix_process_linenos(objfile*)’:
xcoffread.c:579:18: note: ‘main_subfile’ declared here
579 | struct subfile main_subfile;
| ^~~~~~~~~~~~
xcoffread.c:496:19: note: ‘inclTable’ declared here
496 | static InclTable *inclTable; /* global include table */
| ^~~~~~~~~
Fix this by making main_subfile file static that is allocated and
deallocated together with inclTable and allocate_include_entry and
xcoff_symfile_finish. Adjust the use of main_subfile in
process_linenos to take a pointer to the struct subfile.
---
gdb/xcoffread.c | 24 +++++++++++++-----------
1 file changed, 13 insertions(+), 11 deletions(-)
Comments
On 4/29/23 17:13, Mark Wielaard wrote:
> GCC 13 points out that main_subfile has local function scope, but a
> pointer to it is assigned to the global inclTable array subfile
> element field:
>
> In function ‘void process_linenos(CORE_ADDR, CORE_ADDR)’,
> inlined from ‘void aix_process_linenos(objfile*)’ at xcoffread.c:727:19,
> inlined from ‘void aix_process_linenos(objfile*)’ at xcoffread.c:720:1:
> xcoffread.c:629:37: error: storing the address of local variable ‘main_subfile’ in ‘*inclTable.19_45 + _28._inclTable::subfile’ [-Werror=dangling-pointer=]
> 629 | inclTable[ii].subfile = &main_subfile;
> | ~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~
> xcoffread.c: In function ‘void aix_process_linenos(objfile*)’:
> xcoffread.c:579:18: note: ‘main_subfile’ declared here
> 579 | struct subfile main_subfile;
> | ^~~~~~~~~~~~
> xcoffread.c:496:19: note: ‘inclTable’ declared here
> 496 | static InclTable *inclTable; /* global include table */
> | ^~~~~~~~~
>
> Fix this by making main_subfile file static that is allocated and
> deallocated together with inclTable and allocate_include_entry and
> xcoff_symfile_finish. Adjust the use of main_subfile in
> process_linenos to take a pointer to the struct subfile.
I'm not familiar at all with this code, but your change looks reasonable
to me.
Some style comments:
> ---
> gdb/xcoffread.c | 24 +++++++++++++-----------
> 1 file changed, 13 insertions(+), 11 deletions(-)
>
> diff --git a/gdb/xcoffread.c b/gdb/xcoffread.c
> index d71127b40f6..db6f2df6c0a 100644
> --- a/gdb/xcoffread.c
> +++ b/gdb/xcoffread.c
> @@ -498,6 +498,9 @@ static int inclIndx; /* last entry to table */
> static int inclLength; /* table length */
> static int inclDepth; /* nested include depth */
>
> +/* subfile structure for the main compilation unit. */
> +static struct subfile *main_subfile;
Remove "struct".
> @@ -548,6 +551,7 @@ allocate_include_entry (void)
> inclTable = XCNEWVEC (InclTable, INITIAL_INCLUDE_TABLE_LENGTH);
> inclLength = INITIAL_INCLUDE_TABLE_LENGTH;
> inclIndx = 0;
> + main_subfile = new (struct subfile);
"new subfile" would be enough.
> for (int ii = 0; ii < inclIndx; ++ii)
> {
> - if (inclTable[ii].subfile != ((struct subfile *) &main_subfile)
> + if (inclTable[ii].subfile != ((struct subfile *) main_subfile)
Since you touch this line, I think you could remove the cast.
main_subfile is already of the right type.
Simon
Hi Simon,
On Mon, May 01, 2023 at 09:53:39AM -0400, Simon Marchi wrote:
> > Fix this by making main_subfile file static that is allocated and
> > deallocated together with inclTable and allocate_include_entry and
> > xcoff_symfile_finish. Adjust the use of main_subfile in
> > process_linenos to take a pointer to the struct subfile.
>
> I'm not familiar at all with this code, but your change looks reasonable
> to me.
I am also not familiar with this code, but noticed that the
fedora-latest and suse-tumbleweed buildbots started failing when they
upgraded to GCC 13.1. The simplest solution seemed to be to give both
structures the same lifetime. I didn't see any regressions with this
fix (but don't actually know if the testsuite triggers this code).
> Some style comments:
>
> > ---
> > gdb/xcoffread.c | 24 +++++++++++++-----------
> > 1 file changed, 13 insertions(+), 11 deletions(-)
> >
> > diff --git a/gdb/xcoffread.c b/gdb/xcoffread.c
> > index d71127b40f6..db6f2df6c0a 100644
> > --- a/gdb/xcoffread.c
> > +++ b/gdb/xcoffread.c
> > @@ -498,6 +498,9 @@ static int inclIndx; /* last entry to table */
> > static int inclLength; /* table length */
> > static int inclDepth; /* nested include depth */
> >
> > +/* subfile structure for the main compilation unit. */
> > +static struct subfile *main_subfile;
>
> Remove "struct".
OK.
> > @@ -548,6 +551,7 @@ allocate_include_entry (void)
> > inclTable = XCNEWVEC (InclTable, INITIAL_INCLUDE_TABLE_LENGTH);
> > inclLength = INITIAL_INCLUDE_TABLE_LENGTH;
> > inclIndx = 0;
> > + main_subfile = new (struct subfile);
>
> "new subfile" would be enough.
OK.
> > for (int ii = 0; ii < inclIndx; ++ii)
> > {
> > - if (inclTable[ii].subfile != ((struct subfile *) &main_subfile)
> > + if (inclTable[ii].subfile != ((struct subfile *) main_subfile)
>
> Since you touch this line, I think you could remove the cast.
> main_subfile is already of the right type.
Right.
Will sent v2 with those changes.
Thanks,
Mark
@@ -498,6 +498,9 @@ static int inclIndx; /* last entry to table */
static int inclLength; /* table length */
static int inclDepth; /* nested include depth */
+/* subfile structure for the main compilation unit. */
+static struct subfile *main_subfile;
+
static void allocate_include_entry (void);
static void
@@ -548,6 +551,7 @@ allocate_include_entry (void)
inclTable = XCNEWVEC (InclTable, INITIAL_INCLUDE_TABLE_LENGTH);
inclLength = INITIAL_INCLUDE_TABLE_LENGTH;
inclIndx = 0;
+ main_subfile = new (struct subfile);
}
else if (inclIndx >= inclLength)
{
@@ -575,9 +579,6 @@ process_linenos (CORE_ADDR start, CORE_ADDR end)
file_ptr max_offset
= XCOFF_DATA (this_symtab_objfile)->max_lineno_offset;
- /* subfile structure for the main compilation unit. */
- struct subfile main_subfile;
-
/* In the main source file, any time we see a function entry, we
reset this variable to function's absolute starting line number.
All the following line numbers in the function are relative to
@@ -596,7 +597,7 @@ process_linenos (CORE_ADDR start, CORE_ADDR end)
/* All source lines were in the main source file. None in include
files. */
- enter_line_range (&main_subfile, offset, 0, start, end,
+ enter_line_range (main_subfile, offset, 0, start, end,
&main_source_baseline);
else
@@ -613,7 +614,7 @@ process_linenos (CORE_ADDR start, CORE_ADDR end)
if (offset < inclTable[ii].begin)
{
enter_line_range
- (&main_subfile, offset, inclTable[ii].begin - linesz,
+ (main_subfile, offset, inclTable[ii].begin - linesz,
start, 0, &main_source_baseline);
}
@@ -624,9 +625,9 @@ process_linenos (CORE_ADDR start, CORE_ADDR end)
main_source_baseline = inclTable[ii].funStartLine;
enter_line_range
- (&main_subfile, inclTable[ii].begin, inclTable[ii].end,
+ (main_subfile, inclTable[ii].begin, inclTable[ii].end,
start, 0, &main_source_baseline);
- inclTable[ii].subfile = &main_subfile;
+ inclTable[ii].subfile = main_subfile;
}
else
{
@@ -648,24 +649,24 @@ process_linenos (CORE_ADDR start, CORE_ADDR end)
enter remaining lines of the main file, if any left. */
if (offset < max_offset + 1 - linesz)
{
- enter_line_range (&main_subfile, offset, 0, start, end,
+ enter_line_range (main_subfile, offset, 0, start, end,
&main_source_baseline);
}
}
/* Process main file's line numbers. */
- if (!main_subfile.line_vector_entries.empty ())
+ if (!main_subfile->line_vector_entries.empty ())
{
/* Line numbers are not necessarily ordered. xlc compilation will
put static function to the end. */
- arrange_linetable (main_subfile.line_vector_entries);
+ arrange_linetable (main_subfile->line_vector_entries);
}
/* Now, process included files' line numbers. */
for (int ii = 0; ii < inclIndx; ++ii)
{
- if (inclTable[ii].subfile != ((struct subfile *) &main_subfile)
+ if (inclTable[ii].subfile != ((struct subfile *) main_subfile)
&& !inclTable[ii].subfile->line_vector_entries.empty ())
{
/* Line numbers are not necessarily ordered. xlc compilation will
@@ -1803,6 +1804,7 @@ xcoff_symfile_finish (struct objfile *objfile)
{
xfree (inclTable);
inclTable = NULL;
+ delete main_subfile;
}
inclIndx = inclLength = inclDepth = 0;
}