Patchwork [libfortran] Fix PR 54736, memory corruption with GFORTRAN_CONVERT_UNIT

login
register
mail settings
Submitter Thomas Koenig
Date Oct. 1, 2012, 6:34 p.m.
Message ID <5069E240.6050304@netcologne.de>
Download mbox | patch
Permalink /patch/188330/
State New
Headers show

Comments

Thomas Koenig - Oct. 1, 2012, 6:34 p.m.
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  <tkoenig@gcc.gnu.org>

         PR libfortran/54736
         * runtime/environ.c (search_unit):  Correct logic
         for binary search.
         (mark_single):  Fix index errors.
Thomas Koenig - Oct. 6, 2012, 11:31 a.m.
Am 01.10.2012 20:34, schrieb Thomas Koenig:
> 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?

Ping?

I would like to start committing patches so I don't have too many
of them in my tree at the same time :-)

	Thomas
Steven Bosscher - Oct. 6, 2012, 11:51 a.m.
On Sat, Oct 6, 2012 at 1:31 PM, Thomas Koenig <tkoenig@netcologne.de> wrote:
> Am 01.10.2012 20:34, schrieb Thomas Koenig:
>>
>> 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?
>
>
> Ping?
>
> I would like to start committing patches so I don't have too many
> of them in my tree at the same time :-)

This looks OK to me.

Ciao!
Steven
Thomas Koenig - Oct. 6, 2012, 5:15 p.m.
Hi Steven,
>> Ping?
>>
>> I would like to start committing patches so I don't have too many
>> of them in my tree at the same time :-)
>
> This looks OK to me.

Committed as rev. 192158.  I'll commit to 4.7 and after that to 4.6
in a few days.

Thanks a lot for the review!

	Thomas

Patch

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;