From 3412196cd904dc02bc04678758c8f2e7c943c09f Mon Sep 17 00:00:00 2001 From: Stefan Rueger Date: Thu, 4 Aug 2022 00:14:19 +0100 Subject: [PATCH] Weaken -U memory type check and move after config file parsing in main.c The check for typos in -U memory names against a list of known memory names now happens after the config files have been read, so newly declared memory names can be considered. This commit also weakens the check against existence of a known memory: it is now sufficent for a name to pass when it could be the initial string of any known memory of any part. Any -U memory that cannot possibly be matched up with a known memory is considered a typo and leads to an exit before the programmer is opened. This to protect users from typos that leave a device partially programmed. When every -U memory name might be matching one of the known memories, the programming is attempted. If the part to be programmed turns out not to have a particular -U memory, AVRDUDE warns the user and skips this -U update. This to support unifying interfaces that call AVRDUDE with potentially more memories than the actual part has (eg, efuse on ATmega8). --- src/avr.c | 15 ++++++++++++--- src/libavrdude.h | 1 + src/main.c | 7 +++++++ src/update.c | 6 ------ 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/src/avr.c b/src/avr.c index 3a7d4a9e..eea0ad8a 100644 --- a/src/avr.c +++ b/src/avr.c @@ -1254,9 +1254,18 @@ void avr_add_mem_order(const char *str) { } int avr_mem_is_known(const char *str) { - for(size_t i=0; i < sizeof avr_mem_order/sizeof *avr_mem_order; i++) - if(avr_mem_order[i] && !strcmp(avr_mem_order[i], str)) - return 1; + if(str && *str) + for(size_t i=0; i < sizeof avr_mem_order/sizeof *avr_mem_order; i++) + if(avr_mem_order[i] && !strcmp(avr_mem_order[i], str)) + return 1; + return 0; +} + +int avr_mem_might_be_known(const char *str) { + if(str && *str) + for(size_t i=0; i < sizeof avr_mem_order/sizeof *avr_mem_order; i++) + if(avr_mem_order[i] && !strncmp(avr_mem_order[i], str, strlen(str))) + return 1; return 0; } diff --git a/src/libavrdude.h b/src/libavrdude.h index f8bdf682..a30c1402 100644 --- a/src/libavrdude.h +++ b/src/libavrdude.h @@ -822,6 +822,7 @@ int avr_put_cycle_count(PROGRAMMER * pgm, AVRPART * p, int cycles); void avr_add_mem_order(const char *str); int avr_mem_is_known(const char *str); +int avr_mem_might_be_known(const char *str); #define disable_trailing_ff_removal() avr_mem_hiaddr(NULL) int avr_mem_hiaddr(AVRMEM * mem); diff --git a/src/main.c b/src/main.c index 2984203a..9f72847f 100644 --- a/src/main.c +++ b/src/main.c @@ -751,6 +751,7 @@ int main(int argc, char * argv []) bitclock = default_bitclock; } + avrdude_message(MSG_NOTICE, "\n"); // developer option -p /[*codws] prints various aspects of part descriptions and exits @@ -918,6 +919,12 @@ int main(int argc, char * argv []) exit(1); } } + + if (!avr_mem_might_be_known(upd->memtype)) { + avrdude_message(MSG_INFO, "%s: unknown memory type %s\n", progname, upd->memtype); + exit(1); + } + // TODO: check whether filename other than "-" is readable/writable } /* diff --git a/src/update.c b/src/update.c index 78703e89..025bfb6b 100644 --- a/src/update.c +++ b/src/update.c @@ -62,12 +62,6 @@ UPDATE * parse_op(char * s) return upd; } - if (!avr_mem_is_known(buf)) { - avrdude_message(MSG_INFO, "%s: unknown memory type %s\n", progname, buf); - free(upd); - return NULL; - } - upd->memtype = (char *)malloc(strlen(buf)+1); if (upd->memtype == NULL) { avrdude_message(MSG_INFO, "%s: out of memory\n", progname);