From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 100548 invoked by alias); 18 Nov 2019 18:22:18 -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 100539 invoked by uid 89); 18 Nov 2019 18:22:18 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-7.2 required=5.0 tests=AWL,BAYES_00,FREEMAIL_FROM,KAM_SHORT,RCVD_IN_DNSWL_NONE,SPF_PASS autolearn=ham version=3.3.1 spammy= X-HELO: mail-pg1-f195.google.com Received: from mail-pg1-f195.google.com (HELO mail-pg1-f195.google.com) (209.85.215.195) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 18 Nov 2019 18:22:17 +0000 Received: by mail-pg1-f195.google.com with SMTP id b10so1494018pgd.4 for ; Mon, 18 Nov 2019 10:22:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:from:to:references:message-id:date:user-agent:mime-version :in-reply-to:content-language:content-transfer-encoding; bh=nQAvAddM7+BMLgRqQxCuSzGpa0C2BXV8AvSQt9kBnBk=; b=CF/7A5DQuHTNEwbyDxPHHPXX+BgaqMOpiH82baPzlqiwEGFyeV395whGeXCxaPvf4e FBw3XcYwg+iA9C6y5Ypf2MYeEwAZCKVVZzqsWKz2sgEAbFQB6c27QcmBsJD/ftClpP03 WIB391p46Mg9FXsuj/rB9MlV8HfgdSKRt7SqrtLTtyGVbRJxq0pL07ZeNeCZ7ft0nFXj UdGyKftTgvc4SUYTK5GoKDQOoLQzEvmRNQ3HYVWetkesv5AiHwJsSEz22vj/+WZZRYx6 aPIg4n9J1+uUeeCwXLI/8macr9Ljb2abAfXXIPmitprloBjqV9FWIf1vxF2Ny4WKwdJ/ h/tg== Return-Path: Received: from [192.168.0.41] (97-118-98-145.hlrn.qwest.net. [97.118.98.145]) by smtp.gmail.com with ESMTPSA id a145sm22886904pfa.7.2019.11.18.10.22.13 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 18 Nov 2019 10:22:14 -0800 (PST) Subject: [PING][PATCH] extend -Wstringop-overflow to allocated objects (PR 91582) From: Martin Sebor To: gcc-patches References: <5b68c166-e94b-2660-04f3-e3fafe69112c@gmail.com> Message-ID: Date: Mon, 18 Nov 2019 18:23:00 -0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 MIME-Version: 1.0 In-Reply-To: <5b68c166-e94b-2660-04f3-e3fafe69112c@gmail.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit X-IsSubscribed: yes X-SW-Source: 2019-11/txt/msg01738.txt.bz2 Ping: https://gcc.gnu.org/ml/gcc-patches/2019-11/msg00652.html On 11/8/19 3:11 PM, Martin Sebor wrote: > Unless it's used with _FORTIFY_SOURCE, -Wstringop-overflow > doesn't consider out-of-bounds accesses to objects allocated > by alloca, malloc, other functions declared with attribute > alloc_size, or even VLAs with variable bounds.  This was > a known limitation of the checks (done just before expansion) > relying on the the object size pass when they were introduced > in GCC 7. > > But since its introduction in GCC 7, the warning has evolved > beyond some of the limitations of the object size pass.  Unlike > it, the warning considers non-constant offsets and stores with > non-constant sizes.  Attached is a simple enhancement that > (finally) adds the ability to also detect overflow in allocated > objects to the warning. > > With the patch GCC detects the overflow in code like this: > >   char* f (void) >   { >     char s[] = "12345"; >     char *p = malloc (strlen (s)); >     strcpy (p, s);   // warning here >     return p; >   } > > but not (yet) in something like this: > >   char* g (const char *s) >   { >     char *p = malloc (strlen (s)); >     strcpy (p, s);   // no warning (yet) >     return p; >   } > > and quite a few other examples.  Doing better requires extending > the strlen pass.  I'm working on this extension and expect to > submit a patch before stage 1 ends. > > Martin > > PS I was originally planning to do all the allocation checking > in the strlen pass but it occurred to me that by also enhancing > the compute_objsize function, all warnings that use it will > benefit.  Besides -Wstringop-overflow this includes a subset > of -Warray-bounds, -Wformat-overflow, and -Wrestrict.  It's > nice when a small enhancement has such a broad positive effect.