From patchwork Sun Jan 28 16:00:21 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Koenig X-Patchwork-Id: 866837 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=gcc.gnu.org (client-ip=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-472164-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="xAZXJzFI"; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3zTy7C1yNXz9sBd for ; Mon, 29 Jan 2018 03:01:25 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:to :from:subject:message-id:date:mime-version:content-type; q=dns; s=default; b=SFmICmWc5pOL1YQMoeUolvADrJcMdxKnXklr9gaWALFMsgKSk2 AKXMIYgjqgiCLe3jdb0DJv+5lPJuErNP2vs6Ptw4OrU2HK6xSu4bsUTt5zfFZu7l bwZn0Qb+RmBxp64aYorwSn4zB8folT07YqUwrSkYgI5EJavOMumui8AZc= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:to :from:subject:message-id:date:mime-version:content-type; s= default; bh=VHpc2420nysbcFUz3Czqjc1IN1o=; b=xAZXJzFIupQz4VbFNHuI NvCF/oyXtGAlcdTCgKEeIuMlm7gSDuHqYQnWoVyZqob5fh7kCrqJFT4eRPNpCxuI oF0T9umRWMT5DxYpAXL5U61PCBwjHrSqEu0r5S3AJh0FQZ8dfj0h4+MKdw7Fjypp bPAHXqI/z5jxA9NUbbY9fyU= Received: (qmail 46205 invoked by alias); 28 Jan 2018 16:01:18 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 44548 invoked by uid 89); 28 Jan 2018 16:01:13 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-11.9 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_LOW, SPF_PASS, T_RP_MATCHES_RCVD autolearn=ham version=3.3.2 spammy=sk:gfortra, bt_character, resolve.c, gfc_symbol X-Spam-User: qpsmtpd, 2 recipients X-HELO: cc-smtpout2.netcologne.de Received: from cc-smtpout2.netcologne.de (HELO cc-smtpout2.netcologne.de) (89.1.8.212) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Sun, 28 Jan 2018 16:01:10 +0000 Received: from cc-smtpin2.netcologne.de (cc-smtpin2.netcologne.de [89.1.8.202]) by cc-smtpout2.netcologne.de (Postfix) with ESMTP id 566FF1289A; Sun, 28 Jan 2018 17:00:24 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by cc-smtpin2.netcologne.de (Postfix) with ESMTP id 49DA411D76; Sun, 28 Jan 2018 17:00:24 +0100 (CET) Received: from [78.35.141.138] (helo=cc-smtpin2.netcologne.de) by localhost with ESMTP (eXpurgate 4.1.9) (envelope-from ) id 5a6df398-02b8-7f0000012729-7f0000018f01-1 for ; Sun, 28 Jan 2018 17:00:24 +0100 Received: from [192.168.178.20] (xdsl-78-35-141-138.netcologne.de [78.35.141.138]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by cc-smtpin2.netcologne.de (Postfix) with ESMTPSA; Sun, 28 Jan 2018 17:00:22 +0100 (CET) To: "fortran@gcc.gnu.org" , gcc-patches From: Thomas Koenig Subject: [patch, fortran] Reject character lengths unequal to one in BIND(C) Message-ID: Date: Sun, 28 Jan 2018 17:00:21 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.5.2 MIME-Version: 1.0 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 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 PR fortran/84073 * gfortran.dg/bind_c_usage_31.f90: New test. 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);