Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix compiler warnings #157

Merged
merged 10 commits into from
Apr 16, 2024
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=Adafruit GPS Library
version=1.7.4
version=1.7.5
author=Adafruit
maintainer=Adafruit <info@adafruit.com>
sentence=An interrupt-based GPS library for no-parsing-required use
Expand Down
3 changes: 2 additions & 1 deletion src/Adafruit_GPS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,8 @@ char Adafruit_GPS::read(void) {
}
// Serial.print(c);

currentline[lineidx++] = c;
currentline[lineidx] = c;
lineidx = lineidx + 1;
if (lineidx >= MAXLINELENGTH)
lineidx = MAXLINELENGTH -
1; // ensure there is someplace to put the next received character
Expand Down
28 changes: 26 additions & 2 deletions src/NMEA_build.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,14 @@ 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
sprintf(nmea, "%s\r\n", nmea);
size_t len = strlen(nmea);
char *nmeaWithCRLF =
(char *)malloc(len + 3); // +2 for \r\n, +1 for null terminator
if (nmeaWithCRLF) {
strcpy(nmeaWithCRLF, nmea); // Copy original string
strcat(nmeaWithCRLF, "\r\n"); // Append \r\n
return nmeaWithCRLF; // return pointer to finished product
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need to return the same buffer, so strcpy it back and free the temp buff

}
}
return nmea; // return pointer to finished product
}
Expand All @@ -590,5 +597,22 @@ 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);
}
}
Loading