Commit 0a566ccb authored by deuce's avatar deuce
Browse files

Use alloca() instead of malloc()/free()

Fixes at least one memory leak in the spy function and at least two (three?)
leaks in unomitor.c
parent 8d12fef6
......@@ -138,14 +138,12 @@ int chat(scfg_t *cfg, int nodenum, node_t *node, box_t *boxch, void(*timecallbac
char *buf;
gettextinfo(&ti);
if((buf=(char *)malloc(ti.screenwidth*ti.screenheight*2))==NULL) {
if((buf=(char *)alloca(ti.screenwidth*ti.screenheight*2))==NULL) {
return(-1);
}
if(getnodedat(cfg,nodenum,node,NULL)) {
free(buf);
if(getnodedat(cfg,nodenum,node,NULL))
return(-1);
}
username(cfg,node->useron,usrname);
......@@ -154,29 +152,24 @@ int chat(scfg_t *cfg, int nodenum, node_t *node, box_t *boxch, void(*timecallbac
sprintf(outpath,"%slchat.dab",cfg->node_path[nodenum-1]);
if((out=sopen(outpath,O_RDWR|O_CREAT|O_BINARY,O_DENYNONE
,S_IREAD|S_IWRITE))==-1) {
free(buf);
,S_IREAD|S_IWRITE))==-1)
return(-1);
}
sprintf(inpath,"%schat.dab",cfg->node_path[nodenum-1]);
if((in=sopen(inpath,O_RDWR|O_CREAT|O_BINARY,O_DENYNONE
,S_IREAD|S_IWRITE))==-1) {
close(out);
free(buf);
return(-1);
}
if((p=(char *)malloc(PCHAT_LEN))==NULL) {
if((p=(char *)alloca(PCHAT_LEN))==NULL) {
close(in);
close(out);
free(buf);
return(-1);
}
memset(p,0,PCHAT_LEN);
write(in,p,PCHAT_LEN);
write(out,p,PCHAT_LEN);
free(p);
lseek(in,0,SEEK_SET);
lseek(out,0,SEEK_SET);
......@@ -276,7 +269,6 @@ int chat(scfg_t *cfg, int nodenum, node_t *node, box_t *boxch, void(*timecallbac
close(out);
togglechat(cfg,nodenum,node,FALSE);
puttext(1,1,ti.screenwidth,ti.screenheight,buf);
free(buf);
window(ti.winleft,ti.wintop,ti.winright,ti.wintop);
gotoxy(ti.curx,ti.cury);
textattr(ti.attribute);
......
......@@ -83,7 +83,7 @@ int spyon(char *sockname) {
i=1;
gettextinfo(&ti);
scrn=(char *)malloc(ti.screenwidth*ti.screenheight*2);
scrn=(char *)alloca(ti.screenwidth*ti.screenheight*2);
gettext(1,1,ti.screenwidth,ti.screenheight,scrn);
textcolor(YELLOW);
textbackground(BLUE);
......
......@@ -102,16 +102,6 @@ void allocfail(uint size)
bail(1);
}
void freeopt(char** opt)
{
int i;
for(i=0;i<(MAX_OPTS+1);i++)
free(opt[i]);
free(opt);
}
void node_toggles(scfg_t *cfg,int nodenum) {
int nodefile;
char** opt;
......@@ -119,10 +109,10 @@ void node_toggles(scfg_t *cfg,int nodenum) {
node_t node;
int save=0;
if((opt=(char **)malloc(sizeof(char *)*(MAX_OPTS+1)))==NULL)
if((opt=(char **)alloca(sizeof(char *)*(MAX_OPTS+1)))==NULL)
allocfail(sizeof(char *)*(MAX_OPTS+1));
for(i=0;i<(MAX_OPTS+1);i++)
if((opt[i]=(char *)malloc(MAX_OPLN))==NULL)
if((opt[i]=(char *)alloca(MAX_OPLN))==NULL)
allocfail(MAX_OPLN);
i=0;
......@@ -186,7 +176,6 @@ void node_toggles(scfg_t *cfg,int nodenum) {
}
putnodedat(cfg,nodenum,&node,nodefile);
}
freeopt(opt);
}
int dospy(int nodenum, bbs_startup_t *bbs_startup) {
......@@ -425,12 +414,11 @@ int view_log(char *filename, char *title)
if(fexist(filename)) {
if((buffile=sopen(filename,O_RDONLY,SH_DENYWR))>=0) {
j=filelength(buffile);
if((buf=(char *)malloc(j+1))!=NULL) {
if((buf=(char *)alloca(j+1))!=NULL) {
read(buffile,buf,j);
close(buffile);
*(buf+j)=0;
uifc.showbuf(WIN_MID,0,0,76,uifc.scrn_len-2,title,buf,NULL,NULL);
free(buf);
return(0);
}
close(buffile);
......@@ -459,10 +447,10 @@ int view_logs(scfg_t *cfg)
localtime_r(&now,&tm);
now -= 60*60*24;
localtime_r(&now,&tm_yest);
if((opt=(char **)malloc(sizeof(char *)*(MAX_OPTS+1)))==NULL)
if((opt=(char **)alloca(sizeof(char *)*(MAX_OPTS+1)))==NULL)
allocfail(sizeof(char *)*(MAX_OPTS+1));
for(i=0;i<(MAX_OPTS+1);i++)
if((opt[i]=(char *)malloc(MAX_OPLN))==NULL)
if((opt[i]=(char *)alloca(MAX_OPLN))==NULL)
allocfail(MAX_OPLN);
i=0;
......@@ -492,7 +480,6 @@ int view_logs(scfg_t *cfg)
while(1) {
switch(uifc.list(WIN_MID|WIN_SAV,0,0,0,&i,0,"View Logs",opt)) {
case -1:
freeopt(opt);
return(0);
case 0:
sprintf(str,"%slogs/%2.2d%2.2d%2.2d.lol",cfg->logs_dir,tm.tm_mon+1,tm.tm_mday
......@@ -545,11 +532,10 @@ int do_cmd(char *cmd)
char *p;
gettextinfo(&ti);
p=malloc(ti.screenheight*ti.screenwidth*2);
p=alloca(ti.screenheight*ti.screenwidth*2);
gettext(1,1,ti.screenwidth,ti.screenheight,p);
i=system(cmd);
puttext(1,1,ti.screenwidth,ti.screenheight,p);
free(p);
return(i);
}
......@@ -564,10 +550,10 @@ int qwk_callouts(scfg_t *cfg)
return(1);
}
if((opt=(char **)malloc(sizeof(char *)*(MAX_OPTS+1)))==NULL)
if((opt=(char **)alloca(sizeof(char *)*(MAX_OPTS+1)))==NULL)
allocfail(sizeof(char *)*(MAX_OPTS+1));
for(i=0;i<(MAX_OPTS+1);i++)
if((opt[i]=(char *)malloc(MAX_OPLN))==NULL)
if((opt[i]=(char *)alloca(MAX_OPLN))==NULL)
allocfail(MAX_OPLN);
......@@ -587,7 +573,6 @@ int qwk_callouts(scfg_t *cfg)
opt[i][0]=0;
switch(uifc.list(WIN_MID|WIN_SAV,0,0,0,&j,0,"QWK Callouts",opt)) {
case -1:
freeopt(opt);
return(0);
break;
default:
......@@ -604,10 +589,10 @@ int run_events(scfg_t *cfg)
int i,j;
char str[1024];
if((opt=(char **)malloc(sizeof(char *)*(MAX_OPTS+1)))==NULL)
if((opt=(char **)alloca(sizeof(char *)*(MAX_OPTS+1)))==NULL)
allocfail(sizeof(char *)*(MAX_OPTS+1));
for(i=0;i<(MAX_OPTS+1);i++)
if((opt[i]=(char *)malloc(MAX_OPLN))==NULL)
if((opt[i]=(char *)alloca(MAX_OPLN))==NULL)
allocfail(MAX_OPLN);
if(cfg->total_events<1) {
......@@ -630,7 +615,6 @@ int run_events(scfg_t *cfg)
opt[i][0]=0;
switch(uifc.list(WIN_MID|WIN_SAV,0,0,0,&j,0,"Run Events",opt)) {
case -1:
freeopt(opt);
return(0);
break;
default:
......@@ -647,10 +631,10 @@ int recycle_servers(scfg_t *cfg)
char **opt;
int i=0;
if((opt=(char **)malloc(sizeof(char *)*(MAX_OPTS+1)))==NULL)
if((opt=(char **)alloca(sizeof(char *)*(MAX_OPTS+1)))==NULL)
allocfail(sizeof(char *)*(MAX_OPTS+1));
for(i=0;i<(MAX_OPTS+1);i++)
if((opt[i]=(char *)malloc(MAX_OPLN))==NULL)
if((opt[i]=(char *)alloca(MAX_OPLN))==NULL)
allocfail(MAX_OPLN);
i=0;
......@@ -671,7 +655,6 @@ int recycle_servers(scfg_t *cfg)
while(1) {
switch(uifc.list(WIN_MID|WIN_SAV,0,0,0,&i,0,"Recycle Servers",opt)) {
case -1:
freeopt(opt);
return(0);
break;
case 0:
......@@ -720,10 +703,10 @@ int edit_cfg(scfg_t *cfg)
char cmd[1024];
char editcmd[1024];
if((opt=(char **)malloc(sizeof(char *)*(MAX_OPTS+1)))==NULL)
if((opt=(char **)alloca(sizeof(char *)*(MAX_OPTS+1)))==NULL)
allocfail(sizeof(char *)*(MAX_OPTS+1));
for(i=0;i<(MAX_OPTS+1);i++)
if((opt[i]=(char *)malloc(MAX_OPLN))==NULL)
if((opt[i]=(char *)alloca(MAX_OPLN))==NULL)
allocfail(MAX_OPLN);
i=0;
......@@ -750,7 +733,6 @@ int edit_cfg(scfg_t *cfg)
while(1) {
switch(uifc.list(WIN_MID|WIN_SAV,0,0,0,&i,0,"System Options",opt)) {
case -1:
freeopt(opt);
return(0);
break;
default:
......@@ -769,10 +751,10 @@ int edit_can(scfg_t *cfg)
char cmd[1024];
char editcmd[1024];
if((opt=(char **)malloc(sizeof(char *)*(MAX_OPTS+1)))==NULL)
if((opt=(char **)alloca(sizeof(char *)*(MAX_OPTS+1)))==NULL)
allocfail(sizeof(char *)*(MAX_OPTS+1));
for(i=0;i<(MAX_OPTS+1);i++)
if((opt[i]=(char *)malloc(MAX_OPLN))==NULL)
if((opt[i]=(char *)alloca(MAX_OPLN))==NULL)
allocfail(MAX_OPLN);
i=0;
......@@ -791,7 +773,6 @@ int edit_can(scfg_t *cfg)
while(1) {
switch(uifc.list(WIN_MID|WIN_SAV,0,0,0,&i,0,"System Options",opt)) {
case -1:
freeopt(opt);
return(0);
break;
default:
......@@ -969,16 +950,16 @@ int main(int argc, char** argv) {
exit(1);
}
if((opt=(char **)malloc(sizeof(char *)*(MAX_OPTS+1)))==NULL)
if((opt=(char **)alloca(sizeof(char *)*(MAX_OPTS+1)))==NULL)
allocfail(sizeof(char *)*(MAX_OPTS+1));
for(i=0;i<(MAX_OPTS+1);i++)
if((opt[i]=(char *)malloc(MAX_OPLN))==NULL)
if((opt[i]=(char *)alloca(MAX_OPLN))==NULL)
allocfail(MAX_OPLN);
if((mopt=(char **)malloc(sizeof(char *)*MAX_OPTS))==NULL)
if((mopt=(char **)alloca(sizeof(char *)*MAX_OPTS))==NULL)
allocfail(sizeof(char *)*MAX_OPTS);
for(i=0;i<MAX_OPTS;i++)
if((mopt[i]=(char *)malloc(MAX_OPLN))==NULL)
if((mopt[i]=(char *)alloca(MAX_OPLN))==NULL)
allocfail(MAX_OPLN);
sprintf(title,"Synchronet UNIX Monitor %s-%s",revision,PLATFORM_DESC);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment