diff --git a/src/utils/inetAddressUtil.cpp b/src/utils/inetAddressUtil.cpp index 0e24fc0..768a0d6 100644 --- a/src/utils/inetAddressUtil.cpp +++ b/src/utils/inetAddressUtil.cpp @@ -146,8 +146,13 @@ InetAddrVector* getSocketAddressList(const std::string & list, int defaultPort, const InetAddrVector* appendList) { InetAddrVector* iav = new InetAddrVector(); - // parse string + // skip leading spaces + size_t len = list.length(); size_t subStart = 0; + while (subStart < len && isspace(list[subStart])) + subStart++; + + // parse string size_t subEnd; while((subEnd = list.find(' ', subStart))!=std::string::npos) { string address = list.substr(subStart, (subEnd-subStart)); @@ -157,7 +162,7 @@ InetAddrVector* getSocketAddressList(const std::string & list, int defaultPort, subStart = list.find_first_not_of(" \t\r\n\v", subEnd); } - if(subStart!=std::string::npos&&list.length()>0) { + if(subStart!=std::string::npos && subStartpush_back(addr); diff --git a/testApp/utils/testInetAddressUtils.cpp b/testApp/utils/testInetAddressUtils.cpp index 30a729c..1ffa24d 100644 --- a/testApp/utils/testInetAddressUtils.cpp +++ b/testApp/utils/testInetAddressUtils.cpp @@ -74,7 +74,38 @@ void test_getSocketAddressList() testOk1(htons(555) == addr.ia.sin_port); testOk1(htonl(0xC0A80304) == addr.ia.sin_addr.s_addr); testOk1("192.168.3.4:555" == inetAddressToString(addr)); - + + + // empty + auto_ptr vec2(getSocketAddressList("", 1111)); + testOk1(static_cast(0) == vec2->size()); + + // just spaces + auto_ptr vec3(getSocketAddressList(" ", 1111)); + testOk1(static_cast(0) == vec3->size()); + + // leading spaces + auto_ptr vec4(getSocketAddressList(" 127.0.0.1 10.10.12.11:1234 192.168.3.4", 555)); + + testOk1(static_cast(3) == vec4->size()); + + addr = vec4->at(0); + testOk1(AF_INET == addr.ia.sin_family); + testOk1(htons(555) == addr.ia.sin_port); + testOk1(htonl(0x7F000001) == addr.ia.sin_addr.s_addr); + testOk1("127.0.0.1:555" == inetAddressToString(addr)); + + addr = vec4->at(1); + testOk1(AF_INET == addr.ia.sin_family); + testOk1(htons(1234) == addr.ia.sin_port); + testOk1(htonl(0x0A0A0C0B) == addr.ia.sin_addr.s_addr); + testOk1("10.10.12.11:1234" == inetAddressToString(addr)); + + addr = vec4->at(2); + testOk1(AF_INET == addr.ia.sin_family); + testOk1(htons(555) == addr.ia.sin_port); + testOk1(htonl(0xC0A80304) == addr.ia.sin_addr.s_addr); + testOk1("192.168.3.4:555" == inetAddressToString(addr)); } void test_ipv4AddressToInt() @@ -329,7 +360,7 @@ void test_multicastLoopback() MAIN(testInetAddressUtils) { - testPlan(68); + testPlan(83); testDiag("Tests for InetAddress utils"); test_getSocketAddressList();