From 02acb1d4f00648b3ececd8fee2dc065f868e432e Mon Sep 17 00:00:00 2001 From: cinap_lenrek Date: Tue, 31 Jul 2012 09:43:24 +0200 Subject: [PATCH] mothra: make emalloc zero memory, fix uninitialized nextline pointer crash --- sys/src/cmd/mothra/forms.c | 6 +++--- sys/src/cmd/mothra/getpix.c | 2 +- sys/src/cmd/mothra/libpanel/entry.c | 5 +++-- sys/src/cmd/mothra/libpanel/rtext.c | 4 ++-- sys/src/cmd/mothra/libpanel/textwin.c | 19 ++++--------------- sys/src/cmd/mothra/mothra.c | 11 ++--------- sys/src/cmd/mothra/mothra.h | 1 - sys/src/cmd/mothra/rdhtml.c | 24 +++++++++++------------- 8 files changed, 26 insertions(+), 46 deletions(-) diff --git a/sys/src/cmd/mothra/forms.c b/sys/src/cmd/mothra/forms.c index f3ca17af0..aa5a89056 100644 --- a/sys/src/cmd/mothra/forms.c +++ b/sys/src/cmd/mothra/forms.c @@ -72,7 +72,7 @@ char *selgen(Panel *, int); char *nullgen(Panel *, int); Field *newfield(Form *form){ Field *f; - f=emallocz(sizeof(Field), 1); + f=emalloc(sizeof(Field)); if(form->efields==0) form->fields=f; else @@ -99,7 +99,7 @@ void rdform(Hglob *g){ htmlerror(g->name, g->lineno, "nested forms illegal\n"); break; } - g->form=emallocz(sizeof(Form), 1); + g->form=emalloc(sizeof(Form)); s=pl_getattr(g->attr, "action"); g->form->action=strdup((s && s[0]) ? s : g->dst->url->fullname); s=pl_getattr(g->attr, "method"); @@ -223,7 +223,7 @@ void rdform(Hglob *g){ if((f=g->form->efields)==0) goto BadTag; if(f->size<8) f->size++; - o=emallocz(sizeof(Option), 1); + o=emalloc(sizeof(Option)); for(op=&f->options;*op;op=&(*op)->next); *op=o; o->next=0; diff --git a/sys/src/cmd/mothra/getpix.c b/sys/src/cmd/mothra/getpix.c index d3896e159..9a56c8a44 100644 --- a/sys/src/cmd/mothra/getpix.c +++ b/sys/src/cmd/mothra/getpix.c @@ -75,7 +75,7 @@ void getimage(Rtext *t, Www *w){ goto Err; } close(fd); - p = emallocz(sizeof(Pix), 1); + p=emalloc(sizeof(Pix)); nstrcpy(p->name, ap->image, sizeof(p->name)); p->b=b; p->width=ap->width; diff --git a/sys/src/cmd/mothra/libpanel/entry.c b/sys/src/cmd/mothra/libpanel/entry.c index 30e12a6e1..a335706d0 100644 --- a/sys/src/cmd/mothra/libpanel/entry.c +++ b/sys/src/cmd/mothra/libpanel/entry.c @@ -28,8 +28,9 @@ void pl_snarfentry(Panel *p, int cut){ write(fd, ep->entry, n); ep->entp=ep->entry; }else{ - n = 1024; - if((s=malloc(n+SLACK))==0){ + n=1024; + s=malloc(n+SLACK); + if(s==0){ close(fd); return; } diff --git a/sys/src/cmd/mothra/libpanel/rtext.c b/sys/src/cmd/mothra/libpanel/rtext.c index 51ef57721..aff671773 100644 --- a/sys/src/cmd/mothra/libpanel/rtext.c +++ b/sys/src/cmd/mothra/libpanel/rtext.c @@ -12,8 +12,7 @@ #define LEAD 4 /* extra space between lines */ Rtext *pl_rtnew(Rtext **t, int space, int indent, Image *b, Panel *p, Font *f, char *s, int hot, void *user){ Rtext *new; - new=malloc(sizeof(Rtext)); - if(new==0) return 0; + new=pl_emalloc(sizeof(Rtext)); new->hot=hot; new->user=user; new->space=space; @@ -23,6 +22,7 @@ Rtext *pl_rtnew(Rtext **t, int space, int indent, Image *b, Panel *p, Font *f, c new->font=f; new->text=s; new->next=0; + new->nextline=0; new->r=Rect(0,0,0,0); if(*t) (*t)->last->next=new; diff --git a/sys/src/cmd/mothra/libpanel/textwin.c b/sys/src/cmd/mothra/libpanel/textwin.c index 01c8a3573..94479f0c8 100644 --- a/sys/src/cmd/mothra/libpanel/textwin.c +++ b/sys/src/cmd/mothra/libpanel/textwin.c @@ -370,8 +370,7 @@ void tw_relocate(Textwin *t, int first, int last, Point dst){ int nbyte; if(firsttop || lastbotloc[first-t->top], nbyte); tw_setloc(t, first, last, dst); if(tw_before(t, dst, srcloc[0])) @@ -445,19 +444,9 @@ void twreshape(Textwin *t, Rectangle r){ } Textwin *twnew(Image *b, Font *f, Rune *text, int ntext){ Textwin *t; - t=malloc(sizeof(Textwin)); - if(t==0) return 0; - t->text=malloc((ntext+SLACK)*sizeof(Rune)); - if(t->text==0){ - free(t); - return 0; - } - t->loc=malloc(SLACK*sizeof(Point)); - if(t->loc==0){ - free(t->text); - free(t); - return 0; - } + t=pl_emalloc(sizeof(Textwin)); + t->text=pl_emalloc((ntext+SLACK)*sizeof(Rune)); + t->loc=pl_emalloc(SLACK*sizeof(Point)); t->eloc=t->loc+SLACK; t->etext=t->text+ntext; t->eslack=t->etext+SLACK; diff --git a/sys/src/cmd/mothra/mothra.c b/sys/src/cmd/mothra/mothra.c index 44ca1db15..5bedc1fe7 100644 --- a/sys/src/cmd/mothra/mothra.c +++ b/sys/src/cmd/mothra/mothra.c @@ -458,14 +458,7 @@ void *emalloc(int n){ v=malloc(n); if(v==0) sysfatal("out of memory"); - setmalloctag(v, getcallerpc(&n)); - return v; -} -void *emallocz(int n, int z){ - void *v; - v = emalloc(n); - if(z) - memset(v, 0, n); + memset(v, 0, n); setmalloctag(v, getcallerpc(&n)); return v; } @@ -982,7 +975,7 @@ mothon(Www *w, int on) x = t->next; if(on){ t->next = nil; - ap=mallocz(sizeof(Action), 1); + ap=emalloc(sizeof(Action)); ap->link = strdup(a->link); plrtstr(&t->next, 0, 0, t->font, strdup("->"), 1, ap); t->next->next = x; diff --git a/sys/src/cmd/mothra/mothra.h b/sys/src/cmd/mothra/mothra.h index dc4b54103..84d117dbf 100644 --- a/sys/src/cmd/mothra/mothra.h +++ b/sys/src/cmd/mothra/mothra.h @@ -89,7 +89,6 @@ void freepix(void *p); int pipeline(char *, int); void getfonts(void); void *emalloc(int); -void *emallocz(int, int); void nstrcpy(char *to, char *from, int len); void freeform(void *p); int Ufmt(Fmt *f); diff --git a/sys/src/cmd/mothra/rdhtml.c b/sys/src/cmd/mothra/rdhtml.c index 0f0c90f1f..7bcf7367b 100644 --- a/sys/src/cmd/mothra/rdhtml.c +++ b/sys/src/cmd/mothra/rdhtml.c @@ -117,19 +117,17 @@ void pl_htmloutput(Hglob *g, int nsp, char *s, Field *field){ if(g->state->image[0]==0 && g->state->link[0]==0 && g->state->name[0]==0 && field==0) ap=0; else{ - ap=mallocz(sizeof(Action), 1); - if(ap!=0){ - if(g->state->image[0]) - ap->image = strdup(g->state->image); - if(g->state->link[0]) - ap->link = strdup(g->state->link); - if(g->state->name[0]) - ap->name = strdup(g->state->name); - ap->ismap=g->state->ismap; - ap->width=g->state->width; - ap->height=g->state->height; - ap->field=field; - } + ap=emalloc(sizeof(Action)); + if(g->state->image[0]) + ap->image = strdup(g->state->image); + if(g->state->link[0]) + ap->link = strdup(g->state->link); + if(g->state->name[0]) + ap->name = strdup(g->state->name); + ap->ismap=g->state->ismap; + ap->width=g->state->width; + ap->height=g->state->height; + ap->field=field; } if(space<0) space=0; if(indent<0) indent=0;