Hi all,

after running cppcheck [1] against some of my own applications which found several issues 
(plus lots of false positives, unfortunately), I tried it on QLandkarte GT.  Attached is 
a bunch if fixes for GarminDev which are not critical (mostly, at least), but IMHO clean 
up the code.  If you think this makes sense, I'll do the same for the "main" 
code...

Opinions?

Cheers,
Albrecht.

[1] <http://sourceforge.net/projects/cppcheck>

---
* src/Garmin.cpp: remove several out-of-bounds assignments - totally harmless 
unless the compiler decides to re-order them
* src/CSerial.cpp: remove redundant break and a unused variable
* src/GPSMap76/CDevice.cpp, src/EtrexLegend/CDevice.cpp, 
src/EtrexLegendC/CDevice.cpp: catch realloc() returning NULL (*very* unlikely, 
but will lead to a crash if it happens)
* src/GPSMap60CSx/CDevice.cpp, src/ForeRunner/CDevice.cpp: remove unused 
variables
Index: src/Garmin.cpp
===================================================================
--- src/Garmin.cpp	(Revision 3449)
+++ src/Garmin.cpp	(Arbeitskopie)
@@ -101,10 +101,8 @@
         tar.dist         = gar_endian(float, src.dist);
         tar.state[0]     = src.state[0];
         tar.state[1]     = src.state[1];
-        tar.state[2]     = 0;
         tar.cc[0]        = src.cc[0];
         tar.cc[1]        = src.cc[1];
-        tar.cc[2]        = 0;
 
         char * pStr     = tar.str;
 
@@ -137,10 +135,8 @@
         tar.dist         = gar_endian(float, src.dist);
         tar.state[0]     = src.state[0];
         tar.state[1]     = src.state[1];
-        tar.state[2]     = 0;
         tar.cc[0]        = src.cc[0];
         tar.cc[1]        = src.cc[1];
-        tar.cc[2]        = 0;
         tar.ete          = gar_endian(uint32_t, src.ete);
 
         const char* p = src.str;
@@ -188,10 +184,8 @@
         tar.dist         = gar_endian(float, src.dist);
         tar.state[0]     = src.state[0];
         tar.state[1]     = src.state[1];
-        tar.state[2]     = 0;
         tar.cc[0]        = src.cc[0];
         tar.cc[1]        = src.cc[1];
-        tar.cc[2]        = 0;
         tar.ete          = gar_endian(uint32_t, src.ete);
 
         char * pStr     = tar.str;
@@ -278,10 +272,8 @@
         tar.dist         = gar_endian(float, src.dist);
         tar.state[0]     = src.state[0];
         tar.state[1]     = src.state[1];
-        tar.state[2]     = 0;
         tar.cc[0]        = src.cc[0];
         tar.cc[1]        = src.cc[1];
-        tar.cc[2]        = 0;
         tar.ete          = gar_endian(uint32_t, src.ete);
         tar.temp         = gar_endian(float, src.temp);
         tar.time         = gar_endian(uint32_t, src.time);
Index: src/CSerial.cpp
===================================================================
--- src/CSerial.cpp	(Revision 3449)
+++ src/CSerial.cpp	(Arbeitskopie)
@@ -587,7 +587,6 @@
         result= ReadFile( hCom, byte, 1, &BytesRead, NULL);
         if (BytesRead == 1) {
             return (1);
-            break;
         }
         Sleep( 20);
 
@@ -634,9 +633,8 @@
 int CSerial::serial_check_ack(uint8_t cmd)
 {
     Packet_t response;
-    int count;
 
-    while ((count = serial_read(response)) > 0) {
+    while (serial_read(response) > 0) {
         if (response.id == Pid_Ack_Byte && response.payload[0] == cmd)
             return 0;
         else if (response.id == Pid_Nak_Byte && response.payload[0] == cmd) {
Index: src/GPSMap76/CDevice.cpp
===================================================================
--- src/GPSMap76/CDevice.cpp	(Revision 3449)
+++ src/GPSMap76/CDevice.cpp	(Arbeitskopie)
@@ -374,7 +374,12 @@
             // realloc memory if chunk does not fit
             if((fill +  response.size - 1) > size) {
                 size += size;
-                pData = (char*)realloc(pData,size);
+                char *newData = (char*)realloc(pData,size);
+                if (!newData) {
+                    free(pData);
+                    throw exce_t(errRead, "Out of memory.");
+                } else
+                    pData = newData;                
             }
 
             memcpy(&pData[fill], response.payload + 1, response.size - 1);
Index: src/EtrexLegend/CDevice.cpp
===================================================================
--- src/EtrexLegend/CDevice.cpp	(Revision 3449)
+++ src/EtrexLegend/CDevice.cpp	(Arbeitskopie)
@@ -376,7 +376,12 @@
             // realloc memory if chunk does not fit
             if((fill +  response.size - 1) > size) {
                 size += size;
-                pData = (char*)realloc(pData,size);
+                char *newData = (char*)realloc(pData,size);
+                if (!newData) {
+                    free(pData);
+                    throw exce_t(errRead, "Out of memory.");
+                } else
+                    pData = newData;
             }
 
             memcpy(&pData[fill], response.payload + 1, response.size - 1);
Index: src/EtrexLegendC/CDevice.cpp
===================================================================
--- src/EtrexLegendC/CDevice.cpp	(Revision 3449)
+++ src/EtrexLegendC/CDevice.cpp	(Arbeitskopie)
@@ -474,7 +474,12 @@
             // realloc memory if chunk does not fit
             if((fill +  response.size - 1) > size) {
                 size += size;
-                pData = (char*)realloc(pData,size);
+                char *newData = (char*)realloc(pData,size);
+                if (!newData) {
+                    free(pData);
+                    throw exce_t(errRead, "Out of memory.");
+                } else
+                    pData = newData;                
             }
 
             memcpy(&pData[fill], response.payload + 1, response.size - 1);
Index: src/GPSMap60CSx/CDevice.cpp
===================================================================
--- src/GPSMap60CSx/CDevice.cpp	(Revision 3449)
+++ src/GPSMap60CSx/CDevice.cpp	(Arbeitskopie)
@@ -888,7 +888,6 @@
     *(uint16_t*)command.payload = gar_endian(uint16_t, Cmnd_Transfer_Rte);
     usb->write(command);
 
-    int         routeidx = 0;
     string      name;
     Route_t *   route = 0;
     int         cancel = 0;
@@ -901,7 +900,6 @@
         if(!usb->read(response)) continue;
 
         if(response.id == Pid_Rte_Hdr) {
-            routeidx = 0;
             D202_Rte_Hdr_t * hdr = (D202_Rte_Hdr_t*)response.payload;
             routes.push_back(Route_t());
             route = &routes.back();
Index: src/ForeRunner/CDevice.cpp
===================================================================
--- src/ForeRunner/CDevice.cpp	(Revision 3449)
+++ src/ForeRunner/CDevice.cpp	(Arbeitskopie)
@@ -380,7 +380,6 @@
     *(uint16_t*)command.payload = gar_endian(uint16_t, Cmnd_Transfer_Trk);
     usb->write(command);
 
-    int         trackidx = 0;
     string      name;
     Track_t *   track = 0;
     int         cancel = 0;
@@ -394,7 +393,6 @@
 
                                  //read track header
         if(response.id == Pid_Trk_Hdr) {
-            trackidx = 0;
             D311_Trk_Hdr_t * hdr = (D311_Trk_Hdr_t*)response.payload;
             tracks.push_back(Track_t());
             track = &tracks.back();
@@ -464,7 +462,6 @@
     *(uint16_t*)command.payload = gar_endian(uint16_t, Cmnd_Transfer_Rte);
     usb->write(command);
 
-    int         routeidx = 0;
     string      name;
     Route_t *   route = 0;
     int         cancel = 0;
@@ -477,7 +474,6 @@
         if(!usb->read(response)) continue;
 
         if(response.id == Pid_Rte_Hdr) {
-            routeidx = 0;
             D202_Rte_Hdr_t * hdr = (D202_Rte_Hdr_t*)response.payload;
             routes.push_back(Route_t());
             route = &routes.back();

Attachment: pgpuE6gDhgwuS.pgp
Description: PGP signature

------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Qlandkartegt-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/qlandkartegt-users

Reply via email to