From patchwork Sat Sep 29 18:01:20 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Thomas Koenig X-Patchwork-Id: 188040 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 668912C007B for ; Sun, 30 Sep 2012 04:01:40 +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=1349546500; h=Comment: DomainKey-Signature:Received:Received:Received:Received:Received: Message-ID:Date:From:User-Agent:MIME-Version:To:Subject: Content-Type:Mailing-List:Precedence:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:Sender:Delivered-To; bh=wOSWsbs TRYNB1qB8yJ08bW/tmJM=; b=wNTcLrEAC6tOoqhL1iCm6D85v/YY7u7HNragVZN zdC4r1FBvzuBlBG6ViJ4hMQstfCEl9S2DxRJ0wT4CcG0+4bctYUEj9DR6ijl/vsi sQTbIfS6pJ+ZF7AqMVjpx7OcfEPNkHW6KgUhdk4GL+YZImcW1Nw6Ne5Ra4l1LhAM eq0I= 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:Content-Type:Mailing-List:Precedence:List-Id:List-Unsubscribe:List-Archive:List-Post:List-Help:Sender:Delivered-To; b=Ze7uJ+X1r6Yf4n2U6WH2EARdm51aZ38Y6p2ZK3q9LxcQAsqclUn0KqiwbPnmzx MQ6p/vFXaGs7EzQIhznP955JDMvBCrarwiZs/Gw8htx1gSTdIYS6ACLS0+bPEGww mmvvAk8PnJDIAoFTdCTx1qRIevAuIn9GDMaHThpidDVCY=; Received: (qmail 11966 invoked by alias); 29 Sep 2012 18:01:29 -0000 Received: (qmail 11924 invoked by uid 22791); 29 Sep 2012 18:01:27 -0000 X-SWARE-Spam-Status: No, hits=-2.0 required=5.0 tests=AWL, BAYES_00, KHOP_RCVD_UNTRUST, RCVD_IN_DNSWL_LOW, RP_MATCHES_RCVD X-Spam-Check-By: sourceware.org Received: from cc-smtpout3.netcologne.de (HELO cc-smtpout3.netcologne.de) (89.1.8.213) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Sat, 29 Sep 2012 18:01:23 +0000 Received: from cc-smtpin2.netcologne.de (cc-smtpin2.netcologne.de [89.1.8.202]) by cc-smtpout3.netcologne.de (Postfix) with ESMTP id 39FD6128F0; Sat, 29 Sep 2012 20:01:22 +0200 (CEST) Received: from [192.168.0.106] (xdsl-78-35-164-32.netcologne.de [78.35.164.32]) by cc-smtpin2.netcologne.de (Postfix) with ESMTPSA id 0219311DB8; Sat, 29 Sep 2012 20:01:20 +0200 (CEST) Message-ID: <50673770.7030502@netcologne.de> Date: Sat, 29 Sep 2012 20:01:20 +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: [patch, libfortran] Fix PR 54736, memory corruption with GFORTRAN_CONVERT_UNIT 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 attached patch fixes the PR. The logic for processing GFORTRAN_CONVERT_UNIT had been quite wrong. I have checked that the original test case, plus a few more, no longer cause assertion failures or memory corruption (also checked with valgrind). No test case because it is not possible to set environment variables from the testsuite. Regression-tested. OK for trunk? I would also like to backport this to 4.7, and maybe 4.6. What do you think? Thomas 2012-09-29 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 191649) +++ runtime/environ.c (Arbeitskopie) @@ -459,21 +459,34 @@ search_unit (int unit, int *ip) { int low, high, mid; - low = -1; - high = n_elist; + if (n_elist == 0) + { + *ip = 0; + return 0; + } + + low = 0; + high = n_elist - 1; while (high - low > 1) { mid = (low + high) / 2; - if (unit <= elist[mid].unit) + if (unit == elist[mid].unit) + { + *ip = mid; + return 1; + } + else if (unit > elist[mid].unit) + low = mid; + else high = mid; - else - low = mid; } - *ip = high; - if (elist[high].unit == unit) - return 1; + + if (unit > elist[high].unit) + *ip = high; else - return 0; + *ip = low; + + return 0; } /* This matches a keyword. If it is found, return the token supplied, @@ -588,13 +601,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;