Skip to content

Commit 490374d

Browse files
committed
Change some strcpy to strncpy
1 parent 81f7b03 commit 490374d

10 files changed

+32
-32
lines changed

Firmware/RTK_Surveyor/Begin.ino

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ void beginBoard()
131131
//The following changes the POR default but will be overwritten by settings in NVM or settings file
132132
settings.ubxConstellations[1].enabled = false;
133133

134-
strcpy(platformFilePrefix, "SFE_Surveyor");
135-
strcpy(platformPrefix, "Surveyor");
134+
strncpy(platformFilePrefix, "SFE_Surveyor", sizeof(platformFilePrefix) - 1);
135+
strncpy(platformPrefix, "Surveyor", sizeof(platformPrefix) - 1);
136136
}
137137
else if (productVariant == RTK_EXPRESS || productVariant == RTK_EXPRESS_PLUS)
138138
{
@@ -159,13 +159,13 @@ void beginBoard()
159159

160160
if (productVariant == RTK_EXPRESS)
161161
{
162-
strcpy(platformFilePrefix, "SFE_Express");
163-
strcpy(platformPrefix, "Express");
162+
strncpy(platformFilePrefix, "SFE_Express", sizeof(platformFilePrefix) - 1);
163+
strncpy(platformPrefix, "Express", sizeof(platformPrefix) - 1);
164164
}
165165
else if (productVariant == RTK_EXPRESS_PLUS)
166166
{
167-
strcpy(platformFilePrefix, "SFE_Express_Plus");
168-
strcpy(platformPrefix, "Express Plus");
167+
strncpy(platformFilePrefix, "SFE_Express_Plus", sizeof(platformFilePrefix) - 1);
168+
strncpy(platformPrefix, "Express Plus", sizeof(platformPrefix) - 1);
169169
}
170170
}
171171
else if (productVariant == RTK_FACET || productVariant == RTK_FACET_LBAND)
@@ -206,13 +206,13 @@ void beginBoard()
206206

207207
if (productVariant == RTK_FACET)
208208
{
209-
strcpy(platformFilePrefix, "SFE_Facet");
210-
strcpy(platformPrefix, "Facet");
209+
strncpy(platformFilePrefix, "SFE_Facet", sizeof(platformFilePrefix) - 1);
210+
strncpy(platformPrefix, "Facet", sizeof(platformPrefix) - 1);
211211
}
212212
else if (productVariant == RTK_FACET_LBAND)
213213
{
214-
strcpy(platformFilePrefix, "SFE_Facet_LBand");
215-
strcpy(platformPrefix, "Facet L-Band");
214+
strncpy(platformFilePrefix, "SFE_Facet_LBand", sizeof(platformFilePrefix) - 1);
215+
strncpy(platformPrefix, "Facet L-Band", sizeof(platformPrefix) - 1);
216216
}
217217
}
218218
#ifdef COMPILE_SD_MMC
@@ -222,8 +222,8 @@ void beginBoard()
222222

223223
settings.enablePrintBatteryMessages = false; // No pesky battery messages
224224

225-
strcpy(platformFilePrefix, "SFE_Reference_Station");
226-
strcpy(platformPrefix, "Reference Station");
225+
strncpy(platformFilePrefix, "SFE_Reference_Station", sizeof(platformFilePrefix) - 1);
226+
strncpy(platformPrefix, "Reference Station", sizeof(platformPrefix) - 1);
227227
}
228228
#endif
229229

Firmware/RTK_Surveyor/Bluetooth.ino

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,11 @@ void bluetoothStart()
147147
#ifdef COMPILE_BT
148148
if (bluetoothState == BT_OFF)
149149
{
150-
char stateName[10];
150+
char stateName[11];
151151
if (systemState >= STATE_ROVER_NOT_STARTED && systemState <= STATE_ROVER_RTK_FIX)
152-
strcpy(stateName, "Rover-");
152+
strncpy(stateName, "Rover-", 10);
153153
else if (systemState >= STATE_BASE_NOT_STARTED && systemState <= STATE_BASE_FIXED_TRANSMITTING)
154-
strcpy(stateName, "Base-");
154+
strncpy(stateName, "Base-", 10);
155155

156156
snprintf(deviceName, sizeof(deviceName), "%s %s%02X%02X", platformPrefix, stateName, btMACAddress[4], btMACAddress[5]);
157157

Firmware/RTK_Surveyor/Display.ino

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2348,12 +2348,12 @@ void displayMessage(const char* message, uint16_t displayTime)
23482348
{
23492349
if (online.display == true)
23502350
{
2351-
char temp[20];
2351+
char temp[21];
23522352
uint8_t fontHeight = 15; //Assume fontsize 1
23532353

23542354
//Count words based on spaces
23552355
uint8_t wordCount = 0;
2356-
strcpy(temp, message); //strtok modifies the message so make copy
2356+
strncpy(temp, message, 20); //strtok modifies the message so make copy
23572357
char * token = strtok(temp, " ");
23582358
while (token != nullptr)
23592359
{
@@ -2368,7 +2368,7 @@ void displayMessage(const char* message, uint16_t displayTime)
23682368

23692369
//drawFrame();
23702370

2371-
strcpy(temp, message);
2371+
strncpy(temp, message, 20);
23722372
token = strtok(temp, " ");
23732373
while (token != nullptr)
23742374
{

Firmware/RTK_Surveyor/NVM.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1234,7 +1234,7 @@ uint8_t loadProfileNames()
12341234
void setProfileName(uint8_t ProfileNumber)
12351235
{
12361236
//Update the name in the array of profile names
1237-
strcpy(profileNames[profileNumber], settings.profileName);
1237+
strncpy(profileNames[profileNumber], settings.profileName, sizeof(profileNames[0]) - 1);
12381238

12391239
//Mark this profile as active
12401240
activeProfiles |= 1 << ProfileNumber;

Firmware/RTK_Surveyor/NtripClient.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ bool ntripClientConnect()
8787
return false;
8888

8989
//Remove any http:// or https:// prefix from host name
90-
char hostname[50];
90+
char hostname[51];
9191
strncpy(hostname, settings.ntripClient_CasterHost, 50); //strtok modifies string to be parsed so we create a copy
9292
char *token = strtok(hostname, "//");
9393
if (token != nullptr)
@@ -117,7 +117,7 @@ bool ntripClientConnect()
117117
char credentials[CREDENTIALS_BUFFER_SIZE];
118118
if (strlen(settings.ntripClient_CasterUser) == 0)
119119
{
120-
strncpy(credentials, "Accept: */*\r\nConnection: close\r\n", sizeof(credentials));
120+
strncpy(credentials, "Accept: */*\r\nConnection: close\r\n", sizeof(credentials) - 1);
121121
}
122122
else
123123
{

Firmware/RTK_Surveyor/NtripServer.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ bool ntripServerConnectCaster()
8686
char serverBuffer[SERVER_BUFFER_SIZE];
8787

8888
//Remove any http:// or https:// prefix from host name
89-
char hostname[50];
89+
char hostname[51];
9090
strncpy(hostname, settings.ntripServer_CasterHost, 50); //strtok modifies string to be parsed so we create a copy
9191
char *token = strtok(hostname, "//");
9292
if (token != nullptr)

Firmware/RTK_Surveyor/menuFirmware.ino

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ void menuFirmware()
146146
else if (incoming == 'e')
147147
{
148148
enableRCFirmware ^= 1;
149-
strcpy(reportedVersion, ""); //Reset to force c) menu
149+
strncpy(reportedVersion, "", sizeof(reportedVersion) - 1); //Reset to force c) menu
150150
}
151151
else if (incoming == 'x')
152152
break;
@@ -233,7 +233,7 @@ void scanForFirmware()
233233
//Check for 'RTK_Surveyor_Firmware' start of file name
234234
if (strncmp(fname, BIN_HEADER, strlen(BIN_HEADER)) == 0)
235235
{
236-
strcpy(binFileNames[binCount++], fname); //Add this to the array
236+
strncpy(binFileNames[binCount++], fname, sizeof(binFileNames[0]) - 1); //Add this to the array
237237
}
238238
else
239239
systemPrintf("Unknown: %s\r\n", fname);
@@ -276,7 +276,7 @@ void scanForFirmware()
276276
//Check for 'RTK_Surveyor_Firmware' start of file name
277277
if (strncmp(fname, BIN_HEADER, strlen(BIN_HEADER)) == 0)
278278
{
279-
strcpy(binFileNames[binCount++], fname); //Add this to the array
279+
strncpy(binFileNames[binCount++], fname, sizeof(binFileNames[0]) - 1); //Add this to the array
280280
}
281281
else
282282
systemPrintf("Unknown: %s\r\n", fname);

Firmware/RTK_Surveyor/menuGNSS.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,8 @@ void menuGNSS()
216216

217217
//Error check for RTK2Go without email in user name
218218
//First force tolower the host name
219-
char lowerHost[50];
220-
strcpy(lowerHost, settings.ntripClient_CasterHost);
219+
char lowerHost[51];
220+
strncpy(lowerHost, settings.ntripClient_CasterHost, 50);
221221
for (int x = 0 ; x < 50 ; x++)
222222
{
223223
if (lowerHost[x] == '\0') break;

Firmware/RTK_Surveyor/menuMessages.ino

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ void beginLogging(const char *customFileName)
353353
}
354354
else
355355
{
356-
strcpy(fileName, customFileName);
356+
strncpy(fileName, customFileName, sizeof(fileName) - 1);
357357
}
358358

359359
//Attempt to write to file system. This avoids collisions with file writing in F9PSerialReadTask()
@@ -535,7 +535,7 @@ bool findLastLog(char *lastLogName)
535535
{
536536
if (strstr(fname, LOG_PREFIX) != nullptr)
537537
{
538-
strcpy(lastLogName, fname); //Store this file as last known log file
538+
strncpy(lastLogName, fname, sizeof(lastLogName) - 1); //Store this file as last known log file
539539
foundAFile = true;
540540
}
541541
}
@@ -568,7 +568,7 @@ bool findLastLog(char *lastLogName)
568568
{
569569
if (strstr(fname, LOG_PREFIX) != nullptr)
570570
{
571-
strcpy(lastLogName, fname); //Store this file as last known log file
571+
strncpy(lastLogName, fname, sizeof(lastLogName) - 1); //Store this file as last known log file
572572
foundAFile = true;
573573
}
574574
}

Firmware/RTK_Surveyor/menuPP.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,12 +262,12 @@ bool pointperfectProvisionDevice()
262262
systemPrintln("ERROR - Failed to allocate tempHolder buffer!\r\n");
263263
break;
264264
}
265-
strcpy(tempHolder, (const char*)((*jsonZtp)["certificate"]));
265+
strncpy(tempHolder, (const char*)((*jsonZtp)["certificate"]), sizeof(tempHolder) - 1);
266266
// systemPrintf("len of PrivateCert: %d\r\n", strlen(tempHolder));
267267
// systemPrintf("privateCert: %s\r\n", tempHolder);
268268
recordFile("certificate", tempHolder, strlen(tempHolder));
269269

270-
strcpy(tempHolder, (const char*)((*jsonZtp)["privateKey"]));
270+
strncpy(tempHolder, (const char*)((*jsonZtp)["privateKey"]), sizeof(tempHolder) - 1);
271271
// systemPrintf("len of privateKey: %d\r\n", strlen(tempHolder));
272272
// systemPrintf("privateKey: %s\r\n", tempHolder);
273273
recordFile("privateKey", tempHolder, strlen(tempHolder));

0 commit comments

Comments
 (0)