Skip to content

Commit

Permalink
Improve string handling
Browse files Browse the repository at this point in the history
  • Loading branch information
direc85 committed Apr 24, 2021
1 parent 51e8ca2 commit ab70b84
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,25 @@ int main(int argc, char *argv[])
QStringList execParts = execCmd.split(' ', QString::SkipEmptyParts);
if(execParts.length()==0)
exit(0);
char *ptrs[execParts.length()+1];

char **ptrs = static_cast<char **>(malloc(sizeof(char *) * static_cast<unsigned int>(execParts.length()+1)));
for(int i=0; i<execParts.length(); i++) {
ptrs[i] = new char[execParts.at(i).toLatin1().length()+1];
memcpy(ptrs[i], execParts.at(i).toLatin1().data(), execParts.at(i).toLatin1().length());
ptrs[i][execParts.at(i).toLatin1().length()] = 0;
const QByteArray data = execParts.at(i).toLatin1();
const unsigned int length = static_cast<unsigned int>(data.length());

ptrs[i] = new char[length+1];
memcpy(ptrs[i], data, length);
ptrs[i][length] = 0;
}
ptrs[execParts.length()] = nullptr;

execvp(ptrs[0], ptrs);

for(unsigned int i = 0; i < sizeof(ptrs); i++) {
delete ptrs[i];
}
ptrs[execParts.length()] = 0;
delete ptrs;

execvp(execParts.first().toLatin1(), ptrs);
exit(0);
}

Expand Down

0 comments on commit ab70b84

Please sign in to comment.