From patchwork Sun Mar 11 15:22:01 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siddhesh Poyarekar X-Patchwork-Id: 884258 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-474556-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=sourceware.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="qqTDF6LX"; 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 3zzlHH52Ccz9sR0 for ; Mon, 12 Mar 2018 02:22:50 +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:from :to:subject:date:message-id; q=dns; s=default; b=q2ERWhvjTTqEw3y OqCq4vXwYFU4htavexv+Zt2oLlS9kijCuvvwdhYZ6cAvUkjXVRAInIH9BOy8cocz HjxoD4Ljt8mB8+er5zOG7tNdGIlG0ZISpresx8F9Z6RarZsfjAYhMKZlRI5GyEox ffDaKQe3Mpdsbbz6yCO/VzCDktdk= 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; s=default; bh=lVQK0GOyhSoHNn8/7BbSO g3PCNA=; b=qqTDF6LXaO2seXKe40m0Cs22oUK/1SxIULPgTFrHYtdlpjfJvqrQf p8tIAKBWein7XVAYlYmm2aF1cjQH2z/mdj/OgJ9cpuiPBJoFozO6uQNwKrYCRvTs STa7TTBdLQ/z1e6lL5P3IdyV8A9mqzxFHxLGZYYOWQQdff6O8lYKQk= Received: (qmail 49495 invoked by alias); 11 Mar 2018 15:22:43 -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 49485 invoked by uid 89); 11 Mar 2018 15:22:42 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-26.1 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_NEUTRAL autolearn=ham version=3.3.2 spammy=underway X-HELO: homiemail-a56.g.dreamhost.com Received: from sub5.mail.dreamhost.com (HELO homiemail-a56.g.dreamhost.com) (208.113.200.129) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Sun, 11 Mar 2018 15:22:41 +0000 Received: from homiemail-a56.g.dreamhost.com (localhost [127.0.0.1]) by homiemail-a56.g.dreamhost.com (Postfix) with ESMTP id 9FF3E600393D for ; Sun, 11 Mar 2018 08:22:39 -0700 (PDT) Received: from devel.in.reserved-bit.com (unknown [202.189.238.75]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: siddhesh@gotplt.org) by homiemail-a56.g.dreamhost.com (Postfix) with ESMTPSA id DB999600393C for ; Sun, 11 Mar 2018 08:22:38 -0700 (PDT) From: Siddhesh Poyarekar To: gcc-patches@gcc.gnu.org Subject: [PATCH] Fix bogus strncpy source length warning on source bound by constant Date: Sun, 11 Mar 2018 20:52:01 +0530 Message-Id: <20180311152201.9613-1-siddhesh@sourceware.org> Avoid issuing a bogus warning when the source of strncpy is bound by a constant and is known to be less than the size of the destination. Testsuite run is underway (not complete yet, but no new errors so far) and a bootstrap is also underway, I'll report status once they're both done. gcc/ * gcc/tree-ssa-strlen.c (handle_builtin_stxncpy): Check bounds of source length if available. gcc/testsuite/ * gcc.dg/builtin-stringop-chk-10.c: New test case. --- gcc/testsuite/gcc.dg/builtin-stringop-chk-10.c | 17 +++++++++++++++++ gcc/tree-ssa-strlen.c | 24 ++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/builtin-stringop-chk-10.c diff --git a/gcc/testsuite/gcc.dg/builtin-stringop-chk-10.c b/gcc/testsuite/gcc.dg/builtin-stringop-chk-10.c new file mode 100644 index 00000000000..13e4bd2f049 --- /dev/null +++ b/gcc/testsuite/gcc.dg/builtin-stringop-chk-10.c @@ -0,0 +1,17 @@ +/* Bogus -Wstringop-overflow on strncpy when size is based on strlen but is + bound by a constant. + { dg-do compile } + { dg-options "-O2 -Wstringop-overflow" } */ + +char dst[1024]; + +void +f1 (const char *src) +{ + unsigned long limit = 512; + unsigned long len = __builtin_strlen (src); /* { dg-bogus "length computed here" } */ + if (len > limit) + len = limit; + + __builtin_strncpy (dst, src, len); /* { dg-bogus "specified bound depends on the length of the source argument" } */ +} diff --git a/gcc/tree-ssa-strlen.c b/gcc/tree-ssa-strlen.c index 72f6a17cd32..49a31a551f5 100644 --- a/gcc/tree-ssa-strlen.c +++ b/gcc/tree-ssa-strlen.c @@ -2125,6 +2125,30 @@ handle_builtin_stxncpy (built_in_function, gimple_stmt_iterator *gsi) return; } + /* When LEN is MIN_EXPR of strlen and a constant, then the copy is bound by + that constant. If the destination size is also constant then compare with + it to avoid a bogus warning. */ + if (TREE_CODE (len) == SSA_NAME) + { + gimple *def_stmt = SSA_NAME_DEF_STMT (len); + + if (is_gimple_assign (def_stmt) + && gimple_assign_rhs_code (def_stmt) == MIN_EXPR) + { + /* RHS1 is the strlen, so check if RHS2 and DSTSIZE are constant. */ + tree rhs2 = gimple_assign_rhs2 (def_stmt); + tree dstsize = compute_objsize (dst, 1); + + if (TREE_CODE (rhs2) == INTEGER_CST + && TREE_CODE (dstsize) == INTEGER_CST + && int_cst_value (rhs2) < int_cst_value (dstsize)) + { + gimple_set_no_warning (stmt, true); + return; + } + } + } + /* Retrieve the strinfo data for the string S that LEN was computed from as some function F of strlen (S) (i.e., LEN need not be equal to strlen(S)). */