jeudi 25 janvier 2018

Bug in std::sort?

OK, I typically default to assuming that the error is in my code, but what I am seeing is making literally no sense.


I have a std::vector<DocumentWidget *>, which I want sorted by some relatively simple algorithm. Here's what I am seeing.

The code looks like this:

std::vector<DocumentWidget *> documents = DocumentWidget::allDocuments();

// NOTE(eteran): on my system, I have observed a **consistent** crash
// when documents.size() == 17 while using std::sort. This makes zero sense at all...
std::sort(documents.begin(), documents.end(), [](const DocumentWidget *a, const DocumentWidget *b) {

    int rc = (a->filenameSet_ == b->filenameSet_) ? 0 : a->filenameSet_ && !b->filenameSet_ ? 1 : -1;
    if (rc != 0) {
        return rc < 0;
    }
    if(a->filename_ < b->filename_) {
        return true;
    }
    return a->path_ < b->path_;
});

Seems simple enough, but it crashes specifically when I have a 17th item in the list! The sorting predicate clearly is not modifying the vector in any way, so I can't see that being an issue. Address sanitizer and valgrind show no errors up until this point.

qSort does not crash, seems to work fine. No other threads that are running are touching this data, and it happens reliably no matter how slowly I step... so it's not a race condition.

When I look in the debugger, the a parameter seems to be where the "one past the end" iterator is pointing. But that shouldn't be happening if std::sort is behaving Crash].

Note There are 17 items in the std::vector, I forced the debugger to display an 18th item to illustrate where a seems to be coming from.

I can't imagine that std::sort is bugged, but I am really struggling to find another explanation. Am I missing some obvious bug here?!

Aucun commentaire:

Enregistrer un commentaire