From patchwork Mon Oct 1 18:34:40 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Thomas Koenig X-Patchwork-Id: 188330 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 4AA542C00B4 for ; Tue, 2 Oct 2012 04:36:11 +1000 (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=1349721371; h=Comment: DomainKey-Signature:Received:Received:Received:Received:Received: Message-ID:Date:From:User-Agent:MIME-Version:To:Subject: References:In-Reply-To:Content-Type:Mailing-List:Precedence: List-Id:List-Unsubscribe:List-Archive:List-Post:List-Help:Sender: Delivered-To; bh=Ww41XhHbEQPiFRqLHMZWvKWRSmE=; b=j8u+w0u4LAc8pja WvzqAzsXOlIEiCE9tMFFt00rpcbknVPxry0WdUtlW24ZG+VJb5ySg7dn2q8jyCy0 YbT7wP4WI75UGQwHHqmVRWDNnd/RWPVKw8JZ/GFKF0A2PC2xGmj0174L0GB7hfmb c9RL80C/A9lRc8UeXZ0fnPebYxUU= 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:Message-ID:Date:From:User-Agent:MIME-Version:To:Subject:References:In-Reply-To:Content-Type:Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post:List-Help:Sender:Delivered-To; b=FZMkbX2FPnutvrIKkocknMI4/x3ESH/wCRHKOLA0PcPGAOAZ1k5PYnUlwcGP1m HWMlBPuGOSFBNiqbSnyeJgOxdKj2s3ZF8xzqTjBZsjv3vKbjIwAsSg33eEaignN/ /i/wNchkRwy0EpTN4VYUzTM/CHOi20OizEUlLu2ncCsG8=; Received: (qmail 21228 invoked by alias); 1 Oct 2012 18:35:56 -0000 Received: (qmail 21048 invoked by uid 22791); 1 Oct 2012 18:35:54 -0000 X-SWARE-Spam-Status: No, hits=-3.5 required=5.0 tests=AWL, BAYES_00, KHOP_RCVD_UNTRUST, KHOP_THREADED, RCVD_IN_DNSWL_LOW, RCVD_IN_HOSTKARMA_NO, RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from cc-smtpout2.netcologne.de (HELO cc-smtpout2.netcologne.de) (89.1.8.212) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Mon, 01 Oct 2012 18:34:46 +0000 Received: from cc-smtpin2.netcologne.de (cc-smtpin2.netcologne.de [89.1.8.202]) by cc-smtpout2.netcologne.de (Postfix) with ESMTP id E397D1261E; Mon, 1 Oct 2012 20:34:42 +0200 (CEST) Received: from [192.168.0.106] (xdsl-78-35-176-162.netcologne.de [78.35.176.162]) by cc-smtpin2.netcologne.de (Postfix) with ESMTPSA id B68D011D96; Mon, 1 Oct 2012 20:34:41 +0200 (CEST) Message-ID: <5069E240.6050304@netcologne.de> Date: Mon, 01 Oct 2012 20:34:40 +0200 From: Thomas Koenig User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:15.0) Gecko/20120825 Thunderbird/15.0 MIME-Version: 1.0 To: "fortran@gcc.gnu.org" , gcc-patches Subject: Re: [patch, libfortran] Fix PR 54736, memory corruption with GFORTRAN_CONVERT_UNIT References: <50673770.7030502@netcologne.de> In-Reply-To: <50673770.7030502@netcologne.de> 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 Hello world, the previous version of the patch has an issue that Shane pointed out in the PR. This version should work; at least it survived all the test cases I could come up with. Regression-tested (again). OK for trunk? Also for 4.6 and 4.7? Thomas 2012-10-01 Thomas König PR libfortran/54736 * runtime/environ.c (search_unit): Correct logic for binary search. (mark_single): Fix index errors. Index: runtime/environ.c =================================================================== --- runtime/environ.c (Revision 191857) +++ runtime/environ.c (Arbeitskopie) @@ -459,21 +459,35 @@ search_unit (int unit, int *ip) { int low, high, mid; - low = -1; - high = n_elist; - while (high - low > 1) + if (n_elist == 0) { + *ip = 0; + return 0; + } + + low = 0; + high = n_elist - 1; + + do + { mid = (low + high) / 2; - if (unit <= elist[mid].unit) - high = mid; + if (unit == elist[mid].unit) + { + *ip = mid; + return 1; + } + else if (unit > elist[mid].unit) + low = mid + 1; else - low = mid; - } - *ip = high; - if (elist[high].unit == unit) - return 1; + high = mid - 1; + } while (low <= high); + + if (unit > elist[mid].unit) + *ip = mid + 1; else - return 0; + *ip = mid; + + return 0; } /* This matches a keyword. If it is found, return the token supplied, @@ -588,13 +602,13 @@ mark_single (int unit) } if (search_unit (unit, &i)) { - elist[unit].conv = endian; + elist[i].conv = endian; } else { - for (j=n_elist; j>=i; j--) + for (j=n_elist-1; j>=i; j--) elist[j+1] = elist[j]; - + n_elist += 1; elist[i].unit = unit; elist[i].conv = endian;