From: Charles-Henri Bruyand Date: Fri, 8 Jun 2018 14:34:26 +0000 (+0200) Subject: check argument lists emptyness X-Git-Tag: auth-4.2.0-alpha1~19^2~5 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6d7e1fd302a87715e99afba8c77859f0ef6aba04;p=thirdparty%2Fpdns.git check argument lists emptyness --- diff --git a/pdns/lua-record.cc b/pdns/lua-record.cc index 07aecc0bbd..60374202aa 100644 --- a/pdns/lua-record.cc +++ b/pdns/lua-record.cc @@ -255,11 +255,17 @@ std::string getGeo(const std::string& ip, GeoIPInterface::GeoIPQueryAttribute qa static ComboAddress pickrandom(const vector& ips) { + if (ips.empty()) { + throw std::invalid_argument("The IP list cannot be empty"); + } return ips[random() % ips.size()]; } static ComboAddress hashed(const ComboAddress& who, const vector& ips) { + if (ips.empty()) { + throw std::invalid_argument("The IP list cannot be empty"); + } ComboAddress::addressOnlyHash aoh; return ips[aoh(who) % ips.size()]; } @@ -267,6 +273,9 @@ static ComboAddress hashed(const ComboAddress& who, const vector& static ComboAddress pickwrandom(const vector >& wips) { + if (wips.empty()) { + throw std::invalid_argument("The IP list cannot be empty"); + } int sum=0; vector > pick; for(auto& i : wips) { @@ -280,6 +289,9 @@ static ComboAddress pickwrandom(const vector >& wips) static ComboAddress pickwhashed(const ComboAddress& bestwho, vector >& wips) { + if (wips.empty()) { + return ComboAddress(); + } int sum=0; vector > pick; for(auto& i : wips) { @@ -356,11 +368,15 @@ static bool getLatLon(const std::string& ip, string& loc) static ComboAddress pickclosest(const ComboAddress& bestwho, const vector& wips) { + if (wips.empty()) { + throw std::invalid_argument("The IP list cannot be empty"); + } map > ranked; double wlat=0, wlon=0; getLatLon(bestwho.toString(), wlat, wlon); // cout<<"bestwho "< ret; + g_log< candidates; for(auto l : query.getRawLabels()) { boost::replace_all(l, "-", "."); try { candidates.emplace_back(l); - } - catch(...) { - break; + } catch (const PDNSException& e) { + // we want the reason to be reported by the lua wrapper + throw std::invalid_argument(e.reason); } } - return pickclosest(bestwho, candidates).toString(); }); @@ -718,7 +736,7 @@ std::vector> luaSynth(const std::string& code, cons * @example pickrandom({ '1.2.3.4', '5.4.3.2' })" */ lua.writeFunction("pickrandom", [](const iplist_t& ips) { - vector conv = convIplist(ips); + vector conv = convIplist(ips); return pickrandom(conv).toString(); });