[lld] [LLD][COFF] Fix ARM64 EC chunks comparator (PR #75407)

Jacek Caban via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 14 02:59:27 PST 2023


================
@@ -1480,7 +1480,8 @@ void Writer::sortECChunks() {
       llvm::stable_sort(sec->chunks, [=](const Chunk *a, const Chunk *b) {
         std::optional<chpe_range_type> aType = a->getArm64ECRangeType(),
                                        bType = b->getArm64ECRangeType();
-        return !aType || (bType && *aType < *bType);
+        return (!aType ? 0 : (unsigned)*aType + 1) <
+               (!bType ? 0 : (unsigned)*bType + 1);
----------------
cjacek wrote:

Good catch, thanks! C-style casts are generally discouraged in LLVM, in this case I think we could entirely avoid casts by tweaking the expression:
```suggestion
        return bType && (!aType || *aType < *bType);
```

It would be also nice to have a test. In this case, reproducing it depends on STL implementation detail, so a mistake like this may not be caught on some setups, but I was able to reproduce the problem on Linux+libstdc++ with: https://gist.github.com/cjacek/7cac304bb03583d90452425cc71101b3

https://github.com/llvm/llvm-project/pull/75407


More information about the llvm-commits mailing list