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

--- Comment #5 from Jerry DeLisle <jvdelisle at gcc dot gnu.org> ---
I found that the attached patch does not work.  At the point of assertion many
of the other functions to free memory have null pointers which leads to
segfaults all along the way.

The following approach appears to work, however, obviously there is a lot of
memory left not freed, so we would rely on the OS to clean it up. Maybe this is
ok, not sure.

+++ b/gcc/fortran/symbol.cc
@@ -4032,7 +4032,7 @@ void
 gfc_free_namespace (gfc_namespace *&ns)
 {
   gfc_namespace *p, *q;
-  int i;
+  int i, ecnt;
   gfc_was_finalized *f;

   if (ns == NULL)
@@ -4042,6 +4042,12 @@ gfc_free_namespace (gfc_namespace *&ns)
   if (ns->refs > 0)
     return;

+  /* In the presence of errors, the namespace may be
+     corrupted or non-sense.  Don't assert() and bail out.  */
+  gfc_get_errors (NULL, &ecnt);
+  if (ecnt > 0)
+    return;
+
   gcc_assert (ns->refs == 0);

   gfc_free_statements (ns->code);

Reply via email to