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,74 +1617,60 @@ 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. * This is a public function. It is used for the environment variables but also for the in-specification defines.
*/ */
void void
process_switch_buffer (char *buf) process_switch_buffer (char *args_source)
{ {
if (buf != NULL) int arg_string_len;
{
int slen;
slen = strlen (buf); if (args_source == NULL)
if (slen > 0) {
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; int max_args = 100;
char *argv[100]; char **args_array;
int count; int args_count;
char *args; char *args_copy;
char *scanflag; char *scanflag;
char *argn; char *next_arg;
/* 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--;
*/
{
int i; int i;
i = 0; /* make a safe copy */
while (i < max) 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] = ""; args_array[i] = "";
i++;
}
} }
scanflag = args; scanflag = args_copy;
count = 0; args_count = 0;
/* ugly use of assignment in condition */ /* ugly use of assignment in condition */
while (count < max) while (args_count < (max_args - 1))
{ {
argn = strtok (scanflag, "\t "); next_arg = strtok (scanflag, "\t ");
scanflag = NULL; scanflag = NULL; // needed for strtok() to work properly
if (argn != NULL) if (next_arg != NULL)
{ {
count++; args_count++;
argv[count] = argn; args_array[args_count] = next_arg;
} }
else else
{ {
break; break;
} }
} }
/* switches.argc = args_count + 1;
warning ("found %i arguments in SCYTHERFLAGS\n", count); switches.argv = args_array;
*/
switches.argc = count + 1;
switches.argv = argv;
process_switches (false); process_switches (false);
} }
}
} }
//! Process environment //! Process environment