pawndisasm: Fix input and output files not being closed properly and 'code' being leaked on errors

This commit is contained in:
Daniel_Cortez 2019-06-06 23:14:22 +07:00
parent b4f899e557
commit c9dc7008fa

View File

@ -456,19 +456,21 @@ static void addchars(char *str,cell value,int pos)
int main(int argc,char *argv[]) int main(int argc,char *argv[])
{ {
char name[FILENAME_MAX]; char name[FILENAME_MAX];
FILE *fplist; FILE *fplist=NULL;
int codesize,count; int codesize,count;
cell *code,*cip; cell *code=NULL,*cip;
OPCODE_PROC func; OPCODE_PROC func;
const char *filename; const char *filename;
long nline,nprevline; long nline,nprevline;
FILE *fpsrc; FILE *fpsrc;
int i,j; int i,j;
char line[sLINEMAX]; char line[sLINEMAX];
int retval=1;
fpamx=NULL;
if (argc<2 || argc>3) { if (argc<2 || argc>3) {
printf("Usage: pawndisasm <input> [output]\n"); printf("Usage: pawndisasm <input> [output]\n");
return 1; goto ret;
} /* if */ } /* if */
if (argc==2) { if (argc==2) {
char *ptr; char *ptr;
@ -481,11 +483,11 @@ int main(int argc,char *argv[])
} /* if */ } /* if */
if ((fpamx=fopen(argv[1],"rb"))==NULL) { if ((fpamx=fopen(argv[1],"rb"))==NULL) {
printf("Unable to open input file \"%s\"\n",argv[1]); printf("Unable to open input file \"%s\"\n",argv[1]);
return 1; goto ret;
} /* if */ } /* if */
if ((fplist=fopen(name,"wt"))==NULL) { if ((fplist=fopen(name,"wt"))==NULL) {
printf("Unable to create output file \"%s\"\n",name); printf("Unable to create output file \"%s\"\n",name);
return 1; goto ret;
} /* if */ } /* if */
/* load debug info */ /* load debug info */
@ -496,11 +498,11 @@ int main(int argc,char *argv[])
if (fread(&amxhdr,sizeof amxhdr,1,fpamx)==0) { if (fread(&amxhdr,sizeof amxhdr,1,fpamx)==0) {
printf("Unable to read AMX header: %s\n", printf("Unable to read AMX header: %s\n",
feof(fpamx) ? "End of file reached" : strerror(errno)); feof(fpamx) ? "End of file reached" : strerror(errno));
return 1; goto ret;
} /* if */ } /* if */
if (amxhdr.magic!=AMX_MAGIC) { if (amxhdr.magic!=AMX_MAGIC) {
printf("Not a valid AMX file\n"); printf("Not a valid AMX file\n");
return 1; goto ret;
} /* if */ } /* if */
codesize=amxhdr.hea-amxhdr.cod; /* size for both code and data */ codesize=amxhdr.hea-amxhdr.cod; /* size for both code and data */
fprintf(fplist,";File version: %d\n",amxhdr.file_version); fprintf(fplist,";File version: %d\n",amxhdr.file_version);
@ -517,7 +519,7 @@ int main(int argc,char *argv[])
/* load the code block */ /* load the code block */
if ((code=malloc(codesize))==NULL) { if ((code=malloc(codesize))==NULL) {
printf("Insufficient memory: need %d bytes\n",codesize); printf("Insufficient memory: need %d bytes\n",codesize);
return 1; goto ret;
} /* if */ } /* if */
/* read and expand the file */ /* read and expand the file */
@ -525,7 +527,7 @@ int main(int argc,char *argv[])
if ((int32_t)fread(code,1,codesize,fpamx)<amxhdr.size-amxhdr.cod) { if ((int32_t)fread(code,1,codesize,fpamx)<amxhdr.size-amxhdr.cod) {
printf("Unable to read code: %s\n", printf("Unable to read code: %s\n",
feof(fpamx) ? "End of file reached" : strerror(errno)); feof(fpamx) ? "End of file reached" : strerror(errno));
return 1; goto ret;
} /* if */ } /* if */
if ((amxhdr.flags & AMX_FLAG_COMPACT)!=0) if ((amxhdr.flags & AMX_FLAG_COMPACT)!=0)
expand((unsigned char *)code,amxhdr.size-amxhdr.cod,amxhdr.hea-amxhdr.cod); expand((unsigned char *)code,amxhdr.size-amxhdr.cod,amxhdr.hea-amxhdr.cod);
@ -596,8 +598,12 @@ int main(int argc,char *argv[])
dbg_FreeInfo(&amxdbg); dbg_FreeInfo(&amxdbg);
} /* if */ } /* if */
retval=0;
ret:
free(code); free(code);
fclose(fpamx); if (fpamx!=NULL)
fclose(fplist); fclose(fpamx);
return 0; if (fplist!=NULL)
fclose(fplist);
return retval;
} }