diff --git a/src/epanet.c b/src/epanet.c index 8eb1ecf..d163d53 100644 --- a/src/epanet.c +++ b/src/epanet.c @@ -1234,7 +1234,7 @@ int DLLEXPORT EN_setoption(EN_Project p, int option, double value) if (demand->Pat == tmpPat) { demand->Pat = pat; - demand->Name = xstrcpy(&demand->Name, "", MAXMSG); + demand->Name = xstrcpy(&demand->Name, "", MAXID); } } } @@ -2758,7 +2758,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; } @@ -2781,11 +2783,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; } diff --git a/src/input1.c b/src/input1.c index fe606d9..e1cbcdf 100644 --- a/src/input1.c +++ b/src/input1.c @@ -334,7 +334,7 @@ void adjustdata(Project *pr) if (demand->Pat == 0) { demand->Pat = hyd->DefPat; - xstrcpy(&demand->Name, "", MAXMSG); + xstrcpy(&demand->Name, "", MAXID); } } } diff --git a/src/input3.c b/src/input3.c index fb35da2..2608f75 100644 --- a/src/input3.c +++ b/src/input3.c @@ -742,7 +742,7 @@ int demanddata(Project *pr) // with what is specified in this section demand->Base = y; demand->Pat = p; - demand->Name = xstrcpy(&demand->Name, parser->Comment, MAXMSG); + demand->Name = xstrcpy(&demand->Name, parser->Comment, MAXID); hyd->NodeDemand[j] = MISSING; // marker - next iteration will append a new category. } @@ -755,7 +755,7 @@ int demanddata(Project *pr) if (demand == NULL) return 101; demand->Base = y; demand->Pat = p; - demand->Name = xstrcpy(&demand->Name, parser->Comment, MAXMSG); + 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()