From 5649834f7d62b9cba3b06c1b5d0dd425978f3ad2 Mon Sep 17 00:00:00 2001 From: Peter Rajnoha Date: Thu, 4 Aug 2016 09:32:05 +0200 Subject: [PATCH] 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. --- tools/lvmcmdline.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tools/lvmcmdline.c b/tools/lvmcmdline.c index 629fb143a..81118c115 100644 --- a/tools/lvmcmdline.c +++ b/tools/lvmcmdline.c @@ -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)