Skip to content

Commit 7c67d4e

Browse files
committed
Prevent printDate / printDays from going out of scope
1 parent 8cdcc2b commit 7c67d4e

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

Firmware/RTK_Everywhere/menuPP.ino

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,10 @@ void menuPointPerfectKeys()
168168
}
169169

170170
// Given a GPS Epoch, return a DD/MM/YYYY string
171-
char *printDateFromGPSEpoch(long long gpsEpoch)
171+
const char *printDateFromGPSEpoch(long long gpsEpoch)
172172
{
173+
static char response[strlen("01/01/1010") + 1]; // Make room for terminator
174+
173175
uint16_t keyGPSWeek;
174176
uint32_t keyGPSToW;
175177
epochToWeekToW(gpsEpoch, &keyGPSWeek, &keyGPSToW);
@@ -179,35 +181,35 @@ char *printDateFromGPSEpoch(long long gpsEpoch)
179181
long expYear;
180182
gpsWeekToWToDate(keyGPSWeek, keyGPSToW, &expDay, &expMonth, &expYear);
181183

182-
char *response = (char *)malloc(strlen("01/01/1010"));
183-
184184
sprintf(response, "%02ld/%02ld/%ld", expDay, expMonth, expYear);
185-
return (response);
185+
return ((const char *)response);
186186
}
187187

188188
// Given a Unix Epoch, return a DD/MM/YYYY string
189189
// https://www.epochconverter.com/programming/c
190-
char *printDateFromUnixEpoch(long long unixEpoch)
190+
const char *printDateFromUnixEpoch(long long unixEpoch)
191191
{
192-
char *buf = (char *)malloc(strlen("01/01/2023") + 1); // Make room for terminator
192+
static char response[strlen("01/01/2023") + 1]; // Make room for terminator
193+
193194
time_t rawtime = unixEpoch;
194195

195196
struct tm ts;
196197
ts = *localtime(&rawtime);
197198

198199
// Format time, "dd/mm/yyyy"
199-
strftime(buf, strlen("01/01/2023") + 1, "%d/%m/%Y", &ts);
200-
return (buf);
200+
strftime(response, strlen("01/01/2023") + 1, "%d/%m/%Y", &ts);
201+
return ((const char *)response);
201202
}
202203

203204
// Given a duration in ms, print days
204-
char *printDaysFromDuration(long long duration)
205+
const char *printDaysFromDuration(long long duration)
205206
{
207+
static char response[strlen("99.99") + 1]; // Make room for terminator
208+
206209
float days = duration / (1000.0 * 60 * 60 * 24); // Convert ms to days
207210

208-
char *response = (char *)malloc(strlen("34.9") + 1); // Make room for terminator
209211
sprintf(response, "%0.2f", days);
210-
return (response);
212+
return ((const char *)response);
211213
}
212214

213215
// Connect to 'home' WiFi and then ThingStream API. This will attach this unique device to the ThingStream network.
@@ -954,8 +956,8 @@ bool pointperfectUpdateKeys()
954956
void mqttCallback(int messageSize)
955957
{
956958
#ifdef COMPILE_WIFI
957-
static uint32_t messageLength;
958-
static byte *message;
959+
static uint32_t messageLength = 0;
960+
static byte *message = nullptr;
959961

960962
do
961963
{
@@ -967,7 +969,7 @@ void mqttCallback(int messageSize)
967969
if (messageLength < messageSize)
968970
{
969971
// Free the previous message buffer
970-
if (message)
972+
if (message != nullptr)
971973
{
972974
free(message);
973975
message = nullptr;

0 commit comments

Comments
 (0)