Author: zoltan
Date: 2007-08-24 17:33:40 -0400 (Fri, 24 Aug 2007)
New Revision: 84825

Modified:
   trunk/mcs/class/corlib/System/Array.cs
   trunk/mcs/class/corlib/System/ChangeLog
Log:
2007-08-24  Zoltan Varga  <[EMAIL PROTECTED]>

        * Array.cs: Fix min + max / 2 overflows in the BinarySearch and qsort 
methods.
        Fixes #82469.


Modified: trunk/mcs/class/corlib/System/Array.cs
===================================================================
--- trunk/mcs/class/corlib/System/Array.cs      2007-08-24 21:27:47 UTC (rev 
84824)
+++ trunk/mcs/class/corlib/System/Array.cs      2007-08-24 21:33:40 UTC (rev 
84825)
@@ -840,7 +840,9 @@
                        int iCmp = 0;
                        try {
                                while (iMin <= iMax) {
-                                       int iMid = (iMin + iMax) / 2;
+                                       // Be careful with overflow
+                                       // 
http://googleresearch.blogspot.com/2006/06/extra-extra-read-all-about-it-nearly.html
+                                       int iMid = iMin + ((iMax - iMin) / 2);
                                        object elt = array.GetValueImpl (iMid);
 
                                        iCmp = comparer.Compare (elt, value);
@@ -1486,7 +1488,9 @@
                        int low = low0;
                        int high = high0;
 
-                       object objPivot = keys.GetValueImpl ((low + high) / 2);
+                       // Be careful with overflows
+                       int mid = low + ((high - low) / 2);
+                       object objPivot = keys.GetValueImpl (mid);
 
                        while (low <= high) {
                                // Move the walls in
@@ -1684,7 +1688,9 @@
                        int low = low0;
                        int high = high0;
 
-                       K keyPivot = keys [(low + high) / 2];
+                       // Be careful with overflows
+                       int mid = low + ((high - low) / 2);
+                       K keyPivot = keys [mid];
 
                        while (low <= high) {
                                // Move the walls in
@@ -1733,7 +1739,9 @@
                        int low = low0;
                        int high = high0;
 
-                       T keyPivot = array [(low + high) / 2];
+                       // Be careful with overflows
+                       int mid = low + ((high - low) / 2);
+                       T keyPivot = array [mid];
 
                        while (low <= high) {
                                // Move the walls in
@@ -2050,7 +2058,8 @@
                        int iCmp = 0;
                        try {
                                while (iMin <= iMax) {
-                                       int iMid = (iMin + iMax) / 2;
+                                       // Be careful with overflows
+                                       int iMid = iMin + ((iMax - iMin) / 2);
                                        iCmp = comparer.Compare (value, array 
[iMid]);
 
                                        if (iCmp == 0)

Modified: trunk/mcs/class/corlib/System/ChangeLog
===================================================================
--- trunk/mcs/class/corlib/System/ChangeLog     2007-08-24 21:27:47 UTC (rev 
84824)
+++ trunk/mcs/class/corlib/System/ChangeLog     2007-08-24 21:33:40 UTC (rev 
84825)
@@ -1,3 +1,8 @@
+2007-08-24  Zoltan Varga  <[EMAIL PROTECTED]>
+
+       * Array.cs: Fix min + max / 2 overflows in the BinarySearch and qsort 
methods.
+       Fixes #82469.
+
 2007-08-23  Marek Safar  <[EMAIL PROTECTED]>
 
        * DateTimeOffset.cs: New .NET 3.5 struct for 2.0 mscorlib.

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to