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 <signal.h>
#include <functional>
#include <limits>
#include <cstdio>
#ifndef HAVE_STRUCT_DIRENT_D_TYPE
@ -359,6 +359,8 @@ bool statusOk(int status);
/* Parse a string into an integer. */
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);
str >> n;
return str && str.get() == EOF;