From 21e00db8713591bad39ce4dd3d41c0e55f666adf Mon Sep 17 00:00:00 2001 From: Luke Wren Date: Wed, 15 Oct 2025 13:04:56 +0100 Subject: [PATCH] Fix some errors from -fanalyzer. Includes a genuine bug in multicore_doorbell_claim() which seemed to use the wrong bit for the second core. --- src/common/pico_sync/mutex.c | 6 +++--- src/common/pico_time/time.c | 4 +++- src/rp2_common/pico_multicore/multicore.c | 2 +- src/rp2_common/pico_rand/rand.c | 2 +- 4 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/common/pico_sync/mutex.c b/src/common/pico_sync/mutex.c index 66e947687..3954da67c 100644 --- a/src/common/pico_sync/mutex.c +++ b/src/common/pico_sync/mutex.c @@ -18,10 +18,10 @@ void __weak runtime_init_mutex(void) { static_assert(!(sizeof(recursive_mutex_t)&3), ""); static_assert(!offsetof(mutex_t, core), ""); static_assert(!offsetof(recursive_mutex_t, core), ""); - extern lock_core_t __mutex_array_start; + extern lock_core_t __mutex_array_start[]; extern lock_core_t __mutex_array_end; - for (lock_core_t *l = &__mutex_array_start; l < &__mutex_array_end; ) { + for (lock_core_t *l = &__mutex_array_start[0]; l < &__mutex_array_end; ) { if (l->spin_lock) { assert(1 == (uintptr_t)l->spin_lock); // indicator for a recursive mutex recursive_mutex_t *rm = (recursive_mutex_t *)l; @@ -225,4 +225,4 @@ void __time_critical_func(recursive_mutex_exit)(recursive_mutex_t *mtx) { } else { spin_unlock(mtx->core.spin_lock, save); } -} \ No newline at end of file +} diff --git a/src/common/pico_time/time.c b/src/common/pico_time/time.c index 78ad26f07..811b513f3 100644 --- a/src/common/pico_time/time.c +++ b/src/common/pico_time/time.c @@ -112,6 +112,7 @@ alarm_pool_t *alarm_pool_create_on_timer(alarm_pool_timer_t *timer, uint hardwar alarm_pool_t *pool = (alarm_pool_t *) malloc(sizeof(alarm_pool_t)); if (pool) { pool->entries = (alarm_pool_entry_t *) calloc(max_timers, sizeof(alarm_pool_entry_t)); + if (!pool->entries) panic("Failed to allocate alarm pool entries"); ta_hardware_alarm_claim(timer, hardware_alarm_num); alarm_pool_post_alloc_init(pool, timer, hardware_alarm_num, max_timers); } @@ -122,6 +123,7 @@ alarm_pool_t *alarm_pool_create_on_timer_with_unused_hardware_alarm(alarm_pool_t alarm_pool_t *pool = (alarm_pool_t *) malloc(sizeof(alarm_pool_t)); if (pool) { pool->entries = (alarm_pool_entry_t *) calloc(max_timers, sizeof(alarm_pool_entry_t)); + if (!pool->entries) panic("Failed to allocate alarm pool entries"); alarm_pool_post_alloc_init(pool, timer, (uint) ta_hardware_alarm_claim_unused(timer, true), max_timers); } return pool; @@ -567,4 +569,4 @@ int64_t remaining_alarm_time_us(alarm_id_t alarm_id) { int32_t remaining_alarm_time_ms(alarm_id_t alarm_id) { return alarm_pool_remaining_alarm_time_ms(alarm_pool_get_default(), alarm_id); } -#endif \ No newline at end of file +#endif diff --git a/src/rp2_common/pico_multicore/multicore.c b/src/rp2_common/pico_multicore/multicore.c index e11595087..453d770b2 100644 --- a/src/rp2_common/pico_multicore/multicore.c +++ b/src/rp2_common/pico_multicore/multicore.c @@ -367,7 +367,7 @@ static inline void clear_claimed_bit(uint8_t *bits, uint bit_index) { static bool multicore_doorbell_claim_under_lock(uint doorbell_num, uint core_mask, bool required) { static_assert(NUM_CORES == 2, ""); uint claimed_cores_for_doorbell = (uint) (is_bit_claimed(doorbell_claimed[0], doorbell_num) | - (is_bit_claimed(doorbell_claimed[1], doorbell_num + 1u) << 1)); + (is_bit_claimed(doorbell_claimed[1], doorbell_num) << 1)); if (claimed_cores_for_doorbell & core_mask) { if (required) { panic( "Multicore doorbell %d already claimed on core mask 0x%x; requested core mask 0x%x\n", diff --git a/src/rp2_common/pico_rand/rand.c b/src/rp2_common/pico_rand/rand.c index 03b0e3d61..bda9bfa77 100644 --- a/src/rp2_common/pico_rand/rand.c +++ b/src/rp2_common/pico_rand/rand.c @@ -239,7 +239,7 @@ static uint64_t capture_additional_rosc_samples(uint n) { #endif static void initialise_rand(void) { - rng_128_t local_rng_state = local_rng_state; + rng_128_t local_rng_state = {0}; uint which = 0; #if PICO_RAND_SEED_ENTROPY_SRC_RAM_HASH ram_hash = sdbm_hash64_sram(ram_hash);