Skip to content

Commit 0d968ae

Browse files
Chris Nourseclaude
authored andcommitted
Fix NULL pointer dereference in STM32 SPI construct
Move mark_deinit() before check_pins() so that self->sck is not NULLed after check_pins sets it. The previous ordering caused a NULL dereference of self->sck->altfn_index, crashing all STM32 boards that use SPI (including STM32F405 boards whose CIRCUITPY filesystem lives on external SPI flash). Fixes #10866 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 4a10c22 commit 0d968ae

File tree

1 file changed

+4
-3
lines changed
  • ports/stm/common-hal/busio

1 file changed

+4
-3
lines changed

ports/stm/common-hal/busio/SPI.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,13 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self,
136136
const mcu_pin_obj_t *sck, const mcu_pin_obj_t *mosi,
137137
const mcu_pin_obj_t *miso, bool half_duplex) {
138138

139+
// Ensure the object starts in its deinit state before check_pins sets
140+
// self->sck, self->mosi, and self->miso.
141+
common_hal_busio_spi_mark_deinit(self);
142+
139143
int periph_index = check_pins(self, sck, mosi, miso);
140144
SPI_TypeDef *SPIx = mcu_spi_banks[periph_index - 1];
141145

142-
// Ensure the object starts in its deinit state.
143-
common_hal_busio_spi_mark_deinit(self);
144-
145146
// Start GPIO for each pin
146147
GPIO_InitTypeDef GPIO_InitStruct = {0};
147148
GPIO_InitStruct.Pin = pin_mask(sck->number);

0 commit comments

Comments
 (0)