@@ -78,17 +78,23 @@ static PN potion_cmd_exec(Potion *P, PN buf, char *filename, char *compile, char
7878
7979 if (!buf && filename ) {
8080 struct stat stats ;
81- if (stat (filename , & stats ) == -1 ) {
82- fprintf (stderr , "** %s does not exist." , filename );
83- goto done ;
84- }
81+ long size ;
82+ char * bufptr ;
83+ // TOCTTOU http://cwe.mitre.org/data/definitions/367.html
8584 fd = open (filename , O_RDONLY | O_BINARY );
8685 if (fd == -1 ) {
87- fprintf (stderr , "** could not open %s. check permissions." , filename );
86+ if (stat (filename , & stats ) == -1 ) {
87+ fprintf (stderr , "** %s does not exist." , filename );
88+ } else {
89+ fprintf (stderr , "** could not open %s. check permissions." , filename );
90+ }
8891 goto done ;
8992 }
90- long size = stats .st_size ;
91- char * bufptr ;
93+ if (stat (filename , & stats ) == -1 ) {
94+ fprintf (stderr , "** %s vanished!" , filename );
95+ goto done ;
96+ }
97+ size = stats .st_size ;
9298 if (addcode ) {
9399 int len = strlen (addcode );
94100 size += len ;
@@ -117,7 +123,7 @@ static PN potion_cmd_exec(Potion *P, PN buf, char *filename, char *compile, char
117123 code = potion_parse (P , buf , filename );
118124 if (!code || PN_TYPE (code ) == PN_TERROR ) {
119125 potion_p (P , code );
120- return code ;
126+ goto done ;
121127 }
122128 DBG_v ("\n-- parsed --\n" );
123129 DBG_Pv (code );
@@ -196,7 +202,6 @@ static PN potion_cmd_exec(Potion *P, PN buf, char *filename, char *compile, char
196202 if (code &&
197203 (written = fwrite (PN_STR_PTR (code ), 1 , PN_STR_LEN (code ), pnb ) == PN_STR_LEN (code ))) {
198204 printf ("** compiled code saved to %s\n" , outpath );
199- fclose (pnb );
200205
201206 if (!compile || !strcmp (compile , "bc" ))
202207 printf ("** run it with: potion %s\n" , outpath );
@@ -209,6 +214,7 @@ static PN potion_cmd_exec(Potion *P, PN buf, char *filename, char *compile, char
209214 fprintf (stderr , "** could not write all %s compiled code (%u/%u) to %s\n" ,
210215 compile ?compile :"bytecode" , written , code ?PN_STR_LEN (code ):0 , outpath );
211216 }
217+ fclose (pnb );
212218 }
213219
214220#if defined(DEBUG )
0 commit comments