ref: c1d9d6e538908292ca76d2394b15885b1ac28ac9
parent: e159e8e54a865a2d1eee7543c77c3584e46464be
author: cinap_lenrek <cinap_lenrek@centraldogma>
date: Mon Sep 5 14:36:58 EDT 2011
mothra: cleanup, fix memory leak in libpanel
--- a/sys/src/cmd/mothra/libpanel/draw.c
+++ b/sys/src/cmd/mothra/libpanel/draw.c
@@ -128,6 +128,7 @@
case FRAME:
return addpt(interior, Pt(4*FWID+2*SPACE, 4*FWID+2*SPACE));
}
+ return Pt(0, 0);
}
void pl_interior(int state, Point *ul, Point *size){
switch(state){
--- a/sys/src/cmd/mothra/libpanel/entry.c
+++ b/sys/src/cmd/mothra/libpanel/entry.c
@@ -89,7 +89,10 @@
USED(p, ul, size);
}
void pl_freeentry(Panel *p){
- free(((Entry *)p->data)->entry);
+ Entry *ep;
+ ep = p->data;
+ free(ep->entry);
+ ep->entry = ep->eent = 0;
}
void plinitentry(Panel *v, int flags, int wid, char *str, void (*hit)(Panel *, char *)){
int elen;
@@ -106,13 +109,11 @@
v->free=pl_freeentry;
elen=100;
if(str) elen+=strlen(str);
- ep->entry=pl_emalloc(elen+SLACK);
+ if(ep->entry==nil)
+ ep->entry=pl_emalloc(elen+SLACK);
ep->eent=ep->entry+elen;
- if(str)
- strcpy(ep->entry, str);
- else ep->entry[0]='\0';
+ strecpy(ep->entry, ep->eent, str ? str : "");
ep->entp=ep->entry+strlen(ep->entry);
- /* strcat(ep->entry, "◀"); */
ep->hit=hit;
v->kind="entry";
}
--- a/sys/src/cmd/mothra/libpanel/mem.c
+++ b/sys/src/cmd/mothra/libpanel/mem.c
@@ -6,7 +6,7 @@
#include "pldefs.h"
void *pl_emalloc(int n){
void *v;
- v=malloc(n);
+ v=mallocz(n, 1);
if(v==0){
fprint(2, "Can't malloc!\n");
exits("no mem");
--- a/sys/src/cmd/mothra/mothra.c
+++ b/sys/src/cmd/mothra/mothra.c
@@ -951,7 +951,7 @@
fd=open("/dev/snarf", OREAD);
strncpy(buf, plentryval(p), sizeof(buf));
len=strlen(buf);
- n=read(fd, buf+len, 1023-len);
+ n=read(fd, buf+len, sizeof(buf)-len-1);
if(n>0){
buf[len+n]='\0';
plinitentry(cmd, PACKE|EXPAND, 0, buf, docmd);
@@ -960,7 +960,7 @@
close(fd);
}
void hit3(int button, int item){
- char name[100], *home;
+ char name[NNAME], *home;
Panel *swap;
int fd;
USED(button);
--- a/sys/src/cmd/mothra/rdhtml.c
+++ b/sys/src/cmd/mothra/rdhtml.c
@@ -35,10 +35,13 @@
"lucidasans/typeunicode.16", 0, 0,
};
Fontdata *pl_whichfont(int f, int s){
- char name[100];
+ char name[NNAME];
+
+ assert(f >= 0 && f < 4);
+ assert(s >= 0 && s < 4);
+
if(fontlist[f][s].font==0){
- snprint(name, sizeof(name), "/lib/font/bit/%s.font",
- fontlist[f][s].name);
+ snprint(name, sizeof(name), "/lib/font/bit/%s.font", fontlist[f][s].name);
fontlist[f][s].font=openfont(display, name);
if(fontlist[f][s].font==0) fontlist[f][s].font=font;
fontlist[f][s].space=stringwidth(fontlist[f][s].font, "0");