| 305 | } |
| 306 | |
| 307 | bool ParseUInt32(const std::string& str, uint32_t *out) |
| 308 | { |
| 309 | if (!ParsePrechecks(str)) |
| 310 | return false; |
| 311 | if (str.size() >= 1 && str[0] == '-') // Reject negative values, unfortunately strtoul accepts these by default if they fit in the range |
| 312 | return false; |
| 313 | char *endp = nullptr; |
| 314 | errno = 0; // strtoul will not set errno if valid |
| 315 | unsigned long int n = strtoul(str.c_str(), &endp, 10); |
| 316 | if(out) *out = (uint32_t)n; |
| 317 | // Note that strtoul returns a *unsigned long int*, so even if it doesn't report an over/underflow |
| 318 | // we still have to check that the returned value is within the range of an *uint32_t*. On 64-bit |
| 319 | // platforms the size of these types may be different. |
| 320 | return endp && *endp == 0 && !errno && |
| 321 | n <= std::numeric_limits<uint32_t>::max(); |
| 322 | } |
| 323 | |
| 324 | bool ParseUInt64(const std::string& str, uint64_t *out) |
| 325 | { |