term: fix memleakOnRealloc
Assign the newly allocated value to a temporary variable and in the case where we cannot allocate memory, free the initial pointer.
This commit is contained in:
parent
7bf9711392
commit
d5b2106644
22
src/term.c
22
src/term.c
|
@ -827,12 +827,26 @@ static int tokenize(char * s, char *** argv)
|
||||||
nbuf[0] = 0;
|
nbuf[0] = 0;
|
||||||
n++;
|
n++;
|
||||||
if ((n % 20) == 0) {
|
if ((n % 20) == 0) {
|
||||||
|
char *buf_tmp;
|
||||||
|
char **bufv_tmp;
|
||||||
/* realloc space for another 20 args */
|
/* realloc space for another 20 args */
|
||||||
bufsize += 20;
|
bufsize += 20;
|
||||||
nargs += 20;
|
nargs += 20;
|
||||||
bufp = buf;
|
bufp = buf;
|
||||||
buf = realloc(buf, bufsize);
|
buf_tmp = realloc(buf, bufsize);
|
||||||
bufv = realloc(bufv, nargs*sizeof(char *));
|
if (buf_tmp == NULL) {
|
||||||
|
free(buf);
|
||||||
|
free(bufv);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
buf = buf_tmp;
|
||||||
|
bufv_tmp = realloc(bufv, nargs*sizeof(char *));
|
||||||
|
if (bufv_tmp == NULL) {
|
||||||
|
free(buf);
|
||||||
|
free(bufv);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
bufv = bufv_tmp;
|
||||||
nbuf = &buf[l];
|
nbuf = &buf[l];
|
||||||
/* correct bufv pointers */
|
/* correct bufv pointers */
|
||||||
k = buf - bufp;
|
k = buf - bufp;
|
||||||
|
@ -950,6 +964,10 @@ int terminal_mode(PROGRAMMER * pgm, struct avrpart * p)
|
||||||
|
|
||||||
/* tokenize command line */
|
/* tokenize command line */
|
||||||
argc = tokenize(q, &argv);
|
argc = tokenize(q, &argv);
|
||||||
|
if (argc < 0) {
|
||||||
|
free(cmdbuf);
|
||||||
|
return argc;
|
||||||
|
}
|
||||||
|
|
||||||
fprintf(stdout, ">>> ");
|
fprintf(stdout, ">>> ");
|
||||||
for (i=0; i<argc; i++)
|
for (i=0; i<argc; i++)
|
||||||
|
|
Loading…
Reference in New Issue