modbus: fix potential null pointer dereference

reorganize code so that null pointer check is done prior to accessing
element.

Signed-off-by: Jacob Siverskog <jacob@teenage.engineering>
This commit is contained in:
Jacob Siverskog 2021-03-23 17:44:59 +01:00 committed by Jukka Rissanen
commit 2954dcf4ed

View file

@ -404,13 +404,15 @@ static void cb_handler_tx(struct modbus_context *ctx)
static void uart_cb_handler(const struct device *dev, void *app_data)
{
struct modbus_context *ctx = (struct modbus_context *)app_data;
struct modbus_serial_config *cfg = ctx->cfg;
struct modbus_serial_config *cfg;
if (ctx == NULL) {
LOG_ERR("Modbus hardware is not properly initialized");
return;
}
cfg = ctx->cfg;
while (uart_irq_update(cfg->dev) && uart_irq_is_pending(cfg->dev)) {
if (uart_irq_rx_ready(cfg->dev)) {