libpanel: fix memory corruption bug in plinitentry(), add pl_erealloc()
This commit is contained in:
parent
1c1bef69aa
commit
bc18faccde
|
@ -34,8 +34,7 @@ void pl_pasteentry(Panel *p, char *s){
|
|||
ep=p->data;
|
||||
n=ep->entp-ep->entry;
|
||||
m=strlen(s);
|
||||
if((e=realloc(ep->entry,n+m+SLACK))==0)
|
||||
return;
|
||||
e=pl_erealloc(ep->entry,n+m+SLACK);
|
||||
ep->entry=e;
|
||||
e+=n;
|
||||
strncpy(e, s, m);
|
||||
|
@ -131,11 +130,7 @@ void pl_typeentry(Panel *p, Rune c){
|
|||
ep->entp+=runetochar(ep->entp, &c);
|
||||
if(ep->entp>ep->eent){
|
||||
n=ep->entp-ep->entry;
|
||||
ep->entry=realloc(ep->entry, n+100+SLACK);
|
||||
if(ep->entry==0){
|
||||
fprint(2, "can't realloc in pl_typeentry\n");
|
||||
exits("no mem");
|
||||
}
|
||||
ep->entry=pl_erealloc(ep->entry, n+100+SLACK);
|
||||
ep->entp=ep->entry+n;
|
||||
ep->eent=ep->entp+100;
|
||||
}
|
||||
|
@ -155,7 +150,7 @@ void pl_freeentry(Panel *p){
|
|||
Entry *ep;
|
||||
ep = p->data;
|
||||
free(ep->entry);
|
||||
ep->entry = ep->eent = 0;
|
||||
ep->entry = ep->eent = ep->entp = 0;
|
||||
}
|
||||
void plinitentry(Panel *v, int flags, int wid, char *str, void (*hit)(Panel *, char *)){
|
||||
int elen;
|
||||
|
@ -174,8 +169,7 @@ void plinitentry(Panel *v, int flags, int wid, char *str, void (*hit)(Panel *, c
|
|||
v->paste=pl_pasteentry;
|
||||
elen=100;
|
||||
if(str) elen+=strlen(str);
|
||||
if(ep->entry==nil)
|
||||
ep->entry=pl_emalloc(elen+SLACK);
|
||||
ep->entry=pl_erealloc(ep->entry, elen+SLACK);
|
||||
ep->eent=ep->entry+elen;
|
||||
strecpy(ep->entry, ep->eent, str ? str : "");
|
||||
ep->entp=ep->entry+strlen(ep->entry);
|
||||
|
|
|
@ -14,6 +14,16 @@ void *pl_emalloc(int n){
|
|||
setmalloctag(v, getcallerpc(&n));
|
||||
return v;
|
||||
}
|
||||
void *pl_erealloc(void *v, int n)
|
||||
{
|
||||
v=realloc(v, n);
|
||||
if(v==0){
|
||||
fprint(2, "Can't realloc!\n");
|
||||
exits("no mem");
|
||||
}
|
||||
setrealloctag(v, getcallerpc(&v));
|
||||
return v;
|
||||
}
|
||||
void pl_unexpected(Panel *g, char *rou){
|
||||
fprint(2, "%s called unexpectedly (%s %lux)\n", rou, g->kind, (ulong)g);
|
||||
abort();
|
||||
|
|
|
@ -45,6 +45,7 @@ enum{
|
|||
};
|
||||
Panel *pl_newpanel(Panel *, int); /* make a new Panel, given parent & data size */
|
||||
void *pl_emalloc(int); /* allocate some space, exit on error */
|
||||
void *pl_erealloc(void*,int); /* reallocate some space, exit on error */
|
||||
void pl_print(Panel *); /* print a Panel tree */
|
||||
Panel *pl_ptinpanel(Point, Panel *); /* highest-priority subpanel containing point */
|
||||
/*
|
||||
|
|
|
@ -302,8 +302,7 @@ char *plrtsnarftext(Rtext *w){
|
|||
n = strlen(w->text)+64;
|
||||
if(p+n >= e){
|
||||
n = (p+n+64)-b;
|
||||
if((t = realloc(b, n))==0)
|
||||
break;
|
||||
t = pl_erealloc(b, n);
|
||||
p = t+(p-b);
|
||||
e = t+n;
|
||||
b = t;
|
||||
|
|
|
@ -17,16 +17,14 @@ void plputsnarf(char *s){
|
|||
}
|
||||
char *plgetsnarf(void){
|
||||
int fd, n, r;
|
||||
char *s, *x;
|
||||
char *s;
|
||||
|
||||
if((fd=open("/dev/snarf", OREAD))<0)
|
||||
return nil;
|
||||
n=0;
|
||||
s=nil;
|
||||
for(;;){
|
||||
if((x=realloc(s, n+1024))==0)
|
||||
break;
|
||||
s=x;
|
||||
s=pl_erealloc(s, n+1024);
|
||||
if((r = read(fd, s+n, 1024)) <= 0)
|
||||
break;
|
||||
n += r;
|
||||
|
|
|
@ -66,11 +66,7 @@ void tw_storeloc(Textwin *t, int l, Point p){
|
|||
int nloc;
|
||||
if(l>=t->eloc-t->loc){
|
||||
nloc=l+SLACK;
|
||||
t->loc=realloc(t->loc, nloc*sizeof(Point));
|
||||
if(t->loc==0){
|
||||
fprint(2, "No mem in tw_storeloc\n");
|
||||
exits("no mem");
|
||||
}
|
||||
t->loc=pl_erealloc(t->loc, nloc*sizeof(Point));
|
||||
t->eloc=t->loc+nloc;
|
||||
}
|
||||
t->loc[l]=p;
|
||||
|
@ -390,16 +386,13 @@ void tw_relocate(Textwin *t, int first, int last, Point dst){
|
|||
*/
|
||||
void twreplace(Textwin *t, int r0, int r1, Rune *ins, int nins){
|
||||
int olen, nlen, tlen, dtop;
|
||||
Rune *ntext;
|
||||
olen=t->etext-t->text;
|
||||
nlen=olen+nins-(r1-r0);
|
||||
tlen=t->eslack-t->text;
|
||||
if(nlen>tlen){
|
||||
tlen=nlen+SLACK;
|
||||
if((ntext=realloc(t->text, tlen*sizeof(Rune)))==0)
|
||||
return;
|
||||
t->text=ntext;
|
||||
t->eslack=ntext+tlen;
|
||||
t->text=pl_erealloc(t->text, tlen*sizeof(Rune));
|
||||
t->eslack=t->text+tlen;
|
||||
}
|
||||
if(olen!=nlen)
|
||||
memmove(t->text+r0+nins, t->text+r1, (olen-r1)*sizeof(Rune));
|
||||
|
|
Loading…
Reference in a new issue