Some coverity cleanups (#41596)

This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2026-04-12 14:00:49 +02:00
committed by GitHub
5 changed files with 34 additions and 17 deletions
+14 -6
View File
@@ -171,7 +171,7 @@
#define U64_GB (UINT64_C(1024) * U64_MB)
#undef MAX
#define MAX(a, b) __MAX(UNIQ, (a), UNIQ, (b))
#define MAX(a, b) __MAX(UNIQ, a, UNIQ, b)
#define __MAX(aq, a, bq, b) \
({ \
const typeof(a) UNIQ_T(A, aq) = (a); \
@@ -234,7 +234,7 @@ assert_cc(sizeof(long long) == sizeof(intmax_t));
})
#undef MIN
#define MIN(a, b) __MIN(UNIQ, (a), UNIQ, (b))
#define MIN(a, b) __MIN(UNIQ, a, UNIQ, b)
#define __MIN(aq, a, bq, b) \
({ \
const typeof(a) UNIQ_T(A, aq) = (a); \
@@ -242,6 +242,14 @@ assert_cc(sizeof(long long) == sizeof(intmax_t));
UNIQ_T(A, aq) < UNIQ_T(B, bq) ? UNIQ_T(A, aq) : UNIQ_T(B, bq); \
})
#define ABS_DIFF(a, b) __ABS_DIFF(UNIQ, a, UNIQ, b)
#define __ABS_DIFF(aq, a, bq, b) \
({ \
const typeof(a) UNIQ_T(A, aq) = (a); \
const typeof(b) UNIQ_T(B, bq) = (b); \
UNIQ_T(A, aq) < UNIQ_T(B, bq) ? UNIQ_T(B, bq) - UNIQ_T(A, aq) : UNIQ_T(A, aq) - UNIQ_T(B, bq); \
})
/* evaluates to (void) if _A or _B are not constant or of different types */
#define CONST_MIN(_A, _B) \
(__builtin_choose_expr( \
@@ -312,7 +320,7 @@ assert_cc(sizeof(long long) == sizeof(intmax_t));
})
#undef CLAMP
#define CLAMP(x, low, high) __CLAMP(UNIQ, (x), UNIQ, (low), UNIQ, (high))
#define CLAMP(x, low, high) __CLAMP(UNIQ, x, UNIQ, low, UNIQ, high)
#define __CLAMP(xq, x, lowq, low, highq, high) \
({ \
const typeof(x) UNIQ_T(X, xq) = (x); \
@@ -329,7 +337,7 @@ assert_cc(sizeof(long long) == sizeof(intmax_t));
* computation should be possible in the given type. Therefore, we use
* [x / y + !!(x % y)]. Note that on "Real CPUs" a division returns both the
* quotient and the remainder, so both should be equally fast. */
#define DIV_ROUND_UP(x, y) __DIV_ROUND_UP(UNIQ, (x), UNIQ, (y))
#define DIV_ROUND_UP(x, y) __DIV_ROUND_UP(UNIQ, x, UNIQ, y)
#define __DIV_ROUND_UP(xq, x, yq, y) \
({ \
const typeof(x) UNIQ_T(X, xq) = (x); \
@@ -341,11 +349,11 @@ assert_cc(sizeof(long long) == sizeof(intmax_t));
#define __ROUND_UP(q, x, y) \
({ \
const typeof(y) UNIQ_T(A, q) = (y); \
const typeof(x) UNIQ_T(B, q) = DIV_ROUND_UP((x), UNIQ_T(A, q)); \
const typeof(x) UNIQ_T(B, q) = DIV_ROUND_UP(x, UNIQ_T(A, q)); \
typeof(x) UNIQ_T(C, q); \
MUL_SAFE(&UNIQ_T(C, q), UNIQ_T(B, q), UNIQ_T(A, q)) ? UNIQ_T(C, q) : (typeof(x)) -1; \
})
#define ROUND_UP(x, y) __ROUND_UP(UNIQ, (x), (y))
#define ROUND_UP(x, y) __ROUND_UP(UNIQ, x, y)
#define CASE_F_1(X) case X:
#define CASE_F_2(X, ...) case X: CASE_F_1( __VA_ARGS__)
+3 -3
View File
@@ -1889,10 +1889,10 @@ static int manager_rebalance_calculate(Manager *m) {
assert(h->rebalance_usage <= usage_sum);
assert(h->rebalance_weight <= weight_sum);
d = ((double) (free_sum / 4096.0) * (double) h->rebalance_weight) / (double) weight_sum; /* Calculate new space for this home in units of 4K */
d = free_sum / 4096.0 * h->rebalance_weight / weight_sum; /* Calculate new space for this home in units of 4K */
/* Convert from units of 4K back to bytes */
if (d >= (double) (UINT64_MAX/4096))
if (d >= UINT64_MAX / 4096)
new_free = UINT64_MAX;
else
new_free = (uint64_t) d * 4096;
@@ -1928,7 +1928,7 @@ static int manager_rebalance_calculate(Manager *m) {
h->rebalance_pending = true;
}
if ((fabs((double) h->rebalance_size - (double) h->rebalance_goal) * 100 / (double) h->rebalance_size) >= 5.0)
if (ABS_DIFF(h->rebalance_size, h->rebalance_goal) * 100.0 / h->rebalance_size >= 5.0)
relevant = true;
}
+7 -5
View File
@@ -2048,11 +2048,13 @@ static int event_add_pressure(
if (errno != ENOENT)
return -errno;
/* We got ENOENT. Three options now: try the fallback if we have one, or return the error as
* is (if based on user/env config), or return -EOPNOTSUPP (because we picked the path, and
* the PSI service apparently is not supported) */
if (!watch_fallback)
return locked ? -ENOENT : -EOPNOTSUPP;
/* We got ENOENT. Two options now: try the fallback if we have one, or return the error as is
* (when based on user/env config). */
if (!watch_fallback) {
assert(locked);
return -ENOENT;
}
path_fd = open(watch_fallback, O_PATH|O_CLOEXEC);
if (path_fd < 0) {
+7
View File
@@ -130,6 +130,13 @@ TEST(MAX) {
assert_se(CLAMP(CLAMP(0, -10, 10), CLAMP(-5, 10, 20), CLAMP(100, -5, 20)) == 10);
}
TEST(ABS_DIFF) {
ASSERT_EQ(ABS_DIFF(5, 3), 2);
ASSERT_EQ(ABS_DIFF(3, 5), 2);
ASSERT_EQ(ABS_DIFF(5llu, 2llu), 3llu);
ASSERT_EQ(ABS_DIFF(3llu, 5llu), 2llu);
}
#pragma GCC diagnostic push
#ifdef __clang__
# pragma GCC diagnostic ignored "-Waddress-of-packed-member"
+3 -3
View File
@@ -700,8 +700,8 @@ static int varlink_call_and_upgrade(const char *url, const char *method, sd_json
_exit(EXIT_FAILURE);
}
r = exec_with_listen_fds(exec_cmdline, /* fds= */ NULL, /* n_fds= */ 0);
/* This is only reached on failure, otherwise we continue with exec_cmldine). */
(void) exec_with_listen_fds(exec_cmdline, /* fds= */ NULL, /* n_fds= */ 0);
/* This is only reached on failure, otherwise we continue with exec_cmdline). */
_exit(EXIT_FAILURE);
}
@@ -993,7 +993,7 @@ static int verb_call(int argc, char *argv[], uintptr_t _data, void *userdata) {
_exit(EXIT_FAILURE);
}
exec_with_listen_fds(exec_cmdline, fd_array, m);
(void) exec_with_listen_fds(exec_cmdline, fd_array, m);
/* This is only reached on failure, otherwise we continue with exec_cmdline. */
_exit(EXIT_FAILURE);
}