From patchwork Thu Jan 3 13:07:20 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arnaud Charlet X-Patchwork-Id: 209248 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) by ozlabs.org (Postfix) with SMTP id 5D4C82C0080 for ; Fri, 4 Jan 2013 00:07:53 +1100 (EST) Comment: DKIM? See http://www.dkim.org DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=gcc.gnu.org; s=default; x=1357823273; h=Comment: DomainKey-Signature:Received:Received:Received:Received:Received: Received:Received:Date:From:To:Cc:Subject:Message-ID: MIME-Version:Content-Type:Content-Disposition:User-Agent: Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:Sender:Delivered-To; bh=wqRCe8qZA4LecwFe6upp U1e+56U=; b=aEDtpWrOhDHUEvCvsa2Zzyo7EV0czAcAREE9XF8+P+jxXgk0p1wa b4dO3eo/T+JgczDaokzQifqjj6mJKQedyLHtRbL2td7cKWNs5SHZaVtRBV2nUF+T Tkx9Pf//ioivA1Te6sjm3p9m9oP7WGgSHZcalnev5lruAMWEuPdZyKY= Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=default; d=gcc.gnu.org; h=Received:Received:X-SWARE-Spam-Status:X-Spam-Check-By:Received:Received:Received:Received:Received:Date:From:To:Cc:Subject:Message-ID:MIME-Version:Content-Type:Content-Disposition:User-Agent:Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post:List-Help:Sender:Delivered-To; b=SgQLtSfcMXCcenOmsmf89aKiCpdFQb3WjWf/f7qess1x/FHFic1OV8gax2/MXg IMpFrvusGQ+9awfdY7UFGGGEyIRvQ3Gawj+tjuv614te+2FvaolCUYOQ6qB2DiFe czzcLY7/hTMcP25ThbFW+xIT02f46+izO+YxkZgrYdpsg=; Received: (qmail 23309 invoked by alias); 3 Jan 2013 13:07:34 -0000 Received: (qmail 23286 invoked by uid 22791); 3 Jan 2013 13:07:33 -0000 X-SWARE-Spam-Status: No, hits=-1.8 required=5.0 tests=AWL, BAYES_00, RCVD_IN_HOSTKARMA_NO X-Spam-Check-By: sourceware.org Received: from rock.gnat.com (HELO rock.gnat.com) (205.232.38.15) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Thu, 03 Jan 2013 13:07:21 +0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id EB1E22E4B7; Thu, 3 Jan 2013 08:07:20 -0500 (EST) Received: from rock.gnat.com ([127.0.0.1]) by localhost (rock.gnat.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id WqYOdk3Jt+Hg; Thu, 3 Jan 2013 08:07:20 -0500 (EST) Received: from kwai.gnat.com (kwai.gnat.com [205.232.38.4]) by rock.gnat.com (Postfix) with ESMTP id C7E162E285; Thu, 3 Jan 2013 08:07:20 -0500 (EST) Received: by kwai.gnat.com (Postfix, from userid 4192) id C6CA53FF09; Thu, 3 Jan 2013 08:07:20 -0500 (EST) Date: Thu, 3 Jan 2013 08:07:20 -0500 From: Arnaud Charlet To: gcc-patches@gcc.gnu.org Cc: Bob Duff Subject: [Ada] Avoid overflow in Table reallocation Message-ID: <20130103130720.GA4537@adacore.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.20 (2009-06-14) 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 This patch avoids an overflow that occurs when tables get bigger than about 12 million elements. No change in functionality (except for enormous tables), so no test available. Tested on x86_64-pc-linux-gnu, committed on trunk 2013-01-03 Bob Duff * table.adb (Reallocate): Calculate new Length in Long_Integer to avoid overflow. Index: table.adb =================================================================== --- table.adb (revision 194841) +++ table.adb (working copy) @@ -6,7 +6,7 @@ -- -- -- B o d y -- -- -- --- Copyright (C) 1992-2009, Free Software Foundation, Inc. -- +-- Copyright (C) 1992-2012, Free Software Foundation, Inc. -- -- -- -- GNAT is free software; you can redistribute it and/or modify it under -- -- terms of the GNU General Public License as published by the Free Soft- -- @@ -172,6 +172,7 @@ procedure Reallocate is New_Size : Memory.size_t; + New_Length : Long_Integer; begin if Max < Last_Val then @@ -186,11 +187,15 @@ -- the increment value or 10, which ever is larger (the reason -- for the use of 10 here is to ensure that the table does really -- increase in size (which would not be the case for a table of - -- length 10 increased by 3% for instance). + -- length 10 increased by 3% for instance). Do the intermediate + -- calculation in Long_Integer to avoid overflow. while Max < Last_Val loop - Length := Int'Max (Length * (100 + Table_Increment) / 100, - Length + 10); + New_Length := + Long_Integer (Length) * + (100 + Long_Integer (Table_Increment)) + / 100; + Length := Int'Max (Int (New_Length), Length + 10); Max := Min + Length - 1; end loop;