diff options
author | M Stoeckl <code@mstoeckl.com> | 2019-01-21 12:46:45 -0500 |
---|---|---|
committer | M Stoeckl <code@mstoeckl.com> | 2019-01-21 12:46:45 -0500 |
commit | c040defd5f5cf5beed264dd057097625d9c0d423 (patch) | |
tree | 35c20f89a714ea5180f26bea18a55cfb979d5303 /common/util.c | |
parent | Move sway-specific functions in common/util.c into sway/ (diff) | |
download | sway-c040defd5f5cf5beed264dd057097625d9c0d423.tar.gz sway-c040defd5f5cf5beed264dd057097625d9c0d423.tar.zst sway-c040defd5f5cf5beed264dd057097625d9c0d423.zip |
Fix edge case bug in numlen, dropping use of math.h functions
(Specifically, numlen when called with INT_MIN gave an incorrect
result, because abs(INT_MIN) == INT_MIN < 0.)
Diffstat (limited to 'common/util.c')
-rw-r--r-- | common/util.c | 9 |
1 files changed, 5 insertions, 4 deletions
diff --git a/common/util.c b/common/util.c index 60f2f160..bd7bed2d 100644 --- a/common/util.c +++ b/common/util.c | |||
@@ -12,11 +12,12 @@ int wrap(int i, int max) { | |||
12 | } | 12 | } |
13 | 13 | ||
14 | int numlen(int n) { | 14 | int numlen(int n) { |
15 | if (n == 0) { | 15 | int j = n <= 0 ? 1 : 0; |
16 | return 1; | 16 | while (n) { |
17 | j++; | ||
18 | n /= 10; | ||
17 | } | 19 | } |
18 | // Account for the '-' in negative numbers. | 20 | return j; |
19 | return log10(abs(n)) + (n > 0 ? 1 : 2); | ||
20 | } | 21 | } |
21 | 22 | ||
22 | uint32_t parse_color(const char *color) { | 23 | uint32_t parse_color(const char *color) { |