This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[patch] Fix PR 23439/23440, two alternatives


A couple of weeks ago I posted two alternatives to fix an
ice-on-invalid-code of both the C and C++ frontend for code like this
(see http://gcc.gnu.org/ml/gcc-patches/2005-09/msg00680.html ):

  void foo() { if ()

Both frontends call annotate_with_file_line with a NULL instead of a
proper filename. The NULL is passed to strcmp which segfaults.

The first patch fixes this by returning early from annotate_with_file_line,
if no filename is given. It also clears an already present annotation
to match the behavior in the MAPPED_LOCATION case (according to Ian's
comment).

The second version changes the frontends: It modifies unknown_location
and its C++ counterpart to include a dummy filename instead of a
NULL-pointer.

A testcase for each frontend is also included.

A final decision on which approach is the better one was never reached,
so I'm reposting updated versions of the patches.

Both versions were bootstrapped and regtested on i686-pc-linux-gnu.
Which version is the preferred one?
OK for mainline and 4.0 branch?

Regards,
Volker

:ADDPATCH C/C++:


First version:

2005-10-05  Volker Reichelt  <reichelt@igpm.rwth-aachen.de>

	PR c/23439
	PR c++/23440
	* tree.c (annotate_with_file_line): Remove annotation for
	empty filename and return early.

===================================================================
--- gcc/gcc/tree.c      16 Aug 2005 00:35:50 -0000      1.500
+++ gcc/gcc/tree.c      11 Sep 2005 09:41:01 -0000
@@ -3109,6 +3109,14 @@ expand_location (source_location loc)
 void
 annotate_with_file_line (tree node, const char *file, int line)
 {
+  /* Remove annotation for unknown location.
+     Empty filenames would crash strcmp.  */
+  if (!file)
+    {
+      SET_EXPR_LOCUS (node, NULL);
+      return;
+    }
+
   /* Roughly one percent of the calls to this function are to annotate
      a node with the same information already attached to that node!
      Just return instead of wasting memory.  */
===================================================================


Second version:

2005-10-05  Volker Reichelt  <reichelt@igpm.rwth-aachen.de>

	PR c/23439
	* toplev.c (unknown_location): Annotate with dummy file name.

===================================================================
--- gcc/gcc/toplev.c	9 Sep 2005 00:46:45 -0000	1.975
+++ gcc/gcc/toplev.c	16 Sep 2005 21:06:17 -0000
@@ -140,7 +140,7 @@ static const char **save_argv;
 const char *main_input_filename;
 
 #ifndef USE_MAPPED_LOCATION
-location_t unknown_location = { NULL, 0 };
+location_t unknown_location = { "dummy_location", 0 };
 #endif
 
 /* Used to enable -fvar-tracking, -fweb and -frename-registers according
===================================================================

2005-09-16  Volker Reichelt  <reichelt@igpm.rwth-aachen.de>

	PR c++/23440
	* parser.c (eof_token): Annotate with dummy file name.

===================================================================
--- gcc/gcc/cp/parser.c	8 Sep 2005 13:14:13 -0000	1.356
+++ gcc/gcc/cp/parser.c	15 Sep 2005 15:40:38 -0000
@@ -76,7 +76,7 @@ static const cp_token eof_token =
 #if USE_MAPPED_LOCATION
   0
 #else
-  {0, 0}
+  {"dummy_location", 0}
 #endif
 };
 
===================================================================


Testcases:

2005-10-05  Volker Reichelt  <reichelt@igpm.rwth-aachen.de>

	PR c/23439
	* gcc.dg/for-1.c: New test.

	PR c++/23440
	* g++.dg/parse/for1.C: New test.

===================================================================
--- gcc/gcc/testsuite/gcc.dg/for-1.c   2003-09-23 19:59:22 +0200
+++ gcc/gcc/testsuite/gcc.dg/for-1.c   2005-09-11 01:19:56 +0200
@@ -0,0 +1,7 @@
+/* PR c/23439 */
+/* Origin: Serge Belyshev <belyshev@depni.sinp.msu.ru> */
+/* { dg-do compile } */
+/* { dg-options "" } */
+
+# 0 "for-1.c"
+void foo() { for  /* { dg-error "at end of input" "" { target *-*-* } 0 } */
===================================================================
--- gcc/gcc/testsuite/g++.dg/parse/for1.C   2003-09-23 19:59:22 +0200
+++ gcc/gcc/testsuite/g++.dg/parse/for1.C   2005-09-11 01:21:43 +0200
@@ -0,0 +1,7 @@
+// PR c++/23440
+// Origin: Volker Reichelt <reichelt@igpm.rwth-aachen.de>
+// { dg-do compile }
+// { dg-options "" }
+
+# 0 "for1.C"
+void foo() { for (;;)  // { dg-error "at end of input" "" { target *-*-* } 0 }
===================================================================



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]