From 4ca66cc7fed14e52f86864829b411df2ad974340 Mon Sep 17 00:00:00 2001 From: 0tkl <118708188+0tkl@users.noreply.github.com> Date: Wed, 2 Aug 2023 11:55:23 +0800 Subject: [PATCH 1/4] handle error codes in tests --- tests/test_demand.cpp | 4 +++- tests/test_overflow.cpp | 4 +++- tests/test_pda.cpp | 4 +++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/tests/test_demand.cpp b/tests/test_demand.cpp index f39d8f7..52388aa 100644 --- a/tests/test_demand.cpp +++ b/tests/test_demand.cpp @@ -7,7 +7,7 @@ Authors: see AUTHORS Copyright: see AUTHORS License: see LICENSE - Last Updated: 03/21/2019 + Last Updated: 08/02/2023 ****************************************************************************** */ @@ -27,7 +27,9 @@ BOOST_AUTO_TEST_CASE(test_categories_save) EN_Project ph = NULL; error = EN_createproject(&ph); + BOOST_REQUIRE(error == 0); error = EN_open(ph, DATA_PATH_NET1, DATA_PATH_RPT, DATA_PATH_OUT); + BOOST_REQUIRE(error == 0); error = EN_getnodeindex(ph, (char *)"12", &Nindex); BOOST_REQUIRE(error == 0); diff --git a/tests/test_overflow.cpp b/tests/test_overflow.cpp index 5ad60a4..8c3e005 100644 --- a/tests/test_overflow.cpp +++ b/tests/test_overflow.cpp @@ -7,7 +7,7 @@ Authors: see AUTHORS Copyright: see AUTHORS License: see LICENSE - Last Updated: 06/16/2019 + Last Updated: 08/02/2023 ****************************************************************************** */ @@ -32,7 +32,9 @@ BOOST_AUTO_TEST_CASE(test_tank_overflow) EN_Project ph = NULL; error = EN_createproject(&ph); + BOOST_REQUIRE(error == 0); error = EN_open(ph, DATA_PATH_NET1, DATA_PATH_RPT, ""); + BOOST_REQUIRE(error == 0); // Get index of the tank and its inlet/outlet pipe error = EN_getnodeindex(ph, (char *)"2", &Nindex); diff --git a/tests/test_pda.cpp b/tests/test_pda.cpp index b7c1636..6091def 100644 --- a/tests/test_pda.cpp +++ b/tests/test_pda.cpp @@ -7,7 +7,7 @@ Authors: see AUTHORS Copyright: see AUTHORS License: see LICENSE - Last Updated: 07/20/2019 + Last Updated: 08/02/2023 ****************************************************************************** */ @@ -30,7 +30,9 @@ BOOST_AUTO_TEST_CASE(test_pda_model) EN_Project ph = NULL; error = EN_createproject(&ph); + BOOST_REQUIRE(error == 0); error = EN_open(ph, DATA_PATH_NET1, DATA_PATH_RPT, ""); + BOOST_REQUIRE(error == 0); // Set Demand Multiplier to 10 to cause negative pressures error = EN_setoption(ph, EN_DEMANDMULT, 10); From 434cd68a23ede580788e7ba8a9af96117514cd32 Mon Sep 17 00:00:00 2001 From: 0tkl <118708188+0tkl@users.noreply.github.com> Date: Wed, 2 Aug 2023 14:12:03 +0800 Subject: [PATCH 2/4] fix potential deref of a null pointer --- src/project.c | 8 ++++---- src/qualroute.c | 4 ++-- src/util/errormanager.c | 3 ++- src/util/filemanager.c | 3 ++- 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/project.c b/src/project.c index eae6ffd..22d07a0 100644 --- a/src/project.c +++ b/src/project.c @@ -7,7 +7,7 @@ Authors: see AUTHORS Copyright: see AUTHORS License: see LICENSE - Last Updated: 04/29/2023 + Last Updated: 08/02/2023 ****************************************************************************** */ @@ -1373,10 +1373,10 @@ char *xstrcpy(char **s1, const char *s2, const size_t n) // s1 points to a valid memory location or is NULL. E.g., // the following code will likely cause a segment fault: // char *s; -// s = xstrcpy(s, "Some text"); +// s = xstrcpy(&s, "Some text"); // while this would work correctly: // char *s = NULL; -// s = xstrcpy(s, "Some text"); +// s = xstrcpy(&s, "Some text"); //---------------------------------------------------------------- { size_t n1 = 0, n2 = 0; @@ -1398,7 +1398,7 @@ char *xstrcpy(char **s1, const char *s2, const size_t n) if (n2 > n1) *s1 = realloc(*s1, (n2 + 1) * sizeof(char)); // Copy the source string into the destination string - strncpy(*s1, s2, n2+1); + if (*s1) strncpy(*s1, s2, n2+1); return *s1; } diff --git a/src/qualroute.c b/src/qualroute.c index b71f218..144be67 100644 --- a/src/qualroute.c +++ b/src/qualroute.c @@ -7,7 +7,7 @@ Description: computes water quality transport over a single time step Authors: see AUTHORS Copyright: see AUTHORS License: see LICENSE -Last Updated: 05/15/2019 +Last Updated: 08/02/2023 ****************************************************************************** */ @@ -609,7 +609,7 @@ void initsegs(Project *pr) addseg(pr, k, v, c); // Create a 2nd segment for the 2-compartment tank model - if (net->Tank[j].MixModel == MIX2) + if (!qual->OutOfMemory && net->Tank[j].MixModel == MIX2) { // ... mixing zone segment v1 = MAX(0, v - net->Tank[j].V1frac * net->Tank[j].Vmax); diff --git a/src/util/errormanager.c b/src/util/errormanager.c index 57c19de..1bda020 100644 --- a/src/util/errormanager.c +++ b/src/util/errormanager.c @@ -7,7 +7,7 @@ Authors: see AUTHORS Copyright: see AUTHORS License: see LICENSE - Last Updated: 04/02/2019 + Last Updated: 08/02/2023 ****************************************************************************** */ @@ -36,6 +36,7 @@ error_handle_t *create_error_manager(void (*p_error_message)(int, char*, int)) { error_handle_t *error_handle; error_handle = (error_handle_t*)calloc(1, sizeof(error_handle_t)); + if (error_handle == NULL) return NULL; error_handle->p_msg_lookup = p_error_message; diff --git a/src/util/filemanager.c b/src/util/filemanager.c index 79a45d2..c06b434 100644 --- a/src/util/filemanager.c +++ b/src/util/filemanager.c @@ -7,7 +7,7 @@ Authors: see AUTHORS Copyright: see AUTHORS License: see LICENSE - Last Updated: 04/01/2019 + Last Updated: 08/02/2023 ****************************************************************************** */ @@ -41,6 +41,7 @@ file_handle_t *create_file_manager() { file_handle_t *file_handle; file_handle = (file_handle_t *)calloc(1, sizeof(file_handle_t)); + if (file_handle == NULL) return NULL; file_handle->filename = NULL; file_handle->file = NULL; From b860f9b16f095fee35ba7d50c1131164b0ae2702 Mon Sep 17 00:00:00 2001 From: 0tkl <118708188+0tkl@users.noreply.github.com> Date: Wed, 2 Aug 2023 14:42:48 +0800 Subject: [PATCH 3/4] add a default value to controlIndex --- src/hydraul.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/hydraul.c b/src/hydraul.c index d6e4424..93907ec 100755 --- a/src/hydraul.c +++ b/src/hydraul.c @@ -7,7 +7,7 @@ Authors: see AUTHORS Copyright: see AUTHORS License: see LICENSE - Last Updated: 08/13/2022 + Last Updated: 08/02/2023 ****************************************************************************** */ @@ -718,7 +718,7 @@ int controltimestep(Project *pr, long *tstep) Network *net = &pr->network; Hydraul *hyd = &pr->hydraul; - int i, j, k, n, controlIndex; + int i, j, k, n, controlIndex = 0; double h, q, v; long t, t1, t2; Slink *link; From 2f63e513f0bf6daf349bafbecc2a50127aa8d370 Mon Sep 17 00:00:00 2001 From: 0tkl <118708188+0tkl@users.noreply.github.com> Date: Wed, 2 Aug 2023 14:12:56 +0800 Subject: [PATCH 4/4] fix potential memory leaks Co-authored-by: Lew Rossman --- src/mempool.c | 8 ++++++-- src/outfile/src/epanet_output.c | 9 +++++++-- src/project.c | 32 +++++++++++++++++++++----------- 3 files changed, 34 insertions(+), 15 deletions(-) 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)