From 02b1489f27899d4ae3c2a75c7df65d7f1b199b37 Mon Sep 17 00:00:00 2001 From: Melissa LeBlanc-Williams Date: Wed, 27 Mar 2024 12:54:27 -0700 Subject: [PATCH] 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