From 072959386259d6db5f1a1323d86ad72a1227b282 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Mon, 21 Jun 2021 15:00:40 +0200 Subject: [PATCH] libmount: check errno after strto..() Addresses: https://github.com/karelzak/util-linux/issues/1356 Signed-off-by: Karel Zak --- libmount/python/pylibmount.c | 3 +++ libmount/src/context_veritydev.c | 3 ++- libmount/src/tab_parse.c | 6 ++++-- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/libmount/python/pylibmount.c b/libmount/python/pylibmount.c index 2e6bb62d7..bfc100fe1 100644 --- a/libmount/python/pylibmount.c +++ b/libmount/python/pylibmount.c @@ -220,9 +220,12 @@ PyMODINIT_FUNC initpylibmount(void) if (!(pylibmount_debug_mask & PYMNT_DEBUG_INIT)) { char *str = getenv("PYLIBMOUNT_DEBUG"); + errno = 0; pylibmount_debug_mask = 0; if (str) pylibmount_debug_mask = strtoul(str, NULL, 0); + if (errno) + pylibmount_debug_mask = 0; pylibmount_debug_mask |= PYMNT_DEBUG_INIT; } diff --git a/libmount/src/context_veritydev.c b/libmount/src/context_veritydev.c index e795c071c..2878d9489 100644 --- a/libmount/src/context_veritydev.c +++ b/libmount/src/context_veritydev.c @@ -61,8 +61,9 @@ static size_t crypt_hex_to_bytes(const char *hex, char **result) for (i = 0; i < len; i++) { memcpy(buf, &hex[i * 2], 2); + errno = 0; bytes[i] = strtoul(buf, &endp, 16); - if (endp != &buf[2]) { + if (errno || endp != &buf[2]) { free(bytes); return -EINVAL; } diff --git a/libmount/src/tab_parse.c b/libmount/src/tab_parse.c index 95585b5b7..917779ab6 100644 --- a/libmount/src/tab_parse.c +++ b/libmount/src/tab_parse.c @@ -50,11 +50,12 @@ static const char *next_s32(const char *s, int *num, int *rc) if (!s || !*s) return s; + errno = 0; *rc = -EINVAL; *num = strtol(s, &end, 10); if (end == NULL || s == end) return s; - if (*end == ' ' || *end == '\t' || *end == '\0') + if (errno == 0 && (*end == ' ' || *end == '\t' || *end == '\0')) *rc = 0; return end; } @@ -66,11 +67,12 @@ static const char *next_u64(const char *s, uint64_t *num, int *rc) if (!s || !*s) return s; + errno = 0; *rc = -EINVAL; *num = (uint64_t) strtoumax(s, &end, 10); if (end == NULL || s == end) return s; - if (*end == ' ' || *end == '\t' || *end == '\0') + if (errno == 0 && (*end == ' ' || *end == '\t' || *end == '\0')) *rc = 0; return end; }