From e805cba507e8dfbb087b9d5a64a705363410ed6e Mon Sep 17 00:00:00 2001 From: Remi Gacogne Date: Thu, 10 Dec 2015 15:14:11 +0100 Subject: [PATCH] Fix strToUID() and strToGID() Reported by @cmouse --- pdns/misc.cc | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/pdns/misc.cc b/pdns/misc.cc index 25b2f5105..c60bc12f9 100644 --- a/pdns/misc.cc +++ b/pdns/misc.cc @@ -1245,15 +1245,20 @@ uid_t strToUID(const string &str) struct passwd * pwd = getpwnam(cstr); if (pwd == NULL) { - char * endptr = 0; - long int val = strtol(cstr, &endptr, 10); + long long val; - if (((val == LONG_MAX || val == LLONG_MIN) && errno == ERANGE) || endptr == cstr || val <= 0) { - throw runtime_error((boost::format("Warning: Unable to parse user ID %s") % cstr).str() ); + try { + val = stoll(str); } - else { - result = val; + catch(std::exception& e) { + throw runtime_error((boost::format("Error: Unable to parse user ID %s") % cstr).str() ); + } + + if (val < std::numeric_limits::min() || val > std::numeric_limits::max()) { + throw runtime_error((boost::format("Error: Unable to parse user ID %s") % cstr).str() ); } + + result = static_cast(val); } else { result = pwd->pw_uid; @@ -1269,15 +1274,20 @@ gid_t strToGID(const string &str) struct group * grp = getgrnam(cstr); if (grp == NULL) { - char * endptr = 0; - long int val = strtol(cstr, &endptr, 10); + long long val; - if (((val == LONG_MAX || val == LLONG_MIN) && errno == ERANGE) || endptr == cstr || val <= 0) { - throw runtime_error((boost::format("Warning: Unable to parse group ID %s") % cstr).str() ); + try { + val = stoll(str); } - else { - result = val; + catch(std::exception& e) { + throw runtime_error((boost::format("Error: Unable to parse group ID %s") % cstr).str() ); + } + + if (val < std::numeric_limits::min() || val > std::numeric_limits::max()) { + throw runtime_error((boost::format("Error: Unable to parse group ID %s") % cstr).str() ); } + + result = static_cast(val); } else { result = grp->gr_gid; -- 2.40.0