From 9ccde79fde393478884a72ecbade3f9dfdfca9d7 Mon Sep 17 00:00:00 2001
From: rliebscher <rliebscher@81a1dc3b-b13d-400b-aceb-764788c761c2>
Date: Sun, 8 Jan 2012 17:09:40 +0000
Subject: [PATCH] Fixed following findings reported by cppcheck 	* avr910.c:625
 (error) Possible null pointer dereference: cmd - otherwise it is redundant to
 check if cmd is null at line 624 	* avr910.c:626 (error) Possible null
 pointer dereference: cmd - otherwise it is redundant to check if cmd is null
 at line 624 	* avr910.c:168 (information) The scope of the variable
 'devtype_1st' can be reduced 	* avr910.c:169 (information) The scope of the
 variable 'dev_supported' can be reduced 	* avrftdi.c:647 (error) Using
 sizeof for array given as function argument returns the size of pointer. 
 * stk500v2.c:3347 (error) Memory leak: b 	* stk500v2.c:3452 (error)
 Memory leak: b 	* usbasp.c:554 (error) Using sizeof for array given as
 function argument returns the size of pointer. 	* usbasp.c:485
 (information) The scope of the variable 'dly' can be reduced

git-svn-id: svn://svn.savannah.nongnu.org/avrdude/trunk/avrdude@1035 81a1dc3b-b13d-400b-aceb-764788c761c2
---
 ChangeLog  | 13 +++++++++++++
 avr910.c   | 10 ++++++----
 avrdude.h  |  4 ++--
 avrftdi.c  |  4 +++-
 stk500v2.c | 12 ++++++++++--
 usbasp.c   |  7 ++++---
 6 files changed, 38 insertions(+), 12 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index e3ae6807..649f93b5 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+2012-01-08  Rene Liebscher <R.Liebscher@gmx.de>
+
+	Fixed following findings reported by cppcheck
+	* avr910.c:625 (error) Possible null pointer dereference: cmd - otherwise it is redundant to check if cmd is null at line 624
+	* avr910.c:626 (error) Possible null pointer dereference: cmd - otherwise it is redundant to check if cmd is null at line 624
+	* avr910.c:168 (information) The scope of the variable 'devtype_1st' can be reduced
+	* avr910.c:169 (information) The scope of the variable 'dev_supported' can be reduced
+	* avrftdi.c:647 (error) Using sizeof for array given as function argument returns the size of pointer.
+	* stk500v2.c:3347 (error) Memory leak: b
+	* stk500v2.c:3452 (error) Memory leak: b
+	* usbasp.c:554 (error) Using sizeof for array given as function argument returns the size of pointer.
+	* usbasp.c:485 (information) The scope of the variable 'dly' can be reduced
+
 2012-01-03  Joerg Wunsch <j.gnu@uriah.heep.sax.de>
 
 	Reported by Jason Kotzin:
diff --git a/avr910.c b/avr910.c
index 4a168f85..40f57d02 100644
--- a/avr910.c
+++ b/avr910.c
@@ -165,8 +165,7 @@ static int avr910_initialize(PROGRAMMER * pgm, AVRPART * p)
   char hw[2];
   char buf[10];
   char type;
-  char c, devtype_1st;
-  int dev_supported = 0;
+  char c;
   AVRPART * part;
 
   /* Get the programmer identifier. Programmer returns exactly 7 chars
@@ -223,6 +222,8 @@ static int avr910_initialize(PROGRAMMER * pgm, AVRPART * p)
   }
 
   if (PDATA(pgm)->devcode == 0) {
+    char devtype_1st;
+    int dev_supported = 0;
 
     /* Get list of devices that the programmer supports. */
 
@@ -614,14 +615,15 @@ static int avr910_paged_write(PROGRAMMER * pgm, AVRPART * p, AVRMEM * m,
     unsigned int blocksize = PDATA(pgm)->buffersize;
 
     if (strcmp(m->desc, "flash") && strcmp(m->desc, "eeprom"))
-      rval = -2;
+      return -2;
 
     if (m->desc[0] == 'e')
       blocksize = 1;		/* Write to eeprom single bytes only */
     avr910_set_addr(pgm, addr);
 
     cmd = malloc(4 + blocksize);
-    if (!cmd) rval = -1;
+    if (!cmd) return -1;
+     
     cmd[0] = 'B';
     cmd[3] = toupper((int)(m->desc[0]));
 
diff --git a/avrdude.h b/avrdude.h
index 0ef19cac..957f94eb 100644
--- a/avrdude.h
+++ b/avrdude.h
@@ -46,11 +46,11 @@ int usleep(unsigned int us);
 #if !defined(HAVE_GETTIMEOFDAY)
 struct timezone;
 int gettimeofday(struct timeval *tv, struct timezone *tz);
+#endif /* HAVE_GETTIMEOFDAY */
+
 #ifdef __cplusplus
 }
 #endif
-#endif /* HAVE_GETTIMEOFDAY */
-
 #endif /* defined(WIN32NATIVE) */
 
 #endif
diff --git a/avrftdi.c b/avrftdi.c
index 653a46a1..982298c4 100644
--- a/avrftdi.c
+++ b/avrftdi.c
@@ -644,7 +644,9 @@ static void avrftdi_display(PROGRAMMER * pgm, const char *p)
 
 static int avrftdi_cmd(PROGRAMMER * pgm, unsigned char cmd[4], unsigned char res[4])
 {
-	return avrftdi_transmit(TRX, cmd, res, sizeof(cmd));
+	/* Do not use 'sizeof(cmd)'. => message from cppcheck:
+	   Using sizeof for array given as function argument returns the size of pointer. */
+	return avrftdi_transmit(TRX, cmd, res, 4);
 }
 
 
diff --git a/stk500v2.c b/stk500v2.c
index 8f102f6f..59fef627 100644
--- a/stk500v2.c
+++ b/stk500v2.c
@@ -3343,8 +3343,10 @@ static int stk600_xprog_paged_load(PROGRAMMER * pgm, AVRPART * p, AVRMEM * mem,
         return -1;
     }
 
-    if (stk500v2_loadaddr(pgm, use_ext_addr) < 0)
+    if (stk500v2_loadaddr(pgm, use_ext_addr) < 0) {
+        free(b);
         return -1;
+    }
 
     while (n_bytes != 0) {
 	b[0] = XPRG_CMD_READ_MEM;
@@ -3359,6 +3361,7 @@ static int stk600_xprog_paged_load(PROGRAMMER * pgm, AVRPART * p, AVRMEM * mem,
 	    fprintf(stderr,
 		    "%s: stk600_xprog_paged_load(): XPRG_CMD_READ_MEM failed\n",
 		    progname);
+	    free(b);
 	    return -1;
 	}
 	memcpy(mem->buf + offset, b + 2, page_size);
@@ -3448,8 +3451,10 @@ static int stk600_xprog_paged_write(PROGRAMMER * pgm, AVRPART * p, AVRMEM * mem,
         return -1;
     }
 
-    if (stk500v2_loadaddr(pgm, use_ext_addr) < 0)
+    if (stk500v2_loadaddr(pgm, use_ext_addr) < 0) {
+        free(b);
         return -1;
+    }
 
     while (n_bytes != 0) {
 	if (page_size > 256) {
@@ -3466,6 +3471,7 @@ static int stk600_xprog_paged_write(PROGRAMMER * pgm, AVRPART * p, AVRMEM * mem,
 		fprintf(stderr,
 			"%s: stk600_xprog_paged_write(): page size not multiple of 256\n",
 			progname);
+		free(b);
 		return -1;
 	    }
 	    unsigned int chunk;
@@ -3494,6 +3500,7 @@ static int stk600_xprog_paged_write(PROGRAMMER * pgm, AVRPART * p, AVRMEM * mem,
 		    fprintf(stderr,
 			    "%s: stk600_xprog_paged_write(): XPRG_CMD_WRITE_MEM failed\n",
 			    progname);
+		    free(b);
 		    return -1;
 		}
 		if (n_bytes < 256)
@@ -3528,6 +3535,7 @@ static int stk600_xprog_paged_write(PROGRAMMER * pgm, AVRPART * p, AVRMEM * mem,
 		fprintf(stderr,
 			"%s: stk600_xprog_paged_write(): XPRG_CMD_WRITE_MEM failed\n",
 			progname);
+		free(b);
 		return -1;
 	    }
 	    if (n_bytes < page_size)
diff --git a/usbasp.c b/usbasp.c
index e5d87c7b..f4bebe2b 100644
--- a/usbasp.c
+++ b/usbasp.c
@@ -482,7 +482,6 @@ static void usbasp_display(PROGRAMMER * pgm, const char * p)
 /* Universal functions: for both SPI and TPI */
 static int usbasp_initialize(PROGRAMMER * pgm, AVRPART * p)
 {
-  int dly;
   unsigned char temp[4];
   unsigned char res[4];
   IMPORT_PDATA(pgm);
@@ -500,7 +499,7 @@ static int usbasp_initialize(PROGRAMMER * pgm, AVRPART * p)
   if(pdata->use_tpi)
   {
     /* calc tpiclk delay */
-    dly = 1500000.0 * pgm->bitclock;
+    int dly = 1500000.0 * pgm->bitclock;
     if(dly < 1)
         dly = 1;
     else if(dly > 2047)
@@ -550,8 +549,10 @@ static int usbasp_initialize(PROGRAMMER * pgm, AVRPART * p)
 static int usbasp_spi_cmd(PROGRAMMER * pgm, unsigned char cmd[4],
                    unsigned char res[4])
 {
+  /* Do not use 'sizeof(res)'. => message from cppcheck:
+     Using sizeof for array given as function argument returns the size of pointer. */
   int nbytes =
-    usbasp_transmit(pgm, 1, USBASP_FUNC_TRANSMIT, cmd, res, sizeof(res));
+    usbasp_transmit(pgm, 1, USBASP_FUNC_TRANSMIT, cmd, res, 4);
 
   if(nbytes != 4){
     fprintf(stderr, "%s: error: wrong responds size\n",