Skip to content

Commit 3ad1a13

Browse files
committed
MCU8MASS-1781 Make some modifications on strncpy and strcpy usage
1 parent de0eb1e commit 3ad1a13

File tree

3 files changed

+21
-28
lines changed

3 files changed

+21
-28
lines changed

src/http_client.cpp

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -287,33 +287,27 @@ HttpResponse HttpClientClass::post(const char* endpoint,
287287

288288
switch (content_type) {
289289
case CONTENT_TYPE_APPLICATION_X_WWW_FORM_URLENCODED:
290-
strncpy_P(content_type_buffer,
291-
HTTP_CONTENT_TYPE_APPLICATION_X_WWW_FORM_URLENCODED,
292-
sizeof(content_type_buffer));
290+
strcpy_P(content_type_buffer,
291+
HTTP_CONTENT_TYPE_APPLICATION_X_WWW_FORM_URLENCODED);
293292
break;
294293

295294
case CONTENT_TYPE_APPLICATION_OCTET_STREAM:
296-
strncpy_P(content_type_buffer,
297-
HTTP_CONTENT_TYPE_APPLICATION_OCTET_STREAM,
298-
sizeof(content_type_buffer));
295+
strcpy_P(content_type_buffer,
296+
HTTP_CONTENT_TYPE_APPLICATION_OCTET_STREAM);
299297
break;
300298

301299
case CONTENT_TYPE_MULTIPART_FORM_DATA:
302-
strncpy_P(content_type_buffer,
303-
HTTP_CONTENT_TYPE_APPLICATION_MULTIPART_FORM_DATA,
304-
sizeof(content_type_buffer));
300+
strcpy_P(content_type_buffer,
301+
HTTP_CONTENT_TYPE_APPLICATION_MULTIPART_FORM_DATA);
305302
break;
306303

307304
case CONTENT_TYPE_APPLICATION_JSON:
308-
strncpy_P(content_type_buffer,
309-
HTTP_CONTENT_TYPE_APPLICATION_APPLICATION_JSON,
310-
sizeof(content_type_buffer));
305+
strcpy_P(content_type_buffer,
306+
HTTP_CONTENT_TYPE_APPLICATION_APPLICATION_JSON);
311307
break;
312308

313309
default:
314-
strncpy_P(content_type_buffer,
315-
HTTP_CONTENT_TYPE_TEXT_PLAIN,
316-
sizeof(content_type_buffer));
310+
strcpy_P(content_type_buffer, HTTP_CONTENT_TYPE_TEXT_PLAIN);
317311
break;
318312
}
319313

src/mqtt_client.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,13 @@ static void internalDisconnectCallback(char* urc_data) {
120120

121121
static void internalOnReceiveCallback(char* urc_data) {
122122

123+
// The incoming urc_data is a buffer of maximum URC_DATA_BUFFER_SIZE (from
124+
// the SequansController)
123125
strncpy(urc_buffer, urc_data, URC_DATA_BUFFER_SIZE);
124126

127+
// Safe guard ourselves
128+
urc_buffer[URC_DATA_BUFFER_SIZE] = '\0';
129+
125130
const bool got_topic = SequansController.extractValueFromCommandResponse(
126131
urc_buffer,
127132
1,

src/sequans_controller.cpp

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -903,13 +903,11 @@ bool SequansControllerClass::extractValueFromCommandResponse(
903903
const size_t destination_buffer_size,
904904
const char start_character) {
905905

906-
// We need a copy in order to not modify the original
907-
size_t rcp_size = strlen(response) + 1;
908-
char response_copy[rcp_size];
909-
strncpy(response_copy, response, rcp_size);
910-
911-
// Safe guard ourselves
912-
response_copy[rcp_size - 1] = '\0';
906+
// We need a copy in order to not modify the original. This copy will
907+
// include the null termination.
908+
const size_t response_size = strlen(response) + 1;
909+
char response_copy[response_size] = "";
910+
strcpy(response_copy, response);
913911

914912
char* data;
915913

@@ -1034,13 +1032,9 @@ bool SequansControllerClass::registerCallback(const char* urc_identifier,
10341032
if (urcs[i].identifier_length == 0) {
10351033

10361034
if (is_flash_string) {
1037-
strncpy_P((char*)urcs[i].identifier,
1038-
urc_identifier,
1039-
URC_IDENTIFIER_BUFFER_SIZE);
1035+
strcpy_P((char*)urcs[i].identifier, urc_identifier);
10401036
} else {
1041-
strncpy((char*)urcs[i].identifier,
1042-
urc_identifier,
1043-
URC_IDENTIFIER_BUFFER_SIZE);
1037+
strcpy((char*)urcs[i].identifier, urc_identifier);
10441038
}
10451039

10461040
urcs[i].identifier_length = urc_identifier_length;

0 commit comments

Comments
 (0)