Ensure enough memory is allocated for buf in terminal write

This commit is contained in:
Stefan Rueger 2022-07-12 11:16:16 +01:00
parent d3ad078577
commit 177834ae7c
1 changed files with 11 additions and 1 deletions

View File

@ -334,6 +334,16 @@ static int cmd_dump(PROGRAMMER * pgm, struct avrpart * p,
}
static size_t maxstrlen(int argc, char **argv) {
size_t max = 0;
for(int i=0; i<argc; i++)
if(strlen(argv[i]) > max)
max = strlen(argv[i]);
return max;
}
static int cmd_write(PROGRAMMER * pgm, struct avrpart * p,
int argc, char * argv[])
{
@ -374,7 +384,7 @@ static int cmd_write(PROGRAMMER * pgm, struct avrpart * p,
}
// Allocate a buffer guaranteed to be large enough
uint8_t * buf = calloc(mem->size + 0x10 + strlen(argv[argc - 2]), sizeof(uint8_t));
uint8_t * buf = calloc(mem->size + 0x10 + maxstrlen(argc-3, argv+3), sizeof(uint8_t));
if (buf == NULL) {
avrdude_message(MSG_INFO, "%s (write): out of memory\n", progname);
return -1;