Rule Definition
If the comparison function does not behave consistently when comparing elements, or it modifies any of the elements, the behavior is undefined.
Note: the unspecified behavior, which relates to the treatment of elements that compare as equal, can be avoided by ensuring that the comparison function never returns 0. When two elements are otherwise equal, the comparison function could return a value that indicates their relative order in the initial array.
The implementation of qsort is likely to be recursive and will, therefore, place unknown demands on stack resources. This is of concern in embedded systems as the stack is likely to be a fixed, often small, size.
Violation Code Sample
void qsort (void* base, size_t num, size_t size, // should not use qsort
int (*comparator)(const void*,const void*));
Reference
MISRA C 2012- 21.9: The library functions bsearch and qsort of shall not be used.
Related Technologies
Technical Criterion
CWE-758 - Reliance on Undefined, Unspecified, or Implementation-Defined Behavior
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.