From 5be3edb9c4f1da1a23044ebff1d8112ed74e5abc Mon Sep 17 00:00:00 2001 From: MCUdude Date: Sun, 3 Sep 2023 19:38:53 +0200 Subject: [PATCH] Add support for "usb" suffix when specifying USB VID and PID and harden against segfault when passing a serial number when using a serial adapter that doesn't have a serial number --- src/main.c | 28 ++++++++++++++++++++-------- src/serialadapter.c | 5 ++--- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/main.c b/src/main.c index 1892de0e5..dc34b536a 100644 --- a/src/main.c +++ b/src/main.c @@ -1165,17 +1165,19 @@ int main(int argc, char * argv []) } // Divide the port string into tokens separated by colon(s). - // There are four ways a port string can be presented: + // There are several ways a port string can be presented: // 1) -P [serialadapter] // 2) -P [serialadapter]:[sernum] // 3) -P [usbvid]:[usbpid] // 4) -P [usbvid]:[usbpid]:[sernum] + // 5) -P usb:[usbvid]:[usbpid] + // 6) -P usb:[usbvid]:[usbpid]:[sernum] if(pgm->conntype == CONNTYPE_SERIAL) { char *portdup = cfg_strdup(__func__, port); - char port_tok[3][256]; + char port_tok[4][256]; memset(port_tok, 0, sizeof(port_tok)); char *tok = strtok(portdup, ":"); - for (int i = 0; i < 3; i++) { + for (int i = 0; i < 4; i++) { if (!tok) break; strncpy(port_tok[i], tok, sizeof port_tok[i] - 1); @@ -1195,13 +1197,23 @@ int main(int argc, char * argv []) } } else { // Port or usb vid/pid + char *vidptr, *pidptr, *snptr = NULL; + if (str_caseeq(port_tok[0], "usb")) { + vidptr = port_tok[1]; + pidptr = port_tok[2]; + snptr = port_tok[3]; + } else { + vidptr = port_tok[0]; + pidptr = port_tok[1]; + snptr = port_tok[2]; + } int vid, pid; - if (sscanf(port_tok[0], "%x", &vid) > 0 && sscanf(port_tok[1], "%x", &pid) > 0) { - if(setport_from_vid_pid(&port, vid, pid, port_tok[2]) < 0) { - if (port_tok[2][0]) - pmsg_warning("serial adapter with USB VID %s and PID %s and serial number %s not found\n", port_tok[0], port_tok[1], port_tok[2]); + if (sscanf(vidptr, "%x", &vid) > 0 && sscanf(pidptr, "%x", &pid) > 0) { + if(setport_from_vid_pid(&port, vid, pid, snptr) < 0) { + if (snptr[0]) + pmsg_warning("serial adapter with USB VID %s and PID %s and serial number %s not found\n", vidptr, pidptr, snptr); else - pmsg_warning("serial adapter with USB VID %s and PID %s not found\n", port_tok[0], port_tok[1]); + pmsg_warning("serial adapter with USB VID %s and PID %s not found\n", vidptr, pidptr); } } } diff --git a/src/serialadapter.c b/src/serialadapter.c index 67875a256..1a3864377 100644 --- a/src/serialadapter.c +++ b/src/serialadapter.c @@ -96,7 +96,7 @@ int setport_from_serialadapter(char **portp, const SERIALADAPTER *ser, const cha // SN present if (sernum && sernum[0]) { // SN matches - if (str_starts(sp[i].sernum, sernum)) + if (sp[i].sernum && str_starts(sp[i].sernum, sernum)) sp[i].match = true; // SN does not match else @@ -173,7 +173,6 @@ int setport_from_vid_pid(char **portp, int vid, int pid, const char *sernum) { for (n = 0; port_list[n]; n++) continue; struct serports *sp = cfg_malloc(__func__, n*sizeof*sp); - int i; for (i = 0; i < n; i++) { struct sp_port *prt = port_list[i]; @@ -192,7 +191,7 @@ int setport_from_vid_pid(char **portp, int vid, int pid, const char *sernum) { // SN present if (sernum && sernum[0]) { // SN matches - if (str_starts(sp[i].sernum, sernum)) + if (sp[i].sernum && str_starts(sp[i].sernum, sernum)) sp[i].match = true; // SN does not match else