From 422488de9c8c92e2f1731862e5cee7f8cc0bc44d Mon Sep 17 00:00:00 2001 From: Stefan Rueger Date: Sun, 21 Apr 2024 16:10:50 +0100 Subject: [PATCH 1/3] Make static functions greppable --- src/jtagmkI.c | 25 +++++++------------------ 1 file changed, 7 insertions(+), 18 deletions(-) diff --git a/src/jtagmkI.c b/src/jtagmkI.c index b02c9626..11787d85 100644 --- a/src/jtagmkI.c +++ b/src/jtagmkI.c @@ -97,8 +97,7 @@ static void jtagmkI_print_parms1(const PROGRAMMER *pgm, const char *p, FILE *fp) static int jtagmkI_resync(const PROGRAMMER *pgm, int maxtries, int signon); -static void jtagmkI_setup(PROGRAMMER * pgm) -{ +static void jtagmkI_setup(PROGRAMMER *pgm) { if ((pgm->cookie = malloc(sizeof(struct pdata))) == 0) { pmsg_error("out of memory allocating private data\n"); exit(1); @@ -106,23 +105,18 @@ static void jtagmkI_setup(PROGRAMMER * pgm) memset(pgm->cookie, 0, sizeof(struct pdata)); } -static void jtagmkI_teardown(PROGRAMMER * pgm) -{ +static void jtagmkI_teardown(PROGRAMMER *pgm) { free(pgm->cookie); } -static void -u32_to_b3(unsigned char *b, unsigned long l) -{ +static void u32_to_b3(unsigned char *b, unsigned long l) { b[2] = l & 0xff; b[1] = (l >> 8) & 0xff; b[0] = (l >> 16) & 0xff; } -static void -u16_to_b2(unsigned char *b, unsigned short l) -{ +static void u16_to_b2(unsigned char *b, unsigned short l) { b[0] = l & 0xff; b[1] = (l >> 8) & 0xff; } @@ -402,7 +396,6 @@ static int jtagmkI_reset(const PROGRAMMER *pgm) { } static int jtagmkI_program_enable_dummy(const PROGRAMMER *pgm, const AVRPART *p) { - return 0; } @@ -458,8 +451,7 @@ static int jtagmkI_program_disable(const PROGRAMMER *pgm) { return 0; } -static unsigned char jtagmkI_get_baud(long baud) -{ +static unsigned char jtagmkI_get_baud(long baud) { for (size_t i = 0; i < sizeof baudtab / sizeof baudtab[0]; i++) if (baud == baudtab[i].baud) return baudtab[i].val; @@ -548,7 +540,6 @@ static int jtagmkI_initialize(const PROGRAMMER *pgm, const AVRPART *p) { static void jtagmkI_disable(const PROGRAMMER *pgm) { - free(PDATA(pgm)->flash_pagecache); PDATA(pgm)->flash_pagecache = NULL; free(PDATA(pgm)->eeprom_pagecache); @@ -562,8 +553,7 @@ static void jtagmkI_enable(PROGRAMMER *pgm, const AVRPART *p) { } -static int jtagmkI_open(PROGRAMMER *pgm, const char *port) -{ +static int jtagmkI_open(PROGRAMMER *pgm, const char *port) { size_t i; pmsg_notice2("jtagmkI_open()\n"); @@ -601,8 +591,7 @@ static int jtagmkI_open(PROGRAMMER *pgm, const char *port) } -static void jtagmkI_close(PROGRAMMER * pgm) -{ +static void jtagmkI_close(PROGRAMMER *pgm) { unsigned char b; pmsg_notice2("jtagmkI_close()\n"); From 74bce432e47a3d4a0e03ea73e37c1102eac0cf3e Mon Sep 17 00:00:00 2001 From: Stefan Rueger Date: Sun, 21 Apr 2024 16:20:49 +0100 Subject: [PATCH 2/3] Utilise magic memory tree interface for jtagmkI.c --- src/avrdude.h | 1 + src/jtagmkI.c | 61 ++++++++++++++++++++------------------------------- 2 files changed, 25 insertions(+), 37 deletions(-) diff --git a/src/avrdude.h b/src/avrdude.h index c679a654..6b777d30 100644 --- a/src/avrdude.h +++ b/src/avrdude.h @@ -44,6 +44,7 @@ extern const char *pgmid; // Programmer -c string #define mmt_strdup(s) cfg_strdup(__func__, s) #define mmt_malloc(n) cfg_malloc(__func__, n) #define mmt_realloc(p, n) cfg_realloc(__func__, p, n) +#define mmt_free(p) free(p) int avrdude_message2(FILE *fp, int lno, const char *file, const char *func, int msgmode, int msglvl, const char *format, ...); diff --git a/src/jtagmkI.c b/src/jtagmkI.c index 11787d85..3826c7e0 100644 --- a/src/jtagmkI.c +++ b/src/jtagmkI.c @@ -98,15 +98,11 @@ static void jtagmkI_print_parms1(const PROGRAMMER *pgm, const char *p, FILE *fp) static int jtagmkI_resync(const PROGRAMMER *pgm, int maxtries, int signon); static void jtagmkI_setup(PROGRAMMER *pgm) { - if ((pgm->cookie = malloc(sizeof(struct pdata))) == 0) { - pmsg_error("out of memory allocating private data\n"); - exit(1); - } - memset(pgm->cookie, 0, sizeof(struct pdata)); + pgm->cookie = mmt_malloc(sizeof(struct pdata)); } static void jtagmkI_teardown(PROGRAMMER *pgm) { - free(pgm->cookie); + mmt_free(pgm->cookie); } @@ -180,23 +176,18 @@ static int jtagmkI_send(const PROGRAMMER *pgm, unsigned char *data, size_t len) msg_debug("\n"); pmsg_debug("jtagmkI_send(): sending %u bytes\n", (unsigned int) len); - if ((buf = malloc(len + 2)) == NULL) - { - pmsg_error("out of memory"); - exit(1); - } - + buf = mmt_malloc(len + 2); memcpy(buf, data, len); buf[len] = ' '; /* "CRC" */ buf[len + 1] = ' '; /* EOP */ if (serial_send(&pgm->fd, buf, len + 2) != 0) { pmsg_error("unable to send command to serial port\n"); - free(buf); + mmt_free(buf); return -1; } - free(buf); + mmt_free(buf); return 0; } @@ -515,18 +506,11 @@ static int jtagmkI_initialize(const PROGRAMMER *pgm, const AVRPART *p) { jtagmkI_setparm(pgm, PARM_FLASH_PAGESIZE_HIGH, PDATA(pgm)->flash_pagesize >> 8); jtagmkI_setparm(pgm, PARM_EEPROM_PAGESIZE, PDATA(pgm)->eeprom_pagesize & 0xff); - free(PDATA(pgm)->flash_pagecache); - free(PDATA(pgm)->eeprom_pagecache); - if ((PDATA(pgm)->flash_pagecache = malloc(PDATA(pgm)->flash_pagesize)) == NULL) { - pmsg_error("out of memory\n"); - return -1; - } - if ((PDATA(pgm)->eeprom_pagecache = malloc(PDATA(pgm)->eeprom_pagesize)) == NULL) { - pmsg_error("out of memory\n"); - free(PDATA(pgm)->flash_pagecache); - return -1; - } - PDATA(pgm)->flash_pageaddr = PDATA(pgm)->eeprom_pageaddr = (unsigned long)-1L; + mmt_free(PDATA(pgm)->flash_pagecache); + mmt_free(PDATA(pgm)->eeprom_pagecache); + PDATA(pgm)->flash_pagecache = mmt_malloc(PDATA(pgm)->flash_pagesize); + PDATA(pgm)->eeprom_pagecache = mmt_malloc(PDATA(pgm)->eeprom_pagesize); + PDATA(pgm)->flash_pageaddr = PDATA(pgm)->eeprom_pageaddr = (unsigned long) -1L; if (jtagmkI_reset(pgm) < 0) return -1; @@ -540,9 +524,9 @@ static int jtagmkI_initialize(const PROGRAMMER *pgm, const AVRPART *p) { static void jtagmkI_disable(const PROGRAMMER *pgm) { - free(PDATA(pgm)->flash_pagecache); + mmt_free(PDATA(pgm)->flash_pagecache); PDATA(pgm)->flash_pagecache = NULL; - free(PDATA(pgm)->eeprom_pagecache); + mmt_free(PDATA(pgm)->eeprom_pagecache); PDATA(pgm)->eeprom_pagecache = NULL; (void)jtagmkI_program_disable(pgm); @@ -627,7 +611,7 @@ static int jtagmkI_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const AV { int block_size, send_size, tries; unsigned int maxaddr = addr + n_bytes; - unsigned char cmd[6], *datacmd; + unsigned char cmd[6], *datacmd = NULL; unsigned char resp[2]; int is_flash = 0; long otimeout = serial_recv_timeout; @@ -646,11 +630,7 @@ static int jtagmkI_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const AV return -1; } - if ((datacmd = malloc(page_size + 1)) == NULL) { - pmsg_error("out of memory\n"); - return -1; - } - + datacmd = mmt_malloc(page_size + 1); cmd[0] = CMD_WRITE_MEM; if (mem_is_flash(m)) { cmd[1] = MTYPE_FLASH_PAGE; @@ -671,6 +651,7 @@ static int jtagmkI_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const AV if (tries != 0 && jtagmkI_resync(pgm, 2000, 0) < 0) { pmsg_error("sync loss, retries exhausted\n"); + mmt_free(datacmd); return -1; } @@ -696,14 +677,17 @@ static int jtagmkI_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const AV /* First part, send the write command. */ jtagmkI_send(pgm, cmd, 6); - if (jtagmkI_recv(pgm, resp, 1) < 0) + if (jtagmkI_recv(pgm, resp, 1) < 0) { + mmt_free(datacmd); return -1; + } if (resp[0] != RESP_OK) { msg_notice2("\n"); pmsg_warning("timeout/error communicating with programmer (resp %c)\n", resp[0]); if (tries++ < MAXTRIES) goto again; serial_recv_timeout = otimeout; + mmt_free(datacmd); return -1; } else { msg_notice2("OK\n"); @@ -721,21 +705,24 @@ static int jtagmkI_paged_write(const PROGRAMMER *pgm, const AVRPART *p, const AV /* Second, send the data command. */ jtagmkI_send(pgm, datacmd, send_size + 1); - if (jtagmkI_recv(pgm, resp, 2) < 0) + if (jtagmkI_recv(pgm, resp, 2) < 0) { + mmt_free(datacmd); return -1; + } if (resp[1] != RESP_OK) { msg_notice2("\n"); pmsg_warning("timeout/error communicating with programmer (resp %c)\n", resp[0]); if (tries++ < MAXTRIES) goto again; serial_recv_timeout = otimeout; + mmt_free(datacmd); return -1; } else { msg_notice2("OK\n"); } } - free(datacmd); + mmt_free(datacmd); serial_recv_timeout = otimeout; #undef MAXTRIES From 1442f2a5dcec99f2f259fa386dc0fa550fa316fa Mon Sep 17 00:00:00 2001 From: Stefan Rueger Date: Mon, 22 Apr 2024 15:47:53 +0100 Subject: [PATCH 3/3] Render double teardown() harmless in jtagmkI --- src/jtagmkI.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/jtagmkI.c b/src/jtagmkI.c index 3826c7e0..9f546cf8 100644 --- a/src/jtagmkI.c +++ b/src/jtagmkI.c @@ -103,6 +103,7 @@ static void jtagmkI_setup(PROGRAMMER *pgm) { static void jtagmkI_teardown(PROGRAMMER *pgm) { mmt_free(pgm->cookie); + pgm->cookie = NULL; }