From patchwork Fri Jun 14 10:08:48 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Janne Blomqvist X-Patchwork-Id: 1115871 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-502953-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: ozlabs.org; dkim=temperror (0-bit key) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="mefu9dd6"; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b="D08UTMpL"; 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 45QGYD4826z9s00 for ; Fri, 14 Jun 2019 20:09:14 +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:from :to:cc:subject:date:message-id; q=dns; s=default; b=J2I3IFnprxuI BqtOymRmDQgaZeIHEvMtI8C1zIhw+RsVUW5VXyNeq1JM+adMuJDFQJ2euqtrPcjR RDqZFfwFxnXsB+ak4NW7OiwBcM+QAkSCNsdwlEt8ez+91R81nw5D0A3sD5Bnujf8 TIWokSYkMm+SpEQk3kzdgRF4HyFpxYs= 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:from :to:cc:subject:date:message-id; s=default; bh=XoELRfDEBbHrYl1Dhv KNlDBBsao=; b=mefu9dd6O/5O9MfuVZmrm2UTB+8jQQKWF2BImOQB9+HdiUF3q7 oyTdow9aiwezA9PFdSMl2twubwQeyIZ7n1uX620qqs1z3CAwh0+4+cx+c4IId3ye QCAQgJW7l/CRm/zqRd7OIR7qLQadrbjQOjY6PReqBfo0NxwLCzRoIAc7s= Received: (qmail 23727 invoked by alias); 14 Jun 2019 10:09:07 -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 23712 invoked by uid 89); 14 Jun 2019 10:09:06 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.4 required=5.0 tests=AWL, BAYES_00, FREEMAIL_FROM, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, RCVD_IN_JMF_BR, SPF_PASS autolearn=ham version=3.3.1 spammy=HX-Languages-Length:1475 X-HELO: mail-lj1-f193.google.com Received: from mail-lj1-f193.google.com (HELO mail-lj1-f193.google.com) (209.85.208.193) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 14 Jun 2019 10:09:05 +0000 Received: by mail-lj1-f193.google.com with SMTP id x25so1821358ljh.2; Fri, 14 Jun 2019 03:09:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=8EM7DH0aT8LH6hmll+qJc/w3if/dygrKtvWhiM/lV6c=; b=D08UTMpLHY/o/Iv0p77O7PHpCZbNS7M6S+9A7F4ZC27Yk+kbwZoy/6jbSKEZyPOs5v iuXVUbxTa2Ta3zeZPqWE7jIF/JAo1sH1dYIgKs2r1lnujYNYrywDBW/uNtDQaBnpIjw8 WGqj8/cfqkT//mDSNa2OMJa3EkOs5P0goG51TCVAfQWpQuX9u6rZLBq5AhNODILYGTcW /HPcjwlv7ByoFrnVv7H280zD4Wg1/LIun5Riy9xd7/lN0DZ1ijrBd3t/jL0zGYwQJPiq tJ2yubqDFjB9mEPbVpH+qFoyTcIVijO77suf9/8zsa/yWN1FBLpwzbeExdbOrIDETG5W jOhA== Received: from ws.lan (88-114-247-254.elisa-laajakaista.fi. [88.114.247.254]) by smtp.gmail.com with ESMTPSA id 2sm489495lju.52.2019.06.14.03.09.01 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Fri, 14 Jun 2019 03:09:02 -0700 (PDT) From: Janne Blomqvist To: gcc-patches@gcc.gnu.org, fortran@gcc.gnu.org Cc: Janne Blomqvist Subject: [PATCH] libgfortran: Use __builtin_mul_overflow in xmallocarray Date: Fri, 14 Jun 2019 13:08:48 +0300 Message-Id: <20190614100848.31732-1-blomqvist.janne@gmail.com> As GCC now provides builtins for doing integer overflow checking, lets use it when checking for overflow in xmallocarray. Regtested on x86_64-pc-linux-gnu, Ok for trunk? libgfortran/ChangeLog: 2019-06-14 Janne Blomqvist * runtime/memory.c (SIZE_MAX):Remove macro definition. (xmallocarray): Use __builtin_mul_overflow. --- libgfortran/runtime/memory.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/libgfortran/runtime/memory.c b/libgfortran/runtime/memory.c index 1a3d33b59dd..09a4ff8733f 100644 --- a/libgfortran/runtime/memory.c +++ b/libgfortran/runtime/memory.c @@ -26,10 +26,6 @@ see the files COPYING3 and COPYING.RUNTIME respectively. If not, see #include "libgfortran.h" #include -#ifndef SIZE_MAX -#define SIZE_MAX ((size_t)-1) -#endif - void * xmalloc (size_t n) @@ -52,18 +48,17 @@ void * xmallocarray (size_t nmemb, size_t size) { void *p; + size_t prod; if (!nmemb || !size) - size = nmemb = 1; -#define HALF_SIZE_T (((size_t) 1) << (__CHAR_BIT__ * sizeof (size_t) / 2)) - else if (__builtin_expect ((nmemb | size) >= HALF_SIZE_T, 0) - && nmemb > SIZE_MAX / size) + prod = 1; + else if (__builtin_mul_overflow (nmemb, size, &prod)) { errno = ENOMEM; os_error ("Integer overflow in xmallocarray"); } - p = malloc (nmemb * size); + p = malloc (prod); if (!p) os_error ("Memory allocation failed in xmallocarray");