提交 970a0395 编写于 作者: D Daniel Gustafsson

Fix memory management in gp_sparse_vector

palloc() is guaranteed to only return on successful allocation, so there
is no need to check it. ereport(ERROR..) is guaranteed never to return,
and to clean up on it's way out, so pfree()ing after an ereport() is not
just unreachable code, it would be a double-free if it was reached.

Also add proper checks on the malloc() and strdup() calls as those are
subject to the usual memory pressure controls by the programmer.
Reviewed-by: NHeikki Linnakangas <hlinnakangas@pivotal.io>
上级 68b11d46
......@@ -226,13 +226,7 @@ double *sdata_to_float8arr(SparseData sdata) {
errmsg("data type of SparseData is not FLOAT64")));
}
if ((array = (double *)palloc(sizeof(double)*(sdata->total_value_count)))
== NULL)
{
ereport(ERROR,(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("Error allocating memory for array\n")));
}
array = (double *) palloc(sizeof(double) * sdata->total_value_count);
iptr = sdata->index->data;
aptr = 0;
for (int i=0; i<sdata->unique_value_count; i++) {
......
......@@ -196,7 +196,18 @@ SvecType *classify_document(char **features, int num_features, char **document,
#endif
(void) hdestroy();
ordinals = (int *)malloc(sizeof(int)*num_features); //Need to use malloc so that hdestroy() can be called.
if (!ordinals)
ereport(ERROR,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("out of memory")));
histogram = (float8 *)malloc(sizeof(float8)*num_features); //Use malloc because pallocs are cleaned up between queries
if (!histogram) {
free(ordinals);
ereport(ERROR,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("out of memory")));
}
for (i=0; i<num_features; i++) {
ordinals[i] = i;
......@@ -206,6 +217,10 @@ SvecType *classify_document(char **features, int num_features, char **document,
for (i=0; i<num_features; i++) {
if (features[i] != NULL) {
item.key = strdup(features[i]);
if (!item.key)
ereport(ERROR,
(errcode(ERRCODE_OUT_OF_MEMORY),
errmsg("out of memory")));
item.data = (void *)(&(ordinals[i]));
(void) hsearch(item,ENTER);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册