Skip to content

Commit 6a7ee7f

Browse files
committed
Change sprintf to snprintf
1 parent 14c52f3 commit 6a7ee7f

File tree

3 files changed

+411
-280
lines changed

3 files changed

+411
-280
lines changed

src/sfe_sara_r5.cpp

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@ SARA_R5::SARA_R5()
99
UBX_CELL_error_t SARA_R5::setUtimeMode(UBX_CELL_utime_mode_t mode, UBX_CELL_utime_sensor_t sensor)
1010
{
1111
UBX_CELL_error_t err;
12+
size_t cmdLen = strlen(UBX_CELL_GNSS_REQUEST_TIME) + 16;
1213
char *command;
1314

14-
command = ubx_cell_calloc_char(strlen(UBX_CELL_GNSS_REQUEST_TIME) + 16);
15+
command = ubx_cell_calloc_char(cmdLen);
1516
if (command == nullptr)
1617
return UBX_CELL_ERROR_OUT_OF_MEMORY;
1718
if (mode == UBX_CELL_UTIME_MODE_STOP) // stop UTIME does not require a sensor
18-
sprintf(command, "%s=%d", UBX_CELL_GNSS_REQUEST_TIME, mode);
19+
snprintf(command, cmdLen, "%s=%d", UBX_CELL_GNSS_REQUEST_TIME, mode);
1920
else
20-
sprintf(command, "%s=%d,%d", UBX_CELL_GNSS_REQUEST_TIME, mode, sensor);
21+
snprintf(command, cmdLen, "%s=%d,%d", UBX_CELL_GNSS_REQUEST_TIME, mode, sensor);
2122

2223
err = sendCommandWithResponse(command, UBX_CELL_RESPONSE_OK_OR_ERROR, nullptr, UBX_CELL_10_SEC_TIMEOUT);
2324
free(command);
@@ -27,16 +28,17 @@ UBX_CELL_error_t SARA_R5::setUtimeMode(UBX_CELL_utime_mode_t mode, UBX_CELL_utim
2728
UBX_CELL_error_t SARA_R5::getUtimeMode(UBX_CELL_utime_mode_t *mode, UBX_CELL_utime_sensor_t *sensor)
2829
{
2930
UBX_CELL_error_t err;
31+
size_t cmdLen = strlen(UBX_CELL_GNSS_REQUEST_TIME) + 2;
3032
char *command;
3133
char *response;
3234

3335
UBX_CELL_utime_mode_t m;
3436
UBX_CELL_utime_sensor_t s;
3537

36-
command = ubx_cell_calloc_char(strlen(UBX_CELL_GNSS_REQUEST_TIME) + 2);
38+
command = ubx_cell_calloc_char(cmdLen);
3739
if (command == nullptr)
3840
return UBX_CELL_ERROR_OUT_OF_MEMORY;
39-
sprintf(command, "%s?", UBX_CELL_GNSS_REQUEST_TIME);
41+
snprintf(command, cmdLen, "%s?", UBX_CELL_GNSS_REQUEST_TIME);
4042

4143
response = ubx_cell_calloc_char(minimumResponseAllocation);
4244
if (response == nullptr)
@@ -83,12 +85,13 @@ UBX_CELL_error_t SARA_R5::getUtimeMode(UBX_CELL_utime_mode_t *mode, UBX_CELL_uti
8385
UBX_CELL_error_t SARA_R5::setUtimeIndication(UBX_CELL_utime_urc_configuration_t config)
8486
{
8587
UBX_CELL_error_t err;
88+
size_t cmdLen = strlen(UBX_CELL_GNSS_TIME_INDICATION) + 16;
8689
char *command;
8790

88-
command = ubx_cell_calloc_char(strlen(UBX_CELL_GNSS_TIME_INDICATION) + 16);
91+
command = ubx_cell_calloc_char(cmdLen);
8992
if (command == nullptr)
9093
return UBX_CELL_ERROR_OUT_OF_MEMORY;
91-
sprintf(command, "%s=%d", UBX_CELL_GNSS_TIME_INDICATION, config);
94+
snprintf(command, cmdLen, "%s=%d", UBX_CELL_GNSS_TIME_INDICATION, config);
9295

9396
err = sendCommandWithResponse(command, UBX_CELL_RESPONSE_OK_OR_ERROR, nullptr, UBX_CELL_STANDARD_RESPONSE_TIMEOUT);
9497
free(command);
@@ -98,15 +101,16 @@ UBX_CELL_error_t SARA_R5::setUtimeIndication(UBX_CELL_utime_urc_configuration_t
98101
UBX_CELL_error_t SARA_R5::getUtimeIndication(UBX_CELL_utime_urc_configuration_t *config)
99102
{
100103
UBX_CELL_error_t err;
104+
size_t cmdLen = strlen(UBX_CELL_GNSS_TIME_INDICATION) + 2;
101105
char *command;
102106
char *response;
103107

104108
UBX_CELL_utime_urc_configuration_t c;
105109

106-
command = ubx_cell_calloc_char(strlen(UBX_CELL_GNSS_TIME_INDICATION) + 2);
110+
command = ubx_cell_calloc_char(cmdLen);
107111
if (command == nullptr)
108112
return UBX_CELL_ERROR_OUT_OF_MEMORY;
109-
sprintf(command, "%s?", UBX_CELL_GNSS_TIME_INDICATION);
113+
snprintf(command, cmdLen, "%s?", UBX_CELL_GNSS_TIME_INDICATION);
110114

111115
response = ubx_cell_calloc_char(minimumResponseAllocation);
112116
if (response == nullptr)
@@ -146,15 +150,16 @@ UBX_CELL_error_t SARA_R5::getUtimeIndication(UBX_CELL_utime_urc_configuration_t
146150
UBX_CELL_error_t SARA_R5::setUtimeConfiguration(int32_t offsetNanoseconds, int32_t offsetSeconds)
147151
{
148152
UBX_CELL_error_t err;
153+
size_t cmdLen = strlen(UBX_CELL_GNSS_TIME_CONFIGURATION) + 48;
149154
char *command;
150155

151-
command = ubx_cell_calloc_char(strlen(UBX_CELL_GNSS_TIME_CONFIGURATION) + 48);
156+
command = ubx_cell_calloc_char(cmdLen);
152157
if (command == nullptr)
153158
return UBX_CELL_ERROR_OUT_OF_MEMORY;
154159
#if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266)
155-
sprintf(command, "%s=%d,%d", UBX_CELL_GNSS_TIME_CONFIGURATION, offsetNanoseconds, offsetSeconds);
160+
snprintf(command, cmdLen, "%s=%d,%d", UBX_CELL_GNSS_TIME_CONFIGURATION, offsetNanoseconds, offsetSeconds);
156161
#else
157-
sprintf(command, "%s=%ld,%ld", UBX_CELL_GNSS_TIME_CONFIGURATION, offsetNanoseconds, offsetSeconds);
162+
snprintf(command, cmdLen, "%s=%ld,%ld", UBX_CELL_GNSS_TIME_CONFIGURATION, offsetNanoseconds, offsetSeconds);
158163
#endif
159164

160165
err = sendCommandWithResponse(command, UBX_CELL_RESPONSE_OK_OR_ERROR, nullptr, UBX_CELL_STANDARD_RESPONSE_TIMEOUT);
@@ -165,16 +170,17 @@ UBX_CELL_error_t SARA_R5::setUtimeConfiguration(int32_t offsetNanoseconds, int32
165170
UBX_CELL_error_t SARA_R5::getUtimeConfiguration(int32_t *offsetNanoseconds, int32_t *offsetSeconds)
166171
{
167172
UBX_CELL_error_t err;
173+
size_t cmdLen = strlen(UBX_CELL_GNSS_TIME_CONFIGURATION) + 2;
168174
char *command;
169175
char *response;
170176

171177
int32_t ons;
172178
int32_t os;
173179

174-
command = ubx_cell_calloc_char(strlen(UBX_CELL_GNSS_TIME_CONFIGURATION) + 2);
180+
command = ubx_cell_calloc_char(cmdLen);
175181
if (command == nullptr)
176182
return UBX_CELL_ERROR_OUT_OF_MEMORY;
177-
sprintf(command, "%s?", UBX_CELL_GNSS_TIME_CONFIGURATION);
183+
snprintf(command, cmdLen, "%s?", UBX_CELL_GNSS_TIME_CONFIGURATION);
178184

179185
response = ubx_cell_calloc_char(minimumResponseAllocation);
180186
if (response == nullptr)
@@ -218,15 +224,16 @@ UBX_CELL_error_t SARA_R5::getUtimeConfiguration(int32_t *offsetNanoseconds, int3
218224
UBX_CELL_error_t SARA_R5::setPDPconfiguration(int profile, UBX_CELL_pdp_configuration_parameter_t parameter, int value)
219225
{
220226
UBX_CELL_error_t err;
227+
size_t cmdLen = strlen(UBX_CELL_MESSAGE_PDP_CONFIG) + 24;
221228
char *command;
222229

223230
if (profile >= UBX_CELL_NUM_PSD_PROFILES)
224231
return UBX_CELL_ERROR_ERROR;
225232

226-
command = ubx_cell_calloc_char(strlen(UBX_CELL_MESSAGE_PDP_CONFIG) + 24);
233+
command = ubx_cell_calloc_char(cmdLen);
227234
if (command == nullptr)
228235
return UBX_CELL_ERROR_OUT_OF_MEMORY;
229-
sprintf(command, "%s=%d,%d,%d", UBX_CELL_MESSAGE_PDP_CONFIG, profile, parameter, value);
236+
snprintf(command, cmdLen, "%s=%d,%d,%d", UBX_CELL_MESSAGE_PDP_CONFIG, profile, parameter, value);
230237

231238
err = sendCommandWithResponse(command, UBX_CELL_RESPONSE_OK_OR_ERROR, nullptr, UBX_CELL_STANDARD_RESPONSE_TIMEOUT);
232239

@@ -244,15 +251,16 @@ UBX_CELL_error_t SARA_R5::setPDPconfiguration(int profile, UBX_CELL_pdp_configur
244251
String value)
245252
{
246253
UBX_CELL_error_t err;
254+
size_t cmdLen = strlen(UBX_CELL_MESSAGE_PDP_CONFIG) + 64;
247255
char *command;
248256

249257
if (profile >= UBX_CELL_NUM_PSD_PROFILES)
250258
return UBX_CELL_ERROR_ERROR;
251259

252-
command = ubx_cell_calloc_char(strlen(UBX_CELL_MESSAGE_PDP_CONFIG) + 64);
260+
command = ubx_cell_calloc_char(cmdLen);
253261
if (command == nullptr)
254262
return UBX_CELL_ERROR_OUT_OF_MEMORY;
255-
sprintf(command, "%s=%d,%d,\"%s\"", UBX_CELL_MESSAGE_PDP_CONFIG, profile, parameter, value.c_str());
263+
snprintf(command, cmdLen, "%s=%d,%d,\"%s\"", UBX_CELL_MESSAGE_PDP_CONFIG, profile, parameter, value.c_str());
256264

257265
err = sendCommandWithResponse(command, UBX_CELL_RESPONSE_OK_OR_ERROR, nullptr, UBX_CELL_STANDARD_RESPONSE_TIMEOUT);
258266

@@ -264,15 +272,16 @@ UBX_CELL_error_t SARA_R5::setPDPconfiguration(int profile, UBX_CELL_pdp_configur
264272
IPAddress value)
265273
{
266274
UBX_CELL_error_t err;
275+
size_t cmdLen = strlen(UBX_CELL_MESSAGE_PDP_CONFIG) + 64;
267276
char *command;
268277

269278
if (profile >= UBX_CELL_NUM_PSD_PROFILES)
270279
return UBX_CELL_ERROR_ERROR;
271280

272-
command = ubx_cell_calloc_char(strlen(UBX_CELL_MESSAGE_PDP_CONFIG) + 64);
281+
command = ubx_cell_calloc_char(cmdLen);
273282
if (command == nullptr)
274283
return UBX_CELL_ERROR_OUT_OF_MEMORY;
275-
sprintf(command, "%s=%d,%d,\"%d.%d.%d.%d\"", UBX_CELL_MESSAGE_PDP_CONFIG, profile, parameter, value[0], value[1],
284+
snprintf(command, cmdLen, "%s=%d,%d,\"%d.%d.%d.%d\"", UBX_CELL_MESSAGE_PDP_CONFIG, profile, parameter, value[0], value[1],
276285
value[2], value[3]);
277286

278287
err = sendCommandWithResponse(command, UBX_CELL_RESPONSE_OK_OR_ERROR, nullptr, UBX_CELL_STANDARD_RESPONSE_TIMEOUT);
@@ -284,15 +293,16 @@ UBX_CELL_error_t SARA_R5::setPDPconfiguration(int profile, UBX_CELL_pdp_configur
284293
UBX_CELL_error_t SARA_R5::performPDPaction(int profile, UBX_CELL_pdp_actions_t action)
285294
{
286295
UBX_CELL_error_t err;
296+
size_t cmdLen = strlen(UBX_CELL_MESSAGE_PDP_ACTION) + 32;
287297
char *command;
288298

289299
if (profile >= UBX_CELL_NUM_PSD_PROFILES)
290300
return UBX_CELL_ERROR_ERROR;
291301

292-
command = ubx_cell_calloc_char(strlen(UBX_CELL_MESSAGE_PDP_ACTION) + 32);
302+
command = ubx_cell_calloc_char(cmdLen);
293303
if (command == nullptr)
294304
return UBX_CELL_ERROR_OUT_OF_MEMORY;
295-
sprintf(command, "%s=%d,%d", UBX_CELL_MESSAGE_PDP_ACTION, profile, action);
305+
snprintf(command, cmdLen, "%s=%d,%d", UBX_CELL_MESSAGE_PDP_ACTION, profile, action);
296306

297307
err = sendCommandWithResponse(command, UBX_CELL_RESPONSE_OK_OR_ERROR, nullptr, UBX_CELL_STANDARD_RESPONSE_TIMEOUT);
298308

@@ -302,6 +312,7 @@ UBX_CELL_error_t SARA_R5::performPDPaction(int profile, UBX_CELL_pdp_actions_t a
302312

303313
UBX_CELL_error_t SARA_R5::getNetworkAssignedIPAddress(int profile, IPAddress *address)
304314
{
315+
size_t cmdLen = strlen(UBX_CELL_NETWORK_ASSIGNED_DATA) + 16;
305316
char *command;
306317
char *response;
307318
UBX_CELL_error_t err;
@@ -310,10 +321,10 @@ UBX_CELL_error_t SARA_R5::getNetworkAssignedIPAddress(int profile, IPAddress *ad
310321
int paramTag = 0; // 0: IP address: dynamic IP address assigned during PDP context activation
311322
int paramVals[4];
312323

313-
command = ubx_cell_calloc_char(strlen(UBX_CELL_NETWORK_ASSIGNED_DATA) + 16);
324+
command = ubx_cell_calloc_char(cmdLen);
314325
if (command == nullptr)
315326
return UBX_CELL_ERROR_OUT_OF_MEMORY;
316-
sprintf(command, "%s=%d,%d", UBX_CELL_NETWORK_ASSIGNED_DATA, profile, paramTag);
327+
snprintf(command, cmdLen, "%s=%d,%d", UBX_CELL_NETWORK_ASSIGNED_DATA, profile, paramTag);
317328

318329
response = ubx_cell_calloc_char(minimumResponseAllocation);
319330
if (response == nullptr)

0 commit comments

Comments
 (0)