Several fixlets for Coverity (#36791)

This commit is contained in:
Lennart Poettering
2025-03-19 22:27:00 +01:00
committed by GitHub
11 changed files with 51 additions and 50 deletions
+1
View File
@@ -7,6 +7,7 @@
#define _INDEX_TO_MASK(type, i, uniq) \
({ \
int UNIQ_T(_i, uniq) = (i); \
assert(UNIQ_T(_i, uniq) >= 0); \
assert(UNIQ_T(_i, uniq) < (int)sizeof(type) * 8); \
((type)1) << UNIQ_T(_i, uniq); \
})
+1
View File
@@ -86,6 +86,7 @@ ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll) {
return n;
assert((size_t) k <= nbytes);
assert(k <= SSIZE_MAX - n);
p += k;
nbytes -= k;
+6 -6
View File
@@ -1814,19 +1814,19 @@ static bool is_sd_boot(EFI_FILE *root_dir, const char16_t *loader_path) {
if (err != EFI_SUCCESS)
return false;
PeSectionVector vector = {};
PeSectionVector vector[1] = {};
pe_locate_profile_sections(
section_table,
n_section_table,
section_names,
/* profile= */ UINT_MAX,
/* validate_base= */ 0,
&vector);
if (vector.memory_size != STRLEN(SD_MAGIC))
vector);
if (vector[0].memory_size != STRLEN(SD_MAGIC))
return false;
err = file_handle_read(handle, vector.file_offset, vector.file_size, &content, &read);
if (err != EFI_SUCCESS || vector.file_size != read)
err = file_handle_read(handle, vector[0].file_offset, vector[0].file_size, &content, &read);
if (err != EFI_SUCCESS || vector[0].file_size != read)
return false;
return memcmp(content, SD_MAGIC, STRLEN(SD_MAGIC)) == 0;
@@ -2093,7 +2093,7 @@ static void boot_entry_add_type2(
/* and now iterate through possible profiles, and create a menu item for each profile we find */
for (unsigned profile = 0; profile < UNIFIED_PROFILES_MAX; profile ++) {
PeSectionVector sections[_SECTION_MAX];
PeSectionVector sections[_SECTION_MAX] = {};
/* Start out with the base sections */
memcpy(sections, base_sections, sizeof(sections));
+11 -10
View File
@@ -349,21 +349,22 @@ static void pe_locate_sections(
if (!firmware_devicetree_exists()) {
/* Find HWIDs table and search for the current device */
PeSectionVector hwids_section = {};
static const char *const hwid_section_names[] = { ".hwids", NULL };
PeSectionVector hwids_section[1] = {};
pe_locate_sections_internal(
section_table,
n_section_table,
(const char *const[]) { ".hwids", NULL },
hwid_section_names,
validate_base,
/* device_table */ NULL,
/* device */ NULL,
&hwids_section);
hwids_section);
if (PE_SECTION_VECTOR_IS_SET(&hwids_section)) {
hwids = (const uint8_t *) SIZE_TO_PTR(validate_base) + hwids_section.memory_offset;
if (PE_SECTION_VECTOR_IS_SET(hwids_section)) {
hwids = (const uint8_t *) SIZE_TO_PTR(validate_base) + hwids_section[0].memory_offset;
EFI_STATUS err = chid_match(hwids, hwids_section.memory_size, DEVICE_TYPE_DEVICETREE, &device);
EFI_STATUS err = chid_match(hwids, hwids_section[0].memory_size, DEVICE_TYPE_DEVICETREE, &device);
if (err != EFI_SUCCESS) {
log_error_status(err, "HWID matching failed, no DT blob will be selected: %m");
hwids = NULL;
@@ -390,15 +391,15 @@ static uint32_t get_compatibility_entry_address(const DosFileHeader *dos, const
assert(pe);
static const char *const section_names[] = { ".compat", NULL };
PeSectionVector vector = {};
PeSectionVector vector[1] = {};
pe_locate_sections(
(const PeSectionHeader *) ((const uint8_t *) dos + section_table_offset(dos, pe)),
pe->FileHeader.NumberOfSections,
section_names,
PTR_TO_SIZE(dos),
&vector);
vector);
if (!PE_SECTION_VECTOR_IS_SET(&vector)) /* not found */
if (!PE_SECTION_VECTOR_IS_SET(vector)) /* not found */
return 0;
typedef struct {
@@ -408,7 +409,7 @@ static uint32_t get_compatibility_entry_address(const DosFileHeader *dos, const
uint32_t entry_point;
} _packed_ LinuxPeCompat1;
size_t addr = vector.memory_offset, size = vector.memory_size;
size_t addr = vector[0].memory_offset, size = vector[0].memory_size;
while (size >= sizeof(LinuxPeCompat1) && addr % alignof(LinuxPeCompat1) == 0) {
const LinuxPeCompat1 *compat = (const LinuxPeCompat1 *) ((const uint8_t *) dos + addr);
+1 -1
View File
@@ -119,7 +119,7 @@ static int server_determine_path_usage(
if (fstatvfs(dirfd(d), &ss) < 0)
return log_ratelimit_error_errno(errno, JOURNAL_LOG_RATELIMIT,
"Failed to fstatvfs(%s): %m", path);
"Failed to fstatvfs(%s): %m", path);
*ret_free = ss.f_bsize * ss.f_bavail;
*ret_used = 0;
+11 -12
View File
@@ -740,10 +740,9 @@ static void client_stop(sd_dhcp_client *client, int error) {
* for retransmission delays, timeout should start at 4s then double
* each attempt with max of 64s, with -1 to +1 sec of random 'fuzz' added.
* This assumes the first call will be using attempt 1. */
static usec_t client_compute_request_timeout(usec_t now, uint64_t attempt) {
static usec_t client_compute_request_timeout(uint64_t attempt) {
usec_t timeout = (UINT64_C(1) << MIN(attempt + 1, UINT64_C(6))) * USEC_PER_SEC;
return usec_sub_signed(usec_add(now, timeout), RFC2131_RANDOM_FUZZ);
return usec_sub_signed(timeout, RFC2131_RANDOM_FUZZ);
}
/* RFC2131 section 4.4.5:
@@ -758,8 +757,8 @@ static usec_t client_compute_request_timeout(usec_t now, uint64_t attempt) {
* of 60 seconds.
* Note that while the default T1/T2 initial times do have random 'fuzz' applied,
* the RFC sec 4.4.5 does not mention adding any fuzz to retries. */
static usec_t client_compute_reacquisition_timeout(usec_t now, usec_t expire) {
return now + MAX(usec_sub_unsigned(expire, now) / 2, 60 * USEC_PER_SEC);
static usec_t client_compute_reacquisition_timeout(usec_t now_usec, usec_t expire) {
return MAX(usec_sub_unsigned(expire, now_usec) / 2, 60 * USEC_PER_SEC);
}
static int cmp_uint8(const uint8_t *a, const uint8_t *b) {
@@ -1242,7 +1241,7 @@ static int client_timeout_resend(
goto error;
client->discover_attempt++;
next_timeout = client_compute_request_timeout(time_now, client->discover_attempt);
next_timeout = client_compute_request_timeout(client->discover_attempt);
break;
case DHCP_STATE_REQUESTING:
case DHCP_STATE_BOUND:
@@ -1250,7 +1249,7 @@ static int client_timeout_resend(
goto error;
client->request_attempt++;
next_timeout = client_compute_request_timeout(time_now, client->request_attempt);
next_timeout = client_compute_request_timeout(client->request_attempt);
break;
case DHCP_STATE_STOPPED:
@@ -1261,11 +1260,11 @@ static int client_timeout_resend(
assert_not_reached();
}
r = event_reset_time(client->event, &client->timeout_resend,
CLOCK_BOOTTIME,
next_timeout, 10 * USEC_PER_MSEC,
client_timeout_resend, client,
client->event_priority, "dhcp4-resend-timer", true);
r = event_reset_time_relative(
client->event, &client->timeout_resend,
CLOCK_BOOTTIME, next_timeout, 10 * USEC_PER_MSEC,
client_timeout_resend, client,
client->event_priority, "dhcp4-resend-timer", true);
if (r < 0)
goto error;
+2 -3
View File
@@ -173,11 +173,10 @@ HandleAction handle_action_sleep_select(Manager *m) {
assert(m);
FOREACH_ELEMENT(i, sleep_actions) {
HandleActionSleepMask action_mask = 1U << *i;
const HandleActionData *a;
_cleanup_free_ char *load_state = NULL;
if (!FLAGS_SET(m->handle_action_sleep_mask, action_mask))
if (!BIT_SET(m->handle_action_sleep_mask, *i))
continue;
a = ASSERT_PTR(handle_action_lookup(*i));
@@ -484,7 +483,7 @@ int config_parse_handle_action_sleep(
continue;
}
*mask |= 1U << a;
SET_BIT(*mask, a);
}
if (*mask == 0)
+2 -2
View File
@@ -700,8 +700,8 @@ static int oci_uid_gid_mappings(const char *name, sd_json_variant *v, sd_json_di
if (r < 0)
return r;
if (data.host_id + data.range < data.host_id ||
data.container_id + data.range < data.container_id)
if (data.range > UINT32_MAX - data.host_id ||
data.range > UINT32_MAX - data.container_id)
return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL),
"UID/GID range goes beyond UID/GID validity range, refusing.");
+5 -6
View File
@@ -153,23 +153,22 @@ static int dump_processes(
if (!hashmap_isempty(cg->pids)) {
const char *name;
size_t n = 0, i;
pid_t *pids;
void *pidp;
int width;
size_t n = 0;
/* Order processes by their PID */
pids = newa(pid_t, hashmap_size(cg->pids));
pid_t *pids = newa(pid_t, hashmap_size(cg->pids));
HASHMAP_FOREACH_KEY(name, pidp, cg->pids)
pids[n++] = PTR_TO_PID(pidp);
assert(n == hashmap_size(cg->pids));
assert(n > 0);
typesafe_qsort(pids, n, pid_compare_func);
width = DECIMAL_STR_WIDTH(pids[n-1]);
int width = DECIMAL_STR_WIDTH(pids[n-1]);
for (i = 0; i < n; i++) {
for (size_t i = 0; i < n; i++) {
_cleanup_free_ char *e = NULL;
const char *special;
bool more;
+8 -7
View File
@@ -458,14 +458,11 @@ static void parse_display_realtime(
const char *source_monotonic,
usec_t *ret) {
usec_t t, s, u;
usec_t t;
assert(j);
assert(ret);
// FIXME: _SOURCE_MONOTONIC_TIMESTAMP is in CLOCK_BOOTTIME, hence we cannot use it for adjusting realtime.
source_monotonic = NULL;
/* First, try _SOURCE_REALTIME_TIMESTAMP. */
if (source_realtime && safe_atou64(source_realtime, &t) >= 0 && VALID_REALTIME(t)) {
*ret = t;
@@ -479,11 +476,15 @@ static void parse_display_realtime(
}
/* If _SOURCE_MONOTONIC_TIMESTAMP is provided, adjust the header timestamp. */
// FIXME: _SOURCE_MONOTONIC_TIMESTAMP is in CLOCK_BOOTTIME, hence we cannot use it for adjusting realtime.
/*
usec_t s, u;
if (source_monotonic && safe_atou64(source_monotonic, &s) >= 0 && VALID_MONOTONIC(s) &&
sd_journal_get_monotonic_usec(j, &u, &(sd_id128_t) {}) >= 0) {
*ret = map_clock_usec_raw(t, u, s);
return;
}
*/
/* Otherwise, use the header timestamp as is. */
*ret = t;
@@ -504,14 +505,14 @@ static void parse_display_timestamp(
assert(ret_display_ts);
assert(ret_boot_id);
// FIXME: _SOURCE_MONOTONIC_TIMESTAMP is in CLOCK_BOOTTIME, hence we cannot use it for adjusting realtime.
source_monotonic = NULL;
if (source_realtime && safe_atou64(source_realtime, &t) >= 0 && VALID_REALTIME(t))
source_ts.realtime = t;
// FIXME: _SOURCE_MONOTONIC_TIMESTAMP is in CLOCK_BOOTTIME, hence we cannot use it for adjusting realtime.
/*
if (source_monotonic && safe_atou64(source_monotonic, &t) >= 0 && VALID_MONOTONIC(t))
source_ts.monotonic = t;
*/
(void) sd_journal_get_realtime_usec(j, &header_ts.realtime);
(void) sd_journal_get_monotonic_usec(j, &header_ts.monotonic, &boot_id);
+3 -3
View File
@@ -222,11 +222,11 @@ static RequestOperation look_for_escape(PTYForward *f, const char *buffer, size_
case 0x1D: { /* Check for ^] */
usec_t nw = now(CLOCK_MONOTONIC);
if (f->escape_counter == 0 || nw > f->escape_timestamp + ESCAPE_USEC) {
if (f->escape_counter == 0 || nw > usec_add(f->escape_timestamp, ESCAPE_USEC)) {
f->escape_timestamp = nw;
f->escape_counter = 1;
} else {
(f->escape_counter)++;
f->escape_counter++;
if (f->escape_counter >= 3)
return REQUEST_EXIT;
@@ -237,7 +237,7 @@ static RequestOperation look_for_escape(PTYForward *f, const char *buffer, size_
case 'a'...'z':
if (f->escape_counter == 2 &&
now(CLOCK_MONOTONIC) <= f->escape_timestamp + ESCAPE_USEC) {
now(CLOCK_MONOTONIC) <= usec_add(f->escape_timestamp, ESCAPE_USEC)) {
f->escape_timestamp = 0;
f->escape_counter = 0;
return REQUEST_HOTKEY_BASE + *p;