Skip to content

Commit

Permalink
v.external: Check for valid list before passing to qsort (OSGeo#4280)
Browse files Browse the repository at this point in the history
Currently, if 'HAVE_OGR' macro is defined, as part of execution,
we order all formats by name using qsort. But, list containing
all formats is assigned based on a conditional and if the
conditional fails, it can be NULL.
Behavior of qsort is undefined when a NULL ptr is passed as array
argument. To avoid getting into that situation, check if the array
is NULL before performing qsort on it.
This issue was found using cppcheck tool.

Signed-off-by: Mohan Yelugoti <ymdatta.work@gmail.com>
  • Loading branch information
ymdatta authored Sep 9, 2024
1 parent 57cb4d9 commit 7c6c12b
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion vector/v.external/list.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ char **format_list(int *count, size_t *len)
}

/* order formats by name */
qsort(list, *count, sizeof(char *), cmp);
if (list)
qsort(list, *count, sizeof(char *), cmp);
#endif
#if defined HAVE_POSTGRES && !defined HAVE_OGR
list = G_realloc(list, ((*count) + 1) * sizeof(char *));
Expand Down

0 comments on commit 7c6c12b

Please sign in to comment.