diff --git a/src/mempool.c b/src/mempool.c index d6c7f61..81087eb 100755 --- a/src/mempool.c +++ b/src/mempool.c @@ -7,7 +7,7 @@ Authors: see AUTHORS Copyright: see AUTHORS 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, David Kirk (ed.), Academic Press, Boston, MA, 1992 @@ -72,7 +72,11 @@ struct Mempool * mempool_create() if (mempool == NULL) return NULL; mempool->first = createMemBlock(); mempool->current = mempool->first; - if (mempool->first == NULL) return NULL; + if (mempool->first == NULL) + { + free(mempool); + return NULL; + } return mempool; } diff --git a/src/outfile/src/epanet_output.c b/src/outfile/src/epanet_output.c index 742aa28..480f18f 100644 --- a/src/outfile/src/epanet_output.c +++ b/src/outfile/src/epanet_output.c @@ -3,7 +3,8 @@ // epanet_output.c -- API for reading results from EPANET binary output file // // Version: 0.40 -// Date 04/02/2019 +// Date 08/02/2023 +// 04/02/2019 // 09/06/2017 // 06/17/2016 // 08/05/2014 @@ -252,12 +253,15 @@ int EXPORT_OUT_API ENR_getNetSize(ENR_Handle p_handle, int** elementCount, int* */ { int errorcode = 0; - int* temp = newIntArray(NELEMENTTYPES); + int* temp; data_t* p_data; p_data = (data_t*)p_handle; if (p_data == NULL) return -1; + // Check memory for count values + else if MEMCHECK(temp = newIntArray(NELEMENTTYPES)) errorcode = 411; + else { temp[0] = p_data->nodeCount; @@ -459,6 +463,7 @@ int EXPORT_OUT_API ENR_getElementName(ENR_Handle p_handle, ENR_ElementType type, *name = temp; *length = MAXID_P1; } + else free(temp); } return set_error(p_data->error_handle, errorcode); diff --git a/src/project.c b/src/project.c index 22d07a0..aba50c8 100644 --- a/src/project.c +++ b/src/project.c @@ -560,31 +560,41 @@ int addlinkvertex(Slink *link, double x, double y) */ { static int CHUNKSIZE = 5; - int n; + int n, newCapacity; Pvertices vertices; - if (link->Vertices == NULL) + double *newX, *newY; + + vertices = link->Vertices; + if (vertices == NULL) { vertices = (struct Svertices *) malloc(sizeof(struct Svertices)); if (vertices == NULL) return 101; vertices->Npts = 0; - vertices->Capacity = CHUNKSIZE; - vertices->X = (double *) calloc(vertices->Capacity, sizeof(double)); - vertices->Y = (double *) calloc(vertices->Capacity, sizeof(double)); + vertices->Capacity = 0; + vertices->X = NULL; + vertices->Y = NULL; link->Vertices = vertices; } - vertices = link->Vertices; if (vertices->Npts >= vertices->Capacity) { - vertices->Capacity += CHUNKSIZE; - vertices->X = realloc(vertices->X, vertices->Capacity * sizeof(double)); - vertices->Y = realloc(vertices->Y, vertices->Capacity * sizeof(double)); + newCapacity = vertices->Capacity + CHUNKSIZE; + newX = realloc(vertices->X, newCapacity * 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; vertices->X[n] = x; vertices->Y[n] = y; vertices->Npts++; - return 0; + return 0; } void freelinkvertices(Slink *link)