Skip to content

Commit 0ec2d5f

Browse files
committed
std.cfg: Improved configuration of qsort().
1 parent 6a65786 commit 0ec2d5f

3 files changed

Lines changed: 27 additions & 0 deletions

File tree

cfg/std.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4340,6 +4340,7 @@ The obsolete function 'gets' is called. With 'gets' you'll get a buffer overrun
43404340
<arg nr="1" direction="inout">
43414341
<not-null/>
43424342
<not-uninit/>
4343+
<minsize type="argvalue" arg="2"/>
43434344
</arg>
43444345
<arg nr="2" direction="in">
43454346
<not-uninit/>
@@ -4352,6 +4353,7 @@ The obsolete function 'gets' is called. With 'gets' you'll get a buffer overrun
43524353
<arg nr="4" direction="in">
43534354
<not-null/>
43544355
<not-uninit/>
4356+
<not-bool/>
43554357
</arg>
43564358
</function>
43574359
<!-- int putc(int c, FILE *stream); -->

test/cfg/std.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,19 @@
2626
#include <inttypes.h>
2727
#include <float.h>
2828

29+
30+
int qsort_cmpfunc (const void * a, const void * b) {
31+
return (*(int*)a - *(int*)b);
32+
}
33+
void nullPointer_qsort(void *base, size_t n, size_t size, int (*cmp)(const void *, const void *))
34+
{
35+
// cppcheck-suppress nullPointer
36+
qsort(NULL, n, size, qsort_cmpfunc);
37+
// cppcheck-suppress nullPointer
38+
qsort(base, n, size, NULL);
39+
qsort(base, n, size, qsort_cmpfunc);
40+
}
41+
2942
// As with all bounds-checked functions, localtime_s is only guaranteed to be available if __STDC_LIB_EXT1__ is defined by the implementation and if the user defines __STDC_WANT_LIB_EXT1__ to the integer constant 1 before including time.h.
3043
#ifdef __STDC_LIB_EXT1__
3144
void uninitvar_localtime_s(const time_t *restrict time, struct tm *restrict result)

test/cfg/std.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@
3232
#include <iterator>
3333
#include <vector>
3434

35+
int qsort_cmpfunc (const void * a, const void * b) {
36+
return (*static_cast<const int*>(a) - *static_cast<const int*>(b));
37+
}
38+
void nullPointer_qsort(void *base, std::size_t n, std::size_t size, int (*cmp)(const void *, const void *))
39+
{
40+
// cppcheck-suppress nullPointer
41+
std::qsort(nullptr, n, size, qsort_cmpfunc);
42+
// cppcheck-suppress nullPointer
43+
std::qsort(base, n, size, nullptr);
44+
std::qsort(base, n, size, qsort_cmpfunc);
45+
}
46+
3547
void *bufferAccessOutOfBounds_memchr(void *s, int c, size_t n)
3648
{
3749
char buf[42]={0};

0 commit comments

Comments
 (0)