From patchwork Thu Mar 14 02:17:16 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Sebor X-Patchwork-Id: 1056310 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-497869-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="GWY/2dda"; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="fQugqytI"; 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 44KXRL5b2mz9s5c for ; Thu, 14 Mar 2019 13:17:30 +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=WKdOjDZbpHG2G7w+aoC6Ojf0Kb8Ya4tOgapRNVAAKDIlrMWiC2 jLy0P9sXJnFo0mRhA+0zFvkqhnFZwYauzMAq/zoSHjmplNd9fntyotGZlM8SHiVu /fQo0ABqLm50GrsVcXfz6/emjmw+G5rV2i2bvsSvTfcBeoOx31+e7Im74= 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=ugtNgE/Zd9B8z0yxPo+rAC4a1rs=; b=GWY/2ddaxYh6azRyoiu+ YNBAc8BYU1LeU5ZOqqrydYbWk0r9TT4B6yRfynQzYYmMGgVgSjphR83chG3EFPk4 uaWCdd2BaVov6nxZrYKen8/j3U2WP4bvQYQqsKAUVhPWnyzKtz2o5J3gz5VJU82E tQZMYCGDOeafivNSDFb/D1w= Received: (qmail 58118 invoked by alias); 14 Mar 2019 02:17:23 -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 58099 invoked by uid 89); 14 Mar 2019 02:17:23 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-11.1 required=5.0 tests=BAYES_00, FREEMAIL_FROM, GIT_PATCH_2, GIT_PATCH_3, KAM_ASCII_DIVIDERS, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.1 spammy=88147, dg-bogus, dgbogus, UD:typeck2.c X-HELO: mail-qt1-f169.google.com Received: from mail-qt1-f169.google.com (HELO mail-qt1-f169.google.com) (209.85.160.169) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 14 Mar 2019 02:17:20 +0000 Received: by mail-qt1-f169.google.com with SMTP id b3so4410930qtj.3 for ; Wed, 13 Mar 2019 19:17:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=to:from:subject:message-id:date:user-agent:mime-version :content-language; bh=wkl5yK5M8mPYef2B6ktc+wwuCYgojeag9DjFXBfWBLE=; b=fQugqytI+PZJabmhoHy18MHWvxvofLQlXfJIIdczKuZzNAHBYgPcD1wqI43rKn4Z3H ZHb0jUlOYYXeHebJouGeChPEOAvUNZdXQxrG2qJ1Qm9ety6Irp2nrAFsiuIIVz+e7vcV lhMLoemJ4pV6rrHPN2n+3jcWl414pApYrl3wHfgEPafEDhEbGFztLCKPDXOmTp+c8MrF CuJcYEBO92iCKdKxqqU9nX/oV4uigdpVMSOwN8RZAzf9S8Fx7yFHA3ZRGZ6ciGyRuKpS WoznEuf96gNEkrIX0TS6eBv7WrMoqdZQ68FuEG7vqY16b13hk8xJDWHIiEmj9Qcehj8a edJA== Received: from [192.168.0.41] (75-166-119-163.hlrn.qwest.net. [75.166.119.163]) by smtp.gmail.com with ESMTPSA id m128sm7788503qkf.53.2019.03.13.19.17.17 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 13 Mar 2019 19:17:17 -0700 (PDT) To: "gcc-patches@gcc.gnu.org" From: Martin Sebor Subject: [PATCH] improve constant string length folding (PR 89688) Message-ID: <78dad3ef-fd7a-381b-6673-ae07f7fcf275@gmail.com> Date: Wed, 13 Mar 2019 20:17:16 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.3.1 MIME-Version: 1.0 X-IsSubscribed: yes PR 89688 points out a bogus warning about an unterminated character array argument to strlen. The root cause is an oversight in the transformation of braced initializer lists to STRING_CSTs where the solution implemented last summer only considers one-dimensional arrays and skips more complex aggregates such as multi-dimensional arrays or structs. The folder (string_constant), on the other hand, assumes that every a constant character array with an initializer is either a properly nul-terminated string (i.e., STRING_CST), or an unterminated array or a single character. If the latter then unless the character value is zero it indicates to its caller that the constant is not a string. As a result, we end up with a warning. To avoid the false positives the attached patch extends the solution to those other kinds of aggregates. Martin PR tree-optimization/89688 - -Wstringop-overflow confused by const 2D array of char gcc/c/ChangeLog: PR tree-optimization/89688 * c-decl.c (finish_decl): Call braced_lists_to_string for more kinds of initializers. gcc/c-family/ChangeLog: PR tree-optimization/89688 * c-common.c (braced_list_to_string): Make static. (braced_lists_to_strings): Define new function. * c-common.h (braced_list_to_string): Remove. (braced_lists_to_strings): Declare. gcc/cp/ChangeLog: PR tree-optimization/89688 * typeck2.c (store_init_value): Call braced_lists_to_string for more kinds of initializers. gcc/testsuite/ChangeLog: PR tree-optimization/89688 * gcc.dg/strlenopt-61.c: New test. * g++.dg/warn/Wstringop-overflow-2.C: New test. Index: gcc/c/c-decl.c =================================================================== --- gcc/c/c-decl.c (revision 269657) +++ gcc/c/c-decl.c (working copy) @@ -5165,11 +5165,10 @@ finish_decl (tree decl, location_t init_loc, tree relayout_decl (decl); } - if (TREE_CODE (type) == ARRAY_TYPE - && TYPE_STRING_FLAG (TREE_TYPE (type)) - && DECL_INITIAL (decl) - && TREE_CODE (DECL_INITIAL (decl)) == CONSTRUCTOR) - DECL_INITIAL (decl) = braced_list_to_string (type, DECL_INITIAL (decl)); + /* Look for braced array initializers for character arrays and + recursively convert them into STRING_CSTs. */ + if (tree init = DECL_INITIAL (decl)) + DECL_INITIAL (decl) = braced_lists_to_strings (type, init); if (VAR_P (decl)) { Index: gcc/c-family/c-common.c =================================================================== --- gcc/c-family/c-common.c (revision 269657) +++ gcc/c-family/c-common.c (working copy) @@ -8814,7 +8814,7 @@ maybe_add_include_fixit (rich_location *richloc, c TYPE into a STRING_CST for convenience and efficiency. Return the converted string on success or the original ctor on failure. */ -tree +static tree braced_list_to_string (tree type, tree ctor) { if (!tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))) @@ -8895,4 +8895,52 @@ braced_list_to_string (tree type, tree ctor) return res; } +/* Attempt to convert a CTOR containing braced array initializer lists + for array TYPE into one containing STRING_CSTs, for convenience and + efficiency. Recurse for arrays of arrays and member initializers. + Return the converted CTOR or STRING_CST on success or the original + CTOR otherwise. */ + +tree +braced_lists_to_strings (tree type, tree ctor) +{ + if (TREE_CODE (ctor) != CONSTRUCTOR) + return ctor; + + tree_code code = TREE_CODE (type); + + tree ttp; + if (code == ARRAY_TYPE) + ttp = TREE_TYPE (type); + else if (code == RECORD_TYPE) + { + ttp = TREE_TYPE (ctor); + if (TREE_CODE (ttp) == ARRAY_TYPE) + { + type = ttp; + ttp = TREE_TYPE (ttp); + } + } + else + return ctor; + + if (TYPE_STRING_FLAG (ttp)) + return braced_list_to_string (type, ctor); + + code = TREE_CODE (ttp); + if (code == ARRAY_TYPE || code == RECORD_TYPE) + { + /* Handle array of arrays or struct member initializers. */ + tree val; + unsigned HOST_WIDE_INT idx; + FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (ctor), idx, val) + { + val = braced_lists_to_strings (ttp, val); + CONSTRUCTOR_ELT (ctor, idx)->value = val; + } + } + + return ctor; +} + #include "gt-c-family-c-common.h" Index: gcc/c-family/c-common.h =================================================================== --- gcc/c-family/c-common.h (revision 269657) +++ gcc/c-family/c-common.h (working copy) @@ -1372,7 +1372,8 @@ extern void maybe_add_include_fixit (rich_location extern void maybe_suggest_missing_token_insertion (rich_location *richloc, enum cpp_ttype token_type, location_t prev_token_loc); -extern tree braced_list_to_string (tree, tree); +extern tree braced_lists_to_strings (tree, tree); + extern bool has_attribute (location_t, tree, tree, tree (*)(tree)); #if CHECKING_P Index: gcc/cp/typeck2.c =================================================================== --- gcc/cp/typeck2.c (revision 269657) +++ gcc/cp/typeck2.c (working copy) @@ -824,10 +824,9 @@ store_init_value (tree decl, tree init, vec