From 736904b13f758948c30a16d7dfc489c7c5176934 Mon Sep 17 00:00:00 2001 From: Zeex Date: Sun, 18 Nov 2018 18:18:23 +0600 Subject: [PATCH] Fix bugs related to merging of input files This patch fixes a couple of bugs related to passing multiple input files to pawncc, e.g. pawncc a.pwn b.pwn Summary of changes: * One of the specified input files could be deleted by the compiler because inpfname was overwritten while parsing the code (#file directives for example), but in the end of compilation it thought that inpfname was holding the name of the temporary file, so it deleted that file. To fix this we now store the temporary file's name separately (in tname). * During the copying of input source code into a temporary file the last line of each input file was duplicated. For some reason feof() returnd zero, indicating the there was more data to read, but subsequent call to fgets() could not read anything, and the output buffer remained unchanged. * Added a newline at the end of each input file. This fixes #206. --- source/compiler/sc1.c | 17 +++++++++-------- .../compiler/tests/gh_206_multiple_inputs.meta | 4 ++++ .../compiler/tests/gh_206_multiple_inputs.pwn | 5 +++++ .../compiler/tests/gh_206_multiple_inputs_2.pwn | 5 +++++ 4 files changed, 23 insertions(+), 8 deletions(-) create mode 100644 source/compiler/tests/gh_206_multiple_inputs.meta create mode 100644 source/compiler/tests/gh_206_multiple_inputs.pwn create mode 100644 source/compiler/tests/gh_206_multiple_inputs_2.pwn diff --git a/source/compiler/sc1.c b/source/compiler/sc1.c index 2ead157..9eb4625 100644 --- a/source/compiler/sc1.c +++ b/source/compiler/sc1.c @@ -473,6 +473,7 @@ int pc_compile(int argc, char *argv[]) int hdrsize=0; #endif char *ptr; + char *tname=NULL; /* set global variables to their initial value */ binf=NULL; @@ -528,7 +529,7 @@ int pc_compile(int argc, char *argv[]) assert(get_sourcefile(0)!=NULL); /* there must be at least one source file */ if (get_sourcefile(1)!=NULL) { /* there are at least two or more source files */ - char *tname,*sname; + char *sname; FILE *ftmp,*fsrc; int fidx; ftmp=pc_createtmpsrc(&tname); @@ -542,15 +543,14 @@ int pc_compile(int argc, char *argv[]) pc_writesrc(ftmp,(unsigned char*)"#file \""); pc_writesrc(ftmp,(unsigned char*)sname); pc_writesrc(ftmp,(unsigned char*)"\"\n"); - while (!pc_eofsrc(fsrc)) { - pc_readsrc(fsrc,tstring,sizeof tstring); + while (pc_readsrc(fsrc,tstring,sizeof tstring)!=NULL) { pc_writesrc(ftmp,tstring); } /* while */ + pc_writesrc(ftmp,"\n"); pc_closesrc(fsrc); } /* for */ pc_closesrc(ftmp); strcpy(inpfname,tname); - free(tname); } else { strcpy(inpfname,get_sourcefile(0)); } /* if */ @@ -764,11 +764,12 @@ cleanup: } /* if */ #endif - if (inpfname!=NULL) { - if (get_sourcefile(1)!=NULL) - remove(inpfname); /* the "input file" was in fact a temporary file */ - free(inpfname); + if (get_sourcefile(1)!=NULL && tname!=NULL) { + remove(tname); /* the "input file" was in fact a temporary file */ + free(tname); } /* if */ + if (inpfname!=NULL) + free(inpfname); if (litq!=NULL) free(litq); stgbuffer_cleanup(); diff --git a/source/compiler/tests/gh_206_multiple_inputs.meta b/source/compiler/tests/gh_206_multiple_inputs.meta new file mode 100644 index 0000000..eb57548 --- /dev/null +++ b/source/compiler/tests/gh_206_multiple_inputs.meta @@ -0,0 +1,4 @@ +{ + 'test_type': 'output_check', + 'extra_args': ['gh_206_multiple_inputs_2.pwn'] +} diff --git a/source/compiler/tests/gh_206_multiple_inputs.pwn b/source/compiler/tests/gh_206_multiple_inputs.pwn new file mode 100644 index 0000000..1545b4c --- /dev/null +++ b/source/compiler/tests/gh_206_multiple_inputs.pwn @@ -0,0 +1,5 @@ +#include + +main() { + b_main(); +} diff --git a/source/compiler/tests/gh_206_multiple_inputs_2.pwn b/source/compiler/tests/gh_206_multiple_inputs_2.pwn new file mode 100644 index 0000000..8306ef7 --- /dev/null +++ b/source/compiler/tests/gh_206_multiple_inputs_2.pwn @@ -0,0 +1,5 @@ +#include + +b_main() { + print("hello"); +}