summaryrefslogtreecommitdiff
path: root/src/basic
diff options
context:
space:
mode:
authorFrantisek Sumsal <frantisek@sumsal.cz>2022-03-13 14:45:03 +0100
committerFrantisek Sumsal <frantisek@sumsal.cz>2022-03-13 19:25:18 +0100
commite3dd9ea8ea4510221f73071ad30ee657ca77565d (patch)
treefa14ad599723c42b8f1eb86ec69ee9079455649d /src/basic
parent34c4dff4d2e592e90dd9833fee4ce1ac981e8ea9 (diff)
downloadsystemd-e3dd9ea8ea4510221f73071ad30ee657ca77565d.tar.gz
macro: account for negative values in DECIMAL_STR_WIDTH()
With negative numbers we wouldn't account for the minus sign, thus returning a string with one character too short, triggering buffer overflows in certain situations.
Diffstat (limited to 'src/basic')
-rw-r--r--src/basic/macro.h14
1 files changed, 7 insertions, 7 deletions
diff --git a/src/basic/macro.h b/src/basic/macro.h
index aa04039e80..9e62f9c71c 100644
--- a/src/basic/macro.h
+++ b/src/basic/macro.h
@@ -319,13 +319,13 @@ static inline int __coverity_check_and_return__(int condition) {
sizeof(type) <= 4 ? 10U : \
sizeof(type) <= 8 ? 20U : sizeof(int[-2*(sizeof(type) > 8)])))
-#define DECIMAL_STR_WIDTH(x) \
- ({ \
- typeof(x) _x_ = (x); \
- size_t ans = 1; \
- while ((_x_ /= 10) != 0) \
- ans++; \
- ans; \
+#define DECIMAL_STR_WIDTH(x) \
+ ({ \
+ typeof(x) _x_ = (x); \
+ size_t ans = IS_SIGNED_INTEGER_TYPE(_x_) ? 2 : 1; \
+ while ((_x_ /= 10) != 0) \
+ ans++; \
+ ans; \
})
#define SWAP_TWO(x, y) do { \