src: make sort order deterministic, next try

We cannot compare pointers as they may point to qsort-local objects.
And we must make sure that cmp(a,b) always agrees with cmp(b,a).
This commit is contained in:
Markus F.X.J. Oberhumer
2023-08-30 16:41:59 +02:00
parent f4e5b29708
commit cfa8107ab9
5 changed files with 54 additions and 24 deletions
+11 -1
View File
@@ -99,7 +99,17 @@ PackVmlinuxBase<T>::compare_Phdr(void const *aa, void const *bb)
if (a->p_paddr < b->p_paddr) return -1; // ascending by .p_paddr
if (a->p_paddr > b->p_paddr) return 1;
// What could remain?
return aa < bb ? -1 : 1; // make sort order deterministic/stable
// try to make sort order deterministic and just compare more fields
#define CMP(field) \
if (a->field != b->field) return a->field < b->field ? -1 : 1
CMP(p_offset);
CMP(p_vaddr);
CMP(p_filesz);
CMP(p_memsz);
CMP(p_flags);
CMP(p_align);
#undef CMP
return 0;
}
template <class T>