ref: 2d2015a2f8e5911c923805a06542b25c9cad6d0f
parent: ca1cd89feec2afd35fa99e37daeb7dbed4534313
author: cinap_lenrek <cinap_lenrek@gmx.de>
date: Tue Jul 31 05:43:24 EDT 2012
mothra: make emalloc zero memory, fix uninitialized nextline pointer crash
--- a/sys/src/cmd/mothra/forms.c
+++ b/sys/src/cmd/mothra/forms.c
@@ -72,7 +72,7 @@
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 @@
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 @@
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;
--- a/sys/src/cmd/mothra/getpix.c
+++ b/sys/src/cmd/mothra/getpix.c
@@ -75,7 +75,7 @@
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;
--- a/sys/src/cmd/mothra/libpanel/entry.c
+++ b/sys/src/cmd/mothra/libpanel/entry.c
@@ -28,8 +28,9 @@
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;
}
--- 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 @@
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;
--- a/sys/src/cmd/mothra/libpanel/textwin.c
+++ b/sys/src/cmd/mothra/libpanel/textwin.c
@@ -370,8 +370,7 @@
int nbyte;
if(first<t->top || last<first || t->bot<last) return;
nbyte=(last-first+1)*sizeof(Point);
- srcloc=malloc(nbyte);
- if(srcloc==0) return;
+ srcloc=pl_emalloc(nbyte);
memmove(srcloc, &t->loc[first-t->top], nbyte);
tw_setloc(t, first, last, dst);
if(tw_before(t, dst, srcloc[0]))
@@ -445,19 +444,9 @@
}
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;
--- a/sys/src/cmd/mothra/mothra.c
+++ b/sys/src/cmd/mothra/mothra.c
@@ -458,17 +458,10 @@
v=malloc(n);
if(v==0)
sysfatal("out of memory");+ memset(v, 0, n);
setmalloctag(v, getcallerpc(&n));
return v;
}
-void *emallocz(int n, int z){- void *v;
- v = emalloc(n);
- if(z)
- memset(v, 0, n);
- setmalloctag(v, getcallerpc(&n));
- return v;
-}
void nstrcpy(char *to, char *from, int len){strncpy(to, from, len);
to[len-1] = 0;
@@ -982,7 +975,7 @@
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;
--- a/sys/src/cmd/mothra/mothra.h
+++ b/sys/src/cmd/mothra/mothra.h
@@ -89,7 +89,6 @@
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);
--- a/sys/src/cmd/mothra/rdhtml.c
+++ b/sys/src/cmd/mothra/rdhtml.c
@@ -117,19 +117,17 @@
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;
--
⑨