Skip to content

Commit 17b67da

Browse files
committed
Make sure memory can be filled with a string
... and not just the last character
1 parent 795dd91 commit 17b67da

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/term.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,10 +422,15 @@ static int cmd_write(PROGRAMMER * pgm, struct avrpart * p,
422422
for (i = start_offset; i < len + start_offset; i++) {
423423
data.is_float = false;
424424
data.size = 0;
425-
data.str_ptr = NULL;
426425

427426
// Handle the next argument
428427
if (i < argc - start_offset + 3) {
428+
// Free string pointer if already allocated
429+
if(data.str_ptr) {
430+
free(data.str_ptr);
431+
data.str_ptr = NULL;
432+
}
433+
429434
// Get suffix if present
430435
char suffix = argv[i][strlen(argv[i]) - 1];
431436
char lsuffix = argv[i][strlen(argv[i]) - 2];
@@ -474,6 +479,8 @@ static int cmd_write(PROGRAMMER * pgm, struct avrpart * p,
474479
avrdude_message(MSG_INFO, "\n%s (write): can't parse data '%s'\n",
475480
progname, argv[i]);
476481
free(buf);
482+
if(data.str_ptr != NULL)
483+
free(data.str_ptr);
477484
return -1;
478485
}
479486
}
@@ -513,7 +520,6 @@ static int cmd_write(PROGRAMMER * pgm, struct avrpart * p,
513520
if(data.str_ptr) {
514521
for(int16_t j = 0; j < strlen(data.str_ptr); j++)
515522
buf[i - start_offset + data.bytes_grown++] = (uint8_t)data.str_ptr[j];
516-
free(data.str_ptr);
517523
} else {
518524
buf[i - start_offset + data.bytes_grown] = data.a[0];
519525
if (llabs(data.ll) > 0x000000FF || data.size >= 2 || data.is_float)
@@ -543,6 +549,9 @@ static int cmd_write(PROGRAMMER * pgm, struct avrpart * p,
543549
return -1;
544550
}
545551

552+
if(data.str_ptr)
553+
free(data.str_ptr);
554+
546555
avrdude_message(MSG_NOTICE, "\nInfo: Writing %d bytes starting from address 0x%02x",
547556
len + data.bytes_grown, addr);
548557
if (write_mode == WRITE_MODE_FILL)

0 commit comments

Comments
 (0)