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 <javier.viguera@digi.com>
This commit is contained in:
Javier Viguera 2025-06-18 15:39:19 +02:00
parent 2e2aac99f1
commit 85480c9f26
2 changed files with 17 additions and 8 deletions

View File

@ -34,6 +34,12 @@ static bool running = true;
static bool prn_msg_info = false; static bool prn_msg_info = false;
static bool prn_msg_count = 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 * usage_and_exit() - Show usage information and exit with 'exitval' return
* value * value
@ -207,16 +213,12 @@ static int parse_filters(char *str, struct can_filter **cfilter, int *nfilters)
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
char *name = basename(argv[0]); char *name = basename(argv[0]);
char *iface; char *iface = NULL;
can_if_cfg_t ifcfg; can_if_cfg_t ifcfg;
int nfilters = 0; int nfilters = 0;
int opt; int opt;
int ret; int ret;
float sp = 0.0; float sp = 0.0;
struct can_filter deffilter;
deffilter.can_id = 0;
deffilter.can_mask = 0;
if (argc <= 3) { if (argc <= 3) {
usage_and_exit(name, EXIT_FAILURE); 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); printf("Requesting CAN interface %s... ", iface);
can_if = ldx_can_request_by_name(iface); can_if = ldx_can_request_by_name(iface);

View File

@ -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) int main(int argc, char **argv)
{ {
char *name = basename(argv[0]); char *name = basename(argv[0]);
char *iface; char *iface = NULL;
can_if_cfg_t ifcfg; can_if_cfg_t ifcfg;
int opt; int opt;
int ret; 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); printf("Requesting CAN interface %s... ", iface);
can_if = ldx_can_request_by_name(iface); can_if = ldx_can_request_by_name(iface);
if (!can_if) { if (!can_if) {
printf("ERROR\n"); printf("ERROR\n");
return EXIT_FAILURE; return EXIT_FAILURE;
@ -304,7 +307,7 @@ int main(int argc, char **argv)
} }
if (!retries) { 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; goto error;
} }