diff options
author | Karel Zak | 2016-02-22 09:50:57 +0100 |
---|---|---|
committer | Karel Zak | 2016-02-22 09:50:57 +0100 |
commit | 80d00db17ee74d1c650cdc53021244954b0f0afc (patch) | |
tree | 183f7d85f9f104ec62bbe5549eda3f2db70f5f5c | |
parent | ipcs: show gid instead of uid (diff) | |
parent | tests: add logger tests "id _numbers_ with spaces" (diff) | |
download | kernel-qcow2-util-linux-80d00db17ee74d1c650cdc53021244954b0f0afc.tar.gz kernel-qcow2-util-linux-80d00db17ee74d1c650cdc53021244954b0f0afc.tar.xz kernel-qcow2-util-linux-80d00db17ee74d1c650cdc53021244954b0f0afc.zip |
Merge branch 'errno-strutils' of https://github.com/rudimeier/util-linux
-rw-r--r-- | lib/strutils.c | 55 | ||||
-rw-r--r-- | tests/expected/logger/errors-check_socket | 7 | ||||
-rw-r--r-- | tests/expected/logger/errors-id_with_space | 6 | ||||
-rwxr-xr-x | tests/ts/logger/errors | 4 |
4 files changed, 48 insertions, 24 deletions
diff --git a/lib/strutils.c b/lib/strutils.c index ef81ebd74..64a6b992e 100644 --- a/lib/strutils.c +++ b/lib/strutils.c @@ -91,7 +91,7 @@ int parse_size(const char *str, uintmax_t *res, int *power) if (p == str || (errno != 0 && (x == UINTMAX_MAX || x == 0))) { - rc = errno ? -errno : -1; + rc = errno ? -errno : -EINVAL; goto err; } if (!p || !*p) @@ -119,7 +119,7 @@ check_suffix: frac = strtoumax(fstr, &p, 0); if (p == fstr || (errno != 0 && (frac == UINTMAX_MAX || frac == 0))) { - rc = errno ? -errno : -1; + rc = errno ? -errno : -EINVAL; goto err; } if (frac && (!p || !*p)) { @@ -164,6 +164,8 @@ check_suffix: done: *res = x; err: + if (rc < 0) + errno = -rc; return rc; } @@ -257,9 +259,10 @@ int16_t strtos16_or_err(const char *str, const char *errmesg) { int32_t num = strtos32_or_err(str, errmesg); - if (num < INT16_MIN || num > INT16_MAX) - errx(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str); - + if (num < INT16_MIN || num > INT16_MAX) { + errno = ERANGE; + err(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str); + } return num; } @@ -267,9 +270,10 @@ uint16_t strtou16_or_err(const char *str, const char *errmesg) { uint32_t num = strtou32_or_err(str, errmesg); - if (num > UINT16_MAX) - errx(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str); - + if (num > UINT16_MAX) { + errno = ERANGE; + err(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str); + } return num; } @@ -277,9 +281,10 @@ int32_t strtos32_or_err(const char *str, const char *errmesg) { int64_t num = strtos64_or_err(str, errmesg); - if (num < INT32_MIN || num > INT32_MAX) - errx(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str); - + if (num < INT32_MIN || num > INT32_MAX) { + errno = ERANGE; + err(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str); + } return num; } @@ -287,9 +292,10 @@ uint32_t strtou32_or_err(const char *str, const char *errmesg) { uint64_t num = strtou64_or_err(str, errmesg); - if (num > UINT32_MAX) - errx(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str); - + if (num > UINT32_MAX) { + errno = ERANGE; + err(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str); + } return num; } @@ -298,9 +304,9 @@ int64_t strtos64_or_err(const char *str, const char *errmesg) int64_t num; char *end = NULL; + errno = 0; if (str == NULL || *str == '\0') goto err; - errno = 0; num = strtoimax(str, &end, 10); if (errno || str == end || (end && *end)) @@ -308,7 +314,7 @@ int64_t strtos64_or_err(const char *str, const char *errmesg) return num; err: - if (errno) + if (errno == ERANGE) err(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str); errx(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str); @@ -319,9 +325,9 @@ uint64_t strtou64_or_err(const char *str, const char *errmesg) uintmax_t num; char *end = NULL; + errno = 0; if (str == NULL || *str == '\0') goto err; - errno = 0; num = strtoumax(str, &end, 10); if (errno || str == end || (end && *end)) @@ -329,7 +335,7 @@ uint64_t strtou64_or_err(const char *str, const char *errmesg) return num; err: - if (errno) + if (errno == ERANGE) err(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str); errx(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str); @@ -341,9 +347,9 @@ double strtod_or_err(const char *str, const char *errmesg) double num; char *end = NULL; + errno = 0; if (str == NULL || *str == '\0') goto err; - errno = 0; num = strtod(str, &end); if (errno || str == end || (end && *end)) @@ -351,7 +357,7 @@ double strtod_or_err(const char *str, const char *errmesg) return num; err: - if (errno) + if (errno == ERANGE) err(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str); errx(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str); @@ -362,9 +368,9 @@ long strtol_or_err(const char *str, const char *errmesg) long num; char *end = NULL; + errno = 0; if (str == NULL || *str == '\0') goto err; - errno = 0; num = strtol(str, &end, 10); if (errno || str == end || (end && *end)) @@ -372,8 +378,9 @@ long strtol_or_err(const char *str, const char *errmesg) return num; err: - if (errno) + if (errno == ERANGE) err(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str); + errx(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str); } @@ -382,9 +389,9 @@ unsigned long strtoul_or_err(const char *str, const char *errmesg) unsigned long num; char *end = NULL; + errno = 0; if (str == NULL || *str == '\0') goto err; - errno = 0; num = strtoul(str, &end, 10); if (errno || str == end || (end && *end)) @@ -392,7 +399,7 @@ unsigned long strtoul_or_err(const char *str, const char *errmesg) return num; err: - if (errno) + if (errno == ERANGE) err(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str); errx(STRTOXX_EXIT_CODE, "%s: '%s'", errmesg, str); diff --git a/tests/expected/logger/errors-check_socket b/tests/expected/logger/errors-check_socket index 79a47f49d..bf0283732 100644 --- a/tests/expected/logger/errors-check_socket +++ b/tests/expected/logger/errors-check_socket @@ -14,6 +14,13 @@ socket data, id_with_space: socket data, id_with_space: +socket data, id_with_space: + +socket data, id_with_space: +<13>Feb 13 23:31:30 id_with_leading space[123]: message + +socket data, id_with_space: + socket data, tag_with_space: <13>Feb 13 23:31:30 A B: tag_with_space diff --git a/tests/expected/logger/errors-id_with_space b/tests/expected/logger/errors-id_with_space index 79bd05024..0f3bc4576 100644 --- a/tests/expected/logger/errors-id_with_space +++ b/tests/expected/logger/errors-id_with_space @@ -2,3 +2,9 @@ test_logger: failed to parse id: 'A B' ret: 1 test_logger: failed to parse id: 'A B' ret: 1 +test_logger: failed to parse id: '1 23' +ret: 1 +<13>Feb 13 23:31:30 id_with_leading space[123]: message +ret: 0 +test_logger: failed to parse id: '123 ' +ret: 1 diff --git a/tests/ts/logger/errors b/tests/ts/logger/errors index c0fab946f..33c10578d 100755 --- a/tests/ts/logger/errors +++ b/tests/ts/logger/errors @@ -74,6 +74,10 @@ ts_finalize_subtest ts_init_subtest "id_with_space" logger_fun -t "id_with_space" --id="A B" "message" logger_fun -t "rfc5424_id_with_space" --rfc5424 --id="A B" "message" +logger_fun -t "id_with_space" --id="1 23" "message" +logger_fun -t "id_with_leading space" --id=" 123" "message" +logger_fun -t "id_with_trailing space" --id="123 " "message" + ts_finalize_subtest # should not fail |