Bug 24932 - GCC segfault's on strcmp in tree.c:annotate_with_file_line
Summary: GCC segfault's on strcmp in tree.c:annotate_with_file_line
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 4.1.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
Depends on:
Reported: 2005-11-18 18:08 UTC by Gustavo Sverzut Barbieri
Modified: 2009-02-22 16:36 UTC (History)
1 user (show)

See Also:
Known to work:
Known to fail:
Last reconfirmed:

gcc-annotate_with_file_line.patch (224 bytes, patch)
2005-11-18 18:09 UTC, Gustavo Sverzut Barbieri
Details | Diff
gcc-lower_function_body.patch (305 bytes, patch)
2005-11-18 18:36 UTC, Gustavo Sverzut Barbieri
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Gustavo Sverzut Barbieri 2005-11-18 18:08:33 UTC
I'm writing a tutorial on a HelloWorld compiler front-end for GCC (http://svn.gna.org/viewcvs/gsc/branches/hello-world/doc/hello.xml) and noticed that a function with statements but no DECL_SOURCE_FILE set, seg-faults on  cgraph_finalize_function(), because it calls gimple-low.c:lower_function_body(), which in turn does:

SET_EXPR_LOCATION (x, cfun->function_end_locus);

however, if cfun->function_end_locus is empty ({NULL, 0}), it will call tree.c:annotate_with_file_line() with file=NULL, and it will fail on this "if":

  if (last_annotated_node
      && last_annotated_node->line == line
      && (last_annotated_node->file == file
	  || !strcmp (last_annotated_node->file, file)))

because strcmp (last_annotated_node->file, file), with file=NULL will seg-fault.

The attached patch checks if file is NULL before using strcmp.

The program that fails build is: http://svn.gna.org/viewcvs/gsc/branches/hello-world/compiler/hello1.c?rev=551
Comment 1 Gustavo Sverzut Barbieri 2005-11-18 18:09:13 UTC
Created attachment 10274 [details]

Fix segfault
Comment 2 Andrew Pinski 2005-11-18 18:17:56 UTC
Actually this should never happen in general.  Your front-end really should be adding locus on the statements.

I will let someone else decide if this is a real middle-end bug or a bug in your front-end.

I should note that I don't like the check in annotate_with_file_line at all.  I would rather have a check in gimple-low.c instead.
Comment 3 Gustavo Sverzut Barbieri 2005-11-18 18:36:26 UTC
Created attachment 10276 [details]

Alternative patch that also fix seg fault, but earlier.

It's uglier than the previous, and is more restricted, since it just protect the behaviour from gimple-low.c:lower_function_body(), while the previous patch does the check for every use of annotate_with_file_line, used by SET_EXPR_LOCATION.

I cannot know if this is a bug in the middle-end, since there is no documentation on when I need to set this value. If so, probably we can use gcc_assert() to confirm this?
Comment 4 Gustavo Sverzut Barbieri 2005-12-07 10:49:07 UTC
So, this is a bug.

I just need a confirmation if it's a bug in the middle-end layer, so there is already a patch, or if this is a bug in the front-end, then I will add the check to the gimplify code.

Since there is no documentation on the expected behaviour, we need to be safe.

Also, it would be good to have this before our presentation at LinuxConf.AU, then we can be sure what's expected and how to teach others.
Comment 5 Gustavo Sverzut Barbieri 2005-12-24 22:47:43 UTC
any news on this front?

I'll present a tutorial in LinuxConf.au and need to know if front-end should provide the information or middle-end should handle it.

In both cases I'll provide the patch to gcc, but I need to know where the bug is.
Comment 6 Steven Bosscher 2009-02-22 16:36:08 UTC
Locations are now handled differently (mapped locations).