Skip to content

Commit

Permalink
sel4vm, guest_ram: fix compare function
Browse files Browse the repository at this point in the history
The compare function is used with qsort to sort the guest ram regions.
However, the compare function was returning an int, when the region
checked is a uintptr_t. Subtracting these values and returning an int
can cause undefined behavior. This commit fixes the compare function
such that the return is compatible with qsort.

Signed-off-by: Chris Guikema <[email protected]>
  • Loading branch information
chrisguikema committed Oct 26, 2023
1 parent deab837 commit b86f908
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion libsel4vm/src/guest_ram.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,15 @@ static int ram_region_cmp(const void *a, const void *b)
{
const vm_ram_region_t *aa = a;
const vm_ram_region_t *bb = b;
return aa->start - bb->start;

/* qsort expected return values */
if (aa->start == bb->start) {
return 0;
} else if (aa->start < bb->start) {
return -1;
} else {
return 1;
}
}

static void sort_guest_ram_regions(vm_mem_t *guest_memory)
Expand Down

0 comments on commit b86f908

Please sign in to comment.