From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 6083 invoked by alias); 30 Aug 2018 00:12:48 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Received: (qmail 6074 invoked by uid 89); 30 Aug 2018 00:12:47 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-10.6 required=5.0 tests=AWL,BAYES_00,FREEMAIL_FROM,GIT_PATCH_2,GIT_PATCH_3,KAM_ASCII_DIVIDERS,RCVD_IN_DNSWL_NONE,SPF_PASS autolearn=ham version=3.3.2 spammy= X-HELO: mail-qt0-f194.google.com Received: from mail-qt0-f194.google.com (HELO mail-qt0-f194.google.com) (209.85.216.194) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 30 Aug 2018 00:12:45 +0000 Received: by mail-qt0-f194.google.com with SMTP id o15-v6so7916345qtk.6 for ; Wed, 29 Aug 2018 17:12:45 -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; bh=xlxZdZI33wkGzk5ekaAFqxmvUJjdGk5LodBr/8wGBKE=; b=WOrDMutSKDJK8RpaWM7DdRRgmBWuFpMn4QYStWMNSH66FBnVw5B8z/UbNXnPA7qjdV 5/KqS+sn+UAXGb209IlrufIUtPTb2sQTrZ7hT+yPyLilJVi1o8/0botRkulGEwjWak4b OJ5hImLyCWLlhsQWtjOxTdL4wOVZfpsJvBzzvYk/nyWrWdt4coU3XFmWBPWUMP3rbpcH tjInCMBDbQYZpDQXapoWRgrLAof/hsjuSxAulumlWumVP5kLnoYO4HMw3RM2CXbGOzBZ Ca+AKar/SJJmYXxegDF03SK3JsfvwyI2g3VoEi60PH/lZF0VlSpHLMozfzNu7tCAzYYb S2TQ== Return-Path: Received: from localhost.localdomain (75-166-100-32.hlrn.qwest.net. [75.166.100.32]) by smtp.gmail.com with ESMTPSA id 80-v6sm3121895qkj.75.2018.08.29.17.12.41 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 29 Aug 2018 17:12:42 -0700 (PDT) To: Gcc Patch List From: Martin Sebor Subject: [PATCH] look harder for MEM_REF operand equality to avoid -Wstringop-truncation (PR 84561) Message-ID: <6c2126fa-32b3-693b-e3da-cf70391710bf@gmail.com> Date: Thu, 30 Aug 2018 00:12:00 -0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="------------964A9A1CF39AEC95C096164B" X-IsSubscribed: yes X-SW-Source: 2018-08/txt/msg01934.txt.bz2 This is a multi-part message in MIME format. --------------964A9A1CF39AEC95C096164B Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Content-length: 632 The attached patch adds code to work harder to determine whether the destination of an assignment involving MEM_REF is the same as the destination of a prior strncpy call. The included test case demonstrates when this situation comes up. During ccp, dstbase and lhsbase returned by get_addr_base_and_unit_offset() end up looking like this: _8 = &pb_3(D)->a; _9 = _8; _1 = _9; strncpy (MEM_REF (&pb_3(D)->a), ...); MEM[(struct S *)_1].a[n_7] = 0; so the loops follow the simple assignments until we get at the ADDR_EXPR assigned to _8 which is the same as the strncpy destination. Tested on x86_64-linux. Martin --------------964A9A1CF39AEC95C096164B Content-Type: text/x-patch; name="gcc-84561.diff" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="gcc-84561.diff" Content-length: 3278 PR tree-optimization/84561 - -Wstringop-truncation with -O2 depends on strncpy's size type gcc/ChangeLog: PR tree-optimization/84561 * tree-ssa-strlen.c (maybe_diag_stxncpy_trunc): Look harder for MEM_REF operand equality. gcc/testsuite/ChangeLog: PR tree-optimization/84561 * gcc.dg/Wstringop-truncation-6.c: New test. Index: gcc/tree-ssa-strlen.c =================================================================== --- gcc/tree-ssa-strlen.c (revision 263965) +++ gcc/tree-ssa-strlen.c (working copy) @@ -1978,11 +1978,43 @@ maybe_diag_stxncpy_trunc (gimple_stmt_iterator gsi poly_int64 lhsoff; tree lhsbase = get_addr_base_and_unit_offset (lhs, &lhsoff); - if (lhsbase - && dstbase - && known_eq (dstoff, lhsoff) - && operand_equal_p (dstbase, lhsbase, 0)) + bool eqloff = lhsbase && dstbase && known_eq (dstoff, lhsoff); + + if (eqloff && operand_equal_p (dstbase, lhsbase, 0)) return false; + + if (eqloff + && TREE_CODE (dstbase) == MEM_REF + && TREE_CODE (lhsbase) == MEM_REF + && tree_int_cst_equal (TREE_OPERAND (dstbase, 1), + TREE_OPERAND (lhsbase, 1))) + { + /* For MEM_REFs with the same offset follow the chain of + SSA_NAME assignments to their source and compare those + for equality. */ + dstbase = TREE_OPERAND (dstbase, 0); + while (TREE_CODE (dstbase) == SSA_NAME) + { + gimple *def = SSA_NAME_DEF_STMT (dstbase); + if (gimple_assign_single_p (def)) + dstbase = gimple_assign_rhs1 (def); + else + break; + } + + lhsbase = TREE_OPERAND (lhsbase, 0); + while (TREE_CODE (lhsbase) == SSA_NAME) + { + gimple *def = SSA_NAME_DEF_STMT (lhsbase); + if (gimple_assign_single_p (def)) + lhsbase = gimple_assign_rhs1 (def); + else + break; + } + + if (operand_equal_p (dstbase, lhsbase, 0)) + return false; + } } int prec = TYPE_PRECISION (TREE_TYPE (cnt)); Index: gcc/testsuite/gcc.dg/Wstringop-truncation-6.c =================================================================== --- gcc/testsuite/gcc.dg/Wstringop-truncation-6.c (nonexistent) +++ gcc/testsuite/gcc.dg/Wstringop-truncation-6.c (working copy) @@ -0,0 +1,59 @@ +/* PR tree-optimization/84561 - -Wstringop-truncation with -O2 depends + on strncpy's size type + { dg-do compile } + { dg-options "-O2 -Wall" } */ + +typedef __SIZE_TYPE__ size_t; + +enum { N = 3 }; + +struct S +{ + char a[N + 1]; +}; + +void set (struct S *ps, const char* s, size_t n) +{ + if (n > N) + n = N; + + __builtin_strncpy (ps->a, s, n); /* { dg-bogus "\\\[-Wstringop-truncation]" } */ + ps->a[n] = 0; +} + +struct A +{ + struct S str; +}; + +void setStringSize_t (struct A *pa, const char *s, size_t n) +{ + set (&pa->str, s, n); +} + +void setStringUnsignedInt (struct A *pa, const char* s, unsigned int n) +{ + set (&pa->str, s, n); +} + +struct B +{ + struct A a; +}; + +struct A* getA (struct B *pb) +{ + return &pb->a; +} + +void f (struct A *pa) +{ + setStringUnsignedInt (pa, "123", N); // No warning here. + setStringSize_t (pa, "123", N); // No warning here. +} + +void g (struct B *pb) +{ + setStringUnsignedInt (getA (pb), "123", N); // Unexpected warning here. + setStringSize_t (getA (pb), "123", N); // No warning here. +} --------------964A9A1CF39AEC95C096164B--