[09/23] sim/erc32: removed type mismatch compiler warnings

Message ID 1424159099-5148-10-git-send-email-jiri@gaisler.se
State Superseded
Headers

Commit Message

Jiri Gaisler Feb. 17, 2015, 7:44 a.m. UTC
  * func.c (batch, exec_cmd) save return value to avoid warnings.
	Also print simulation time as long long.
---
 sim/erc32/func.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
  

Comments

Mike Frysinger Feb. 17, 2015, 9:10 a.m. UTC | #1
On 17 Feb 2015 08:44, Jiri Gaisler wrote:
> --- a/sim/erc32/func.c
> +++ b/sim/erc32/func.c
>
> -	fgets(lbuf, 1023, fp);
> +	tmp = fgets(lbuf, 1023, fp);

not a new issue, but fgets needs to die in a fire.  getline is a trivial API and 
does a lot of nice stuff for you :).
-mike
  
Jiri Gaisler Feb. 18, 2015, 2:40 p.m. UTC | #2
On 02/17/2015 10:10 AM, Mike Frysinger wrote:
> On 17 Feb 2015 08:44, Jiri Gaisler wrote:
>> --- a/sim/erc32/func.c
>> +++ b/sim/erc32/func.c
>>
>> -	fgets(lbuf, 1023, fp);
>> +	tmp = fgets(lbuf, 1023, fp);
> 
> not a new issue, but fgets needs to die in a fire.  getline is a trivial API and 
> does a lot of nice stuff for you :).
> -mike
> 

I thought fgets was secure, as it cannot write past the (statically) allocated buffer.

Jiri.
  
Mike Frysinger Feb. 18, 2015, 4:56 p.m. UTC | #3
On 18 Feb 2015 15:40, Jiri Gaisler wrote:
> On 02/17/2015 10:10 AM, Mike Frysinger wrote:
> > On 17 Feb 2015 08:44, Jiri Gaisler wrote:
> >> --- a/sim/erc32/func.c
> >> +++ b/sim/erc32/func.c
> >>
> >> -	fgets(lbuf, 1023, fp);
> >> +	tmp = fgets(lbuf, 1023, fp);
> > 
> > not a new issue, but fgets needs to die in a fire.  getline is a trivial API and 
> > does a lot of nice stuff for you :).
> 
> I thought fgets was secure, as it cannot write past the (statically) allocated buffer.

it also mishandles lines longer than the arbitrarily picked length :)

i guess i should rephrase:
 - gets() must be nuked from orbit
 - fgets() should die in a fire
-mike
  

Patch

diff --git a/sim/erc32/func.c b/sim/erc32/func.c
index 1661175..a3da1c5 100644
--- a/sim/erc32/func.c
+++ b/sim/erc32/func.c
@@ -81,6 +81,7 @@  batch(sregs, fname)
 {
     FILE           *fp;
     char            lbuf[1024];
+    char           *tmp;
 
     if ((fp = fopen(fname, "r")) == NULL) {
 	fprintf(stderr, "couldn't open batch file %s\n", fname);
@@ -88,7 +89,7 @@  batch(sregs, fname)
     }
     while (!feof(fp)) {
 	lbuf[0] = 0;
-	fgets(lbuf, 1023, fp);
+	tmp = fgets(lbuf, 1023, fp);
 	if ((strlen(lbuf) > 0) && (lbuf[strlen(lbuf) - 1] == '\n'))
 	    lbuf[strlen(lbuf) - 1] = 0;
 	printf("sis> %s\n", lbuf);
@@ -382,7 +383,7 @@  exec_cmd(sregs, cmd)
 {
     char           *cmd1, *cmd2;
     int32           stat;
-    uint32          len, i, clen, j;
+    uint32          len, i, clen, j, tmp;
     static uint32   daddr = 0;
     char           *cmdsave;
 
@@ -553,7 +554,7 @@  exec_cmd(sregs, cmd)
 	    sim_halt();
 	} else if (strncmp(cmd1, "shell", clen) == 0) {
 	    if ((cmd1 = strtok(NULL, " \t\n\r")) != NULL) {
-		system(&cmdsave[clen]);
+		tmp = system(&cmdsave[clen]);
 	    }
 	} else if (strncmp(cmd1, "step", clen) == 0) {
 	    stat = run_sim(sregs, 1, 1);
@@ -641,8 +642,8 @@  show_stat(sregs)
 	sregs->nbranch;
 #endif
 
-    printf("\n Cycles       : %9d\n\r", ebase.simtime - sregs->simstart);
-    printf(" Instructions : %9d\n", sregs->ninst);
+    printf("\n Cycles       : %9llu\n\r", ebase.simtime - sregs->simstart);
+    printf(" Instructions : %9llu\n", sregs->ninst);
 
 #ifdef STAT
     printf("   integer    : %9.2f %%\n", 100.0 * (float) iinst / (float) sregs->ninst);