core: fix integer truncation checks

Use explicit bounds checks for size_t and long conversions
so compilers cannot optimize away round-trip cast checks.

Follow-up for 78238fd7c9.
This commit is contained in:
dongshengyuan
2026-08-19 02:28:19 +09:00
committed by Yu Watanabe
parent f969973b90
commit 31ef154f15
2 changed files with 3 additions and 3 deletions
+2 -2
View File
@@ -159,11 +159,11 @@ const sd_bus_vtable bus_socket_vtable[] = {
};
static bool check_size_t_truncation(uint64_t t) {
return (size_t) t == t;
return t <= SIZE_MAX;
}
static bool check_long_truncation(int64_t t) {
return (int64_t) (long) t == t;
return t <= LONG_MAX && t >= LONG_MIN;
}
static const char* socket_protocol_to_string(int32_t i) {
+1 -1
View File
@@ -992,7 +992,7 @@ int config_parse_iec_size(
assert(rvalue);
r = parse_size(rvalue, 1024, &v);
if (r >= 0 && (uint64_t) (size_t) v != v)
if (r >= 0 && v > SIZE_MAX)
r = -ERANGE;
if (r < 0)
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);