mothra: make emalloc zero memory, fix uninitialized nextline pointer crash

This commit is contained in:
cinap_lenrek 2012-07-31 09:43:24 +02:00
parent a5d30a3d04
commit 02acb1d4f0
8 changed files with 26 additions and 46 deletions

View file

@ -72,7 +72,7 @@ char *selgen(Panel *, int);
char *nullgen(Panel *, int); char *nullgen(Panel *, int);
Field *newfield(Form *form){ Field *newfield(Form *form){
Field *f; Field *f;
f=emallocz(sizeof(Field), 1); f=emalloc(sizeof(Field));
if(form->efields==0) if(form->efields==0)
form->fields=f; form->fields=f;
else else
@ -99,7 +99,7 @@ void rdform(Hglob *g){
htmlerror(g->name, g->lineno, "nested forms illegal\n"); htmlerror(g->name, g->lineno, "nested forms illegal\n");
break; break;
} }
g->form=emallocz(sizeof(Form), 1); g->form=emalloc(sizeof(Form));
s=pl_getattr(g->attr, "action"); s=pl_getattr(g->attr, "action");
g->form->action=strdup((s && s[0]) ? s : g->dst->url->fullname); g->form->action=strdup((s && s[0]) ? s : g->dst->url->fullname);
s=pl_getattr(g->attr, "method"); s=pl_getattr(g->attr, "method");
@ -223,7 +223,7 @@ void rdform(Hglob *g){
if((f=g->form->efields)==0) goto BadTag; if((f=g->form->efields)==0) goto BadTag;
if(f->size<8) if(f->size<8)
f->size++; f->size++;
o=emallocz(sizeof(Option), 1); o=emalloc(sizeof(Option));
for(op=&f->options;*op;op=&(*op)->next); for(op=&f->options;*op;op=&(*op)->next);
*op=o; *op=o;
o->next=0; o->next=0;

View file

@ -75,7 +75,7 @@ void getimage(Rtext *t, Www *w){
goto Err; goto Err;
} }
close(fd); close(fd);
p = emallocz(sizeof(Pix), 1); p=emalloc(sizeof(Pix));
nstrcpy(p->name, ap->image, sizeof(p->name)); nstrcpy(p->name, ap->image, sizeof(p->name));
p->b=b; p->b=b;
p->width=ap->width; p->width=ap->width;

View file

@ -28,8 +28,9 @@ void pl_snarfentry(Panel *p, int cut){
write(fd, ep->entry, n); write(fd, ep->entry, n);
ep->entp=ep->entry; ep->entp=ep->entry;
}else{ }else{
n = 1024; n=1024;
if((s=malloc(n+SLACK))==0){ s=malloc(n+SLACK);
if(s==0){
close(fd); close(fd);
return; return;
} }

View file

@ -12,8 +12,7 @@
#define LEAD 4 /* extra space between lines */ #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 *pl_rtnew(Rtext **t, int space, int indent, Image *b, Panel *p, Font *f, char *s, int hot, void *user){
Rtext *new; Rtext *new;
new=malloc(sizeof(Rtext)); new=pl_emalloc(sizeof(Rtext));
if(new==0) return 0;
new->hot=hot; new->hot=hot;
new->user=user; new->user=user;
new->space=space; 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->font=f;
new->text=s; new->text=s;
new->next=0; new->next=0;
new->nextline=0;
new->r=Rect(0,0,0,0); new->r=Rect(0,0,0,0);
if(*t) if(*t)
(*t)->last->next=new; (*t)->last->next=new;

View file

@ -370,8 +370,7 @@ void tw_relocate(Textwin *t, int first, int last, Point dst){
int nbyte; int nbyte;
if(first<t->top || last<first || t->bot<last) return; if(first<t->top || last<first || t->bot<last) return;
nbyte=(last-first+1)*sizeof(Point); nbyte=(last-first+1)*sizeof(Point);
srcloc=malloc(nbyte); srcloc=pl_emalloc(nbyte);
if(srcloc==0) return;
memmove(srcloc, &t->loc[first-t->top], nbyte); memmove(srcloc, &t->loc[first-t->top], nbyte);
tw_setloc(t, first, last, dst); tw_setloc(t, first, last, dst);
if(tw_before(t, dst, srcloc[0])) 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 *twnew(Image *b, Font *f, Rune *text, int ntext){
Textwin *t; Textwin *t;
t=malloc(sizeof(Textwin)); t=pl_emalloc(sizeof(Textwin));
if(t==0) return 0; t->text=pl_emalloc((ntext+SLACK)*sizeof(Rune));
t->text=malloc((ntext+SLACK)*sizeof(Rune)); t->loc=pl_emalloc(SLACK*sizeof(Point));
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->eloc=t->loc+SLACK; t->eloc=t->loc+SLACK;
t->etext=t->text+ntext; t->etext=t->text+ntext;
t->eslack=t->etext+SLACK; t->eslack=t->etext+SLACK;

View file

@ -458,14 +458,7 @@ void *emalloc(int n){
v=malloc(n); v=malloc(n);
if(v==0) if(v==0)
sysfatal("out of memory"); sysfatal("out of memory");
setmalloctag(v, getcallerpc(&n)); memset(v, 0, n);
return v;
}
void *emallocz(int n, int z){
void *v;
v = emalloc(n);
if(z)
memset(v, 0, n);
setmalloctag(v, getcallerpc(&n)); setmalloctag(v, getcallerpc(&n));
return v; return v;
} }
@ -982,7 +975,7 @@ mothon(Www *w, int on)
x = t->next; x = t->next;
if(on){ if(on){
t->next = nil; t->next = nil;
ap=mallocz(sizeof(Action), 1); ap=emalloc(sizeof(Action));
ap->link = strdup(a->link); ap->link = strdup(a->link);
plrtstr(&t->next, 0, 0, t->font, strdup("->"), 1, ap); plrtstr(&t->next, 0, 0, t->font, strdup("->"), 1, ap);
t->next->next = x; t->next->next = x;

View file

@ -89,7 +89,6 @@ void freepix(void *p);
int pipeline(char *, int); int pipeline(char *, int);
void getfonts(void); void getfonts(void);
void *emalloc(int); void *emalloc(int);
void *emallocz(int, int);
void nstrcpy(char *to, char *from, int len); void nstrcpy(char *to, char *from, int len);
void freeform(void *p); void freeform(void *p);
int Ufmt(Fmt *f); int Ufmt(Fmt *f);

View file

@ -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) if(g->state->image[0]==0 && g->state->link[0]==0 && g->state->name[0]==0 && field==0)
ap=0; ap=0;
else{ else{
ap=mallocz(sizeof(Action), 1); ap=emalloc(sizeof(Action));
if(ap!=0){ if(g->state->image[0])
if(g->state->image[0]) ap->image = strdup(g->state->image);
ap->image = strdup(g->state->image); if(g->state->link[0])
if(g->state->link[0]) ap->link = strdup(g->state->link);
ap->link = strdup(g->state->link); if(g->state->name[0])
if(g->state->name[0]) ap->name = strdup(g->state->name);
ap->name = strdup(g->state->name); ap->ismap=g->state->ismap;
ap->ismap=g->state->ismap; ap->width=g->state->width;
ap->width=g->state->width; ap->height=g->state->height;
ap->height=g->state->height; ap->field=field;
ap->field=field;
}
} }
if(space<0) space=0; if(space<0) space=0;
if(indent<0) indent=0; if(indent<0) indent=0;