Skip to content

Commit

Permalink
common/strtol.cc: Get error testing also to work on FreeBSD
Browse files Browse the repository at this point in the history
 - And report the same error types.
 - Changed to report for the last error since the value is there but
   not allowed characters follow.

Error found by: run-cli-tests, because the wrong string was returned.

Signed-off-by: Willem Jan Withagen <wjw@digiware.nl>
  • Loading branch information
wjwithagen committed Nov 17, 2016
1 parent eb8b4c8 commit 1b1485f
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/common/strtol.cc
Expand Up @@ -28,6 +28,17 @@ long long strict_strtoll(const char *str, int base, std::string *err)
errno = 0; /* To distinguish success/failure after call (see man page) */
long long ret = strtoll(str, &endptr, base);

if (endptr == str
#if defined(FREEBSD)
|| (ret == 0 && errno == EINVAL)
#endif
) {
errStr = "Expected option value to be integer, got '";
errStr.append(str);
errStr.append("'");
*err = errStr;
return 0;
}
if ((errno == ERANGE && (ret == LLONG_MAX || ret == LLONG_MIN))
|| (errno != 0 && ret == 0)) {
errStr = "The option value '";
Expand All @@ -37,18 +48,11 @@ long long strict_strtoll(const char *str, int base, std::string *err)
*err = errStr;
return 0;
}
if (endptr == str) {
errStr = "Expected option value to be integer, got '";
errStr.append(str);
errStr.append("'");
*err = errStr;
return 0;
}
if (*endptr != '\0') {
errStr = "The option value '";
errStr.append(str);
errStr.append("'");
errStr.append(" seems to be invalid");
errStr.append(" contains invalid digits");
*err = errStr;
return 0;
}
Expand Down

0 comments on commit 1b1485f

Please sign in to comment.