diff --git a/src/epanet.c b/src/epanet.c index 49ee7b7..d1bb49c 100644 --- a/src/epanet.c +++ b/src/epanet.c @@ -2021,7 +2021,7 @@ int DLLEXPORT EN_setnodeid(EN_Project p, int index, char *newid) if (index <= 0 || index > net->Nnodes) return 203; n = strlen(newid); if (n < 1 || n > MAXID) return 209; - if (strcspn(newid, " ;") < n) return 209; + if (!cstr_isvalid(newid)) return 250; // Check if another node with same name exists if (hashtable_find(net->NodeHashTable, newid) > 0) return 215; @@ -3208,7 +3208,7 @@ int DLLEXPORT EN_setlinkid(EN_Project p, int index, char *newid) if (index <= 0 || index > net->Nlinks) return 204; n = strlen(newid); if (n < 1 || n > MAXID) return 211; - if (strcspn(newid, " ;") < n) return 211; + if (!cstr_isvalid(newid)) return 250; // Check if another link with same name exists if (hashtable_find(net->LinkHashTable, newid) > 0) return 215; @@ -4085,7 +4085,12 @@ int DLLEXPORT EN_setpatternid(EN_Project p, int index, char *id) if (!p->Openflag) return 102; if (index < 1 || index > p->network.Npats) return 205; + + // Check is id name contains invalid characters + if (!cstr_isvalid(id)) return 250; + if (strlen(id) > MAXID) return 250; + for (i = 1; i <= p->network.Npats; i++) { if (i != index && strcmp(id, p->network.Pattern[i].ID) == 0) return 215; @@ -4352,6 +4357,10 @@ int DLLEXPORT EN_setcurveid(EN_Project p, int index, char *id) if (!p->Openflag) return 102; if (index < 1 || index > p->network.Ncurves) return 205; + + // Check is id name contains invalid characters + if (!cstr_isvalid(id)) return 250; + if (strlen(id) > MAXID) return 250; for (i = 1; i <= p->network.Ncurves; i++) { diff --git a/tests/test_curve.cpp b/tests/test_curve.cpp index db59ad5..7472c4f 100644 --- a/tests/test_curve.cpp +++ b/tests/test_curve.cpp @@ -69,6 +69,8 @@ BOOST_FIXTURE_TEST_CASE(test_curve_comments, FixtureOpenClose) BOOST_FIXTURE_TEST_CASE(test_curve_id_isvalid, FixtureInitClose) { + int index; + error = EN_addcurve(ph, "C1"); BOOST_REQUIRE(error == 0); @@ -80,6 +82,10 @@ BOOST_FIXTURE_TEST_CASE(test_curve_id_isvalid, FixtureInitClose) error = EN_addcurve(ph, "C;2"); BOOST_REQUIRE(error == 250); + + EN_getcurveindex(ph, "C1", &index); + error = EN_setcurveid(ph, index, "C;2"); + BOOST_REQUIRE(error == 250); } diff --git a/tests/test_link.cpp b/tests/test_link.cpp index 51c1d1f..aef009e 100644 --- a/tests/test_link.cpp +++ b/tests/test_link.cpp @@ -55,8 +55,10 @@ BOOST_FIXTURE_TEST_CASE(test_adddelete_link, FixtureInitClose) } -BOOST_FIXTURE_TEST_CASE(test_link_isvalid, FixtureInitClose) +BOOST_FIXTURE_TEST_CASE(test_link_id_isvalid, FixtureInitClose) { + int index; + // Build a network EN_addnode(ph, (char *)"N1", EN_JUNCTION); EN_addnode(ph, (char *)"N2", EN_JUNCTION); @@ -73,6 +75,10 @@ BOOST_FIXTURE_TEST_CASE(test_link_isvalid, FixtureInitClose) error = EN_addlink(ph, (char *)"L;2", EN_PIPE, (char *)"N1", (char *)"N2"); BOOST_REQUIRE(error == 250); + + EN_getlinkindex(ph, "L1", &index); + error = EN_setlinkid(ph, index, "L;1"); + BOOST_REQUIRE(error == 250); } BOOST_AUTO_TEST_CASE(test_setlinktype) diff --git a/tests/test_node.cpp b/tests/test_node.cpp index d35062f..ebe22a9 100644 --- a/tests/test_node.cpp +++ b/tests/test_node.cpp @@ -49,17 +49,23 @@ BOOST_FIXTURE_TEST_CASE(test_adddelete_node, FixtureInitClose) BOOST_FIXTURE_TEST_CASE(test_node_validate_id, FixtureInitClose) { + int index; + error = EN_addnode(ph, (char *)"N2", EN_JUNCTION); BOOST_REQUIRE(error == 0); - error = EN_addnode(ph, (char *)"N 2", EN_JUNCTION); + error = EN_addnode(ph, (char *)"N 3", EN_JUNCTION); BOOST_REQUIRE(error == 250); - error = EN_addnode(ph, (char *)"N\"2", EN_JUNCTION); + error = EN_addnode(ph, (char *)"N\"3", EN_JUNCTION); BOOST_REQUIRE(error == 250); - error = EN_addnode(ph, (char *)"N;2", EN_JUNCTION); + error = EN_addnode(ph, (char *)"N;3", EN_JUNCTION); BOOST_REQUIRE(error == 250); + + EN_getnodeindex(ph, "N2", &index); + error = EN_setnodeid(ph, index, "N;2"); + BOOST_REQUIRE(error = 250); } diff --git a/tests/test_pattern.cpp b/tests/test_pattern.cpp index c2b3cc7..129d213 100644 --- a/tests/test_pattern.cpp +++ b/tests/test_pattern.cpp @@ -147,8 +147,10 @@ BOOST_FIXTURE_TEST_CASE(test_pattern_comments, FixtureOpenClose) BOOST_CHECK(check_string(comment, (char *)"Time Pattern 1")); } -BOOST_FIXTURE_TEST_CASE(test_pat_isvalid_id, FixtureInitClose) +BOOST_FIXTURE_TEST_CASE(test_pat_id_isvalid, FixtureInitClose) { + int index; + error = EN_addpattern(ph, "P1"); BOOST_REQUIRE(error == 0); @@ -160,6 +162,11 @@ BOOST_FIXTURE_TEST_CASE(test_pat_isvalid_id, FixtureInitClose) error = EN_addpattern(ph, "P;2"); BOOST_REQUIRE(error == 250); + + EN_getpatternindex(ph, "P1", &index); + error = EN_setpatternid(ph, index, "P;1"); + BOOST_REQUIRE(error == 250); + }