1
0
mirror of git://sourceware.org/git/lvm2.git synced 2024-12-21 13:34:40 +03:00

lvmcmdline: return 0/NULL if cmd->arg_values not set and arg_count/grouped_arg_count/arg_value called

We may call arg_count/grouped_arg_count/arg_value soon enough that
cmd->arg_values is not set yet.

Normally, when running a command, we execute lvm_run_command which in
turn calls _process_command_line to allocate and parse the command line
values and stores them in cmd->arg_values.

However, if we run lvm shell, this one doesn't accept any command line
options and we parse the command line for each command that is executed
within the lvm shell then. If we used any code that tries to access
cmd->arg_values through any of the the arg handling functions too
early, we could end up with a segfault due to uninitialized (NULL)
cmd->arg_values.

This patch just saves extra checks in all the code where arg handling
may be called too early so that the cmd->arg_values is not set up yet.
This does not apply to any of existing code, but subsequent patches
will need that.
This commit is contained in:
Peter Rajnoha 2016-08-04 09:32:05 +02:00
parent 1fde4bf4d0
commit 5649834f7d

View File

@ -63,12 +63,12 @@ static struct cmdline_context _cmdline;
/* Command line args */
unsigned arg_count(const struct cmd_context *cmd, int a)
{
return cmd->arg_values[a].count;
return cmd->arg_values ? cmd->arg_values[a].count : 0;
}
unsigned grouped_arg_count(const struct arg_values *av, int a)
{
return av[a].count;
return av ? av[a].count : 0;
}
unsigned arg_is_set(const struct cmd_context *cmd, int a)
@ -182,7 +182,7 @@ const char *arg_long_option_name(int a)
const char *arg_value(const struct cmd_context *cmd, int a)
{
return cmd->arg_values[a].value;
return cmd->arg_values ? cmd->arg_values[a].value : NULL;
}
const char *arg_str_value(const struct cmd_context *cmd, int a, const char *def)