Bug 13566 – std.algorithm.cmp treats string length as element
Status
RESOLVED
Resolution
WORKSFORME
Severity
major
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2014-10-01T23:20:08Z
Last change time
2019-11-08T06:53:19Z
Assigned to
No Owner
Creator
Ali Cehreli
Comments
Comment #0 by acehreli — 2014-10-01T23:20:08Z
The following code cannot be compiled on a 64-bit environment:
import std.algorithm;
bool myPred(dchar a, dchar b)
{
return false;
}
void main()
{
cmp!myPred("", "");
}
phobos/std/algorithm.d(7314): Error: myPred (dchar a, dchar b) is not callable using argument types (ulong, ulong)
phobos/std/algorithm.d(7314): Error: myPred (dchar a, dchar b) is not callable using argument types (ulong, ulong)
deneme.d(179975): Error: template instance std.algorithm.cmp!(myPred, string, string) error instantiating
A quick investigation reveals that one of the overloads of threeWay() inside std.algorithm.cmp takes two size_t parameters that are supposed to be string lengths. Unfortunately, threeWay() then tries to pass those lengths to the predicate:
// For speed only
static int threeWay(size_t a, size_t b)
{
static if (size_t.sizeof == int.sizeof && isLessThan)
return a - b;
else
return binaryFun!pred(b, a) ? 1 : binaryFun!pred(a, b) ? -1 : 0; // <-- HERE
}
I've also noticed that unittests of cmp() don't consider special predicates at all. ;)
Ali
Comment #1 by acehreli — 2015-06-18T23:48:21Z
I hit this again. :(
Comment #2 by monarchdodra — 2015-07-01T15:44:46Z
"static if (size_t.sizeof == int.sizeof"
wut?
Comment #3 by monarchdodra — 2015-07-01T16:01:00Z
(In reply to monarchdodra from comment #2)
> "static if (size_t.sizeof == int.sizeof"
>
> wut?
Nevermind, I've been out of it recently :(