Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib/vector/vlib: Fix possible null pointer dereference #4638

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions lib/vector/Vlib/cats.c
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ int Vect_cat_list_to_array(const struct cat_list *list, int **vals, int *nvals)

G_debug(1, "Vect_cat_list_to_array()");

*nvals = n_cats = 0;
*nvals = n_cats = n_ucats = 0;
cats = NULL;
for (i = 0; i < list->n_ranges; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to me to be a better solution to make an early exit before this for statement, with something like:

if (list->n_ranges <= 0)
    return -1;

If list->n_ranges is 0 or less, cats and n_cats are never set... and the rest doesn't make any sense.

@metzm Perhaps you may have some insight in this?

Copy link
Contributor Author

@ymdatta ymdatta Dec 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nilason : Thanks for the review.

But, I am worried that '-1' indicates that something has gone wrong while converting using Vect_cat_list_to_array function, but here there is nothing wrong and it's just that the argument has no elements in it. What do you think about it?

n = list->max[i] - list->min[i] + 1;
Expand All @@ -509,13 +509,16 @@ int Vect_cat_list_to_array(const struct cat_list *list, int **vals, int *nvals)
n_cats += n;
}

/* sort array */
qsort(cats, n_cats, sizeof(int), cmp);
if (cats) {
/* sort array */
qsort(cats, n_cats, sizeof(int), cmp);

/* skip duplicated values */
ucats = G_malloc(sizeof(int) * n_cats);
last_cat = ucats[0] = cats[0];
n_ucats = 1;
}

/* skip duplicated values */
ucats = G_malloc(sizeof(int) * n_cats);
last_cat = ucats[0] = cats[0];
n_ucats = 1;
for (i = 1; i < n_cats; i++) {
if (last_cat == cats[i])
continue;
Expand Down
Loading