From 85480c9f265da56373742e6ba0d178a3c030ac50 Mon Sep 17 00:00:00 2001 From: Javier Viguera Date: Wed, 18 Jun 2025 15:39:19 +0200 Subject: [PATCH] apix-can-examples: fix static analyzer warnings * Initialize 'iface' and validate it before use to avoid undefined behavior. * Move 'deffilter' to global static scope in can-recv-example to prevent stack address escape. * Fix incorrect retry count in failure message in can-send-example. No functional changes. Signed-off-by: Javier Viguera --- apix-can-examples/can-recv-example.c | 16 +++++++++++----- apix-can-examples/can-send-example.c | 9 ++++++--- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/apix-can-examples/can-recv-example.c b/apix-can-examples/can-recv-example.c index 439e267..10631f5 100644 --- a/apix-can-examples/can-recv-example.c +++ b/apix-can-examples/can-recv-example.c @@ -34,6 +34,12 @@ static bool running = true; static bool prn_msg_info = false; static bool prn_msg_count = false; +/* Default CAN filter if not specified in command line */ +static struct can_filter deffilter = { + .can_id = 0, + .can_mask = 0 +}; + /* * usage_and_exit() - Show usage information and exit with 'exitval' return * value @@ -207,16 +213,12 @@ static int parse_filters(char *str, struct can_filter **cfilter, int *nfilters) int main(int argc, char **argv) { char *name = basename(argv[0]); - char *iface; + char *iface = NULL; can_if_cfg_t ifcfg; int nfilters = 0; int opt; int ret; float sp = 0.0; - struct can_filter deffilter; - - deffilter.can_id = 0; - deffilter.can_mask = 0; if (argc <= 3) { usage_and_exit(name, EXIT_FAILURE); @@ -277,6 +279,10 @@ int main(int argc, char **argv) } } + if (!iface) { + fprintf(stderr, "Error: CAN interface not specified\n"); + return EXIT_FAILURE; + } printf("Requesting CAN interface %s... ", iface); can_if = ldx_can_request_by_name(iface); diff --git a/apix-can-examples/can-send-example.c b/apix-can-examples/can-send-example.c index 230e1ad..1d060bb 100644 --- a/apix-can-examples/can-send-example.c +++ b/apix-can-examples/can-send-example.c @@ -177,7 +177,7 @@ void update_msg(struct canfd_frame *frame, uint32_t id, uint8_t dlc, uint8_t fla int main(int argc, char **argv) { char *name = basename(argv[0]); - char *iface; + char *iface = NULL; can_if_cfg_t ifcfg; int opt; int ret; @@ -262,10 +262,13 @@ int main(int argc, char **argv) } } + if (!iface) { + fprintf(stderr, "Error: CAN interface not specified\n"); + return EXIT_FAILURE; + } printf("Requesting CAN interface %s... ", iface); can_if = ldx_can_request_by_name(iface); - if (!can_if) { printf("ERROR\n"); return EXIT_FAILURE; @@ -304,7 +307,7 @@ int main(int argc, char **argv) } if (!retries) { - printf("Failed to send CAN frame after %d tries\n", ret); + printf("Failed to send CAN frame after %d tries\n", TX_RETRIES); goto error; }