]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
macro: paranoia about overflow
authorYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 18 Oct 2023 06:46:34 +0000 (15:46 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 19 Oct 2023 09:31:44 +0000 (18:31 +0900)
E.g. Consider the case ALIGN_TO(SIZE_MAX - 3, 4). The overflow check
passes as the condition
```
SIZE_MAX - 3 > SIZE_MAX - (4 - 1)
```
is false.
However, the value
```
l + ali - 1
```
may overflow as it is equivalent to
```
SIZE_MAX - 3 + 4 - 1
```

src/fundamental/macro-fundamental.h

index fa9aeafb98ec28be38e47ed620fab904a96bbf78..a311b01e30c4248e7f2d868dd43b5dfac880ce54 100644 (file)
@@ -376,7 +376,7 @@ static inline size_t ALIGN_TO(size_t l, size_t ali) {
         if (l > SIZE_MAX - (ali - 1))
                 return SIZE_MAX; /* indicate overflow */
 
-        return ((l + ali - 1) & ~(ali - 1));
+        return ((l + (ali - 1)) & ~(ali - 1));
 }
 
 #define ALIGN2(l) ALIGN_TO(l, 2)