From: Junio C Hamano Date: Tue, 6 Dec 2005 21:41:48 +0000 (-0800) Subject: qsort() ptrdiff_t may be larger than int X-Git-Tag: v0.99.9m^2~53 X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=e23eff8be92a2a2cb66b53deef020063cff285ed;p=git.git qsort() ptrdiff_t may be larger than int Morten Welinder 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 --- diff --git a/object.c b/object.c index 427e14ca..cf5931a9 100644 --- a/object.c +++ b/object.c @@ -82,7 +82,12 @@ 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; + if (*pa == *pb) + return 0; + else if (*pa < *pb) + return -1; + else + return 1; } void set_object_refs(struct object *obj, struct object_refs *refs)