string2Int: Barf on negative numbers for unsigned types

This commit is contained in:
Eelco Dolstra 2016-01-19 13:00:02 +01:00
parent 3baf8be1d1
commit 5b8c09c124

View file

@ -8,7 +8,7 @@
#include <unistd.h> #include <unistd.h>
#include <signal.h> #include <signal.h>
#include <functional> #include <functional>
#include <limits>
#include <cstdio> #include <cstdio>
#ifndef HAVE_STRUCT_DIRENT_D_TYPE #ifndef HAVE_STRUCT_DIRENT_D_TYPE
@ -359,6 +359,8 @@ bool statusOk(int status);
/* Parse a string into an integer. */ /* Parse a string into an integer. */
template<class N> bool string2Int(const string & s, N & n) template<class N> bool string2Int(const string & s, N & n)
{ {
if (string(s, 0, 1) == "-" && !std::numeric_limits<N>::is_signed)
return false;
std::istringstream str(s); std::istringstream str(s);
str >> n; str >> n;
return str && str.get() == EOF; return str && str.get() == EOF;