Skip to content

Commit 92fc041

Browse files
decsnykartben
authored andcommitted
spi_nxp_lpspi: Fix resource leak in transceive
There is a bug here clearly which is that if there is some error in the transceive function, it returns without releasing the context. This should be fixed by properly handling the errors with a context release before returning. Signed-off-by: Declan Snyder <[email protected]> (cherry picked from commit 4e1c4cd)
1 parent 74cd0a9 commit 92fc041

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

drivers/spi/spi_nxp_lpspi/spi_nxp_lpspi.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -238,30 +238,31 @@ static int transceive(const struct device *dev, const struct spi_config *spi_cfg
238238
LPSPI_Type *base = (LPSPI_Type *)DEVICE_MMIO_NAMED_GET(dev, reg_base);
239239
struct spi_mcux_data *data = dev->data;
240240
struct lpspi_driver_data *lpspi_data = (struct lpspi_driver_data *)data->driver_data;
241-
int ret;
241+
struct spi_context *ctx = &data->ctx;
242+
int ret = 0;
242243

243244
spi_context_lock(&data->ctx, asynchronous, cb, userdata, spi_cfg);
244245

245246
lpspi_data->word_size_bytes = SPI_WORD_SIZE_GET(spi_cfg->operation) / BITS_PER_BYTE;
246247
if (lpspi_data->word_size_bytes > 4) {
247248
LOG_ERR("Maximum 4 byte word size");
248249
ret = -EINVAL;
249-
return ret;
250+
goto error;
250251
}
251252

252-
spi_context_buffers_setup(&data->ctx, tx_bufs, rx_bufs, lpspi_data->word_size_bytes);
253+
spi_context_buffers_setup(ctx, tx_bufs, rx_bufs, lpspi_data->word_size_bytes);
253254

254255
ret = spi_mcux_configure(dev, spi_cfg);
255256
if (ret) {
256-
return ret;
257+
goto error;
257258
}
258259

259260
LPSPI_FlushFifo(base, true, true);
260261
LPSPI_ClearStatusFlags(base, (uint32_t)kLPSPI_AllStatusFlag);
261262
LPSPI_DisableInterrupts(base, (uint32_t)kLPSPI_AllInterruptEnable);
262263

263264
LOG_DBG("Starting LPSPI transfer");
264-
spi_context_cs_control(&data->ctx, true);
265+
spi_context_cs_control(ctx, true);
265266

266267
LPSPI_SetFifoWatermarks(base, 0, 0);
267268
LPSPI_Enable(base, true);
@@ -277,7 +278,11 @@ static int transceive(const struct device *dev, const struct spi_config *spi_cfg
277278
LPSPI_EnableInterrupts(base, (uint32_t)kLPSPI_TxInterruptEnable |
278279
(uint32_t)kLPSPI_RxInterruptEnable);
279280

280-
return spi_context_wait_for_completion(&data->ctx);
281+
return spi_context_wait_for_completion(ctx);
282+
283+
error:
284+
spi_context_release(ctx, ret);
285+
return ret;
281286
}
282287

283288
static int spi_mcux_transceive_sync(const struct device *dev, const struct spi_config *spi_cfg,

0 commit comments

Comments
 (0)