public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Louis Krupp <louis.krupp@zoho.com>
To: "gcc-patches" <gcc-patches@gcc.gnu.org>, "fortran" <fortran@gcc.gnu.org>
Subject: Possible patch for PR fortran/67806
Date: Mon, 12 Oct 2015 05:18:00 -0000	[thread overview]
Message-ID: <1505a7c9fab.f52df49364480.2316667444406332595@zoho.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 2094 bytes --]

The problem involves a derived type with a character component declared CHARACTER(NULL()) or CHARACTER(NULL(n)), where mold argument n is an integer pointer.

I might be missing something, but I'm not sure there's a point to having a character variable whose length is the target of a null pointer.  This program, for example,
crashes with a SEGV reported at line 10 (with line 11 deleted, the program runs to completion):

  1 program z
  2 implicit none
  3    integer, target :: k = 0
  4    integer, pointer :: p => k
  5    nullify(p)
  6    call s(p)
  7 contains
  8    subroutine s(n)
  9       integer, pointer :: n
 10       character (len=n) q
 11       q = 'a'
 12    end subroutine
 13 end program

What to do with CHARACTER(NULL([mold])), besides fix the ICE?  It might have been possible to generate code to define a null pointer and generate code to dereference it and get the expected SEGV, but it seemed easier and possibly more productive to treat CHARACTER(NULL(..)) as an error.  I don't know how what the standard has to say about this.  It might have been one of those things its authors never thought about.

Since this problem is detected at different places in the code, the attached test case gives the following errors with the attached patch:

! { dg-do compile }
! PR 67806
! 1. Initialize a variable of derived type with a string component having
!    a length that is the target of the NULL intrinsic.
! 2. Declare a derived type with a string component having a length that is
!    the target of the NULL intrinsic with an integer mold argument.
subroutine s1
   type t
      character(null()) :: c ! { dg-error "is target of NULL pointer" }
                1
Error: Character length of component ‘c’ is target of NULL pointer at (1)
   end type
   type(t) :: x = t('a')
end subroutine

subroutine s2
   integer, pointer :: n
   type t
      character(null(n)) :: c ! { dg-error "is target of NULL pointer" }
                1
Error: Character length is target of NULL pointer at (1)
   end type
end subroutine

[-- Attachment #2: init_bad_string_comp_1.f90 --]
[-- Type: application/octet-stream, Size: 604 bytes --]

! { dg-do compile }
! PR 67806
! 1. Initialize a variable of derived type with a string component having
!    a length that is the target of the NULL intrinsic.
! 2. Declare a derived type with a string component having a length that is
!    the target of the NULL intrinsic with an integer mold argument.
subroutine s1
   type t
      character(null()) :: c ! { dg-error "is target of NULL pointer" }
   end type
   type(t) :: x = t('a')
end subroutine

subroutine s2
   integer, pointer :: n
   type t
      character(null(n)) :: c ! { dg-error "is target of NULL pointer" }
   end type
end subroutine

[-- Attachment #3: patch.txt --]
[-- Type: text/plain, Size: 1699 bytes --]

Index: gcc/fortran/resolve.c
===================================================================
--- gcc/fortran/resolve.c	(revision 228700)
+++ gcc/fortran/resolve.c	(working copy)
@@ -1134,7 +1134,8 @@ resolve_structure_cons (gfc_expr *expr, int init)
   t = true;
 
   if (expr->ts.type == BT_DERIVED)
-    resolve_fl_derived0 (expr->ts.u.derived);
+    if (!resolve_fl_derived0 (expr->ts.u.derived))
+      return false;
 
   cons = gfc_constructor_first (expr->value.constructor);
 
@@ -10882,6 +10883,13 @@ resolve_charlen (gfc_charlen *cl)
 	}
     }
 
+  if (cl->length && cl->length->expr_type == EXPR_NULL)
+    {
+      gfc_error ("Character length is target of NULL pointer at %L",
+		 &cl->length->where);
+      return false;
+    }
+
   /* "If the character length parameter value evaluates to a negative
      value, the length of character entities declared is zero."  */
   if (cl->length && !gfc_extract_int (cl->length, &i) && i < 0)
@@ -13090,10 +13098,16 @@ resolve_fl_derived0 (gfc_symbol *sym)
 	     || (!resolve_charlen(c->ts.u.cl))
 	     || !gfc_is_constant_expr (c->ts.u.cl->length))
 	   {
-	     gfc_error ("Character length of component %qs needs to "
-			"be a constant specification expression at %L",
+	     gfc_error (c->ts.u.cl->length &&
+			c->ts.u.cl->length->expr_type == EXPR_NULL ?
+			  "Character length of component %qs is target of "
+			  "NULL pointer at %L"
+			:
+			  "Character length of component %qs needs to "
+			  "be a constant specification expression at %L",
 			c->name,
-			c->ts.u.cl->length ? &c->ts.u.cl->length->where : &c->loc);
+			c->ts.u.cl->length ?
+			  &c->ts.u.cl->length->where : &c->loc);
 	     return false;
 	   }
 	}

             reply	other threads:[~2015-10-12  5:18 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-10-12  5:18 Louis Krupp [this message]
2015-10-12 15:41 ` Steve Kargl
2015-10-12 21:50   ` Louis Krupp

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1505a7c9fab.f52df49364480.2316667444406332595@zoho.com \
    --to=louis.krupp@zoho.com \
    --cc=fortran@gcc.gnu.org \
    --cc=gcc-patches@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).