@matheusbsbs / matheus-bagliano / commits / e23eff8be9
Fork 0

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>

Junio C Hamano committed Dec 6, 2005 at 13:41 UTC e23eff8be92a2a2cb66b53deef020063cff285ed
1 file changed +6 -1
object.c
+6 -1
@@ -82,7 +82,12 @@ static int compare_object_pointers(const void *a, const void *b)
82 {
83 const struct object * const *pa = a;
84 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;
91 }
92
93 void set_object_refs(struct object *obj, struct object_refs *refs)