From patchwork Wed Dec 5 10:38:58 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arnaud Charlet X-Patchwork-Id: 203834 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 09CB32C00A7 for ; Wed, 5 Dec 2012 21:39:24 +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=1355308765; 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=6ozYsD0UIhdf4szHhiOZ xr505Ug=; b=jzeCg0vYVFdlLlw3Q5hIJJWlbnuOB5taxFvZpPcoPwUzr8WmjngF E6wkoWdp5J2QvYJlamR7+9bm0q+cF4jODGv+SFmvN2qf3lSiU5aGda2RUalxy4q3 MjWG6/1kmTbyy8UiqI/l1qkPreTwZOB0fLB5M2AsL2/TTkA2xS+71gc= 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=A9K0lswVPuKAx/HaoDQHvC5UYL+83liHLoQNEsCMoJGrykYHE4Ztfd/d5BZfcS cWZMskW51e+F2qPmlbbwu2ZYb9Y9lGrbpECUg0p06YsUuZQ0qV89I46Rwa4unHw0 8Ow/6S6m5NxTSvjkSdAWbjQl/b0ObokesHAsAybI4svd4=; Received: (qmail 32308 invoked by alias); 5 Dec 2012 10:39:20 -0000 Received: (qmail 32298 invoked by uid 22791); 5 Dec 2012 10:39:19 -0000 X-SWARE-Spam-Status: No, hits=-1.9 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; Wed, 05 Dec 2012 10:38:58 +0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id 31CFC2E2C7; Wed, 5 Dec 2012 05:38:58 -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 zLL2ffbZDu0s; Wed, 5 Dec 2012 05:38:58 -0500 (EST) Received: from kwai.gnat.com (kwai.gnat.com [205.232.38.4]) by rock.gnat.com (Postfix) with ESMTP id 18EF22E0E6; Wed, 5 Dec 2012 05:38:58 -0500 (EST) Received: by kwai.gnat.com (Postfix, from userid 4192) id 17BCD919E3; Wed, 5 Dec 2012 05:38:58 -0500 (EST) Date: Wed, 5 Dec 2012 05:38:58 -0500 From: Arnaud Charlet To: gcc-patches@gcc.gnu.org Cc: Bob Duff Subject: [Ada] Avoid storage leak in gnatchop Message-ID: <20121205103858.GA8153@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 removes a storage leak in gnatchop, and makes it slightly more efficient. No noticeable change in behavior, so no test. Tested on x86_64-pc-linux-gnu, committed on trunk 2012-12-05 Bob Duff * gnatchop.adb (Read_File): Avoid storage leak, and in most cases avoid an extra copy of the string. Index: gnatchop.adb =================================================================== --- gnatchop.adb (revision 194188) +++ gnatchop.adb (working copy) @@ -6,7 +6,7 @@ -- -- -- B o d y -- -- -- --- Copyright (C) 1998-2011, Free Software Foundation, Inc. -- +-- Copyright (C) 1998-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- -- @@ -1004,7 +1004,7 @@ is Length : constant File_Offset := File_Offset (File_Length (FD)); -- Include room for EOF char - Buffer : constant String_Access := new String (1 .. Length + 1); + Buffer : String_Access := new String (1 .. Length + 1); This_Read : Integer; Read_Ptr : File_Offset := 1; @@ -1020,9 +1020,16 @@ end loop; Buffer (Read_Ptr) := EOF; - Contents := new String (1 .. Read_Ptr); - Contents.all := Buffer (1 .. Read_Ptr); + if Read_Ptr = Length then + Contents := Buffer; + + else + Contents := new String (1 .. Read_Ptr); + Contents.all := Buffer (1 .. Read_Ptr); + Free (Buffer); + end if; + -- Things aren't simple on VMS due to the plethora of file types and -- organizations. It seems clear that there shouldn't be more bytes -- read than are contained in the file though.