D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 7762 - Possible bug in std.algorithm.sort(ubyte[])
Summary: Possible bug in std.algorithm.sort(ubyte[])
Status: RESOLVED WORKSFORME
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: x86 Windows
: P2 normal
Assignee: No Owner
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2012-03-24 09:39 UTC by bearophile_hugs
Modified: 2015-06-09 05:14 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2012-03-24 09:39:41 UTC
This program causes a segfault about 30-50% of the times it is run, on Windows 32 bit dmd 2.059head:


Compile with:
dmd -property -g -wi test.d


import std.stdio, std.algorithm, std.random;
void main() {
    immutable digits = "0123456789";
    enum nLoops = 5_000_000;
    enum maxLen = 20;
    char[maxLen] items;
    auto rnd = Xorshift();
    rnd.seed(0);
    foreach (i; 0 .. nLoops) {
        immutable len = uniform(0, maxLen, rnd);
        foreach (j; 0 .. len)
            items[j] = digits[uniform(0, digits.length, rnd)];
        string ns = items[0 .. len].idup;
        ns = cast(string)(cast(ubyte[])ns.dup).sort().release();
    }
}


The stack trace:


object.Error: Access Violation
----------------
...\dmd2\src\phobos\std\algorithm.d(7283): bool std.algorithm.__T8isSortedS593std10functional31__T9binaryFunVAyaa5_61203c2062Z9binaryFunTAhZ.isSorted(ubyte[])
...\dmd2\src\phobos\std\algorithm.d(6744): D3std9algorithm60__T4sortVAyaa5_61203cÙ62VE€Ž«12SwapStrategy0TAhZ€…¹F€ƒ‰S€„«5range37€ƒÒ11S€ƒšedR€„’€ƒª€‘ÝZ€¢
...\test.d(14): _Dmain
----------------


The bug disappears if you remove ".sort().release()".


Maybe it doesn't crash wi ldc2 linux 64, dmd-front-end 2.058 (I don't know what compilation switches).
Comment 1 AndyC 2015-01-27 02:47:05 UTC
I ran the test code using dmd 2.066.1 on linux x64 and windows x64 (neither was the reported hardware, so not closing)

I ran it 10 times in a row and got no crash.

Can anyone try it on windows x86?
Comment 2 zeljkog 2015-01-27 03:28:12 UTC
2.66.1 win7
dmd -property -g -wi test.d 
everything ok

master
dmd -property -g -wi test.d 
can not compile 

dmd -g -wi test.d
everything ok
Comment 3 bearophile_hugs 2015-01-27 12:11:13 UTC
I can't detect this problem any more, closed issue.