From patchwork Mon Jul 30 11:51:25 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bernd Edlinger X-Patchwork-Id: 950850 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-482636-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="vn7tmZ8G"; 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 41fHwX04WTz9ryl for ; Mon, 30 Jul 2018 21:51:39 +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:cc:subject:date:message-id:content-type:mime-version; q=dns; s=default; b=nV+Q/iRvMoKjxbpIEjqrYKmGRVciZhk7JlJDaTGIw+JwC0Evrk Z5XNPMfOq2P6gXUQcVDL2hLXMkVEkym+z1O5+csve5S1tzF8MuTowgTdWzSDAYyl F0oznB0cz8T3OkMIR1PgxFLpGDetEddGHqTgjvcpU2I4RxWRLx7QQOcGs= 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:cc:subject:date:message-id:content-type:mime-version; s= default; bh=wAwoh25+B9jwIVC/0l+QKfSQnbo=; b=vn7tmZ8GqixN9B+svfvY uYxFie/0p70nWtUwuPwRxlmvEIgBQnx4jo6EOQd9+EviWqlBqLZe7J0ty1vkXQw2 dJNeQwUmjkREfs2TAxJjnD3PKFrJhL28Bhk3Nr2h6H/hPdYVhfmqHe6Yh9IwpmoX OTuq5nXnAr/aQyDgJuPcpKA= Received: (qmail 13133 invoked by alias); 30 Jul 2018 11:51:32 -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 13122 invoked by uid 89); 30 Jul 2018 11:51:31 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-11.6 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=integral_type_p, INTEGRAL_TYPE_P, HCC:D*de, H*c:HHH X-HELO: EUR02-VE1-obe.outbound.protection.outlook.com Received: from mail-oln040092069048.outbound.protection.outlook.com (HELO EUR02-VE1-obe.outbound.protection.outlook.com) (40.92.69.48) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 30 Jul 2018 11:51:29 +0000 Received: from HE1EUR02FT033.eop-EUR02.prod.protection.outlook.com (10.152.10.53) by HE1EUR02HT226.eop-EUR02.prod.protection.outlook.com (10.152.11.132) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P384) id 15.20.1038.3; Mon, 30 Jul 2018 11:51:25 +0000 Received: from AM5PR0701MB2657.eurprd07.prod.outlook.com (10.152.10.54) by HE1EUR02FT033.mail.protection.outlook.com (10.152.10.152) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.20.1038.3 via Frontend Transport; Mon, 30 Jul 2018 11:51:25 +0000 Received: from AM5PR0701MB2657.eurprd07.prod.outlook.com ([fe80::b1fe:1b4f:88c3:5243]) by AM5PR0701MB2657.eurprd07.prod.outlook.com ([fe80::b1fe:1b4f:88c3:5243%9]) with mapi id 15.20.1017.010; Mon, 30 Jul 2018 11:51:25 +0000 From: Bernd Edlinger To: GCC Patches CC: Richard Biener , Jakub Jelinek , "Jeff Law" , "Joseph S. Myers" , "Martin Sebor" Subject: [PATCH] Fix the damage done by my other patch from yesterday to strlenopt-49.c Date: Mon, 30 Jul 2018 11:51:25 +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, this is how I would like to handle the over length strings issue in the C FE. If the string constant is exactly the right length and ends in one explicit NUL character, shorten it by one character. I thought Martin would be working on it, but as this is a really simple fix, I would dare to send it to gcc-patches anyway, hope you don't mind... The patch is relative to the other patch here: https://gcc.gnu.org/ml/gcc-patches/2018-07/msg01800.html Bootstrapped and reg-tested on x86_64-pc-linux-gnu. Is it OK for trunk? Thanks Bernd. gcc/c: 2018-07-30 Bernd Edlinger * c-typeck.c (digest_init): Fix overlength strings. testsuite: 2018-07-30 Bernd Edlinger * gcc.dg/strlenopt-49.c: Adjust test expectations. diff -pur gcc/c/c-typeck.c gcc/c/c-typeck.c --- gcc/c/c-typeck.c 2018-06-20 18:35:15.000000000 +0200 +++ gcc/c/c-typeck.c 2018-07-30 12:17:34.175481372 +0200 @@ -7435,29 +7435,38 @@ digest_init (location_t init_loc, tree t } } - TREE_TYPE (inside_init) = type; if (TYPE_DOMAIN (type) != NULL_TREE && TYPE_SIZE (type) != NULL_TREE && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST) { unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (inside_init); + unsigned unit = TYPE_PRECISION (typ1) / BITS_PER_UNIT; /* Subtract the size of a single (possibly wide) character because it's ok to ignore the terminating null char that is counted in the length of the constant. */ - if (compare_tree_int (TYPE_SIZE_UNIT (type), - (len - (TYPE_PRECISION (typ1) - / BITS_PER_UNIT))) < 0) + if (compare_tree_int (TYPE_SIZE_UNIT (type), len - unit) < 0) pedwarn_init (init_loc, 0, ("initializer-string for array of chars " "is too long")); - else if (warn_cxx_compat - && compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0) - warning_at (init_loc, OPT_Wc___compat, - ("initializer-string for array chars " - "is too long for C++")); + else if (compare_tree_int (TYPE_SIZE_UNIT (type), len) < 0) + { + if (warn_cxx_compat) + warning_at (init_loc, OPT_Wc___compat, + ("initializer-string for array chars " + "is too long for C++")); + if (len >= 2 * unit) + { + const char *p = TREE_STRING_POINTER (inside_init); + + len -= unit; + if (memcmp (p + len - unit, "\0\0\0\0", unit) == 0) + inside_init = build_string (len, p); + } + } } + TREE_TYPE (inside_init) = type; return inside_init; } else if (INTEGRAL_TYPE_P (typ1)) diff -pur gcc/testsuite/gcc.dg/strlenopt-49.c gcc/testsuite/gcc.dg/strlenopt-49.c --- gcc/testsuite/gcc.dg/strlenopt-49.c 2018-07-30 13:02:34.735478726 +0200 +++ gcc/testsuite/gcc.dg/strlenopt-49.c 2018-07-30 13:08:21.074859303 +0200 @@ -11,9 +11,6 @@ const char a3[3] = "12\0"; const char a8[8] = "1234567\0"; const char a9[9] = "12345678\0"; -const char ax[9] = "12345678\0\0\0\0"; /* { dg-warning "initializer-string for array of chars is too long" } */ -const char ay[9] = "\00012345678\0\0\0\0"; /* { dg-warning "initializer-string for array of chars is too long" } */ - int len1 (void) { @@ -27,27 +24,13 @@ int len (void) return len; } -int lenx (void) -{ - size_t lenx = strlen (ax); - return lenx; -} - -int leny (void) -{ - size_t leny = strlen (ay); - return leny; -} - int cmp88 (void) { int cmp88 = memcmp (a8, "1234567\0", sizeof a8); return cmp88; } -/* { dg-final { scan-tree-dump-times "strlen" 0 "gimple" { xfail *-*-* } } } - { dg-final { scan-tree-dump-times "len0 = 0;" 1 "gimple" { xfail *-*-* } } } - { dg-final { scan-tree-dump-times "len = 18;" 1 "gimple" { xfail *-*-* } } } - { dg-final { scan-tree-dump-times "lenx = 8;" 1 "gimple" { xfail *-*-* } } } - { dg-final { scan-tree-dump-times "leny = 0;" 1 "gimple" { xfail *-*-* } } } - { dg-final { scan-tree-dump-times "cmp88 = 0;" 1 "gimple" { xfail *-*-* } } } */ +/* { dg-final { scan-tree-dump-times "strlen" 0 "gimple" } } + { dg-final { scan-tree-dump-times "len0 = 0;" 1 "gimple" } } + { dg-final { scan-tree-dump-times "len = 18;" 1 "gimple" } } + { dg-final { scan-tree-dump-times "cmp88 = 0;" 1 "gimple" } } */