Bug 13073 – Wrong uint/int array comparison

Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P1
Component
druntime
Product
D
Version
D2
Platform
All
OS
All
Creation time
2014-07-08T09:16:00Z
Last change time
2014-07-12T04:14:02Z
Keywords
pull, wrong-code
Assigned to
nobody
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2014-07-08T09:16:58Z
Tagged this as major, but it's borderline critical. Found by "Archibald" and "anonymous" on NG D.learn: http://forum.dlang.org/thread/[email protected] void main() { uint x = 0x22_DF_FF_FF; uint y = 0xA2_DF_FF_FF; assert(!(x < y && y < x)); // OK. uint[] a = [x]; uint[] b = [y]; assert(!(a < b && b < a)); // Fails. uint[1] a1 = [x]; uint[1] b1 = [y]; assert(!(a1 < b1 && b1 < a1)); // Fails. }
Comment #1 by k.hara.pg — 2014-07-08T14:10:21Z
This is druntime issue. From druntime/src/rt/typeinfo/ti_Aint.d class TypeInfo_Ak : TypeInfo_Ai { override string toString() const { return "uint[]"; } override int compare(in void* p1, in void* p2) const { uint[] s1 = *cast(uint[]*)p1; uint[] s2 = *cast(uint[]*)p2; size_t len = s1.length; if (s2.length < len) len = s2.length; for (size_t u = 0; u < len; u++) { int result = s1[u] - s2[u]; // <---- if (result) return result; } if (s1.length < s2.length) return -1; else if (s1.length > s2.length) return 1; return 0; } override @property inout(TypeInfo) next() inout { return cast(inout)typeid(uint); } }
Comment #2 by hsteoh — 2014-07-11T22:55:13Z
Comment #3 by hsteoh — 2014-07-12T00:02:13Z
Comparison of int[] is also wrong; it uses subtraction, which is OK in terms of sign handling, but wrong because of possibility of overflow (e.g., int.max - int.min overflows and doesn't return a positive result).
Comment #4 by github-bugzilla — 2014-07-12T03:42:51Z
Commits pushed to master at https://github.com/D-Programming-Language/druntime https://github.com/D-Programming-Language/druntime/commit/edbcce94acaa10fa0137bcec99f095a8e7c0ed8f Add unittest for issue 13073. https://github.com/D-Programming-Language/druntime/commit/b0920b65de2ff5c9b777642fc438691a59f63611 Merge pull request #881 from quickfur/issue13073 Fix wrong comparison of uint[]/int[] due to invalid subtraction.
Comment #5 by hsteoh — 2014-07-12T04:14:02Z
Confirmed fixed in git HEAD.