Fix a crash issue (regression)

Fix bad pointer deletion.
Fix incorrect pointer deletion.
Fix a memory leak.
This commit is contained in:
Don HO 2017-07-23 10:10:26 +02:00
parent e76b5dc869
commit cb7d6070fb
1 changed files with 26 additions and 22 deletions

View File

@ -98,61 +98,65 @@ bool checkSingleFile(const TCHAR *commandLine)
} }
//commandLine should contain path to n++ executable running //commandLine should contain path to n++ executable running
void parseCommandLine(const TCHAR* cmdLine, ParamVector& paramVector) void parseCommandLine(const TCHAR* commandLine, ParamVector& paramVector)
{ {
if (!cmdLine) if (!commandLine)
return; return;
TCHAR* commandLine = new TCHAR[lstrlen(cmdLine)]; TCHAR* cmdLine = new TCHAR[lstrlen(commandLine) + 1];
lstrcpy(commandLine, cmdLine); lstrcpy(cmdLine, commandLine);
TCHAR* cmdLinePtr = cmdLine;
//remove the first element, since thats the path the the executable (GetCommandLine does that) //remove the first element, since thats the path the the executable (GetCommandLine does that)
TCHAR stopChar = TEXT(' '); TCHAR stopChar = TEXT(' ');
if (commandLine[0] == TEXT('\"')) if (cmdLinePtr[0] == TEXT('\"'))
{ {
stopChar = TEXT('\"'); stopChar = TEXT('\"');
++commandLine; ++cmdLinePtr;
} }
//while this is not really DBCS compliant, space and quote are in the lower 127 ASCII range //while this is not really DBCS compliant, space and quote are in the lower 127 ASCII range
while(commandLine[0] && commandLine[0] != stopChar) while(cmdLinePtr[0] && cmdLinePtr[0] != stopChar)
{ {
++commandLine; ++cmdLinePtr;
} }
// For unknown reason, the following command : // For unknown reason, the following command :
// c:\NppDir>notepad++ // c:\NppDir>notepad++
// (without quote) will give string "notepad++\0notepad++\0" // (without quote) will give string "notepad++\0notepad++\0"
// To avoid the unexpected behaviour we check the end of string before increasing the pointer // To avoid the unexpected behaviour we check the end of string before increasing the pointer
if (commandLine[0] != '\0') if (cmdLinePtr[0] != '\0')
++commandLine; //advance past stopChar ++cmdLinePtr; //advance past stopChar
//kill remaining spaces //kill remaining spaces
while(commandLine[0] == TEXT(' ')) while(cmdLinePtr[0] == TEXT(' '))
++commandLine; ++cmdLinePtr;
bool isFile = checkSingleFile(commandLine); //if the commandline specifies only a file, open it as such bool isFile = checkSingleFile(cmdLinePtr); //if the commandline specifies only a file, open it as such
if (isFile) { if (isFile)
paramVector.push_back(commandLine); {
paramVector.push_back(cmdLinePtr);
delete[] cmdLine;
return; return;
} }
bool isInFile = false; bool isInFile = false;
bool isInWhiteSpace = true; bool isInWhiteSpace = true;
paramVector.clear(); paramVector.clear();
size_t commandLength = lstrlen(commandLine); size_t commandLength = lstrlen(cmdLinePtr);
for (size_t i = 0; i < commandLength; ++i) for (size_t i = 0; i < commandLength; ++i)
{ {
switch(commandLine[i]) switch(cmdLinePtr[i])
{ {
case '\"': //quoted filename, ignore any following whitespace case '\"': //quoted filename, ignore any following whitespace
{ {
if (!isInFile) //" will always be treated as start or end of param, in case the user forgot to add an space if (!isInFile) //" will always be treated as start or end of param, in case the user forgot to add an space
{ {
paramVector.push_back(commandLine+i+1); //add next param(since zero terminated generic_string original, no overflow of +1) paramVector.push_back(cmdLinePtr+i+1); //add next param(since zero terminated generic_string original, no overflow of +1)
} }
isInFile = !isInFile; isInFile = !isInFile;
isInWhiteSpace = false; isInWhiteSpace = false;
//because we dont want to leave in any quotes in the filename, remove them now (with zero terminator) //because we dont want to leave in any quotes in the filename, remove them now (with zero terminator)
commandLine[i] = 0; cmdLinePtr[i] = 0;
} }
break; break;
@ -161,7 +165,7 @@ void parseCommandLine(const TCHAR* cmdLine, ParamVector& paramVector)
{ {
isInWhiteSpace = true; isInWhiteSpace = true;
if (!isInFile) if (!isInFile)
commandLine[i] = 0; //zap spaces into zero terminators, unless its part of a filename cmdLinePtr[i] = 0; //zap spaces into zero terminators, unless its part of a filename
} }
break; break;
@ -169,7 +173,7 @@ void parseCommandLine(const TCHAR* cmdLine, ParamVector& paramVector)
{ {
if (!isInFile && isInWhiteSpace) if (!isInFile && isInWhiteSpace)
{ {
paramVector.push_back(commandLine+i); //add next param paramVector.push_back(cmdLinePtr+i); //add next param
isInWhiteSpace = false; isInWhiteSpace = false;
} }
} }
@ -177,7 +181,7 @@ void parseCommandLine(const TCHAR* cmdLine, ParamVector& paramVector)
} }
//the commandline generic_string is now a list of zero terminated strings concatenated, and the vector contains all the substrings //the commandline generic_string is now a list of zero terminated strings concatenated, and the vector contains all the substrings
delete commandLine; delete [] cmdLine;
} }
bool isInList(const TCHAR *token2Find, ParamVector & params) bool isInList(const TCHAR *token2Find, ParamVector & params)