From 4ccbf49f0f76c89ff389df339fcca440acd87668 Mon Sep 17 00:00:00 2001 From: Stefan Rueger Date: Thu, 9 May 2024 19:03:34 +0100 Subject: [PATCH] Change pinmask_to_str() to pinmask_to_strdup() No longer is the string returned in static space (which the next call overwrites); instead a mmt_strdup()'d string is returned that needs mmt_free()'ing. --- src/avrftdi.c | 9 +++++---- src/libavrdude.h | 11 +++++------ src/pindefs.c | 46 ++++++++++++++++++++++++---------------------- 3 files changed, 34 insertions(+), 32 deletions(-) diff --git a/src/avrftdi.c b/src/avrftdi.c index f48eafaa..0faa6bb3 100644 --- a/src/avrftdi.c +++ b/src/avrftdi.c @@ -222,10 +222,11 @@ static int set_pin(const PROGRAMMER *pgm, int pinfunc, int value) { return 0; } - pmsg_debug("setting pin %s (%s) as %s: %s (%s active)\n", - pinmask_to_str(pin.mask), ftdi_pin_name(pdata, pin), - avr_pin_name(pinfunc), - (value) ? "high" : "low", (pin.inverse[0]) ? "low" : "high"); + char *pmsk = pinmask_to_strdup(pin.mask); + pmsg_debug("setting pin %s (%s) as %s: %s (%s active)\n", pmsk, + ftdi_pin_name(pdata, pin), avr_pin_name(pinfunc), + (value) ? "high" : "low", (pin.inverse[0]) ? "low" : "high"); + mmt_free(pmsk); pdata->pin_value = SET_BITS_0(pdata->pin_value, pgm, pinfunc, value); diff --git a/src/libavrdude.h b/src/libavrdude.h index ac3037d1..c53cf429 100644 --- a/src/libavrdude.h +++ b/src/libavrdude.h @@ -748,19 +748,18 @@ const char * avr_pin_lcname(int pinname); * This function returns a string of defined pins, eg, ~1, 2, ~4, ~5, 7 or "" * * @param[in] pindef the pin definition for which we want the string representation - * @returns a pointer to a string, which was created by strdup + * @returns a string that was created by mmt_strdup() */ char *pins_to_strdup(const struct pindef_t * const pindef); /** - * This function returns a string representation of pins in the mask, eg, 1,3,5-7,9,12 - * Another execution of this function will overwrite the previous result in the static buffer. - * Consecutive pin number are represented as start-end. + * This function returns a string representation of pins in the mask, eg, 1, 3, 5-7, 9, 12 + * Consecutive pin numbers are represented as start-end. * * @param[in] pinmask the pin mask for which we want the string representation - * @returns pointer to a static string. + * @returns a string that was created by mmt_strdup() */ -const char * pinmask_to_str(const pinmask_t * const pinmask); +char *pinmask_to_strdup(const pinmask_t * const pinmask); /* formerly serial.h */ diff --git a/src/pindefs.c b/src/pindefs.c index 4de9b9bd..5548a7b0 100644 --- a/src/pindefs.c +++ b/src/pindefs.c @@ -152,19 +152,17 @@ int pgm_fill_old_pins(PROGRAMMER * const pgm) { } /** - * This function returns a string representation of pins in the mask eg. 1,3,5-7,9,12 - * Another execution of this function will overwrite the previous result in the static buffer. - * Consecutive pin number are represented as start-end. + * This function returns a string representation of pins in the mask eg. 1, 3, 5-7, 9, 12 + * Consecutive pin numbers are represented as start-end. * * @param[in] pinmask the pin mask for which we want the string representation - * @returns pointer to a static string. + * @returns a string that was created by mmt_strdup() */ -const char * pinmask_to_str(const pinmask_t * const pinmask) { - static char buf[(PIN_MAX + 1) * 5]; // should be enough for PIN_MAX=255 +char *pinmask_to_strdup(const pinmask_t * const pinmask) { + char buf[6 * (PIN_MAX + 1)]; char *p = buf; int n; int pin; - const char * fmt; int start = -1; int end = -1; @@ -189,11 +187,8 @@ const char * pinmask_to_str(const pinmask_t * const pinmask) { start = pin; end = start; } - if(output) { - fmt = (buf[0] == 0) ? "%d" : ",%d"; - n = sprintf(p, fmt, pin); - p += n; - } + if(output) + p += n = sprintf(p, *buf? ", %d": "%d", pin); } } if(start != end) { @@ -201,10 +196,7 @@ const char * pinmask_to_str(const pinmask_t * const pinmask) { p += n; } - if(buf[0] == 0) - return "(no pins)"; - - return buf; + return mmt_strdup(*buf? buf: "(no pins)"); } @@ -277,26 +269,36 @@ int pins_check(const PROGRAMMER *const pgm, const struct pin_checklist_t *const } if(invalid) { if(output) { + char *mskinvalid = pinmask_to_strdup(invalid_used); pmsg_error("%s: these pins are not valid pins for this function: %s\n", - avr_pin_name(pinname), pinmask_to_str(invalid_used)); + avr_pin_name(pinname), mskinvalid); + mmt_free(mskinvalid); + char *mskvalid = pinmask_to_strdup(valid_pins->mask); pmsg_notice("%s: valid pins for this function are: %s\n", - avr_pin_name(pinname), pinmask_to_str(valid_pins->mask)); + avr_pin_name(pinname), mskvalid); + mmt_free(mskvalid); } is_ok = false; } if(inverse) { if(output) { + char *mskinvalidinv = pinmask_to_strdup(inverse_used); pmsg_error("%s: these pins are not usable as inverse pins for this function: %s\n", - avr_pin_name(pinname), pinmask_to_str(inverse_used)); + avr_pin_name(pinname), mskinvalidinv); + mmt_free(mskinvalidinv); + char *mskvalidinv = pinmask_to_strdup(valid_pins->inverse); pmsg_notice("%s: valid inverse pins for this function are: %s\n", - avr_pin_name(pinname), pinmask_to_str(valid_pins->inverse)); + avr_pin_name(pinname), mskvalidinv); + mmt_free(mskvalidinv); } is_ok = false; } if(used) { if(output) { + char *pmsk = pinmask_to_strdup(already_used); pmsg_error("%s: these pins are set for other functions too: %s\n", - avr_pin_name(pinname), pinmask_to_str(already_used)); + avr_pin_name(pinname), pmsk); + mmt_free(pmsk); is_ok = false; } } @@ -320,7 +322,7 @@ int pins_check(const PROGRAMMER *const pgm, const struct pin_checklist_t *const * This function returns a string of defined pins, eg, ~1, 2, ~4, ~5, 7 or "" * * @param[in] pindef the pin definition for which we want the string representation - * @returns a pointer to a string, which was created by mmt_strdup() + * @returns a string that was created by mmt_strdup() */ char *pins_to_strdup(const struct pindef_t * const pindef) { char buf[6*(PIN_MAX+1)], *p = buf;