fix potential memory leaks

Co-authored-by: Lew Rossman <LRossman@cinci.rr.com>
This commit is contained in:
0tkl
2023-08-02 14:12:56 +08:00
parent b860f9b16f
commit 2f63e513f0
3 changed files with 34 additions and 15 deletions

View File

@@ -7,7 +7,7 @@
Authors: see AUTHORS Authors: see AUTHORS
Copyright: see AUTHORS Copyright: see AUTHORS
License: see LICENSE License: see LICENSE
Last Updated: 05/15/2019 Last Updated: 08/02/2023
This module is based code by Steve Hill in Graphics Gems III, This module is based code by Steve Hill in Graphics Gems III,
David Kirk (ed.), Academic Press, Boston, MA, 1992 David Kirk (ed.), Academic Press, Boston, MA, 1992
@@ -72,7 +72,11 @@ struct Mempool * mempool_create()
if (mempool == NULL) return NULL; if (mempool == NULL) return NULL;
mempool->first = createMemBlock(); mempool->first = createMemBlock();
mempool->current = mempool->first; mempool->current = mempool->first;
if (mempool->first == NULL) return NULL; if (mempool->first == NULL)
{
free(mempool);
return NULL;
}
return mempool; return mempool;
} }

View File

@@ -3,7 +3,8 @@
// epanet_output.c -- API for reading results from EPANET binary output file // epanet_output.c -- API for reading results from EPANET binary output file
// //
// Version: 0.40 // Version: 0.40
// Date 04/02/2019 // Date 08/02/2023
// 04/02/2019
// 09/06/2017 // 09/06/2017
// 06/17/2016 // 06/17/2016
// 08/05/2014 // 08/05/2014
@@ -252,12 +253,15 @@ int EXPORT_OUT_API ENR_getNetSize(ENR_Handle p_handle, int** elementCount, int*
*/ */
{ {
int errorcode = 0; int errorcode = 0;
int* temp = newIntArray(NELEMENTTYPES); int* temp;
data_t* p_data; data_t* p_data;
p_data = (data_t*)p_handle; p_data = (data_t*)p_handle;
if (p_data == NULL) return -1; if (p_data == NULL) return -1;
// Check memory for count values
else if MEMCHECK(temp = newIntArray(NELEMENTTYPES)) errorcode = 411;
else else
{ {
temp[0] = p_data->nodeCount; temp[0] = p_data->nodeCount;
@@ -459,6 +463,7 @@ int EXPORT_OUT_API ENR_getElementName(ENR_Handle p_handle, ENR_ElementType type,
*name = temp; *name = temp;
*length = MAXID_P1; *length = MAXID_P1;
} }
else free(temp);
} }
return set_error(p_data->error_handle, errorcode); return set_error(p_data->error_handle, errorcode);

View File

@@ -560,26 +560,36 @@ int addlinkvertex(Slink *link, double x, double y)
*/ */
{ {
static int CHUNKSIZE = 5; static int CHUNKSIZE = 5;
int n; int n, newCapacity;
Pvertices vertices; Pvertices vertices;
if (link->Vertices == NULL) double *newX, *newY;
vertices = link->Vertices;
if (vertices == NULL)
{ {
vertices = (struct Svertices *) malloc(sizeof(struct Svertices)); vertices = (struct Svertices *) malloc(sizeof(struct Svertices));
if (vertices == NULL) return 101; if (vertices == NULL) return 101;
vertices->Npts = 0; vertices->Npts = 0;
vertices->Capacity = CHUNKSIZE; vertices->Capacity = 0;
vertices->X = (double *) calloc(vertices->Capacity, sizeof(double)); vertices->X = NULL;
vertices->Y = (double *) calloc(vertices->Capacity, sizeof(double)); vertices->Y = NULL;
link->Vertices = vertices; link->Vertices = vertices;
} }
vertices = link->Vertices;
if (vertices->Npts >= vertices->Capacity) if (vertices->Npts >= vertices->Capacity)
{ {
vertices->Capacity += CHUNKSIZE; newCapacity = vertices->Capacity + CHUNKSIZE;
vertices->X = realloc(vertices->X, vertices->Capacity * sizeof(double)); newX = realloc(vertices->X, newCapacity * sizeof(double));
vertices->Y = realloc(vertices->Y, vertices->Capacity * sizeof(double)); newY = realloc(vertices->Y, newCapacity * sizeof(double));
if (newX == NULL || newY == NULL)
{
free(newX);
free(newY);
return 101;
}
vertices->Capacity = newCapacity;
vertices->X = newX;
vertices->Y = newY;
} }
if (vertices->X == NULL || vertices->Y == NULL) return 101;
n = vertices->Npts; n = vertices->Npts;
vertices->X[n] = x; vertices->X[n] = x;
vertices->Y[n] = y; vertices->Y[n] = y;