From patchwork Tue Mar 11 07:47:24 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 107622 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 231123858417 for ; Tue, 11 Mar 2025 07:48:28 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 231123858417 Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=FDPCE7Po X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTP id D0A853858CD1 for ; Tue, 11 Mar 2025 07:47:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D0A853858CD1 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org D0A853858CD1 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1741679257; cv=none; b=xFP2og91ec4mDgRj26TXO2r5CMEdg5rRALZJxmmXNt5Q644pUByGHW717iDgB7YL6wHEA3K7jDCtKtkIyOkJUeMfBj0SCpvjo+wKlWNawI3pBxtWAGashofmoNBYvlhMn4gR3Kal7cXd2yScjz1T/17eUzmMu67jzkzTcfuT0Vs= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1741679257; c=relaxed/simple; bh=8aKnAx51hVgxTwIupqC85XjQUGRC79aDyjKPTW35QOw=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=uzuabrYcEc/s0luOaOGEeF5tBqFfPQ+B33xjBIIazmYuZ6qmx8B7Ax+hK7Avlu095YCpfKbI0hhKFhMx+ohsJzfp5VUNJWgMcAcy00NFJtXDJTZvAqj64TPThIs/EbQAZrdXq07sEeUW/DnHsq+5Soh46MlyNDrH3D/6/0l+B+c= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D0A853858CD1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1741679257; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type; bh=66FJW6BMkY0Y0fkZWY2NIY5IMkV89HI94nXfHcwhVeg=; b=FDPCE7PoVCA5UGV0l7IpqRUb9RKpZlwIdVbgUBgrKR36YEnIamP7ZxKVpsdanrcAChyCCn wSioVwj4LPf2w/VCpfUJOJJglJN02MjJjtzmG7TDpF7KmvaBlYWJNAOa0MtO1NUXDFo7yk xigLE0cIRGnwntVIqS7czPk6U3feSNo= Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-274-XU529BJqPQ24fx6dRlb64Q-1; Tue, 11 Mar 2025 03:47:33 -0400 X-MC-Unique: XU529BJqPQ24fx6dRlb64Q-1 X-Mimecast-MFC-AGG-ID: XU529BJqPQ24fx6dRlb64Q_1741679252 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 5776A18001E1 for ; Tue, 11 Mar 2025 07:47:32 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.22.89.222]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id A18111944F12; Tue, 11 Mar 2025 07:47:31 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 52B7lRPq1927085 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Tue, 11 Mar 2025 08:47:27 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 52B7lQCg1927084; Tue, 11 Mar 2025 08:47:26 +0100 Date: Tue, 11 Mar 2025 08:47:24 +0100 From: Jakub Jelinek To: "Joseph S. Myers" Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] c: Don't emit -Wunterminated-string-initialization warning for multi-dimensional nonstring array initializers [PR117178] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: Wx_i77Cx1oGFMAx6_UK77AHiRnz9k9shhu6iFZA5qmo_1741679252 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.5 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces~patchwork=sourceware.org@gcc.gnu.org Hi! My/Kees' earlier patches adjusted -Wunterminated-string-initialization warning so that it doesn't warn about initializers of nonstring decls and that nonstring attribute is allowed on multi-dimensional arrays. Unfortunately as this testcase shows, we still warn about initializers of multi-dimensional array nonstring decls. The problem is that in that case field passed to output_init_element is actually INTEGER_CST, index into the array. For RECORD_OR_UNION_TYPE_P (constructor_type) field is a FIELD_DECL which we want to use, but otherwise (in arrays) IMHO we want to use constructor_fields (which is the innermost FIELD_DECL whose part is being initialized), or - if that is NULL - constructor_decl, the whole decl being initialized with multi-dimensional array type. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2025-03-11 Jakub Jelinek PR c/117178 * c-typeck.cc (output_init_element): Pass field to digest_init only for record/union types, otherwise pass constructor_fields if non-NULL and constructor_decl if constructor_fields is NULL. * gcc.dg/Wunterminated-string-initialization-2.c: New test. Jakub --- gcc/c/c-typeck.cc.jj 2025-03-10 09:30:59.826060100 +0100 +++ gcc/c/c-typeck.cc 2025-03-10 11:43:00.159461692 +0100 @@ -11419,7 +11419,11 @@ output_init_element (location_t loc, tre if (!require_constexpr_value || !npc || TREE_CODE (constructor_type) != POINTER_TYPE) - new_value = digest_init (loc, field, type, new_value, origtype, npc, + new_value = digest_init (loc, + RECORD_OR_UNION_TYPE_P (constructor_type) + ? field : constructor_fields + ? constructor_fields : constructor_decl, + type, new_value, origtype, npc, int_const_expr, arith_const_expr, strict_string, require_constant_value, require_constexpr_value); if (new_value == error_mark_node) --- gcc/testsuite/gcc.dg/Wunterminated-string-initialization-2.c.jj 2025-03-10 11:50:48.945971803 +0100 +++ gcc/testsuite/gcc.dg/Wunterminated-string-initialization-2.c 2025-03-10 11:50:18.272396452 +0100 @@ -0,0 +1,62 @@ +/* PR c/117178 */ +/* { dg-do compile } */ +/* { dg-options "-Wunterminated-string-initialization" } */ + +const char a[][4] __attribute__((nonstring)) = { + "ABCD", + "EFGH", + "IJK", + "LMNO" +}; +const char b[][2][2] __attribute__((nonstring)) = { + { "PQ", + "R" }, + { "S", + "TU" } +}; +struct S { int c; char d[3][5] __attribute__((nonstring)); int e; } f = { + 0, + { "abcde", + "defg", + "hijkl" }, + 1 +}; +struct T { int g; char h[3][2][2] __attribute__((nonstring)); int i; } j = { + 0, + { { "m", + "no" }, + { "pq", + "r" }, + { "s", + "tu" } }, + 1 +}; +const char k[][4] = { + "ABCD", /* { dg-warning "initializer-string for array of 'char' truncates NUL terminator but destination lacks 'nonstring' attribute \\\(5 chars into 4 available\\\)" } */ + "EFGH", /* { dg-warning "initializer-string for array of 'char' truncates NUL terminator but destination lacks 'nonstring' attribute \\\(5 chars into 4 available\\\)" } */ + "IJK", + "LMNO" /* { dg-warning "initializer-string for array of 'char' truncates NUL terminator but destination lacks 'nonstring' attribute \\\(5 chars into 4 available\\\)" } */ +}; +const char l[][2][2] = { + { "PQ", /* { dg-warning "initializer-string for array of 'char' truncates NUL terminator but destination lacks 'nonstring' attribute \\\(3 chars into 2 available\\\)" } */ + "R" }, + { "S", + "TU" } /* { dg-warning "initializer-string for array of 'char' truncates NUL terminator but destination lacks 'nonstring' attribute \\\(3 chars into 2 available\\\)" } */ +}; +struct U { int m; char n[3][5]; int o; } p = { + 0, + { "abcde", /* { dg-warning "initializer-string for array of 'char' truncates NUL terminator but destination lacks 'nonstring' attribute \\\(6 chars into 5 available\\\)" } */ + "defg", + "hijkl" }, /* { dg-warning "initializer-string for array of 'char' truncates NUL terminator but destination lacks 'nonstring' attribute \\\(6 chars into 5 available\\\)" } */ + 1 +}; +struct V { int q; char r[3][2][2]; int s; } t = { + 0, + { { "m", + "no" }, /* { dg-warning "initializer-string for array of 'char' truncates NUL terminator but destination lacks 'nonstring' attribute \\\(3 chars into 2 available\\\)" } */ + { "pq", /* { dg-warning "initializer-string for array of 'char' truncates NUL terminator but destination lacks 'nonstring' attribute \\\(3 chars into 2 available\\\)" } */ + "r" }, + { "s", + "tu" } }, /* { dg-warning "initializer-string for array of 'char' truncates NUL terminator but destination lacks 'nonstring' attribute \\\(3 chars into 2 available\\\)" } */ + 1 +};