From owner-freebsd-hackers@freebsd.org Wed Sep 14 11:46:36 2016 Return-Path: Delivered-To: freebsd-hackers@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id CEE8DAC4FC5 for ; Wed, 14 Sep 2016 11:46:36 +0000 (UTC) (envelope-from ed@nuxi.nl) Received: from mail-yw0-x22a.google.com (mail-yw0-x22a.google.com [IPv6:2607:f8b0:4002:c05::22a]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client CN "smtp.gmail.com", Issuer "Google Internet Authority G2" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 9851A1FCB for ; Wed, 14 Sep 2016 11:46:36 +0000 (UTC) (envelope-from ed@nuxi.nl) Received: by mail-yw0-x22a.google.com with SMTP id i129so15304214ywb.0 for ; Wed, 14 Sep 2016 04:46:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nuxi-nl.20150623.gappssmtp.com; s=20150623; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=8hwzm+jFH0PYvPLe7Rwf7GXVZEYHX4RMg8dmDog1vTU=; b=MrSuhUqAm8ZeX94VK363uZ8vVCjM2F19O48Ti9d4851YuEFoGXBQrR1FqfHY2fksuh paSVTnuDh3giOryUdj+Ne626pc4XjdL5nvgHDHhMYTcxjUsl0nBDuSj0GGWkiHOgrKPr SpHFzmk5ygNLsL+k11RSbID3L9Z7aNY6FdeldUspFvu+uhYArDXZQwBB7/CVoQpBJ7Bz mYHt66TWpD24aZxDH3KFLIrMhiDH0ebNaumHdnL+9oartnZy8+qH3ttRfmLxg+P6yd5e ci4nWhEwWwwSTwGAxpmlEKDSA3TKqgniKem18W+y4pNiKm7Uh8u2bq3uqGD3X3GU6BJR 10KA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=8hwzm+jFH0PYvPLe7Rwf7GXVZEYHX4RMg8dmDog1vTU=; b=ILXS6XUTSNdca1EBtyYJlTiLcMlFUVRT7PYOIImU7YfjSU+Z/NGh4qvG1RpLrGBxke oDpJ/jcCVribTTAs6276CzwprcJmec+r5CbVD3NK6fjsmu4mAEd2l5XFSXhAvF2EPA91 wbVnZZyAZpBDw6fevolLeGVzZ/Krj3ngVezISdpPAeg+uBcX3BXzPMWznXGSF/IWkN+E hxe3y9/fw1wmTG4W2gQP2V5pzEAOZOf7BNpOobSpjRWWq5t4ZLzSmvxcH9ODWvtAiicA w2s0rTrjHPTV+IJmhklVOtigJQE6KJlZZihViJ8PHSU2kORoA3kgqJYL6UlbyodHF4dA Thrg== X-Gm-Message-State: AE9vXwP3j+hGVt3jijavv27PfCsizUk12K9JMXhnJAHB/kDysHeiXGQpq+DxvEOaBiT3CTBTvqbwOP/2nXa9eg== X-Received: by 10.129.147.130 with SMTP id k124mr1761790ywg.116.1473853595573; Wed, 14 Sep 2016 04:46:35 -0700 (PDT) MIME-Version: 1.0 Received: by 10.13.201.71 with HTTP; Wed, 14 Sep 2016 04:46:35 -0700 (PDT) In-Reply-To: <3fe9ba0e-0089-a59c-a09e-8c6f8b74b6bc@openmailbox.org> References: <3fe9ba0e-0089-a59c-a09e-8c6f8b74b6bc@openmailbox.org> From: Ed Schouten Date: Wed, 14 Sep 2016 13:46:35 +0200 Message-ID: Subject: Re: Is replacing alloca(3) where possible a good thing to do? To: twilight Cc: FreeBSD Hackers Content-Type: text/plain; charset=UTF-8 X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 14 Sep 2016 11:46:36 -0000 Hi, 2016-09-14 12:53 GMT+02:00 twilight : > I again, > in cddl/* alloca(3) is used very intensively for creating dynamic arrays. > But, well, it's kinda obsolete and sometimes not safe and portable. > Is replacing alloca(3) with dynamic arrays a good thing? Or should > everything be left as it is? With dynamic arrays you are referring to C99's Variable-Length Arrays (VLAs), right? The advantage of C99 VLAs is that they are standardised, unlike alloca(), which is good. What I also like about them compared to alloca() is that they are easier to implement from a compiler's point of view, as they are bound to a block scope and not to a function. alloca() can be called in a loop, for example, which is quite nasty. Still, C99 VLAs and alloca() both share the problem that as there is no upper bound on the allocated space, you may easily run into stack overflows. Especially for externally-facing APIs, you may have absolutely no idea whether it's safe to allocate a buffer having a size provided by the caller. This is one of the reasons why C11 made them optional again. So the best solution is to replace any use of alloca() with malloc()/free(). That said, as you were interested in making such changes in cddl/*, I guess you'll also have to go through the process of sending those patches to Illumos. -- Ed Schouten Nuxi, 's-Hertogenbosch, the Netherlands KvK-nr.: 62051717