From patchwork Wed Oct 3 17:23:34 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeff Law X-Patchwork-Id: 978463 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-486892-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="T1KaDjd3"; 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 42QNCl6hvhz9s9m for ; Thu, 4 Oct 2018 03:23:46 +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:message-id:date:mime-version:content-type; q=dns; s= default; b=fogXeNuwEkEz1w3UCoOL0ycXBwfTj9kHuLQF7ouf7agKQbFRZJ7G3 vM/2EG5TfedznkA6pKJ4XIjLvQdlFkFt1wX3CngWRfqfBEc2YhUIrkDy4Edmvw3G 1JLY8rjyJa+Ew/XCSNv0Wtt4zFP9Xutir6c5zdpbH7q76oZ6laUlKY= 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:message-id:date:mime-version:content-type; s= default; bh=opVRMFNLWs017cmGM766i0dQUnA=; b=T1KaDjd3+LsJy+4BKGJN g91wbO3Oegnu0E/lDgkz3i5sQyUsxe0q2fa5pZw8isjk0nT3W3dGh24rjKp/RQLK P1MPymeiIvJf1tESv93JvAfTt+3pZsfsAEWChousKSQ4w2Ad5KjrT+TlUenB+hMb n5xRdTKqMJHZtrMX9U1mtF0= Received: (qmail 92036 invoked by alias); 3 Oct 2018 17:23:39 -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 92025 invoked by uid 89); 3 Oct 2018 17:23:38 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-24.9 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, LIKELY_SPAM_BODY, SPF_HELO_PASS autolearn=ham version=3.3.2 spammy=Minor, termination, c_strlen, H*MI:1b48 X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 03 Oct 2018 17:23:37 +0000 Received: from smtp.corp.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.26]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 477083082A2F for ; Wed, 3 Oct 2018 17:23:36 +0000 (UTC) Received: from localhost.localdomain (ovpn-112-4.rdu2.redhat.com [10.10.112.4]) by smtp.corp.redhat.com (Postfix) with ESMTP id A3508309136D for ; Wed, 3 Oct 2018 17:23:35 +0000 (UTC) From: Jeff Law Openpgp: preference=signencrypt To: gcc-patches Subject: [committed] Minor fixes to get_range_strlen and strlen folding Message-ID: <73f2ec79-1b48-610a-f99f-046d84d839e9@redhat.com> Date: Wed, 3 Oct 2018 11:23:34 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.0 MIME-Version: 1.0 X-IsSubscribed: yes So this is the infrastructure changes necessary to enable Martin's patch to detect strings without NUL termination passed to the sprintf routines. There's three tightly related changes. First in get_range_strlen, we verify that c_strlen found an unterminated string before setting *nonstr. Second, also in get_range_strlen, in the event we encounter an unterminated string, we still want to bubble up the min/max lengths. The sprintf code will check those against the directive's specified length. If the length specified by the sprintf directive ensures we won't walk off the end of the string, then the warning is suppressed. Bubbling those lengths up caused heartburn for the strlen folding bits which blindly looked at the returned min/max without first verifying that the string was properly terminated. So the 3rd hunk disables strlen folding when get_range_strlen indicates the string was not properly terminated. Bootstrapped and regression tested both in isolation and with Martin's patch to warn for unterminated strings used in sprintf calls. Installing on the trunk. I'll be installing Martin's patch momentarily as well. Jeff commit 89d3e5cf174b6555ba76eb4bef49555fe00fc5b6 Author: Jeff Law Date: Wed Oct 3 12:02:12 2018 -0400 * gimple-fold.c (get_range_strlen): Only set *nonstr when an unterminated string is discovered. Bubble up range even for unterminated strings. (gimple_fold_builtin_strlen): Do not fold if get_range_strlen indicates the string was not terminated via NONSTR. diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 6acb077b357..aa10aa07269 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,4 +1,12 @@ -2018-10-3 Aldy Hernandez +2018-10-02 Jeff Law + + * gimple-fold.c (get_range_strlen): Only set *nonstr when + an unterminated string is discovered. Bubble up range + even for unterminated strings. + (gimple_fold_builtin_strlen): Do not fold if get_range_strlen + indicates the string was not terminated via NONSTR. + +2018-10-03 Aldy Hernandez * tree-vrp.c (extract_range_from_unary_expr): Special case all pointer conversions. diff --git a/gcc/gimple-fold.c b/gcc/gimple-fold.c index fa1fc60876c..fe6bc08bdd9 100644 --- a/gcc/gimple-fold.c +++ b/gcc/gimple-fold.c @@ -1344,8 +1344,13 @@ get_range_strlen (tree arg, tree length[2], bitmap *visited, int type, /* If we potentially had a non-terminated string, then bubble that information up to the caller. */ - if (!val) - *nonstr = data.decl; + if (!val && data.decl) + { + *nonstr = data.decl; + *minlen = data.len; + *maxlen = data.len; + return type == 0 ? false : true; + } } if (!val && fuzzy) @@ -3596,6 +3601,7 @@ gimple_fold_builtin_strlen (gimple_stmt_iterator *gsi) tree nonstr; tree lenrange[2]; if (!get_range_strlen (arg, lenrange, 1, true, &nonstr) + && !nonstr && lenrange[0] && TREE_CODE (lenrange[0]) == INTEGER_CST && lenrange[1] && TREE_CODE (lenrange[1]) == INTEGER_CST) {