diff mbox series

[fortran] Reject character lengths unequal to one in BIND(C)

Message ID a8e53be8-186f-efcc-6fc9-41d90e1d96ef@netcologne.de
State New
Headers show
Series [fortran] Reject character lengths unequal to one in BIND(C) | expand

Commit Message

Thomas Koenig Jan. 28, 2018, 4 p.m. UTC
Hello world,

At the moment, gfortran silently accepts

   type, bind(c) :: a
      character(len=2,kind=c_char) :: b
   end type a

translating this into I don't know what. With the -fc-prototypes
option, we now get

typedef struct a {
     char b;
} a;

which is clearly bogus. Any user code which has the broken code above
will then likely crash and burn when using a gfortran-generated
prototype.

The attached patch solves that particular problem by issuing errors
in this case.  OK for trunk?

Regards

	Thomas

2017-01-28  Thomas Koenig  <tkoenig@gcc.gnu.org>

         PR fortran/84073
         * resolve.c (resolve_component): Ensure BIND(C) character
         components have length one.
         (resolve_symbol): Likewise for variables.

2017-01-28  Thomas Koenig  <tkoenig@gcc.gnu.org>

         PR fortran/84073
         * gfortran.dg/bind_c_usage_31.f90: New test.

Comments

Steve Kargl Jan. 29, 2018, 2:41 a.m. UTC | #1
On Sun, Jan 28, 2018 at 05:00:21PM +0100, Thomas Koenig wrote:
> Hello world,
> 
> At the moment, gfortran silently accepts
> 
>    type, bind(c) :: a
>       character(len=2,kind=c_char) :: b
>    end type a
> 
> translating this into I don't know what. With the -fc-prototypes
> option, we now get
> 
> typedef struct a {
>      char b;
> } a;
> 
> which is clearly bogus. Any user code which has the broken code above
> will then likely crash and burn when using a gfortran-generated
> prototype.
> 
> The attached patch solves that particular problem by issuing errors
> in this case.  OK for trunk?
> 

yes.
diff mbox series

Patch

Index: resolve.c
===================================================================
--- resolve.c	(Revision 257131)
+++ resolve.c	(Arbeitskopie)
@@ -13557,6 +13557,17 @@  resolve_component (gfc_component *c, gfc_symbol *s
       return false;
     }
 
+  /* F2003, 15.2.1 - length has to be one.  */
+  if (sym->attr.is_bind_c && c->ts.type == BT_CHARACTER
+      && (c->ts.u.cl == NULL || c->ts.u.cl->length == NULL
+	  || !gfc_is_constant_expr (c->ts.u.cl->length)
+	  || mpz_cmp_si (c->ts.u.cl->length->value.integer, 1) != 0))
+    {
+      gfc_error ("Component %qs of BIND(C) type at %L must have length one",
+		 c->name, &c->loc);
+      return false;
+    }
+
   if (c->attr.proc_pointer && c->ts.interface)
     {
       gfc_symbol *ifc = c->ts.interface;
@@ -14804,6 +14815,15 @@  resolve_symbol (gfc_symbol *sym)
 		     "module level scope", sym->name, &(sym->declared_at));
 	  t = false;
 	}
+      else if (sym->ts.type == BT_CHARACTER
+	       && (sym->ts.u.cl == NULL || sym->ts.u.cl->length == NULL
+		   || !gfc_is_constant_expr (sym->ts.u.cl->length)
+		   || mpz_cmp_si (sym->ts.u.cl->length->value.integer, 1) != 0))
+	{
+	  gfc_error ("BIND(C) Variable %qs at %L must have length one",
+		     sym->name, &sym->declared_at);
+	  t = false;
+	}
       else if (sym->common_head != NULL && sym->attr.implicit_type == 0)
         {
           t = verify_com_block_vars_c_interop (sym->common_head);