diff --git a/src/frontend/breakp.c b/src/frontend/breakp.c index 98bf66c92..5b5e702b1 100644 --- a/src/frontend/breakp.c +++ b/src/frontend/breakp.c @@ -165,6 +165,7 @@ com_iplot(wordlist *wl) d->db_type = DB_IPLOT; d->db_nodename1 = copy(s); } + tfree(s);/*DG: avoid memory leak */ d->db_also = currentdb; currentdb = d; wl = wl->wl_next; diff --git a/src/frontend/breakp2.c b/src/frontend/breakp2.c index 6764dcb11..c714002c6 100644 --- a/src/frontend/breakp2.c +++ b/src/frontend/breakp2.c @@ -85,6 +85,7 @@ settrace(wordlist *wl, int what, char *name) d->db_nodename1 = copy(s); /* wrd_chtrace(s, TRUE, what); */ } + tfree(s);/*DG avoid memoy leak */ if (dbs) { for (td = dbs; td->db_next; td = td->db_next) ; diff --git a/src/frontend/com_compose.c b/src/frontend/com_compose.c index a29a3a8e4..1e0ee732e 100644 --- a/src/frontend/com_compose.c +++ b/src/frontend/com_compose.c @@ -479,5 +479,6 @@ com_compose(wordlist *wl) result->v_dims[0] = length; vec_new(result); cp_addkword(CT_VECTOR, result->v_name); + tfree(resname);/*DG: resname has been copied so its remains allocated: memory leak One can remove this and not copy resname*/ return; } diff --git a/src/frontend/com_display.c b/src/frontend/com_display.c index c61dd2da1..71c0c2e31 100644 --- a/src/frontend/com_display.c +++ b/src/frontend/com_display.c @@ -41,6 +41,7 @@ com_display(wordlist *wl) while (wl) { s = cp_unquote(wl->wl_word); d = vec_get(s); + tfree(s);/*DG to avoid the cp_unquote memory leak */ if (d == NULL) fprintf(cp_err, "Error: no such vector as %s.\n", wl->wl_word); diff --git a/src/frontend/com_echo.c b/src/frontend/com_echo.c index 1638a41ae..6c980b4b3 100644 --- a/src/frontend/com_echo.c +++ b/src/frontend/com_echo.c @@ -8,7 +8,7 @@ void com_echo(wordlist *wlist) -{ +{ char*copyword; bool nl = TRUE; if (wlist && eq(wlist->wl_word, "-n")) { @@ -17,7 +17,10 @@ com_echo(wordlist *wlist) } while (wlist) { - fputs(cp_unquote(wlist->wl_word), cp_out); + /* fputs(cp_unquote(wlist->wl_word), cp_out); very bad the string allocated by cp_unquote could not be freed: memory leak*/ + copyword=cp_unquote(wlist->wl_word); + fputs(copyword, cp_out); + tfree(copyword); if (wlist->wl_next) fputs(" ", cp_out); wlist = wlist->wl_next; diff --git a/src/frontend/com_setscale.c b/src/frontend/com_setscale.c index dc76a7687..7418f30c1 100644 --- a/src/frontend/com_setscale.c +++ b/src/frontend/com_setscale.c @@ -22,6 +22,7 @@ com_setscale(wordlist *wl) if (wl) { s = cp_unquote(wl->wl_word); d = vec_get(s); + if(s) tfree(s);/*DG to avoid the cp_unquote memory leak */ if (d == NULL) fprintf(cp_err, "Error: no such vector as %s.\n", wl->wl_word); diff --git a/src/frontend/com_strcmp.c b/src/frontend/com_strcmp.c index 2475985c5..1a035397a 100644 --- a/src/frontend/com_strcmp.c +++ b/src/frontend/com_strcmp.c @@ -21,7 +21,8 @@ com_strcmp(wordlist *wl) s2 = cp_unquote(wl->wl_next->wl_next->wl_word); i = strcmp(s1, s2); - + tfree(s1);/*DG cp_unquote memory leak*/ + tfree(s2); cp_vset(var, VT_NUM, (char *) &i); return; } diff --git a/src/frontend/cpitf.c b/src/frontend/cpitf.c index 72a267539..c1f68436e 100644 --- a/src/frontend/cpitf.c +++ b/src/frontend/cpitf.c @@ -23,7 +23,7 @@ ft_cpinit(void) wordlist *wl; wordlist wl1, wl2, wl3; bool found = FALSE, t = TRUE; - char buf[BSIZE_SP], **x, *s, *r; + char buf[BSIZE_SP], **x, *s, *r,*copys; struct comm *c; int i; FILE *fp; @@ -206,7 +206,7 @@ ft_cpinit(void) /* Now source the standard startup file. */ /* XXX strange */ - for (s = cp_tildexpand(Lib_Path); s && *s; ) { + for (copys=s=cp_tildexpand(Lib_Path); s && *s; ) {/*DG*/ while (isspace(*s)) s++; for (r = buf; *s && !isspace(*s); r++, s++) @@ -236,7 +236,7 @@ ft_cpinit(void) } tcap_init( ); - + tfree(copys);/*DG Avoid memory leak*/ return; } diff --git a/src/frontend/inpcom.c b/src/frontend/inpcom.c index ce6c394f8..64afea4ad 100644 --- a/src/frontend/inpcom.c +++ b/src/frontend/inpcom.c @@ -112,7 +112,7 @@ void inp_readall(FILE *fp, struct line **data) { struct line *end = NULL, *cc, *prev = NULL, *working, *newcard; - char *buffer, *s, *t, c; + char *buffer, *s, *t, c,*copys; int line = 1; FILE *newfp; @@ -140,7 +140,12 @@ inp_readall(FILE *fp, struct line **data) ; *t = '\0'; if (*s == '~') - s = cp_tildexpand(s); + { + copys=cp_tildexpand(s); /*DG*/ + /*s = cp_tildexpand(s); very bad the last reference is los: memory leak*/ + strcpy(s,copys); + tfree(copys); + } if (!(newfp = inp_pathopen(s, "r"))) { perror(s); continue; diff --git a/src/frontend/parser/complete.c b/src/frontend/parser/complete.c index de74d60f1..595bc4f5d 100644 --- a/src/frontend/parser/complete.c +++ b/src/frontend/parser/complete.c @@ -87,7 +87,10 @@ cp_ccom(wordlist *wlist, char *buf, bool esc) int i=0; int j, arg; - buf = cp_unquote(copy(buf)); +/* buf = cp_unquote(copy(buf)); DG: ugly*/ + s=cp_unquote(buf);/*DG*/ + strcpy(buf,s); + tfree(s); cp_wstrip(buf); if (wlist->wl_next) { /* Not the first word. */ cc = getccom(wlist->wl_word); @@ -194,7 +197,7 @@ static wordlist * ccfilec(char *buf) { DIR *wdir; - char *lcomp, *dir; + char *lcomp, *dir,*copydir; struct direct *de; wordlist *wl = NULL, *t; struct passwd *pw; @@ -231,7 +234,10 @@ ccfilec(char *buf) *lcomp = '\0'; lcomp++; if (*dir == cp_til) { - dir = cp_tildexpand(dir); + copydir=cp_tildexpand(dir);/*DG*/ + /*dir = cp_tildexpand(dir); very bad the last reference is lost: memory leak*/ + strcpy(dir,copydir); + tfree(copydir); if (dir == NULL) return (NULL); } diff --git a/src/frontend/postcoms.c b/src/frontend/postcoms.c index 802f82acc..b79ecc496 100644 --- a/src/frontend/postcoms.c +++ b/src/frontend/postcoms.c @@ -40,12 +40,15 @@ com_unlet(wordlist *wl) void com_load(wordlist *wl) { - +char *copypath; if (!wl) ft_loadfile(ft_rawfile); else while (wl) { - ft_loadfile(cp_unquote(wl->wl_word)); + /*ft_loadfile(cp_unquote(wl->wl_word)); DG: bad memory leak*/ + copypath=cp_unquote(wl->wl_word);/*DG*/ + ft_loadfile(copypath); + tfree(copypath); wl = wl->wl_next; } @@ -140,9 +143,16 @@ com_print(wordlist *wl) out_printf("%s = %s\n", buf, printnum(*v->v_realdata)); } else { + /*DG: memory leak here copy of the string returned by printnum will never be freed out_printf("%s = %s,%s\n", buf, copy(printnum(realpart(v->v_compdata))), copy(printnum(imagpart(v->v_compdata)))); + */ + out_printf("%s = %s,%s\n", buf, + printnum(realpart(v->v_compdata)), + printnum(imagpart(v->v_compdata))); + + } } else { out_printf("%s = ( ", buf); @@ -160,9 +170,10 @@ com_print(wordlist *wl) } else out_send("\t"); } else { + /*DG*/ (void) sprintf(buf, "%s,%s", - copy(printnum(realpart(&v->v_compdata[i]))), - copy(printnum(imagpart(&v->v_compdata[i])))); + printnum(realpart(&v->v_compdata[i])), + printnum(imagpart(&v->v_compdata[i]))); out_send(buf); ll += strlen(buf); ll = (ll + 7) / 8; @@ -460,6 +471,7 @@ com_transpose(wordlist *wl) while (wl) { s = cp_unquote(wl->wl_word); d = vec_get(s); + tfree(s); /*DG: Avoid Memory Leak */ if (d == NULL) fprintf(cp_err, "Error: no such vector as %s.\n", wl->wl_word); diff --git a/src/frontend/resource.c b/src/frontend/resource.c index a9d82630b..a24751a92 100644 --- a/src/frontend/resource.c +++ b/src/frontend/resource.c @@ -58,13 +58,17 @@ init_time(void) void com_rusage(wordlist *wl) { +char* copyword; /* Fill in the SPICE accounting structure... */ if (wl && (eq(wl->wl_word, "everything") || eq(wl->wl_word, "all"))) { printres((char *) NULL); } else if (wl) { for (; wl; wl = wl->wl_next) { - printres(cp_unquote(wl->wl_word)); + /* printres(cp_unquote(wl->wl_word)); DG: bad, memory leak*/ + copyword=cp_unquote(wl->wl_word);/*DG*/ + printres(copyword); + tfree(copyword); if (wl->wl_next) (void) putc('\n', cp_out); } diff --git a/src/frontend/streams.c b/src/frontend/streams.c index bb51c2aff..2c7b38049 100644 --- a/src/frontend/streams.c +++ b/src/frontend/streams.c @@ -45,7 +45,7 @@ cp_redirect(wordlist *wl) bool gotinput = FALSE, gotoutput = FALSE, goterror = FALSE; bool app = FALSE, erralso = FALSE; wordlist *w, *bt, *nw; - char *s; + char *s,*copyword; FILE *tmpfp; w = wl->wl_next; /* Don't consider empty commands. */ @@ -67,7 +67,11 @@ cp_redirect(wordlist *wl) if (*w->wl_word == cp_lt) { /* Do reasonable stuff here... */ } else { - tmpfp = fopen(cp_unquote(w->wl_word), "r"); + /*tmpfp = fopen(cp_unquote(w->wl_word), "r"); DG very bad: memory leak the string allocated by cp_unquote is lost*/ + copyword=cp_unquote(w->wl_word);/*DG*/ + tmpfp = fopen(copyword, "r"); + tfree(copyword); + if (!tmpfp) { perror(w->wl_word); goto error; @@ -133,6 +137,7 @@ cp_redirect(wordlist *wl) tmpfp = fopen(s, "a"); else tmpfp = fopen(s, "w+"); + tfree(s);/*DG cp_unquote memory leak*/ if (!tmpfp) { perror(w->wl_word); goto error; diff --git a/src/frontend/variable.c b/src/frontend/variable.c index 473af5677..d29b9bf95 100644 --- a/src/frontend/variable.c +++ b/src/frontend/variable.c @@ -34,7 +34,7 @@ wordlist * cp_varwl(struct variable *var) { wordlist *wl = NULL, *w, *wx = NULL; - char buf[BSIZE_SP]; + char buf[BSIZE_SP],*copystring; struct variable *vt; switch(var->va_type) { @@ -50,7 +50,11 @@ cp_varwl(struct variable *var) sprintf(buf, "%G", var->va_real); break; case VT_STRING: - strcpy(buf, cp_unquote(var->va_string)); + /*strcpy(buf, cp_unquote(var->va_string)); DG: memory leak here*/ + copystring= cp_unquote(var->va_string);/*DG*/ + strcpy(buf,copystring); + tfree(copystring); + break; case VT_LIST: /* The tricky case. */ for (vt = var->va_vlist; vt; vt = vt->va_next) { @@ -84,8 +88,11 @@ cp_vset(char *varname, char type, char *value) struct variable *v, *u, *w; int i; bool alreadythere = FALSE; - - varname = cp_unquote(varname); + char* copyvarname; +/* varname = cp_unquote(varname); DG: Memory leak old varname is lost*/ + copyvarname= cp_unquote(varname); + strcpy(varname,copyvarname); + tfree(copyvarname); w = NULL; for (v = variables; v; v = v->va_next) { if (eq(varname, v->va_name)) { @@ -244,13 +251,15 @@ cp_setparse(wordlist *wl) vv->va_bool = TRUE; vv->va_next = vars; vars = vv; + tfree(name);/*DG: cp_unquote Memory leak*/ continue; } if (wl && eq(wl->wl_word, "=")) { wl = wl->wl_next; if (wl == NULL) { fprintf(cp_err, "Error: bad set form.\n"); - return (NULL); + tfree(name);/*DG: cp_unquote Memory leak*/ + return (NULL); } val = wl->wl_word; wl = wl->wl_next; @@ -265,7 +274,8 @@ cp_setparse(wordlist *wl) fprintf(cp_err, "Error: %s equals what?.\n", name); - return (NULL); + tfree(name);/*DG: cp_unquote Memory leak: free name before exiting*/ + return (NULL); } else { val = wl->wl_word; wl = wl->wl_next; @@ -273,9 +283,13 @@ cp_setparse(wordlist *wl) } } else { fprintf(cp_err, "Error: bad set form.\n"); - return (NULL); + tfree(name);/*DG: cp_unquote Memory leak: free name befor exiting */ + return (NULL); } - val = cp_unquote(val); + /* val = cp_unquote(val); DG: bad old val is lost*/ + copyval=cp_unquote(val);/*DG*/ + strcpy(val,copyval); + tfree(copyval); if (eq(val, "(")) { /* ) */ /* The beginning of a list... We have to walk down the * list until we find a close paren... If there are nested @@ -299,6 +313,7 @@ cp_setparse(wordlist *wl) vv->va_type = VT_STRING; vv->va_string = copy(ss); } + tfree(ss);/*DG: must free ss any way to avoid cp_unquote memory leak*/ if (listv) { lv->va_next = vv; lv = vv; @@ -336,6 +351,7 @@ cp_setparse(wordlist *wl) vv->va_type = VT_STRING; vv->va_string = copy(val); } + tfree(ss);/*DG: avoid cp_unquote memory leak */ } return (vars); } @@ -372,7 +388,8 @@ cp_remvar(char *varname) else if (eq(varname, "noclobber")) cp_noclobber = FALSE; else if (eq(varname, "prompt")) - cp_promptstring = ""; + /* cp_promptstring = ""; Memory leak here the last allocated reference wil be lost*/ + if(cp_promptstring)strcpy(cp_promptstring,"");/*DG avoid memory leak*/ else if (eq(varname, "cpdebug")) cp_debug = FALSE; else if (eq(varname, "ignoreeof")) @@ -469,6 +486,7 @@ cp_getvar(char *name, int type, char *retval) s = cp_unquote(v->va_string); cp_wstrip(s); (void) strcpy(retval, s); + tfree(s);/*DG*/ break; } case VT_LIST: { /* Funny case... */ diff --git a/src/main.c b/src/main.c index 8cced1086..205c192a5 100644 --- a/src/main.c +++ b/src/main.c @@ -243,7 +243,7 @@ main(int argc, char **argv) int c; int err; bool gotone = FALSE; - + char* copystring;/*DG*/ #ifdef SIMULATOR int error2; @@ -517,7 +517,9 @@ main(int argc, char **argv) com_version(NULL); DevInit( ); if (News_File && *News_File) { - fp = fopen(cp_tildexpand(News_File), "r"); + copystring=cp_tildexpand(News_File);/*DG Memory leak */ + fp = fopen(copystring, "r"); + tfree(copystring); if (fp) { while (fgets(buf, BSIZE_SP, fp)) fputs(buf, stdout);