Skip to content

Commit 5558e1f

Browse files
committed
Update getSubscriberNo and getCCID. Make them memory-safe.
1 parent d0059d0 commit 5558e1f

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

src/SparkFun_u-blox_SARA-R5_Arduino_Library.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,7 +1028,8 @@ String SARA_R5::getIMSI(void)
10281028
String SARA_R5::getCCID(void)
10291029
{
10301030
char *response;
1031-
char ccidResponse[21] = {0x00}; // E.g. +CCID: 8939107900010087330
1031+
const int maxTextLen = 21;
1032+
char ccidResponse[maxTextLen] = {0x00}; // E.g. +CCID: 8939107900010087330
10321033
SARA_R5_error_t err;
10331034

10341035
response = sara_r5_calloc_char(minimumResponseAllocation);
@@ -1042,9 +1043,12 @@ String SARA_R5::getCCID(void)
10421043
{
10431044
searchPtr += strlen("\r\n+CCID:"); // Move searchPtr to first character - probably a space
10441045
while (*searchPtr == ' ') searchPtr++; // skip spaces
1045-
if (sscanf(searchPtr, "%s", ccidResponse) != 1)
1046+
if (strlen(searchPtr) < maxTextLen) // Check we have enough space to hold the text
10461047
{
1047-
memset(ccidResponse, 0, 21);
1048+
if (sscanf(searchPtr, "%s", ccidResponse) != 1)
1049+
{
1050+
ccidResponse[0] = 0;
1051+
}
10481052
}
10491053
}
10501054
}
@@ -1055,7 +1059,8 @@ String SARA_R5::getCCID(void)
10551059
String SARA_R5::getSubscriberNo(void)
10561060
{
10571061
char *response;
1058-
char idResponse[128] = {0x00}; // E.g. +CNUM: "ABCD . AAA","123456789012",129
1062+
const int maxTextLen = 128;
1063+
char idResponse[maxTextLen] = {0x00}; // E.g. +CNUM: "ABCD . AAA","123456789012",129
10591064
SARA_R5_error_t err;
10601065

10611066
response = sara_r5_calloc_char(minimumResponseAllocation);
@@ -1064,9 +1069,18 @@ String SARA_R5::getSubscriberNo(void)
10641069
SARA_R5_RESPONSE_OK_OR_ERROR, response, SARA_R5_10_SEC_TIMEOUT);
10651070
if (err == SARA_R5_ERROR_SUCCESS)
10661071
{
1067-
if (sscanf(response, "\r\n+CNUM: %s", idResponse) != 1)
1072+
char *searchPtr = strstr(response, "\r\n+CNUM:");
1073+
if (searchPtr != nullptr)
10681074
{
1069-
memset(idResponse, 0, 128);
1075+
searchPtr += strlen("\r\n+CNUM:"); // Move searchPtr to first character - probably a space
1076+
while (*searchPtr == ' ') searchPtr++; // skip spaces
1077+
if (strlen(searchPtr) < maxTextLen) // Check we have enough space to hold the text
1078+
{
1079+
if (sscanf(searchPtr, "%s", idResponse) != 1)
1080+
{
1081+
idResponse[0] = 0;
1082+
}
1083+
}
10701084
}
10711085
}
10721086
free(response);

0 commit comments

Comments
 (0)