From 5277e384af9cef6ebaa8222a0e5e7d73e7b4cf44 Mon Sep 17 00:00:00 2001 From: stefanrueger Date: Thu, 22 Feb 2024 15:29:22 +1300 Subject: [PATCH 1/5] Harden against port string being NULL --- src/serialadapter.c | 4 +++- src/strutil.c | 3 +++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/serialadapter.c b/src/serialadapter.c index 9f785458e..d1a93c1f6 100644 --- a/src/serialadapter.c +++ b/src/serialadapter.c @@ -57,12 +57,14 @@ static int sa_snmatch(const char *sn, const char *q) { return sn && (str_starts(sn, q) || (str_starts(q , "...") && str_ends(sn, q+3))); } +#define null_len(s) ((s)? strlen(s): 0) + // Order two SERPORTs port strings: base first then trailing numbers, if any static int sa_portcmp(const void *p, const void *q) { int ret; const char *a = ((SERPORT *) p)->port, *b = ((SERPORT *) q)->port; const char *na = str_endnumber(a), *nb = str_endnumber(b); - size_t la = strlen(a) - (na? strlen(na): 0), lb = strlen(b) - (nb? strlen(nb): 0); + size_t la = null_len(a) - null_len(na), lb = null_len(b) - null_len(nb); // Compare string bases first if(la && lb && (ret = strncasecmp(a, b, la < lb? la: lb))) diff --git a/src/strutil.c b/src/strutil.c index de3df8fb7..560605566 100644 --- a/src/strutil.c +++ b/src/strutil.c @@ -384,6 +384,9 @@ char *str_utoa(unsigned n, char *buf, int base) { char *str_endnumber(const char *str) { const char *ret = NULL; + if(!str) + return NULL; + for(const char *end = str + strlen(str)-1; end >= str; end--) if(isdigit((unsigned char) *end)) ret = end; From 75eccf7d8e6f8183538bb3e061c881b58fb2c1ca Mon Sep 17 00:00:00 2001 From: stefanrueger Date: Thu, 22 Feb 2024 15:33:49 +1300 Subject: [PATCH 2/5] Change port array in PROGRAMMER to be const char * --- src/arduino.c | 2 +- src/avr910.c | 2 +- src/buspirate.c | 2 +- src/butterfly.c | 2 +- src/developer_opts.c | 5 ++--- src/ft245r.c | 2 +- src/jtag3.c | 4 ++-- src/jtagmkI.c | 2 +- src/jtagmkII.c | 14 +++++++------- src/libavrdude.h | 3 +-- src/linuxspi.c | 2 +- src/serialupdi.c | 2 +- src/stk500.c | 2 +- src/stk500v2.c | 10 +++++----- src/urclock.c | 2 +- src/wiring.c | 2 +- src/xbee.c | 2 +- 17 files changed, 29 insertions(+), 31 deletions(-) diff --git a/src/arduino.c b/src/arduino.c index 7c705da2a..ba4a40023 100644 --- a/src/arduino.c +++ b/src/arduino.c @@ -78,7 +78,7 @@ static int arduino_read_sig_bytes(const PROGRAMMER *pgm, const AVRPART *p, const static int arduino_open(PROGRAMMER *pgm, const char *port) { union pinfo pinfo; - strcpy(pgm->port, port); + pgm->port = port; pinfo.serialinfo.baud = pgm->baudrate? pgm->baudrate: 115200; pinfo.serialinfo.cflags = SERIAL_8N1; if (serial_open(port, pinfo, &pgm->fd)==-1) { diff --git a/src/avr910.c b/src/avr910.c index f7f5fb2b1..24a6a36ec 100644 --- a/src/avr910.c +++ b/src/avr910.c @@ -357,7 +357,7 @@ static int avr910_open(PROGRAMMER *pgm, const char *port) { pgm->baudrate = 19200; } - strcpy(pgm->port, port); + pgm->port = port; pinfo.serialinfo.baud = pgm->baudrate; pinfo.serialinfo.cflags = SERIAL_8N1; if (serial_open(port, pinfo, &pgm->fd)==-1) { diff --git a/src/buspirate.c b/src/buspirate.c index 17089cac9..f4f1ec3fd 100644 --- a/src/buspirate.c +++ b/src/buspirate.c @@ -426,7 +426,7 @@ static int buspirate_open(PROGRAMMER *pgm, const char *port) { pinfo.serialinfo.baud = pgm->baudrate; pinfo.serialinfo.cflags = SERIAL_8N1; - strcpy(pgm->port, port); + pgm->port = port; if (serial_open(port, pinfo, &pgm->fd)==-1) { return -1; } diff --git a/src/butterfly.c b/src/butterfly.c index 73acd329b..0dfbf403a 100644 --- a/src/butterfly.c +++ b/src/butterfly.c @@ -351,7 +351,7 @@ static void butterfly_enable(PROGRAMMER *pgm, const AVRPART *p) { static int butterfly_open(PROGRAMMER *pgm, const char *port) { union pinfo pinfo; - strcpy(pgm->port, port); + pgm->port = port; /* * If baudrate was not specified use 19200 Baud */ diff --git a/src/developer_opts.c b/src/developer_opts.c index 2a42df36e..07ebe76ae 100644 --- a/src/developer_opts.c +++ b/src/developer_opts.c @@ -1201,11 +1201,9 @@ static void dev_pgm_raw(const PROGRAMMER *pgm) { if(dp.usbproduct && *dp.usbproduct) dev_raw_dump(dp.usbproduct, strlen(dp.usbproduct)+1, id, "usbprod", 0); - // Zap all bytes beyond terminating nul of desc, type and port array + // Zap all bytes beyond terminating nul of type array if((len = (int) strlen(dp.type)+1) < (int) sizeof dp.type) memset(dp.type + len, 0, sizeof dp.type - len); - if((len = (int) strlen(dp.port)+1) < (int) sizeof dp.port) - memset(dp.port + len, 0, sizeof dp.port - len); // Zap address values dp.desc = NULL; @@ -1219,6 +1217,7 @@ static void dev_pgm_raw(const PROGRAMMER *pgm) { dp.usbvendor = NULL; dp.usbproduct = NULL; dp.hvupdi_support = NULL; + dp.port = NULL; // Only dump contents of PROGRAMMER struct up to and excluding the fd component dev_raw_dump((char *) &dp, offsetof(PROGRAMMER, fd), id, "pgm", 0); diff --git a/src/ft245r.c b/src/ft245r.c index f885bb887..9684161ae 100644 --- a/src/ft245r.c +++ b/src/ft245r.c @@ -826,7 +826,7 @@ static int ft245r_open(PROGRAMMER *pgm, const char *port) { return rv; } - strcpy(pgm->port, port); + pgm->port = port; // read device string cut after 8 chars (max. length of serial number) if ((sscanf(port, "usb:%8s", device) != 1)) { diff --git a/src/jtag3.c b/src/jtag3.c index f5fdd2a88..2522f076b 100644 --- a/src/jtag3.c +++ b/src/jtag3.c @@ -1708,7 +1708,7 @@ int jtag3_open_common(PROGRAMMER *pgm, const char *port, int mode_switch) { pgm->fd.usb.wep = USBDEV_BULK_EP_WRITE_3; pgm->fd.usb.eep = 0; - strcpy(pgm->port, port); + pgm->port = port; rv = serial_open(port, pinfo, &pgm->fd); } if (rv < 0) { @@ -1723,7 +1723,7 @@ int jtag3_open_common(PROGRAMMER *pgm, const char *port, int mode_switch) { pgm->fd.usb.wep = USBDEV_BULK_EP_WRITE_3; pgm->fd.usb.eep = USBDEV_EVT_EP_READ_3; - strcpy(pgm->port, port); + pgm->port = port; rv = serial_open(port, pinfo, &pgm->fd); } #endif /* HAVE_LIBUSB */ diff --git a/src/jtagmkI.c b/src/jtagmkI.c index 513019319..c85e8024d 100644 --- a/src/jtagmkI.c +++ b/src/jtagmkI.c @@ -568,7 +568,7 @@ static int jtagmkI_open(PROGRAMMER *pgm, const char *port) pmsg_notice2("jtagmkI_open()\n"); - strcpy(pgm->port, port); + pgm->port = port; PDATA(pgm)->initial_baudrate = -1L; for (i = 0; i < sizeof(baudtab) / sizeof(baudtab[0]); i++) { diff --git a/src/jtagmkII.c b/src/jtagmkII.c index 4ace06597..c470be265 100644 --- a/src/jtagmkII.c +++ b/src/jtagmkII.c @@ -1457,7 +1457,7 @@ static int jtagmkII_open(PROGRAMMER *pgm, const char *port) { #endif } - strcpy(pgm->port, port); + pgm->port = port; if (serial_open(port, pinfo, &pgm->fd)==-1) { return -1; } @@ -1509,7 +1509,7 @@ static int jtagmkII_open_dw(PROGRAMMER *pgm, const char *port) { #endif } - strcpy(pgm->port, port); + pgm->port = port; if (serial_open(port, pinfo, &pgm->fd)==-1) { return -1; } @@ -1561,7 +1561,7 @@ static int jtagmkII_open_pdi(PROGRAMMER *pgm, const char *port) { #endif } - strcpy(pgm->port, port); + pgm->port = port; if (serial_open(port, pinfo, &pgm->fd)==-1) { return -1; } @@ -1620,7 +1620,7 @@ static int jtagmkII_dragon_open(PROGRAMMER *pgm, const char *port) { #endif } - strcpy(pgm->port, port); + pgm->port = port; if (serial_open(port, pinfo, &pgm->fd)==-1) { return -1; } @@ -1673,7 +1673,7 @@ static int jtagmkII_dragon_open_dw(PROGRAMMER *pgm, const char *port) { #endif } - strcpy(pgm->port, port); + pgm->port = port; if (serial_open(port, pinfo, &pgm->fd)==-1) { return -1; } @@ -1726,7 +1726,7 @@ static int jtagmkII_dragon_open_pdi(PROGRAMMER *pgm, const char *port) { #endif } - strcpy(pgm->port, port); + pgm->port = port; if (serial_open(port, pinfo, &pgm->fd)==-1) { return -1; } @@ -3231,7 +3231,7 @@ static int jtagmkII_open32(PROGRAMMER *pgm, const char *port) { #endif } - strcpy(pgm->port, port); + pgm->port = port; if (serial_open(port, pinfo, &pgm->fd)==-1) { return -1; } diff --git a/src/libavrdude.h b/src/libavrdude.h index 648fceba3..65deedf62 100644 --- a/src/libavrdude.h +++ b/src/libavrdude.h @@ -866,7 +866,6 @@ typedef struct { // Memory cache for a subset of cached pages #define OFF 0 // Many contexts: reset, power, LEDs, ... #define ON 1 // Many contexts -#define PGM_PORTLEN PATH_MAX #define PGM_TYPELEN 32 typedef enum { @@ -945,7 +944,7 @@ typedef struct programmer_t { // Values below are not set by config_gram.y; ensure fd is first for dev_pgm_raw() union filedescriptor fd; char type[PGM_TYPELEN]; - char port[PGM_PORTLEN]; + const char *port; unsigned int pinno[N_PINS]; // TODO to be removed if old pin data no longer needed exit_vcc_t exit_vcc; // Should these be set in avrdude.conf? exit_reset_t exit_reset; diff --git a/src/linuxspi.c b/src/linuxspi.c index 82ce35ec2..3d73172b3 100644 --- a/src/linuxspi.c +++ b/src/linuxspi.c @@ -178,7 +178,7 @@ static int linuxspi_open(PROGRAMMER *pgm, const char *pt) { } } - strcpy(pgm->port, port); + pgm->port = port; fd_spidev = open(pgm->port, O_RDWR); if (fd_spidev < 0) { pmsg_ext_error("unable to open the spidev device %s: %s\n", pgm->port, strerror(errno)); diff --git a/src/serialupdi.c b/src/serialupdi.c index b1f2a9ad0..5b072ac11 100644 --- a/src/serialupdi.c +++ b/src/serialupdi.c @@ -64,7 +64,7 @@ static void serialupdi_teardown(PROGRAMMER * pgm) } static int serialupdi_open(PROGRAMMER *pgm, const char *port) { - strcpy(pgm->port, port); + pgm->port = port; return updi_link_open(pgm); } diff --git a/src/stk500.c b/src/stk500.c index 7f804ef8a..87c139d94 100644 --- a/src/stk500.c +++ b/src/stk500.c @@ -864,7 +864,7 @@ static void stk500_enable(PROGRAMMER *pgm, const AVRPART *p) { static int stk500_open(PROGRAMMER *pgm, const char *port) { union pinfo pinfo; - strcpy(pgm->port, port); + pgm->port = port; pinfo.serialinfo.baud = pgm->baudrate? pgm->baudrate: 115200; pinfo.serialinfo.cflags = SERIAL_8N1; if (serial_open(port, pinfo, &pgm->fd)==-1) { diff --git a/src/stk500v2.c b/src/stk500v2.c index 20a0c633e..1b381aa20 100644 --- a/src/stk500v2.c +++ b/src/stk500v2.c @@ -2207,7 +2207,7 @@ static int stk500v2_open(PROGRAMMER *pgm, const char *port) { #endif } - strcpy(pgm->port, port); + pgm->port = port; if (serial_open(port, pinfo, &pgm->fd)==-1) { return -1; } @@ -2270,7 +2270,7 @@ static int stk600_open(PROGRAMMER *pgm, const char *port) { #endif } - strcpy(pgm->port, port); + pgm->port = port; if (serial_open(port, pinfo, &pgm->fd)==-1) { return -1; } @@ -4049,7 +4049,7 @@ static int stk500v2_jtagmkII_open(PROGRAMMER *pgm, const char *port) { #endif } - strcpy(pgm->port, port); + pgm->port = port; if (serial_open(port, pinfo, &pgm->fd)==-1) { return -1; } @@ -4159,7 +4159,7 @@ static int stk500v2_dragon_isp_open(PROGRAMMER *pgm, const char *port) { #endif } - strcpy(pgm->port, port); + pgm->port = port; if (serial_open(port, pinfo, &pgm->fd)==-1) { return -1; } @@ -4235,7 +4235,7 @@ static int stk500v2_dragon_hv_open(PROGRAMMER *pgm, const char *port) { #endif } - strcpy(pgm->port, port); + pgm->port = port; if (serial_open(port, pinfo, &pgm->fd)==-1) { return -1; } diff --git a/src/urclock.c b/src/urclock.c index 12927b8db..3ae60a077 100644 --- a/src/urclock.c +++ b/src/urclock.c @@ -2228,7 +2228,7 @@ static void urclock_disable(const PROGRAMMER *pgm) { static int urclock_open(PROGRAMMER *pgm, const char *port) { union pinfo pinfo; - strcpy(pgm->port, port); + pgm->port = port; pinfo.serialinfo.baud = pgm->baudrate? pgm->baudrate: 115200; pinfo.serialinfo.cflags = SERIAL_8N1; if(serial_open(port, pinfo, &pgm->fd) == -1) diff --git a/src/wiring.c b/src/wiring.c index 61cd9f37c..6ac9751a2 100644 --- a/src/wiring.c +++ b/src/wiring.c @@ -123,7 +123,7 @@ static int wiring_open(PROGRAMMER *pgm, const char *port) { int timetosnooze; union pinfo pinfo; - strcpy(pgm->port, port); + pgm->port = port; pinfo.serialinfo.baud = pgm->baudrate ? pgm->baudrate: 115200; pinfo.serialinfo.cflags = SERIAL_8N1; serial_open(port, pinfo, &pgm->fd); diff --git a/src/xbee.c b/src/xbee.c index 52b3c5df9..af36611ae 100644 --- a/src/xbee.c +++ b/src/xbee.c @@ -1555,7 +1555,7 @@ static int xbee_getsync(const PROGRAMMER *pgm) { static int xbee_open(PROGRAMMER *pgm, const char *port) { union pinfo pinfo; - strcpy(pgm->port, port); + pgm->port = port; pinfo.serialinfo.baud = pgm->baudrate; pinfo.serialinfo.cflags = SERIAL_8N1; From a24e6eb74b19b991f42a5508e31a2f85659be5d6 Mon Sep 17 00:00:00 2001 From: stefanrueger Date: Thu, 22 Feb 2024 21:13:14 +1300 Subject: [PATCH 3/5] Update libavrdude version --- src/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c02d4d541..332bf0463 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -279,7 +279,7 @@ add_library(libavrdude set_target_properties(libavrdude PROPERTIES PREFIX "" PUBLIC_HEADER "libavrdude.h" - VERSION 1.0.0 + VERSION 1.0.1 SOVERSION 1 ) From a83f3cf087bf58623377ea0679ced1ed560d772a Mon Sep 17 00:00:00 2001 From: stefanrueger Date: Fri, 23 Feb 2024 13:36:27 +1300 Subject: [PATCH 4/5] Update libavrdude major version --- src/CMakeLists.txt | 4 ++-- src/Makefile.am | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 332bf0463..50883a1be 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -279,8 +279,8 @@ add_library(libavrdude set_target_properties(libavrdude PROPERTIES PREFIX "" PUBLIC_HEADER "libavrdude.h" - VERSION 1.0.1 - SOVERSION 1 + VERSION 2.0.0 + SOVERSION 2 ) target_include_directories(libavrdude diff --git a/src/Makefile.am b/src/Makefile.am index 8863291a9..5ee959600 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -219,7 +219,7 @@ libavrdude_a_SOURCES = \ xbee.h \ xbee.c libavrdude_la_SOURCES = $(libavrdude_a_SOURCES) -libavrdude_la_LDFLAGS = -version-info 1:0 +libavrdude_la_LDFLAGS = -version-info 2:0 include_HEADERS = libavrdude.h From 85527607275c25237f417e5aefec1cd3ed71864a Mon Sep 17 00:00:00 2001 From: stefanrueger Date: Sat, 24 Feb 2024 12:41:51 +1300 Subject: [PATCH 5/5] Add comment re use of libavrdude --- src/libavrdude.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/libavrdude.h b/src/libavrdude.h index f69fa8f83..7c485b0bf 100644 --- a/src/libavrdude.h +++ b/src/libavrdude.h @@ -35,6 +35,19 @@ #include "libavrdude-avrintel.h" #undef LIBAVRDUDE_INCLUDE_INTERNAL_HEADERS +/* + * The libavrdude library contains useful functions for programming + * Microchip's 8-bit AVR microprocessors. The command line program avrdude + * was written using this library; its source code is a good example of how + * to use the library. Out of necessity libavrdude routinely changes + * PROGRAMMER, AVRPART and other structures to keep up with new programmers + * and with new parts and programming interfaces from Microchip. Any + * application that uses this library should ensure that it links to a + * libavrdude binary that is compatible with this header file, ideally the + * version that was shipped together with this header file or one that was + * compiled from source together with the application. + */ + typedef uint32_t pinmask_t; /* * Values returned by library functions.