From patchwork Fri Apr 13 16:49:16 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Sebor X-Patchwork-Id: 898023 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-476348-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="TG6jUv0y"; 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 40N3fD0lh7z9s0W for ; Sat, 14 Apr 2018 02:49: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:to :from:subject:message-id:date:mime-version:content-type; q=dns; s=default; b=Bz3a5rj0yzEf2nVqEqB5505FIACDm5NTFWi5ElBFdZJfs+YFsP XRAU00+9JjXz5QzQgGG8J7x9ZvrDsc4vzKQfRQKzoNLu31uPwp9qdBTaoTadoRiL LDTNd7+DAfUnAzt0GCkLQa9dPbHv0XjhoXuZztjHkAzSpWAGafWIEBItI= 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:to :from:subject:message-id:date:mime-version:content-type; s= default; bh=I0if9DlewkBpXuJ543mRgUmUwdM=; b=TG6jUv0yqUMrPDeSDA3e rhOQgV/OzLI2WkQQUmShky4jrXjpVEVQiv9NvMxfSHhGFbY11Httkz/dg+kmyZtk 0dtp5Hbasx9Al7HZ1g2YqLHyN37TxRaaW2Yirad7Tz9srgEinTruTD6QRPPOYnR2 L9FYeCBd/j6ruoV3ZsOyh4M= Received: (qmail 79402 invoked by alias); 13 Apr 2018 16:49: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 79365 invoked by uid 89); 13 Apr 2018 16:49:23 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-10.7 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=SRC X-HELO: mail-oi0-f42.google.com Received: from mail-oi0-f42.google.com (HELO mail-oi0-f42.google.com) (209.85.218.42) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 13 Apr 2018 16:49:21 +0000 Received: by mail-oi0-f42.google.com with SMTP id 71-v6so8930851oie.12 for ; Fri, 13 Apr 2018 09:49:21 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:to:from:subject:message-id:date:user-agent :mime-version; bh=kfr2+8OHEhE1MCKeFp765SjuwhfIoisss4veNkjHSLc=; b=hq4rVOZI/zYXhN8ttQdAMc2O2iQozS7EySWV9fRG6vIMAKAAwkcbbN68tn0X32LW6q 12V+xMt4ib4wh8H6muL5zrZh8/CHb4b0mdiZwhTT3aQzH4nvX9prJ2gQmBI7Xye2BZNc M1931ibhryh+hb80iDh/ORfVsFUyYNIu2Q6OuZ6kmpGLlhkkpIXxRdwnLc9AqAZzdGMz peEEysmXzr6OI+DguX8QLpB9XqWIa7E21FCXlJk9CVM6AOUqhvdppV5JR7e0ayP/iH6x m7WYpfQ6GWJ8oj6+iYdd0lc4cFJii5Qk/JQU7eVpvuq0AOgz0KqjHFlP6hOdO1GBRiqn PC2Q== X-Gm-Message-State: ALQs6tBDVy5gnh+GrdazMnp6BrDin3bXIb8XQFGq28p04xkQIu31Pu0G YZbDGaPk6BScVyXsUmcywNQiPQ== X-Google-Smtp-Source: AIpwx4+wfitrlbrpdeP7xar6VYnJL+2LJgWTCCWHHOecwgRBQkHfvkw0y2MwUwmoeeGTFkLiDTb7fg== X-Received: by 2002:aca:3c04:: with SMTP id j4-v6mr8163989oia.54.1523638159295; Fri, 13 Apr 2018 09:49:19 -0700 (PDT) Received: from localhost.localdomain (174-16-100-37.hlrn.qwest.net. [174.16.100.37]) by smtp.gmail.com with ESMTPSA id 43-v6sm3990114otz.32.2018.04.13.09.49.17 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 13 Apr 2018 09:49:18 -0700 (PDT) To: Gcc Patch List From: Martin Sebor Subject: [PATCH] avoid -Wrestrict for null pointers (PR 85365) Message-ID: <4e1d58ce-92a7-1ed1-31be-8b8fcbba9141@gmail.com> Date: Fri, 13 Apr 2018 10:49:16 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0 MIME-Version: 1.0 X-IsSubscribed: yes PR 85365 is another example of a false positive caused by the interaction between the instrumentation inserted by sanitizers, jump threading, and a middle-end warning. In this case, the warning is easy to avoid by suppressing -Wrestrict for null pointers. Although undefined, since they do no point to an object, strictly speaking they do not indicate an overlap, and so issuing a -Wrestrict warning is not quite appropriate anyway. Testing in progress. Martin PR c/85365 - -Wrestrict false positives with -fsanitize=undefined gcc/ChangeLog: PR c/85365 * gimple-fold.c (gimple_fold_builtin_strcpy): Suppress -Wrestrict for null pointers. (gimple_fold_builtin_stxcpy_chk): Same. * gimple-ssa-warn-restrict.c (check_bounds_or_overlap): Same. gcc/testsuite/ChangeLog: PR c/85365 * gcc.dg/Wrestrict-15.c: New test. Index: gcc/gimple-fold.c =================================================================== --- gcc/gimple-fold.c (revision 259298) +++ gcc/gimple-fold.c (working copy) @@ -1612,7 +1612,11 @@ gimple_fold_builtin_strcpy (gimple_stmt_iterator * /* If SRC and DEST are the same (and not volatile), return DEST. */ if (operand_equal_p (src, dest, 0)) { - if (!gimple_no_warning_p (stmt)) + /* Issue -Wrestrict unless the pointers are null (those do + not point to objects and so do not indicate an overlap; + such calls could be the result of sanitization and jump + threading). */ + if (!integer_zerop (dest) && !gimple_no_warning_p (stmt)) { tree func = gimple_call_fndecl (stmt); @@ -2593,7 +2597,11 @@ gimple_fold_builtin_stxcpy_chk (gimple_stmt_iterat /* If SRC and DEST are the same (and not volatile), return DEST. */ if (fcode == BUILT_IN_STRCPY_CHK && operand_equal_p (src, dest, 0)) { - if (!gimple_no_warning_p (stmt)) + /* Issue -Wrestrict unless the pointers are null (those do + not point to objects and so do not indicate an overlap; + such calls could be the result of sanitization and jump + threading). */ + if (!integer_zerop (dest) && !gimple_no_warning_p (stmt)) { tree func = gimple_call_fndecl (stmt); Index: gcc/gimple-ssa-warn-restrict.c =================================================================== --- gcc/gimple-ssa-warn-restrict.c (revision 259298) +++ gcc/gimple-ssa-warn-restrict.c (working copy) @@ -1880,11 +1880,20 @@ check_bounds_or_overlap (gcall *call, tree dst, tr if (operand_equal_p (dst, src, 0)) { - warning_at (loc, OPT_Wrestrict, - "%G%qD source argument is the same as destination", - call, func); - gimple_set_no_warning (call, true); - return false; + /* Issue -Wrestrict unless the pointers are null (those do + not point to objects and so do not indicate an overlap; + such calls could be the result of sanitization and jump + threading). */ + if (!integer_zerop (dst) && !gimple_no_warning_p (call)) + { + warning_at (loc, OPT_Wrestrict, + "%G%qD source argument is the same as destination", + call, func); + gimple_set_no_warning (call, true); + return false; + } + + return true; } /* Return false when overlap has been detected. */ Index: gcc/testsuite/gcc.dg/Wrestrict-15.c =================================================================== --- gcc/testsuite/gcc.dg/Wrestrict-15.c (nonexistent) +++ gcc/testsuite/gcc.dg/Wrestrict-15.c (working copy) @@ -0,0 +1,38 @@ +/* PR 85365 - -Wrestrict false positives with -fsanitize=undefined + { dg-do compile } + { dg-options "-O2 -Wrestrict -fsanitize=undefined" } */ + +typedef __SIZE_TYPE__ size_t; + +char *strcpy (char *, const char *); +char *strcat (char *, const char *); + +size_t strlen (char *); + +extern char a[], b[], d[]; + +size_t t1 (char *g, int i) +{ + /* The following exercises the handling in gimple-fold.c. */ + strcpy (g + 4, i ? b : a); /* { dg-bogus "\\\[-Wrestrict]" } */ + return strlen (g + 4); +} + +void t2 (char *g, int i) +{ + strcpy (g + 4, i ? b : a); /* { dg-bogus "\\\[-Wrestrict]" } */ + strcat (g + 4, d); +} + +void t3 (char *g, int i) +{ + /* The following exercises the handling in gimple-ssa-warn-restrict.c. */ + strcat (g + 4, i ? b : a); /* { dg-bogus "\\\[-Wrestrict]" } */ + strcat (g + 4, d); +} + +void t4 (char *p, char *q) +{ + strcpy (p, q); /* { dg-bogus "\\\[-Wrestrict]" } */ + strcat (p, q + 32); +}