diff --git a/src/epanet.c b/src/epanet.c index 55b1265..ac6476b 100644 --- a/src/epanet.c +++ b/src/epanet.c @@ -2812,7 +2812,9 @@ int DLLEXPORT EN_getdemandname(EN_Project p, int nodeIndex, int demandIndex, for (d = p->network.Node[nodeIndex].D; n < demandIndex && d->next != NULL; d = d->next) n++; if (n != demandIndex) return 253; - strcpy(demandName, d->Name); + + if (d->Name) strcpy(demandName, d->Name); + else demandName[0] = '\0'; return 0; } @@ -2835,11 +2837,14 @@ int DLLEXPORT EN_setdemandname(EN_Project p, int nodeIndex, int demandIndex, if (!p->Openflag) return 102; if (nodeIndex <= 0 || nodeIndex > p->network.Njuncs) return 203; + // Check that demandName is not too long + if (strlen(demandName) > MAXID) return 250; + // Locate demand category record and assign demandName to it for (d = p->network.Node[nodeIndex].D; n < demandIndex && d->next != NULL; d = d->next) n++; if (n != demandIndex) return 253; - d->Name = xstrcpy(&d->Name, demandName, MAXMSG); + d->Name = xstrcpy(&d->Name, demandName, MAXID); return 0; } @@ -4417,26 +4422,26 @@ int DLLEXPORT EN_setcurvevalue(EN_Project p, int curveIndex, int pointIndex, if (curveIndex <= 0 || curveIndex > net->Ncurves) return 206; curve = &net->Curve[curveIndex]; if (pointIndex <= 0) return 251; - + // Check that new point maintains increasing x values if (n - 1 >= 0) x1 = curve->X[n-1]; if (n + 1 < curve->Npts) x2 = curve->X[n+1]; if (x <= x1 || x >= x2) return 230; - + // Expand curve if need be if (pointIndex > curve->Npts) pointIndex = curve->Npts + 1; if (pointIndex >= curve->Capacity) { if (resizecurve(curve, curve->Capacity + 10) > 0) return 101; } - + // Increase curve's number of points if need be if (pointIndex > curve->Npts) { curve->Npts++; n = curve->Npts - 1; } - + // Insert new point into curve curve->X[n] = x; curve->Y[n] = y; diff --git a/src/input3.c b/src/input3.c index 2035d8d..61324a3 100644 --- a/src/input3.c +++ b/src/input3.c @@ -762,7 +762,7 @@ int demanddata(Project *pr) if (parser->Comment[0]) { demand->Name = xstrcpy(&demand->Name, parser->Comment, MAXID); - } + } demand->next = NULL; cur_demand->next = demand; } diff --git a/tests/test_demand.cpp b/tests/test_demand.cpp index 2955079..9a02ff7 100644 --- a/tests/test_demand.cpp +++ b/tests/test_demand.cpp @@ -20,7 +20,7 @@ BOOST_AUTO_TEST_SUITE (test_demand) BOOST_AUTO_TEST_CASE(test_categories_save) { - int error = 0; + int error = 0; int Nindex, ndem; EN_Project ph = NULL; @@ -34,15 +34,19 @@ BOOST_AUTO_TEST_CASE(test_categories_save) BOOST_REQUIRE(error == 0); BOOST_CHECK(ndem == 1); - error = EN_setdemandname(ph, Nindex, ndem, (char *)"Demand category name"); + char demname[31]; + error = EN_getdemandname(ph, Nindex, ndem, demname); + BOOST_REQUIRE(error == 0); + + error = EN_setdemandname(ph, Nindex, ndem, (char *)"Demand category name"); BOOST_REQUIRE(error == 0); error = EN_saveinpfile(ph, "net1_dem_cat.inp"); BOOST_REQUIRE(error == 0); error = EN_close(ph); - BOOST_REQUIRE(error == 0); - error = EN_deleteproject(&ph); - BOOST_REQUIRE(error == 0); + BOOST_REQUIRE(error == 0); + error = EN_deleteproject(&ph); + BOOST_REQUIRE(error == 0); } BOOST_AUTO_TEST_CASE(test_categories_reopen, * boost::unit_test::depends_on("test_demand/test_categories_save")) @@ -52,11 +56,11 @@ BOOST_AUTO_TEST_CASE(test_categories_reopen, * boost::unit_test::depends_on("tes EN_Project ph = NULL; - BOOST_TEST_CHECKPOINT("Reopening saved input file"); + BOOST_TEST_CHECKPOINT("Reopening saved input file"); error = EN_createproject(&ph); - BOOST_REQUIRE(error == 0); - error = EN_open(ph, "net1_dem_cat.inp", DATA_PATH_RPT, DATA_PATH_OUT); - BOOST_REQUIRE(error == 0); + BOOST_REQUIRE(error == 0); + error = EN_open(ph, "net1_dem_cat.inp", DATA_PATH_RPT, DATA_PATH_OUT); + BOOST_REQUIRE(error == 0); error = EN_getnodeindex(ph, (char *)"12", &Nindex); BOOST_REQUIRE(error == 0); @@ -64,16 +68,16 @@ BOOST_AUTO_TEST_CASE(test_categories_reopen, * boost::unit_test::depends_on("tes BOOST_REQUIRE(error == 0); BOOST_CHECK(ndem == 1); - char demname[80]; - error = EN_getdemandname(ph, Nindex, ndem, demname); + char demname[31]; + error = EN_getdemandname(ph, Nindex, ndem, demname); BOOST_REQUIRE(error == 0); BOOST_CHECK(check_string(demname, "Demand category name")); - error = EN_close(ph); - BOOST_REQUIRE(error == 0); - error = EN_deleteproject(&ph); - BOOST_REQUIRE(error == 0); + error = EN_close(ph); + BOOST_REQUIRE(error == 0); + error = EN_deleteproject(&ph); + BOOST_REQUIRE(error == 0); } BOOST_AUTO_TEST_SUITE_END()