]> sourceware.org Git - lvm2.git/commitdiff
lvmcmdline: return 0/NULL if cmd->arg_values not set and arg_count/grouped_arg_count...
authorPeter Rajnoha <prajnoha@redhat.com>
Thu, 4 Aug 2016 07:32:05 +0000 (09:32 +0200)
committerPeter Rajnoha <prajnoha@redhat.com>
Tue, 9 Aug 2016 16:24:45 +0000 (18:24 +0200)
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

index 629fb143a2f7bd6998867448c5e8df581bf7e860..81118c115b0ddecd287fc3e2b3daafdec581fbbc 100644 (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)
This page took 0.040529 seconds and 5 git commands to generate.