From 2a3cf8264df07af0d2ee3ca72cfa95986ba38b81 Mon Sep 17 00:00:00 2001 From: Melissa LeBlanc-Williams Date: Wed, 27 Mar 2024 12:27:44 -0700 Subject: [PATCH 1/7] Attempt to fix compiler warnings --- library.properties | 2 +- src/Adafruit_GPS.cpp | 3 ++- src/NMEA_build.cpp | 17 ++++++++++++++++- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/library.properties b/library.properties index c0e240d..d4b2a44 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=Adafruit GPS Library -version=1.7.4 +version=1.7.5 author=Adafruit maintainer=Adafruit sentence=An interrupt-based GPS library for no-parsing-required use diff --git a/src/Adafruit_GPS.cpp b/src/Adafruit_GPS.cpp index e3664c6..bed5c38 100644 --- a/src/Adafruit_GPS.cpp +++ b/src/Adafruit_GPS.cpp @@ -351,7 +351,8 @@ char Adafruit_GPS::read(void) { } // Serial.print(c); - currentline[lineidx++] = c; + currentline[lineidx] = c; + lineidx++; if (lineidx >= MAXLINELENGTH) lineidx = MAXLINELENGTH - 1; // ensure there is someplace to put the next received character diff --git a/src/NMEA_build.cpp b/src/NMEA_build.cpp index 072b4af..832a0c6 100644 --- a/src/NMEA_build.cpp +++ b/src/NMEA_build.cpp @@ -590,5 +590,20 @@ void Adafruit_GPS::addChecksum(char *buff) { cs ^= buff[i]; i++; } - sprintf(buff, "%s*%02X", buff, cs); + + // Calculate the needed buffer size: original length + 3 (*XX) + 1 (null terminator) + int neededSize = strlen(buff) + 4; + char *tempBuffer = (char *)malloc(neededSize); + + if (tempBuffer != NULL) { + // Use snprintf to safely format the string with the checksum + snprintf(tempBuffer, neededSize, "%s*%02X", buff, cs); + + // Copy the formatted string back to the original buffer + // Note: Make sure the original buffer is large enough to hold the new string. + strcpy(buff, tempBuffer); + + // Free the allocated memory to avoid memory leaks + free(tempBuffer); + } } From ccfb4443691521365ad112b0722d25e78d2e6b43 Mon Sep 17 00:00:00 2001 From: Melissa LeBlanc-Williams Date: Wed, 27 Mar 2024 12:27:44 -0700 Subject: [PATCH 2/7] Attempt to fix compiler warnings --- library.properties | 2 +- src/Adafruit_GPS.cpp | 3 ++- src/NMEA_build.cpp | 17 ++++++++++++++++- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/library.properties b/library.properties index c0e240d..d4b2a44 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=Adafruit GPS Library -version=1.7.4 +version=1.7.5 author=Adafruit maintainer=Adafruit sentence=An interrupt-based GPS library for no-parsing-required use diff --git a/src/Adafruit_GPS.cpp b/src/Adafruit_GPS.cpp index e3664c6..bed5c38 100644 --- a/src/Adafruit_GPS.cpp +++ b/src/Adafruit_GPS.cpp @@ -351,7 +351,8 @@ char Adafruit_GPS::read(void) { } // Serial.print(c); - currentline[lineidx++] = c; + currentline[lineidx] = c; + lineidx++; if (lineidx >= MAXLINELENGTH) lineidx = MAXLINELENGTH - 1; // ensure there is someplace to put the next received character diff --git a/src/NMEA_build.cpp b/src/NMEA_build.cpp index 072b4af..832a0c6 100644 --- a/src/NMEA_build.cpp +++ b/src/NMEA_build.cpp @@ -590,5 +590,20 @@ void Adafruit_GPS::addChecksum(char *buff) { cs ^= buff[i]; i++; } - sprintf(buff, "%s*%02X", buff, cs); + + // Calculate the needed buffer size: original length + 3 (*XX) + 1 (null terminator) + int neededSize = strlen(buff) + 4; + char *tempBuffer = (char *)malloc(neededSize); + + if (tempBuffer != NULL) { + // Use snprintf to safely format the string with the checksum + snprintf(tempBuffer, neededSize, "%s*%02X", buff, cs); + + // Copy the formatted string back to the original buffer + // Note: Make sure the original buffer is large enough to hold the new string. + strcpy(buff, tempBuffer); + + // Free the allocated memory to avoid memory leaks + free(tempBuffer); + } } From 02b1489f27899d4ae3c2a75c7df65d7f1b199b37 Mon Sep 17 00:00:00 2001 From: Melissa LeBlanc-Williams Date: Wed, 27 Mar 2024 12:54:27 -0700 Subject: [PATCH 3/7] Attempt to fix compiler warnings 2 --- src/NMEA_build.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/NMEA_build.cpp b/src/NMEA_build.cpp index e113e53..9a8548f 100644 --- a/src/NMEA_build.cpp +++ b/src/NMEA_build.cpp @@ -565,12 +565,11 @@ char *Adafruit_GPS::build(char *nmea, const char *thisSource, addChecksum(nmea); // Successful completion if (!noCRLF) { // Add Carriage Return and Line Feed to comply with NMEA-183 - int neededSize = strlen(nmea) + 2; - char *tempBuffer = (char *)malloc(neededSize); - if (tempBuffer != NULL) { - sprintf(tempBuffer, neededSize, "%s\r\n", nmea); - strcpy(nmea, tempBuffer); - free(tempBuffer); + size_t len = strlen(nmea); + char *newStr = malloc(len + 3); // +2 for \r\n, +1 for null terminator + if (newStr) { + strcpy(newStr, nmea); // Copy original string + strcat(newStr, "\r\n"); // Append \r\n } } return nmea; // return pointer to finished product From 2850fdeff74a55cccaf22a472c17b8b3dfa6ce8f Mon Sep 17 00:00:00 2001 From: Melissa LeBlanc-Williams Date: Wed, 27 Mar 2024 13:00:29 -0700 Subject: [PATCH 4/7] Attempt to fix compiler warnings 3 --- src/NMEA_build.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/NMEA_build.cpp b/src/NMEA_build.cpp index 9a8548f..c7e9fb8 100644 --- a/src/NMEA_build.cpp +++ b/src/NMEA_build.cpp @@ -566,7 +566,7 @@ char *Adafruit_GPS::build(char *nmea, const char *thisSource, addChecksum(nmea); // Successful completion if (!noCRLF) { // Add Carriage Return and Line Feed to comply with NMEA-183 size_t len = strlen(nmea); - char *newStr = malloc(len + 3); // +2 for \r\n, +1 for null terminator + char *newStr = (char *)malloc(len + 3); // +2 for \r\n, +1 for null terminator if (newStr) { strcpy(newStr, nmea); // Copy original string strcat(newStr, "\r\n"); // Append \r\n From b9abc8358187c66745e27e83af702c23f6156b69 Mon Sep 17 00:00:00 2001 From: Melissa LeBlanc-Williams Date: Wed, 27 Mar 2024 13:10:34 -0700 Subject: [PATCH 5/7] Run clang format --- src/NMEA_build.cpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/NMEA_build.cpp b/src/NMEA_build.cpp index c7e9fb8..934438a 100644 --- a/src/NMEA_build.cpp +++ b/src/NMEA_build.cpp @@ -566,10 +566,11 @@ char *Adafruit_GPS::build(char *nmea, const char *thisSource, addChecksum(nmea); // Successful completion if (!noCRLF) { // Add Carriage Return and Line Feed to comply with NMEA-183 size_t len = strlen(nmea); - char *newStr = (char *)malloc(len + 3); // +2 for \r\n, +1 for null terminator + char *newStr = + (char *)malloc(len + 3); // +2 for \r\n, +1 for null terminator if (newStr) { - strcpy(newStr, nmea); // Copy original string - strcat(newStr, "\r\n"); // Append \r\n + strcpy(newStr, nmea); // Copy original string + strcat(newStr, "\r\n"); // Append \r\n } } return nmea; // return pointer to finished product @@ -596,7 +597,8 @@ void Adafruit_GPS::addChecksum(char *buff) { i++; } - // Calculate the needed buffer size: original length + 3 (*XX) + 1 (null terminator) + // Calculate the needed buffer size: original length + 3 (*XX) + 1 (null + // terminator) int neededSize = strlen(buff) + 4; char *tempBuffer = (char *)malloc(neededSize); @@ -605,7 +607,8 @@ void Adafruit_GPS::addChecksum(char *buff) { snprintf(tempBuffer, neededSize, "%s*%02X", buff, cs); // Copy the formatted string back to the original buffer - // Note: Make sure the original buffer is large enough to hold the new string. + // Note: Make sure the original buffer is large enough to hold the new + // string. strcpy(buff, tempBuffer); // Free the allocated memory to avoid memory leaks From 792e13d2b2dda20e171f6f46392762b7210dd5a1 Mon Sep 17 00:00:00 2001 From: Melissa LeBlanc-Williams Date: Thu, 28 Mar 2024 08:25:16 -0700 Subject: [PATCH 6/7] Actually return NMEA string with CRLF --- src/NMEA_build.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/NMEA_build.cpp b/src/NMEA_build.cpp index 934438a..355b962 100644 --- a/src/NMEA_build.cpp +++ b/src/NMEA_build.cpp @@ -566,11 +566,12 @@ char *Adafruit_GPS::build(char *nmea, const char *thisSource, addChecksum(nmea); // Successful completion if (!noCRLF) { // Add Carriage Return and Line Feed to comply with NMEA-183 size_t len = strlen(nmea); - char *newStr = + char *nmeaWithCRLF = (char *)malloc(len + 3); // +2 for \r\n, +1 for null terminator - if (newStr) { - strcpy(newStr, nmea); // Copy original string - strcat(newStr, "\r\n"); // Append \r\n + if (nmeaWithCRLF) { + strcpy(nmeaWithCRLF, nmea); // Copy original string + strcat(nmeaWithCRLF, "\r\n"); // Append \r\n + return nmeaWithCRLF; // return pointer to finished product } } return nmea; // return pointer to finished product From 605dfc3f2b99593f0f5d408ca6639a7ad90e649c Mon Sep 17 00:00:00 2001 From: Melissa LeBlanc-Williams Date: Mon, 1 Apr 2024 13:10:56 -0700 Subject: [PATCH 7/7] Add suggested fix --- src/NMEA_build.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/NMEA_build.cpp b/src/NMEA_build.cpp index 355b962..fadfd90 100644 --- a/src/NMEA_build.cpp +++ b/src/NMEA_build.cpp @@ -571,7 +571,8 @@ char *Adafruit_GPS::build(char *nmea, const char *thisSource, if (nmeaWithCRLF) { strcpy(nmeaWithCRLF, nmea); // Copy original string strcat(nmeaWithCRLF, "\r\n"); // Append \r\n - return nmeaWithCRLF; // return pointer to finished product + strcpy(nmea, nmeaWithCRLF); // Copy back to original buffer + free(nmeaWithCRLF); // Free the allocated memory } } return nmea; // return pointer to finished product