Replace string arrays with const char * and allocated space (part 2)

This commit replaces fixed-string buffers in PROGRAMMER, AVRPART and AVRMEM
that are dealt with by the parser and grammar. Now, string assignments are
always to const char *, ie, these are read-only strings with arbitrary
length.

config_gram.y now only needs to consider one type of string assignment.

This commit also

  - Replaces the simple linear-search cache_string() function with faster
    hashed cache_string(). Either way, the returned value is likely to be
    shared, so should never be free()'d.

  - Duplicates hvupdi_support list in pgm_dup() and frees it in pgm_free()

  - Adds const qualifier to some function args in avrpart.c and pgm.c

  - Hardens some functions against being called with NULL pointers

  - Ensures _new() and _dup() functions for parts, programmers and memory
    return a suitable memory. Out of memory triggers exit in one of three
    functions, cfg_malloc(), cfg_realloc() and cfg_strdup(); there is
    rarely anything useful that AVRDUDE or, for that matter, any
    application compiled against libavrdude can do once you run out of
    memory as AVRDUDE/libavrdude rely heavily on allocation of memory.
This commit is contained in:
Stefan Rueger
2022-08-10 16:14:56 +01:00
parent 7375477f70
commit f4c5a8350d
13 changed files with 359 additions and 482 deletions

View File

@@ -193,16 +193,18 @@ static int pickit2_open(PROGRAMMER * pgm, char * port)
}
else
{
// get the device description while we're at it
short buff[PGM_DESCLEN-1], i;
HidD_GetProductString(PDATA(pgm)->usb_handle, buff, PGM_DESCLEN-1);
// Get the device description while we're at it and overlay it on pgm->desc
short wbuf[80-1];
char *cbuf = cfg_malloc("pickit2_open()", sizeof wbuf/sizeof*wbuf + (pgm->desc? strlen(pgm->desc): 0) + 2);
HidD_GetProductString(PDATA(pgm)->usb_handle, wbuf, sizeof wbuf/sizeof*wbuf);
// convert from wide chars, but do not overwrite trailing '\0'
memset(&(pgm->desc), 0, PGM_DESCLEN);
for (i = 0; i < (PGM_DESCLEN-1) && buff[i]; i++)
{
pgm->desc[i] = (char)buff[i]; // TODO what about little/big endian???
}
if(pgm->desc && *pgm->desc)
strcpy(cbuf, pgm->desc);
// Convert from wide chars and overlay over initial part of desc
for (int i = 0; i < sizeof wbuf/sizeof*wbuf && wbuf[i]; i++)
cbuf[i] = (char) wbuf[i]; // TODO what about little/big endian???
pgm->desc = cache_string(cbuf);
}
#else
if (usb_open_device(&(PDATA(pgm)->usb_handle), PICKIT2_VID, PICKIT2_PID) < 0)