From patchwork Fri Aug 24 21:24:45 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bernd Edlinger X-Patchwork-Id: 962095 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-484400-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=hotmail.de Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="XnkzBpmb"; 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 41xvSX3VR6z9ryt for ; Sat, 25 Aug 2018 07:25:00 +1000 (AEST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:subject:date:message-id:content-type:mime-version; q=dns; s= default; b=IFIGGAwnyRNBfjLy83cBCLQkIPq46s6ZhjfUcYomaSB/c6OWRwL6L msrwCl7kkuRikP+DerOT+PayiBr7IqKw5f8nPMCAfpxk0sMZVDW/HX9sXkYAxs0A n6eFXsG0+3eGaen/HtccbV4aiaraU9FvBtu6l2ZZt+hFCI/TkgEpzY= 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:from :to:subject:date:message-id:content-type:mime-version; s= default; bh=Ugyf4rXUbVDqaUaCJLowGRCd2jg=; b=XnkzBpmbZp1tQ1FT0MYG J9wu9yDH2qDbpQysL33uCOwOrJdw+583emA3C9aQwx2tWApROLz12ticC8445F6g lGpxvJFnXn4FcVm3GOyq8JB8GwU7K0dUCKoYxZIV9NhGM1D6UERfdMQCKa/0prEI bzg0izXXcW9suHRRb7PF0y8= Received: (qmail 20921 invoked by alias); 24 Aug 2018 21:24:53 -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 20902 invoked by uid 89); 24 Aug 2018 21:24:52 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-11.4 required=5.0 tests=AWL, BAYES_00, FREEMAIL_FROM, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_PASS, SPF_PASS autolearn=ham version=3.3.2 spammy=unified, pr86711.c, pr86711c, UD:pr86711.c X-HELO: EUR02-HE1-obe.outbound.protection.outlook.com Received: from mail-oln040092068098.outbound.protection.outlook.com (HELO EUR02-HE1-obe.outbound.protection.outlook.com) (40.92.68.98) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 24 Aug 2018 21:24:49 +0000 Received: from AM5EUR02FT020.eop-EUR02.prod.protection.outlook.com (10.152.8.58) by AM5EUR02HT211.eop-EUR02.prod.protection.outlook.com (10.152.9.75) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.20.1080.9; Fri, 24 Aug 2018 21:24:45 +0000 Received: from AM5PR0701MB2657.eurprd07.prod.outlook.com (10.152.8.55) by AM5EUR02FT020.mail.protection.outlook.com (10.152.8.125) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.20.1080.9 via Frontend Transport; Fri, 24 Aug 2018 21:24:45 +0000 Received: from AM5PR0701MB2657.eurprd07.prod.outlook.com ([fe80::24cf:823c:758c:41b7]) by AM5PR0701MB2657.eurprd07.prod.outlook.com ([fe80::24cf:823c:758c:41b7%7]) with mapi id 15.20.1101.007; Fri, 24 Aug 2018 21:24:45 +0000 From: Bernd Edlinger To: "gcc-patches@gcc.gnu.org" , Richard Biener , Jeff Law , Martin Sebor Subject: [PATCH] Adjust string_constant to new STRING_CST semantics Date: Fri, 24 Aug 2018 21:24:45 +0000 Message-ID: received-spf: None (protection.outlook.com: hotmail.de does not designate permitted sender hosts) authentication-results: spf=none (sender IP is ) smtp.mailfrom=bernd.edlinger@hotmail.de; MIME-Version: 1.0 Hi, I noticed that the latest patches to make STRING_CST semantics for literals and for initializers consistent also fixes PR 86711 and PR 86714 (but not PR 87053). The code change in the string_constant does not change any test case, the new test cases are already fixed once we have the unified STRING_CST semantics. Thus https://gcc.gnu.org/ml/gcc-patches/2018-08/msg01569.html would be the pre-condition for this patch. If the pre-condition patch is applied then my earlier patch to fix PR 86711/86714 is no longer necessary. Likely neither Martin's patch (at least that part about PR 86711/86714). Bootstrapped and reg-tested on x86_64-pc-linux-gnu. Is it OK for trunk? Thanks Bernd. gcc: 2018-08-24 Bernd Edlinger * expr.c (string_constant): Adjust function comment. Remove bogus check for zero termination. testsuite: 2018-08-24 Bernd Edlinger PR middle-end/86711 PR middle-end/86714 * gcc.c-torture/execute/pr86711.c: New test. * gcc.c-torture/execute/pr86714.c: New test. diff -Npur gcc/expr.c gcc/expr.c --- gcc/expr.c 2018-08-22 22:34:01.000000000 +0200 +++ gcc/expr.c 2018-08-24 20:24:21.592913041 +0200 @@ -11304,8 +11304,9 @@ is_aligning_offset (const_tree offset, c if it doesn't. If we return nonzero, set *PTR_OFFSET to the (possibly non-constant) offset in bytes within the string that ARG is accessing. The type of the offset is sizetype. If MEM_SIZE is non-zero the storage - size of the memory is returned. If MEM_SIZE is zero, the string is - only returned when it is properly zero terminated. */ + size of the memory is returned. The returned STRING_CST object is + valid up to TREE_STRING_LENGTH. Bytes between TREE_STRING_LENGTH + and MEM_SIZE are ZERO. MEM_SIZE is at least TREE_STRING_LENGTH. */ tree string_constant (tree arg, tree *ptr_offset, tree *mem_size) @@ -11404,6 +11405,8 @@ string_constant (tree arg, tree *ptr_off *ptr_offset = fold_convert (sizetype, offset); if (mem_size) *mem_size = TYPE_SIZE_UNIT (TREE_TYPE (array)); + gcc_checking_assert (tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (array))) + >= TREE_STRING_LENGTH (array)); return array; } @@ -11446,27 +11449,10 @@ string_constant (tree arg, tree *ptr_off if (!init || TREE_CODE (init) != STRING_CST) return NULL_TREE; - tree array_size = DECL_SIZE_UNIT (array); - if (!array_size || TREE_CODE (array_size) != INTEGER_CST) - return NULL_TREE; - - /* Avoid returning a string that doesn't fit in the array - it is stored in, like - const char a[4] = "abcde"; - but do handle those that fit even if they have excess - initializers, such as in - const char a[4] = "abc\000\000"; - The excess elements contribute to TREE_STRING_LENGTH() - but not to strlen(). */ - unsigned HOST_WIDE_INT charsize - = tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (init)))); - unsigned HOST_WIDE_INT length = TREE_STRING_LENGTH (init); - length = string_length (TREE_STRING_POINTER (init), charsize, - length / charsize); if (mem_size) *mem_size = TYPE_SIZE_UNIT (TREE_TYPE (init)); - else if (compare_tree_int (array_size, length + 1) < 0) - return NULL_TREE; + gcc_checking_assert (tree_to_shwi (TYPE_SIZE_UNIT (TREE_TYPE (init))) + >= TREE_STRING_LENGTH (init)); *ptr_offset = offset; return init; diff -Npur gcc/testsuite/gcc.c-torture/execute/pr86711.c gcc/testsuite/gcc.c-torture/execute/pr86711.c --- gcc/testsuite/gcc.c-torture/execute/pr86711.c 1970-01-01 01:00:00.000000000 +0100 +++ gcc/testsuite/gcc.c-torture/execute/pr86711.c 2018-08-24 20:29:15.960882045 +0200 @@ -0,0 +1,11 @@ +/* PR middle-end/86711 */ + +static const char a[2][4] = { "1234", "5678" }; + +int main () +{ + void *p = __builtin_memchr (a, 0, 5); + + if (p) + __builtin_abort (); +} diff -Npur gcc/testsuite/gcc.c-torture/execute/pr86714.c gcc/testsuite/gcc.c-torture/execute/pr86714.c --- gcc/testsuite/gcc.c-torture/execute/pr86714.c 1970-01-01 01:00:00.000000000 +0100 +++ gcc/testsuite/gcc.c-torture/execute/pr86714.c 2018-08-24 20:29:15.960882045 +0200 @@ -0,0 +1,12 @@ +/* PR middle-end/86714 */ + +const char a[2][3] = { "1234", "xyz" }; +char b[6]; + +int main () +{ + __builtin_memcpy (b, a, 4); + __builtin_memset (b + 4, 'a', 2); + if (__builtin_memcmp (b, "123xaa", 6)) + __builtin_abort (); +}