summaryrefslogtreecommitdiff
path: root/src/basic/macro.h
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2022-03-14 12:01:47 +0100
committerLennart Poettering <lennart@poettering.net>2022-03-14 18:10:49 +0100
commit92463840f804be31332fba65b60c1533500f0974 (patch)
tree44908894eed431f558bdb112cf05b190d27b57ed /src/basic/macro.h
parent14a8002ae5f4e4e8987228dc2739ffabc2e036ec (diff)
downloadsystemd-92463840f804be31332fba65b60c1533500f0974.tar.gz
macro: DECIMAL_STR_WIDTH() is about *values* not *types*
Hence, check if the value is negative, not whether the type can carry negatives. Follow-up for: e3dd9ea8ea4510221f73071ad30ee657ca77565d
Diffstat (limited to 'src/basic/macro.h')
-rw-r--r--src/basic/macro.h16
1 files changed, 13 insertions, 3 deletions
diff --git a/src/basic/macro.h b/src/basic/macro.h
index 9e62f9c71c..214654386c 100644
--- a/src/basic/macro.h
+++ b/src/basic/macro.h
@@ -319,12 +319,22 @@ static inline int __coverity_check_and_return__(int condition) {
sizeof(type) <= 4 ? 10U : \
sizeof(type) <= 8 ? 20U : sizeof(int[-2*(sizeof(type) > 8)])))
+/* Returns the number of chars needed to format the specified integer value. It's hence more specific than
+ * DECIMAL_STR_MAX() which answers the same question for all possible values of the specified type. Does
+ * *not* include space for a trailing NUL. (If you wonder why we special case _x_ == 0 here: it's to trick
+ * out gcc's -Wtype-limits, which would complain on comparing an unsigned type with < 0, otherwise. By
+ * special-casing == 0 here first, we can use <= 0 instead of < 0 to trick out gcc.) */
#define DECIMAL_STR_WIDTH(x) \
({ \
typeof(x) _x_ = (x); \
- size_t ans = IS_SIGNED_INTEGER_TYPE(_x_) ? 2 : 1; \
- while ((_x_ /= 10) != 0) \
- ans++; \
+ size_t ans; \
+ if (_x_ == 0) \
+ ans = 1; \
+ else { \
+ ans = _x_ <= 0 ? 2 : 1; \
+ while ((_x_ /= 10) != 0) \
+ ans++; \
+ } \
ans; \
})