I haven't read thorough your code. But there is at least one issue I've found.
You're using realloc
to allocate and resize memory. The realloc
function is generally used to resize memory. But if you pass a null pointer to it, it will directly allocate memory instead.
The issue in your code is here:
// -> +1 for NULL command in the end
commands = realloc(commands,sizeof(*commands)*(c+1+1));
commTcopy = realloc(commTcopy,sizeof(*commTcopy)*strlen(commT)+1);
//...
char **command;
// command pointer for easier handling
command = *(commands+c);
int c1 = 0;
while(argT != NULL){
// alloc memory for arguments
// -> number of commands (c1+1)
// -> +1 for NULL argument in the end
command = realloc(command,sizeof(*command)*(c1+1+1));
At each round of iteration you reallocated commands
to a larger array, this is fine. But the issue is the allocated memory cell *(commands + c)
contains uninitialized data, which may not be NULL. You saved that value to command
and passed it to realloc
. realloc
just finds the pointer provided is not NULL so it assumes you are reallocating memory, which is in fact not. Thus a crash.
As I've understanded your code, you assigned command
to *(commands + c)
later so the previous command = *(commands + c)
is just redundant, change it to command = NULL
may fix your problem.