From 90a016a8f662710b3f72027abe4f671ac8f75675 Mon Sep 17 00:00:00 2001 From: Arnaud Ferraris Date: Sat, 12 Dec 2020 23:59:53 +0100 Subject: [PATCH] src: be more careful before dereferencing potentially NULL pointers --- src/at.c | 17 +++++++++++++---- src/gpio.c | 2 +- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/src/at.c b/src/at.c index 742f50e..67c6d69 100644 --- a/src/at.c +++ b/src/at.c @@ -52,7 +52,7 @@ static int configure_serial(const char *tty) static gboolean send_at_command(struct EG25Manager *manager) { char command[256]; - struct AtCommand *at_cmd = g_list_nth_data(manager->at_cmds, 0); + struct AtCommand *at_cmd = manager->at_cmds ? g_list_nth_data(manager->at_cmds, 0) : NULL; if (at_cmd) { if (at_cmd->subcmd == NULL && at_cmd->value == NULL && at_cmd->expected == NULL) @@ -88,7 +88,10 @@ static gboolean send_at_command(struct EG25Manager *manager) static void next_at_command(struct EG25Manager *manager) { - struct AtCommand *at_cmd = g_list_nth_data(manager->at_cmds, 0); + struct AtCommand *at_cmd = manager->at_cmds ? g_list_nth_data(manager->at_cmds, 0) : NULL; + + if (!at_cmd) + return; if (at_cmd->cmd) g_free(at_cmd->cmd); @@ -106,7 +109,10 @@ static void next_at_command(struct EG25Manager *manager) static void retry_at_command(struct EG25Manager *manager) { - struct AtCommand *at_cmd = g_list_nth_data(manager->at_cmds, 0); + struct AtCommand *at_cmd = manager->at_cmds ? g_list_nth_data(manager->at_cmds, 0) : NULL; + + if (!at_cmd) + return; at_cmd->retries++; if (at_cmd->retries > 3) { @@ -119,7 +125,10 @@ static void retry_at_command(struct EG25Manager *manager) static void process_at_result(struct EG25Manager *manager, char *response) { - struct AtCommand *at_cmd = g_list_nth_data(manager->at_cmds, 0); + struct AtCommand *at_cmd = manager->at_cmds ? g_list_nth_data(manager->at_cmds, 0) : NULL; + + if (!at_cmd) + return; if (at_cmd->expected && !strstr(response, at_cmd->expected)) { if (at_cmd->value) diff --git a/src/gpio.c b/src/gpio.c index d22b30d..53d87d6 100644 --- a/src/gpio.c +++ b/src/gpio.c @@ -177,7 +177,7 @@ gboolean gpio_check_poweroff(struct EG25Manager *manager, gboolean keep_down) if (manager->gpio_in[GPIO_IN_STATUS] && gpiod_line_get_value(manager->gpio_in[GPIO_IN_STATUS]) == 1) { - if (keep_down) { + if (keep_down && manager->gpio_out[GPIO_OUT_RESET]) { // Asserting RESET line to prevent modem from rebooting gpiod_line_set_value(manager->gpio_out[GPIO_OUT_RESET], 1); }