From owner-freebsd-arch@freebsd.org Sun Feb 14 05:53:58 2016 Return-Path: Delivered-To: freebsd-arch@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 980D3AA0B97 for ; Sun, 14 Feb 2016 05:53:58 +0000 (UTC) (envelope-from wlosh@bsdimp.com) Received: from mailman.ysv.freebsd.org (mailman.ysv.freebsd.org [IPv6:2001:1900:2254:206a::50:5]) by mx1.freebsd.org (Postfix) with ESMTP id 786C9E26 for ; Sun, 14 Feb 2016 05:53:58 +0000 (UTC) (envelope-from wlosh@bsdimp.com) Received: by mailman.ysv.freebsd.org (Postfix) id 76D67AA0B96; Sun, 14 Feb 2016 05:53:58 +0000 (UTC) Delivered-To: arch@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 5C7C2AA0B94 for ; Sun, 14 Feb 2016 05:53:58 +0000 (UTC) (envelope-from wlosh@bsdimp.com) Received: from mail-qg0-x231.google.com (mail-qg0-x231.google.com [IPv6:2607:f8b0:400d:c04::231]) (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 21917E25 for ; Sun, 14 Feb 2016 05:53:58 +0000 (UTC) (envelope-from wlosh@bsdimp.com) Received: by mail-qg0-x231.google.com with SMTP id y89so91189685qge.2 for ; Sat, 13 Feb 2016 21:53:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bsdimp-com.20150623.gappssmtp.com; s=20150623; h=mime-version:sender:in-reply-to:references:date:message-id:subject :from:to:cc:content-type; bh=WzB4iIaVBqkYo1PLpvMolS6fzBy72oexlcWvF1RfQ9o=; b=c6PnAuyJsizlafsiQQ+YGo8VWW95WGAV9UY5gtkYLrQGgJovGPVAGhn+jbzAmBXd2a j8UhBY2h/jCci9M92v8rz7yBk63anXlxo4Vt9SCxXxMAX+s/0RRql+6vE1YRbdJ7gi3t bUNC+154muTxFiWKCIDIcyNf6TFppwrsm7wzkRxmZVzIwnuMidhRNMhrXJEqC6gWf2sP 1ASR3mwhft2CicTkPALk7IqFWTHBbCUjQLrDkANlUKFRna9BAVkFHn8eYPrL2mR5Va6W lBKdkdiEz8YJGYs8VtNtjyDaoyPQ4PMZyXemGtcjFM04jXWTJC75BdiMAsayQLoAmT3N Nwmg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:sender:in-reply-to:references:date :message-id:subject:from:to:cc:content-type; bh=WzB4iIaVBqkYo1PLpvMolS6fzBy72oexlcWvF1RfQ9o=; b=M9QOIzSGmxd+/zrjLZKO/Tt1YW+KhtUnpygcAT9EAXB0qDuD0tzkPAV2us32tz7QZU bmgcH4aQEM4RHSkogs3Mwa4XHlkVyF8OZqhGlmds/jRR1dTwFpD3kXnc8drfajj0dUt1 efiIzWSY6wNUgX4NF1xF7/QPRb8TatPI+SCDIO8In64UuK/HutPt2DjIMXBYxbt4sIqo 1bsyY86MgxvppFaNqL/mNi6/0C42/KWRAXNhY0AZXUwex/duFDz2NPDsO25kjzt11ox/ c2x358BZicM7ztttOKtUDECX/p1whq/DkNci+BQVdh3vPwLXEPyThAhBgeSyLNy6e/h6 UkPQ== X-Gm-Message-State: AG10YORvQIWS4DKuNtfijlJbmXf2XbKOEf697JaDOUVb2aJdZgptqqAKJLcRWhBvf8czDm6pxCzfAfQA+Awn0Q== MIME-Version: 1.0 X-Received: by 10.140.93.87 with SMTP id c81mr12737309qge.46.1455429236384; Sat, 13 Feb 2016 21:53:56 -0800 (PST) Sender: wlosh@bsdimp.com Received: by 10.140.30.166 with HTTP; Sat, 13 Feb 2016 21:53:56 -0800 (PST) X-Originating-IP: [50.253.99.174] In-Reply-To: References: Date: Sat, 13 Feb 2016 22:53:56 -0700 X-Google-Sender-Auth: W0Ie7GlbXdL6f0eX89yye7x0UVE Message-ID: Subject: Re: OpenBSD mallocarray From: Warner Losh To: Ed Schouten Cc: C Turt , "freebsd-arch@freebsd.org" Content-Type: text/plain; charset=UTF-8 X-Content-Filtered-By: Mailman/MimeDel 2.1.20 X-BeenThere: freebsd-arch@freebsd.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: Discussion related to FreeBSD architecture List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 14 Feb 2016 05:53:58 -0000 On Sat, Feb 13, 2016 at 6:21 AM, Ed Schouten wrote: > Hi there, > > 2016-02-01 20:57 GMT+01:00 C Turt : > > if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) && > > nmemb > 0 && SIZE_MAX / nmemb < size) { > > In my opinion functions like these are good additions, as long as we > make sure that we stop importing garbage expressions like the one > above. It's already bad enough that we copy-pasted this gobbledygook > into fread(), fwrite(), calloc(), reallocarray(), etc. > > Both the latest versions of Clang and GCC support the following builtins: > > bool __builtin_add_overflow(type1 a, type2 b, type3 *res); > bool __builtin_sub_overflow(type1 a, type2 b, type3 *res); > bool __builtin_mul_overflow(type1 a, type2 b, type3 *res); > > These functions perform addition, subtraction and multiplication, > returning whether overflow has occurred in the process. This is a lot > more efficient (and readable) than the expression above, as it simply > uses the CPU's mul instruction, followed by a jump-on-overflow. > > GCC 4.2.1 doesn't support these builtins, but they can easily be > emulated by using static inline functions that use the code above. If > we want them to be type generic, we can use 's > __generic(), which either expands to C11's _Generic() or falls back to > GCC's __builtin_types_compatible_p()/__builtin_choose_expr(). > > I'd say it would make a lot of sense to add a new header file, e.g. > , that adds compiler-independent wrappers for these > builtins: > > #if recent version of GCC/Clang > #define add_overflow(a, b, res) __builtin_add_overflow(a, b, res) > #else > #define add_overflow(a, b, res) (__generic(*(res), unsigned int, ..., > ...)(a, b, res)) > #endif > This actually makes a great deal of sense to me. Warner