From fe8f2c6e7b4647dbe7fb0f248726bf2bd44093b1 Mon Sep 17 00:00:00 2001 From: Thomas Koenig Date: Sun, 21 Oct 2012 13:43:32 +0000 Subject: [PATCH] re PR libfortran/54736 (GFORTRAN_CONVERT_UNIT causes malloc error on several platforms) MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 2012-10-21 Thomas König PR libfortran/54736 Backport from trunk * runtime/environ.c (search_unit): Correct logic for binary search. (mark_single): Fix index errors. From-SVN: r192653 --- libgfortran/ChangeLog | 8 +++++++ libgfortran/runtime/environ.c | 42 +++++++++++++++++++++++------------ 2 files changed, 36 insertions(+), 14 deletions(-) diff --git a/libgfortran/ChangeLog b/libgfortran/ChangeLog index 3921f72a63cf..a57a66dac293 100644 --- a/libgfortran/ChangeLog +++ b/libgfortran/ChangeLog @@ -1,3 +1,11 @@ +2012-10-21 Thomas König + + PR libfortran/54736 + Backport from trunk + * runtime/environ.c (search_unit): Correct logic + for binary search. + (mark_single): Fix index errors. + 2012-05-12 Tobias Burnus PR fortran/53310 diff --git a/libgfortran/runtime/environ.c b/libgfortran/runtime/environ.c index a6ce645e0e17..accf2b10c28b 100644 --- a/libgfortran/runtime/environ.c +++ b/libgfortran/runtime/environ.c @@ -453,21 +453,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, @@ -582,13 +596,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; -- 2.47.2