diff options
| author | Wladimir J. van der Laan <[email protected]> | 2014-05-03 10:20:58 +0200 |
|---|---|---|
| committer | Wladimir J. van der Laan <[email protected]> | 2014-05-09 16:45:56 +0200 |
| commit | 0d4ea1cf8a349cf59795ac68645afe70e98c6b3a (patch) | |
| tree | 10aaa665a70c821e0f9773c190e47fa330adcc11 /src/util.cpp | |
| parent | Fix transaction tests (diff) | |
| download | discoin-0d4ea1cf8a349cf59795ac68645afe70e98c6b3a.tar.xz discoin-0d4ea1cf8a349cf59795ac68645afe70e98c6b3a.zip | |
util: add parseint32 function with strict error reporting
None of the current integer parsing functions in util
check whether the result is valid and fits in the range
of the type. This is required for less sloppy error reporting.
Diffstat (limited to 'src/util.cpp')
| -rw-r--r-- | src/util.cpp | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/src/util.cpp b/src/util.cpp index a919b4b85..36ac23b1d 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -1427,3 +1427,17 @@ void RenameThread(const char* name) #endif } +bool ParseInt32(const std::string& str, int32_t *out) +{ + char *endp = NULL; + errno = 0; // strtol will not set errno if valid + long int n = strtol(str.c_str(), &endp, 10); + if(out) *out = (int)n; + // Note that strtol returns a *long int*, so even if strtol doesn't report a over/underflow + // we still have to check that the returned value is within the range of an *int32_t*. On 64-bit + // platforms the size of these types may be different. + return endp && *endp == 0 && !errno && + n >= std::numeric_limits<int32_t>::min() && + n <= std::numeric_limits<int32_t>::max(); +} + |