Skip to content

Commit

Permalink
kvpair_sort: sanity check to forbid NULL pointer
Browse files Browse the repository at this point in the history
Technically, we're not allowed to pass NULL to qsort(), even if
nmemb==0:
    Where an argument declared as size_t nmemb specifies the length of
    the array for a function, nmemb can have the value zero...
    Pointer arguments on such a call shall still have valid values, as
    described in 7.1.4.
    C99, 7.20.5/1 Searching and sorting utilities
and:
    If an argument to a function has an invalid value (such as...
    a null pointer...) ... the behavior is undefined.
    C99, 7.1.4/1 Use of library functions
  • Loading branch information
gperciva committed Mar 5, 2024
1 parent fdee484 commit 14a1940
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/datastruct/kvpair.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <assert.h>
#include <stdlib.h>

#include "kvldskey.h"
Expand Down Expand Up @@ -45,6 +46,9 @@ void
kvpair_sort(struct kvpair * pairs, size_t npairs, size_t mlen)
{

/* Sanity check. */
assert(pairs != NULL);

/* See above comments about cookie_mlen and non-threadsafety. */
cookie_mlen = mlen;

Expand Down

0 comments on commit 14a1940

Please sign in to comment.