From patchwork Thu Nov 17 18:14:26 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Sebor X-Patchwork-Id: 82785 Delivered-To: patch@linaro.org Received: by 10.182.1.168 with SMTP id 8csp1039816obn; Thu, 17 Nov 2016 10:14:56 -0800 (PST) X-Received: by 10.129.113.84 with SMTP id m81mr4606151ywc.223.1479406496272; Thu, 17 Nov 2016 10:14:56 -0800 (PST) Return-Path: Received: from sourceware.org (server1.sourceware.org. [209.132.180.131]) by mx.google.com with ESMTPS id z3si917993ywb.486.2016.11.17.10.14.55 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 17 Nov 2016 10:14:56 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-return-441852-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) client-ip=209.132.180.131; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org; spf=pass (google.com: domain of gcc-patches-return-441852-patch=linaro.org@gcc.gnu.org designates 209.132.180.131 as permitted sender) smtp.mailfrom=gcc-patches-return-441852-patch=linaro.org@gcc.gnu.org; dmarc=fail (p=NONE dis=NONE) header.from=gmail.com 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=OPM4KEOX47Ke7U8qmWPnCMNlD8JfeZ0fOS/KpN+r1v4O+dhOhf OB5qlNykEm2FGth6OZRZjvIW34x3gPVDL//FHkybsdJC2Y6Nqv8HOjNuGRCboeZ7 SYMQMSpaDbnv4yxLwUS94yUvykk0y6e9mND+hZXSwYwIrgxSlPmzbjomE= 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=WFQ25v5b2YD3HIj71Pk2oh0Y9cg=; b=rZWm42d1O80/rNlvcFgi S+DFC0UWQ3aeSJWqymvBr9BwowTl5c4PNzN5buuU/Vl8Pgjd8labMCqz+DPRnqKW ZBmawIz9carBuqPrqd7l5HhqveA6R0gI5EaMu1l01pQT/B0hq26Fz/GwAMbVHVWw GCVd/XTOUX+3/ymNpYEYB40= Received: (qmail 4990 invoked by alias); 17 Nov 2016 18:14:42 -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 4980 invoked by uid 89); 17 Nov 2016 18:14:41 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-0.2 required=5.0 tests=AWL, BAYES_00, FREEMAIL_FROM, LIKELY_SPAM_BODY, RCVD_IN_DNSWL_LOW, RCVD_IN_SORBS_SPAM, SPF_PASS autolearn=no version=3.3.2 spammy=Walloc-zero, walloczero, Walloczero, walloc-zero X-HELO: mail-qk0-f174.google.com Received: from mail-qk0-f174.google.com (HELO mail-qk0-f174.google.com) (209.85.220.174) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 17 Nov 2016 18:14:31 +0000 Received: by mail-qk0-f174.google.com with SMTP id n204so232714773qke.2 for ; Thu, 17 Nov 2016 10:14:31 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:to:from:subject:message-id:date:user-agent :mime-version; bh=L1pMc6UQAN32+xXxWqoEbLbVOl06GBKLWv9hGASH6DU=; b=Ilo2s620sUvrobjJGPo0bpmHcjS9v9yHxMDfA7oblyaMlGzT2zBF3Bk+GH1s3+z5I/ ppltatS2JwtsQIbvCGUBUW+8YGceu2DEnLHfykqhe+FsqV9BbqCsf1ZS9W7rohN4kQf2 qSJjt5DfLa+saW7+u4ZOHMaUMa9V8rN1Vw4w5sTlIP9isfjkUsX2XQTyewrjsiabcZXM tFOUwPEVYtDUlA8WKL5lRqq2nNlzgIurvpOe/vDQgi0bBQNAv1+p4igm92H9hR01U88V MVKY2hmtAgj5KGf7hrs8GeQPo6NloTRxJESPU/Yq2g5PnjnBi9uFmbeMB+Ob+iKN4sHH 44yw== X-Gm-Message-State: AKaTC00oqumWQA6AlSfjzCaSOesexlA0Dg+MdYop4H8M285TMEW7RJAWszj+fGu1grLvCw== X-Received: by 10.55.111.134 with SMTP id k128mr4658329qkc.253.1479406469534; Thu, 17 Nov 2016 10:14:29 -0800 (PST) Received: from [192.168.0.26] (75-166-206-79.hlrn.qwest.net. [75.166.206.79]) by smtp.gmail.com with ESMTPSA id b16sm2027376qkg.41.2016.11.17.10.14.28 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 17 Nov 2016 10:14:29 -0800 (PST) To: Gcc Patch List From: Martin Sebor Subject: [PATCH] avoid calling alloca(0) Message-ID: <48395050-0eab-1253-78be-daa406685ad5@gmail.com> Date: Thu, 17 Nov 2016 11:14:26 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.3.0 MIME-Version: 1.0 X-IsSubscribed: yes Testing my patch to warn on overflow in calls to allocation functions (bugs 77531 and 78284): https://gcc.gnu.org/ml/gcc-patches/2016-11/msg01672.html exposed a number of instances of alloca(0) calls in GCC sources. Besides this patch which enables the new warnings with -Wextra (the patch is still in the review queue) the same code also triggers a -Walloca-larger-than warning (which has to be explicitly enabled). It doesn't look to me like these calls are bugs per se but in the interest of eating our own dog food so to speak and being able to compile GCC with -Walloca-larger-than (and not to have to disable the yet-to-be committed -Walloc-zero warning) the attached patch makes sure that the alloca calls that are subject to the warnings always request at least 1 byte. Thanks Martin PS Alloca(0) is not necessarily undefined but the pointer the call returns need not be distinct from other pointers returned from other such calls in the same function (and there are compilers that do not make it distinct). Relying on the pointer being distinct could be a source of subtle bugs, hence the warning. gcc/fortran/ChangeLog: * interface.c (compare_actual_formal): Avoid calling alloca with an argument of zero. * symbol.c (do_traverse_symtree): Same. * trans-intrinsic.c (gfc_conv_intrinsic_ishftc): Same. gcc/ChangeLog: * reg-stack.c (subst_asm_stack_regs): Same. * tree-ssa-threadedge.c (record_temporary_equivalences_from_stmts_at_dest): Same. diff --git a/gcc/fortran/interface.c b/gcc/fortran/interface.c index a0cb0bb..2da51d7 100644 --- a/gcc/fortran/interface.c +++ b/gcc/fortran/interface.c @@ -2821,7 +2821,8 @@ compare_actual_formal (gfc_actual_arglist **ap, gfc_formal_arglist *formal, for (f = formal; f; f = f->next) n++; - new_arg = XALLOCAVEC (gfc_actual_arglist *, n); + /* Take care not to call alloca with a zero argument. */ + new_arg = XALLOCAVEC (gfc_actual_arglist *, n + !n); for (i = 0; i < n; i++) new_arg[i] = NULL; diff --git a/gcc/fortran/symbol.c b/gcc/fortran/symbol.c index 0b711ca..cf403c4 100644 --- a/gcc/fortran/symbol.c +++ b/gcc/fortran/symbol.c @@ -3979,7 +3979,8 @@ do_traverse_symtree (gfc_symtree *st, void (*st_func) (gfc_symtree *), gcc_assert ((st_func && !sym_func) || (!st_func && sym_func)); nodes = count_st_nodes (st); - st_vec = XALLOCAVEC (gfc_symtree *, nodes); + /* Take care not to call alloca with a zero argument. */ + st_vec = XALLOCAVEC (gfc_symtree *, nodes + !nodes); node_cntr = 0; fill_st_vector (st, st_vec, node_cntr); diff --git a/gcc/fortran/trans-intrinsic.c b/gcc/fortran/trans-intrinsic.c index 463bb58..e4715f8 100644 --- a/gcc/fortran/trans-intrinsic.c +++ b/gcc/fortran/trans-intrinsic.c @@ -5525,7 +5525,8 @@ gfc_conv_intrinsic_ishftc (gfc_se * se, gfc_expr * expr) unsigned int num_args; num_args = gfc_intrinsic_argument_list_length (expr); - args = XALLOCAVEC (tree, num_args); + /* Take care not to call alloca with a zero argument. */ + args = XALLOCAVEC (tree, num_args + !num_args); gfc_conv_intrinsic_function_args (se, expr, args, num_args); diff --git a/gcc/reg-stack.c b/gcc/reg-stack.c index 4e86fa9..7da5a5f 100644 --- a/gcc/reg-stack.c +++ b/gcc/reg-stack.c @@ -2050,9 +2050,10 @@ subst_asm_stack_regs (rtx_insn *insn, stack_ptr regstack) for (i = 0, note = REG_NOTES (insn); note; note = XEXP (note, 1)) i++; - note_reg = XALLOCAVEC (rtx, i); - note_loc = XALLOCAVEC (rtx *, i); - note_kind = XALLOCAVEC (enum reg_note, i); + /* Take care not to call alloca with a zero argument. */ + note_reg = XALLOCAVEC (rtx, i + !i); + note_loc = XALLOCAVEC (rtx *, i + !i); + note_kind = XALLOCAVEC (enum reg_note, i + !i); n_notes = 0; for (note = REG_NOTES (insn); note; note = XEXP (note, 1)) diff --git a/gcc/tree-ssa-threadedge.c b/gcc/tree-ssa-threadedge.c index 534292c..0564e69 100644 --- a/gcc/tree-ssa-threadedge.c +++ b/gcc/tree-ssa-threadedge.c @@ -340,7 +340,8 @@ record_temporary_equivalences_from_stmts_at_dest (edge e, unsigned int num, i = 0; num = NUM_SSA_OPERANDS (stmt, SSA_OP_ALL_USES); - copy = XALLOCAVEC (tree, num); + /* Take care not to call alloca with a zero argument. */ + copy = XALLOCAVEC (tree, num + !num); /* Make a copy of the uses & vuses into USES_COPY, then cprop into the operands. */