Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
Merge pull request #881 from quickfur/issue13073
Browse files Browse the repository at this point in the history
Fix wrong comparison of uint[]/int[] due to invalid subtraction.
  • Loading branch information
9rnsr committed Jul 12, 2014
2 parents a13628a + 36cae44 commit b0920b6
Showing 1 changed file with 32 additions and 6 deletions.
38 changes: 32 additions & 6 deletions src/rt/typeinfo/ti_Aint.d
Expand Up @@ -49,9 +49,10 @@ class TypeInfo_Ai : TypeInfo_Array
len = s2.length;
for (size_t u = 0; u < len; u++)
{
int result = s1[u] - s2[u];
if (result)
return result;
if (s1[u] < s2[u])
return -1;
else if (s1[u] > s2[u])
return 1;
}
if (s1.length < s2.length)
return -1;
Expand All @@ -77,6 +78,16 @@ unittest
assert(a == [[5,3,8], [5,3,8,7]]);
}

unittest
{
// Issue 13073: original code uses int subtraction which is susceptible to
// integer overflow, causing the following case to fail.
int[] a = [int.max, int.max];
int[] b = [int.min, int.min];
assert(a > b);
assert(b < a);
}

// uint[]

class TypeInfo_Ak : TypeInfo_Ai
Expand All @@ -93,9 +104,10 @@ class TypeInfo_Ak : TypeInfo_Ai
len = s2.length;
for (size_t u = 0; u < len; u++)
{
int result = s1[u] - s2[u];
if (result)
return result;
if (s1[u] < s2[u])
return -1;
else if (s1[u] > s2[u])
return 1;
}
if (s1.length < s2.length)
return -1;
Expand All @@ -110,6 +122,20 @@ class TypeInfo_Ak : TypeInfo_Ai
}
}

unittest
{
// Original test case from issue 13073
uint x = 0x22_DF_FF_FF;
uint y = 0xA2_DF_FF_FF;
assert(!(x < y && y < x));
uint[] a = [x];
uint[] b = [y];
assert(!(a < b && b < a)); // Original failing case
uint[1] a1 = [x];
uint[1] b1 = [y];
assert(!(a1 < b1 && b1 < a1)); // Original failing case
}

// dchar[]

class TypeInfo_Aw : TypeInfo_Ak
Expand Down

0 comments on commit b0920b6

Please sign in to comment.