Skip to content

Commit e23eff8

Browse files
author
Junio C Hamano
committed
qsort() ptrdiff_t may be larger than int
Morten Welinder <mwelinder@gmail.com> writes: > The code looks wrong. It assumes that pointers are no larger than ints. > If pointers are larger than ints, the code does not necessarily compute > a consistent ordering and qsort is allowed to do whatever it wants. > > Morten > > static int compare_object_pointers(const void *a, const void *b) > { > const struct object * const *pa = a; > const struct object * const *pb = b; > return *pa - *pb; > } Signed-off-by: Junio C Hamano <junkio@cox.net>
1 parent a6da939 commit e23eff8

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

object.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,12 @@ static int compare_object_pointers(const void *a, const void *b)
8282
{
8383
const struct object * const *pa = a;
8484
const struct object * const *pb = b;
85-
return *pa - *pb;
85+
if (*pa == *pb)
86+
return 0;
87+
else if (*pa < *pb)
88+
return -1;
89+
else
90+
return 1;
8691
}
8792

8893
void set_object_refs(struct object *obj, struct object_refs *refs)

0 commit comments

Comments
 (0)