Fix crash in BLI_kdtree_range_search

Called memcpy with a NULL pointer,
causing the following NULL check to get optimized away.
This commit is contained in:
Campbell Barton 2014-09-11 12:34:30 +10:00
parent 229c2c14f7
commit bae66459df

@ -406,20 +406,22 @@ static int range_compare(const void *a, const void *b)
else else
return 0; return 0;
} }
static void add_in_range(KDTreeNearest **ptn, unsigned int found, unsigned int *totfoundstack, int index, float dist, float *co) static void add_in_range(
KDTreeNearest **r_foundstack,
unsigned int *r_foundstack_tot_alloc,
unsigned int found,
const int index, const float dist, const float *co)
{ {
KDTreeNearest *to; KDTreeNearest *to;
if (found >= *totfoundstack) { if (UNLIKELY(found >= *r_foundstack_tot_alloc)) {
KDTreeNearest *temp = MEM_mallocN((*totfoundstack + KD_FOUND_ALLOC_INC) * sizeof(KDTreeNode), "KDTree.treefoundstack"); *r_foundstack = MEM_reallocN_id(
memcpy(temp, *ptn, *totfoundstack * sizeof(KDTreeNearest)); *r_foundstack,
if (*ptn) (*r_foundstack_tot_alloc += KD_FOUND_ALLOC_INC) * sizeof(KDTreeNode),
MEM_freeN(*ptn); __func__);
*ptn = temp;
*totfoundstack += KD_FOUND_ALLOC_INC;
} }
to = (*ptn) + found; to = (*r_foundstack) + found;
to->index = index; to->index = index;
to->dist = sqrtf(dist); to->dist = sqrtf(dist);
@ -464,7 +466,7 @@ int BLI_kdtree_range_search__normal(
else { else {
dist2 = squared_distance(root->co, co, nor); dist2 = squared_distance(root->co, co, nor);
if (dist2 <= range2) if (dist2 <= range2)
add_in_range(&foundstack, found++, &totfoundstack, root->index, dist2, root->co); add_in_range(&foundstack, &totfoundstack, found++, root->index, dist2, root->co);
if (root->left) if (root->left)
stack[cur++] = root->left; stack[cur++] = root->left;
@ -486,7 +488,7 @@ int BLI_kdtree_range_search__normal(
else { else {
dist2 = squared_distance(node->co, co, nor); dist2 = squared_distance(node->co, co, nor);
if (dist2 <= range2) if (dist2 <= range2)
add_in_range(&foundstack, found++, &totfoundstack, node->index, dist2, node->co); add_in_range(&foundstack, &totfoundstack, found++, node->index, dist2, node->co);
if (node->left) if (node->left)
stack[cur++] = node->left; stack[cur++] = node->left;