I believe this point could use some expansion. What Eric was refering to
was the fact that a qsort implementation expects a comparison function that
returns negative if the first element should appear earlier than the second
element, positive if the second element should appear earlier, and 0 if you
don't care. In particular, if compare(a, b) returns positive, compare(b, a)
must return negative.
Exactly. Also, if compare(a,b) and compare(a,c) both return
zero, then compare(b,c) must return zero. It's easy to see that
the function as shown doesn't satisfy this. (Consider n=0 for
simplicity, then compare(10,1) and compare(10,5) both return zero,
but compare(1,5) returns one.)
If the above isn't true, well, I don't know if there is any requirement on
what qsort is allowed to do. The most obvious thing it could do in this
case is not sort the array as you expect (for example, given the array [1000
1] and N=0, it may leave the array undisturbed). Other misbehaviors (such
as infinite looping, or returning an array which is not a permutation of the
original, or simply crashing) are less likely, but are still conceivable.
As far as I can see, the behavior is undefined. The "shall
be consistent" requirement is in 7.22.5, which is not part of a
constraint. 4p2 tells us that violating a non-constraint "shall"
yields undefined behavior.
As to outcomes, both crashes and infinite loops seem likely
possibilities to me. For example, a qsort() using the Quicksort
algorithm with Sedgewick's median-of-three heuristic may well
walk off one end or the other of the array if the comparator
misbehaves and fails to stop it. Even if this doesn't loop or
crash, it might exchange some array elements with bytes outside
the array limits.
That's another reason to keep comparators simple: It's
easier to verify their correctness.