Terminate filename strings and increase any array by 1 that uses strcpy_P.

strcpy_P null terminates the provided string. strlen does not count \0s so any string that uses strcpy_P needs an extra byte for \0.
This commit is contained in:
Nathan Seidle 2021-10-12 15:00:08 -06:00
parent 795dc84f68
commit bc12729f30

View File

@ -96,11 +96,11 @@ SerialPort<0, 512, 0> NewSerial;
void(* Reset_AVR) (void) = 0; //Way of resetting the ATmega void(* Reset_AVR) (void) = 0; //Way of resetting the ATmega
#define CFG_FILENAME "config.txt" //This is the name of the file that contains the unit settings #define CFG_FILENAME "config.txt\0" //This is the name of the file that contains the unit settings
#define MAX_CFG "115200,255,255,1,1,1,1,255,255\0" // This is used to calculate the longest possible configuration string. These actual values are not used #define MAX_CFG "115200,255,255,1,1,1,1,255,255\0" // This is used to calculate the longest possible configuration string. These actual values are not used
#define CFG_LENGTH (strlen(MAX_CFG) + 1) //Length of text found in config file. strlen ignores \0 so we have to add it back #define CFG_LENGTH (strlen(MAX_CFG) + 1) //Length of text found in config file. strlen ignores \0 so we have to add it back
#define SEQ_FILENAME "SEQLOG00.TXT" //This is the name for the file when you're in sequential mode #define SEQ_FILENAME "SEQLOG00.TXT\0" //This is the name for the file when you're in sequential mode
//Internal EEPROM locations for the user settings //Internal EEPROM locations for the user settings
#define LOCATION_SYSTEM_SETTING 0x02 #define LOCATION_SYSTEM_SETTING 0x02
@ -420,7 +420,7 @@ void seqLog(void)
{ {
SdFile seqFile; SdFile seqFile;
char sequentialFileName[strlen(SEQ_FILENAME)]; //Limited to 8.3 char sequentialFileName[strlen(SEQ_FILENAME) + 1]; //Limited to 8.3
strcpy_P(sequentialFileName, PSTR(SEQ_FILENAME)); //This is the name of the config file. 'config.sys' is probably a bad idea. strcpy_P(sequentialFileName, PSTR(SEQ_FILENAME)); //This is the name of the config file. 'config.sys' is probably a bad idea.
//Try to create sequential file //Try to create sequential file
@ -847,7 +847,7 @@ void readConfigFile(void)
if (!sd.chdir()) systemError(ERROR_ROOT_INIT); // open the root directory if (!sd.chdir()) systemError(ERROR_ROOT_INIT); // open the root directory
char configFileName[strlen(CFG_FILENAME)]; //Limited to 8.3 char configFileName[strlen(CFG_FILENAME) + 1]; //Limited to 8.3
strcpy_P(configFileName, PSTR(CFG_FILENAME)); //This is the name of the config file. 'config.sys' is probably a bad idea. strcpy_P(configFileName, PSTR(CFG_FILENAME)); //This is the name of the config file. 'config.sys' is probably a bad idea.
//Check to see if we have a config file //Check to see if we have a config file
@ -1091,19 +1091,12 @@ void recordConfigFile(void)
if (!sd.chdir()) systemError(ERROR_ROOT_INIT); // open the root directory if (!sd.chdir()) systemError(ERROR_ROOT_INIT); // open the root directory
char configFileName[strlen(CFG_FILENAME)]; char configFileName[strlen(CFG_FILENAME) + 1];
strcpy_P(configFileName, PSTR(CFG_FILENAME)); //This is the name of the config file. 'config.sys' is probably a bad idea. strcpy_P(configFileName, PSTR(CFG_FILENAME)); //This is the name of the config file. 'config.sys' is probably a bad idea.
//If there is currently a config file, trash it //If there is currently a config file, trash it
if (myFile.open(configFileName, O_WRITE)) { if (sd.exists(configFileName))
if (!myFile.remove()) { sd.remove(configFileName);
NewSerial.println(F("Remove config failed"));
myFile.close(); //Close this file
return;
}
}
//myFile.close(); //Not sure if we need to close the file before we try to reopen it
//Create config file //Create config file
myFile.open(configFileName, O_CREAT | O_APPEND | O_WRITE); myFile.open(configFileName, O_CREAT | O_APPEND | O_WRITE);
@ -1184,7 +1177,7 @@ void commandShell(void)
SdFile tempFile; SdFile tempFile;
sd.chdir("/", true); //Change to root directory sd.chdir("/", true); //Change to root directory
char buffer[30]; char commandBuffer[30];
byte tempVar; byte tempVar;
//char parentDirectory[13]; //This tracks the current parent directory. Limited to 13 characters. //char parentDirectory[13]; //This tracks the current parent directory. Limited to 13 characters.
@ -1211,7 +1204,7 @@ void commandShell(void)
NewSerial.print(F(">")); NewSerial.print(F(">"));
//Read command //Read command
if (readLine(buffer, sizeof(buffer)) < 1) if (readLine(commandBuffer, sizeof(commandBuffer)) < 1)
{ {
#ifdef INCLUDE_SIMPLE_EMBEDDED #ifdef INCLUDE_SIMPLE_EMBEDDED
commandSucceeded = 1; commandSucceeded = 1;
@ -1564,7 +1557,7 @@ void commandShell(void)
NewSerial.print(F("<")); //give a different prompt NewSerial.print(F("<")); //give a different prompt
//read one line of text //read one line of text
dataLen = readLine(buffer, sizeof(buffer)); dataLen = readLine(commandBuffer, sizeof(commandBuffer));
if (!dataLen) { if (!dataLen) {
#ifdef INCLUDE_SIMPLE_EMBEDDED #ifdef INCLUDE_SIMPLE_EMBEDDED
commandSucceeded = 1; commandSucceeded = 1;
@ -1583,13 +1576,13 @@ void commandShell(void)
//} //}
//write text to file //write text to file
if (tempFile.write((byte*) buffer, dataLen) != dataLen) { if (tempFile.write((byte*) commandBuffer, dataLen) != dataLen) {
if ((feedbackMode & EXTENDED_INFO) > 0) if ((feedbackMode & EXTENDED_INFO) > 0)
NewSerial.println(F("error writing to file")); NewSerial.println(F("error writing to file"));
break; break;
} }
if (dataLen < (sizeof(buffer) - 1)) tempFile.write("\n\r", 2); //If we didn't fill up the buffer then user must have sent NL. Append new line and return if (dataLen < (sizeof(commandBuffer) - 1)) tempFile.write("\n\r", 2); //If we didn't fill up the buffer then user must have sent NL. Append new line and return
} }
tempFile.close(); tempFile.close();
@ -1831,9 +1824,9 @@ void commandShell(void)
} }
//Reads a line until the \n enter character is found //Reads a line until the \n enter character is found
byte readLine(char* buffer, byte bufferLength) byte readLine(char* readBuffer, byte bufferLength)
{ {
memset(buffer, 0, bufferLength); //Clear buffer memset(readBuffer, 0, bufferLength); //Clear buffer
byte readLength = 0; byte readLength = 0;
while (readLength < bufferLength - 1) { while (readLength < bufferLength - 1) {
@ -1847,7 +1840,7 @@ byte readLine(char* buffer, byte bufferLength)
continue; continue;
--readLength; --readLength;
buffer[readLength] = '\0'; //Put a terminator on the string in case we are finished readBuffer[readLength] = '\0'; //Put a terminator on the string in case we are finished
NewSerial.print((char)0x08); //Move back one space NewSerial.print((char)0x08); //Move back one space
NewSerial.print(F(" ")); //Put a blank there to erase the letter from the terminal NewSerial.print(F(" ")); //Put a blank there to erase the letter from the terminal
@ -1862,7 +1855,7 @@ byte readLine(char* buffer, byte bufferLength)
if (c == '\r') { if (c == '\r') {
NewSerial.println(); NewSerial.println();
buffer[readLength] = '\0'; readBuffer[readLength] = '\0';
break; break;
} }
else if (c == '\n') { else if (c == '\n') {
@ -1884,13 +1877,13 @@ byte readLine(char* buffer, byte bufferLength)
//See issue 168: https://github.com/sparkfun/OpenLog/issues/168 //See issue 168: https://github.com/sparkfun/OpenLog/issues/168
}*/ }*/
else { else {
buffer[readLength] = c; readBuffer[readLength] = c;
++readLength; ++readLength;
} }
} }
//Split the command line into arguments //Split the command line into arguments
splitCmdLineArgs(buffer, bufferLength); splitCmdLineArgs(readBuffer, bufferLength);
return readLength; return readLength;
} }
@ -2155,7 +2148,7 @@ void systemMenu(void)
SdFile myFile; SdFile myFile;
if (!sd.chdir()) systemError(ERROR_ROOT_INIT); // open the root directory if (!sd.chdir()) systemError(ERROR_ROOT_INIT); // open the root directory
char configFileName[strlen(CFG_FILENAME)]; char configFileName[strlen(CFG_FILENAME) + 1];
strcpy_P(configFileName, PSTR(CFG_FILENAME)); //This is the name of the config file. 'config.sys' is probably a bad idea. strcpy_P(configFileName, PSTR(CFG_FILENAME)); //This is the name of the config file. 'config.sys' is probably a bad idea.
//If there is currently a config file, trash it //If there is currently a config file, trash it