Rewriting option parser wrapper to ensure memory is not lost.

The option parser wrapper allocated memory in an incorrect way, which
could lead to problems especially with --xml-output, since it would
access the memory at a later point in the execution. This could then
propagate to GUI problems, since the GUI uses the --xml-output switch.
Rewrote the code to be more readable and less incorrect.
This commit is contained in:
Cas Cremers 2014-06-10 14:32:52 +01:00
parent 9cc323004c
commit f1f2f28f61

View File

@ -1617,75 +1617,61 @@ switcher (const int process, int index, int commandline)
* This is a public function. It is used for the environment variables but also for the in-specification defines.
*/
void
process_switch_buffer (char *buf)
process_switch_buffer (char *args_source)
{
if (buf != NULL)
{
int slen;
int arg_string_len;
slen = strlen (buf);
if (slen > 0)
if (args_source == NULL)
{
return;
}
arg_string_len = strlen (args_source);
if (arg_string_len > 0)
{
/**
* We scan the buf here, but assume a stupid upper limit of 100 pieces, otherwise this all becomes fairly vague.
* We scan the args_source here, but assume a stupid upper limit of 100 pieces, otherwise this all becomes fairly vague.
*/
int max = 100;
char *argv[100];
int count;
char *args;
int max_args = 100;
char **args_array;
int args_count;
char *args_copy;
char *scanflag;
char *argn;
/* make a safe copy */
args = (char *) malloc (slen + 1);
memcpy (args, buf, slen + 1);
/* warning */
/*
globalError++;
eprintf ("warning: using environment variable SVNSCYTHER ('%s')\n",
args);
globalError--;
*/
{
char *next_arg;
int i;
i = 0;
while (i < max)
/* make a safe copy */
args_array = malloc (sizeof (char *) * max_args);
args_copy = (char *) malloc (arg_string_len + 1);
memcpy (args_copy, args_source, arg_string_len + 1);
for (i = 0; i < max_args; i++)
{
argv[i] = "";
i++;
}
args_array[i] = "";
}
scanflag = args;
count = 0;
scanflag = args_copy;
args_count = 0;
/* ugly use of assignment in condition */
while (count < max)
while (args_count < (max_args - 1))
{
argn = strtok (scanflag, "\t ");
scanflag = NULL;
if (argn != NULL)
next_arg = strtok (scanflag, "\t ");
scanflag = NULL; // needed for strtok() to work properly
if (next_arg != NULL)
{
count++;
argv[count] = argn;
args_count++;
args_array[args_count] = next_arg;
}
else
{
break;
}
}
/*
warning ("found %i arguments in SCYTHERFLAGS\n", count);
*/
switches.argc = count + 1;
switches.argv = argv;
switches.argc = args_count + 1;
switches.argv = args_array;
process_switches (false);
}
}
}
//! Process environment
void