]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test: avoid NO_CAST.INTEGER_OVERFLOW in test-oomd-util (#30365)
authoraslepykh <145323274+aslepykh@users.noreply.github.com>
Fri, 8 Dec 2023 01:54:52 +0000 (04:54 +0300)
committerGitHub <noreply@github.com>
Fri, 8 Dec 2023 01:54:52 +0000 (10:54 +0900)
The  `.mem_total` variable has `uint64_t` type, therefore, when multiplying the number
`20971512` by the number `1024` with the suffix `U`, we will not get the expected result of
`21,474,828,288`, since the number `20971512` without an explicit type indication has
`uint32_t` type.

First, multiplication will occur in accordance with the `uint32_t` type; this operation will
cause a **type overflow**, and only then will this result be assigned to a `uint64_t` type
variable.

It's worth adding the `UL` suffix to the number `20971512` to avoid **overflow**.

Found by Linux Verification Center (portal.linuxtesting.ru) with SVACE.
Author A. Slepykh.

src/oom/test-oomd-util.c

index d76d91e26d5dfe22234e5006497e4fe75dd1bc60..1aef6039e12bbea5a90e599d09b4952e8fdd8c3b 100644 (file)
@@ -291,19 +291,19 @@ static void test_oomd_pressure_above(void) {
 
 static void test_oomd_mem_and_swap_free_below(void) {
         OomdSystemContext ctx = (OomdSystemContext) {
-                .mem_total = 20971512 * 1024U,
-                .mem_used = 3310136 * 1024U,
-                .swap_total = 20971512 * 1024U,
-                .swap_used = 20971440 * 1024U,
+                .mem_total = UINT64_C(20971512) * 1024U,
+                .mem_used = UINT64_C(3310136) * 1024U,
+                .swap_total = UINT64_C(20971512) * 1024U,
+                .swap_used = UINT64_C(20971440) * 1024U,
         };
         assert_se(oomd_mem_available_below(&ctx, 2000) == false);
         assert_se(oomd_swap_free_below(&ctx, 2000) == true);
 
         ctx = (OomdSystemContext) {
-                .mem_total = 20971512 * 1024U,
-                .mem_used = 20971440 * 1024U,
-                .swap_total = 20971512 * 1024U,
-                .swap_used = 3310136 * 1024U,
+                .mem_total = UINT64_C(20971512) * 1024U,
+                .mem_used = UINT64_C(20971440) * 1024U,
+                .swap_total = UINT64_C(20971512) * 1024U,
+                .swap_used = UINT64_C(3310136) * 1024U,
         };
         assert_se(oomd_mem_available_below(&ctx, 2000) == true);
         assert_se(oomd_swap_free_below(&ctx, 2000) == false);