option: Assert value string isn't null
Plenty of code relies on QemuOpt member @str not being null, including
qemu_opts_print(), qemu_opts_to_qdict(), and callbacks passed to
qemu_opt_foreach().
Begs the question whether it can be null.  Only opt_set() creates
QemuOpt.  It sets member @str to its argument @value.  Passing null
for @value would plant a time bomb.  Callers:
* opts_do_parse() can't pass null.
* qemu_opt_set() passes its argument @value.  Callers:
  - qemu_opts_from_qdict_1() can't pass null
  - qemu_opts_set() passes its argument @value, but none of its
    callers pass null.
  - Many more outside qemu-option.c, but they shouldn't pass null,
    either.
Assert member @str isn't null, so that misuse is caught right away.
Simplify parse_option_bool(), parse_option_number() and
parse_option_size() accordingly.  Best viewed with whitespace changes
ignored.
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Message-Id: <1487708048-2131-3-git-send-email-armbru@redhat.com>
			
			
This commit is contained in:
		
							parent
							
								
									694baf57ae
								
							
						
					
					
						commit
						8ee8409eff
					
				@ -128,7 +128,6 @@ int get_param_value(char *buf, int buf_size,
 | 
				
			|||||||
static void parse_option_bool(const char *name, const char *value, bool *ret,
 | 
					static void parse_option_bool(const char *name, const char *value, bool *ret,
 | 
				
			||||||
                              Error **errp)
 | 
					                              Error **errp)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
    if (value != NULL) {
 | 
					 | 
				
			||||||
    if (!strcmp(value, "on")) {
 | 
					    if (!strcmp(value, "on")) {
 | 
				
			||||||
        *ret = 1;
 | 
					        *ret = 1;
 | 
				
			||||||
    } else if (!strcmp(value, "off")) {
 | 
					    } else if (!strcmp(value, "off")) {
 | 
				
			||||||
@ -137,9 +136,6 @@ static void parse_option_bool(const char *name, const char *value, bool *ret,
 | 
				
			|||||||
        error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
 | 
					        error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
 | 
				
			||||||
                   name, "'on' or 'off'");
 | 
					                   name, "'on' or 'off'");
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    } else {
 | 
					 | 
				
			||||||
        *ret = 1;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static void parse_option_number(const char *name, const char *value,
 | 
					static void parse_option_number(const char *name, const char *value,
 | 
				
			||||||
@ -148,16 +144,12 @@ static void parse_option_number(const char *name, const char *value,
 | 
				
			|||||||
    char *postfix;
 | 
					    char *postfix;
 | 
				
			||||||
    uint64_t number;
 | 
					    uint64_t number;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (value != NULL) {
 | 
					 | 
				
			||||||
    number = strtoull(value, &postfix, 0);
 | 
					    number = strtoull(value, &postfix, 0);
 | 
				
			||||||
    if (*postfix != '\0') {
 | 
					    if (*postfix != '\0') {
 | 
				
			||||||
        error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name, "a number");
 | 
					        error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name, "a number");
 | 
				
			||||||
        return;
 | 
					        return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    *ret = number;
 | 
					    *ret = number;
 | 
				
			||||||
    } else {
 | 
					 | 
				
			||||||
        error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name, "a number");
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static const QemuOptDesc *find_desc_by_name(const QemuOptDesc *desc,
 | 
					static const QemuOptDesc *find_desc_by_name(const QemuOptDesc *desc,
 | 
				
			||||||
@ -180,7 +172,6 @@ void parse_option_size(const char *name, const char *value,
 | 
				
			|||||||
    char *postfix;
 | 
					    char *postfix;
 | 
				
			||||||
    double sizef;
 | 
					    double sizef;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (value != NULL) {
 | 
					 | 
				
			||||||
    sizef = strtod(value, &postfix);
 | 
					    sizef = strtod(value, &postfix);
 | 
				
			||||||
    if (sizef < 0 || sizef > UINT64_MAX) {
 | 
					    if (sizef < 0 || sizef > UINT64_MAX) {
 | 
				
			||||||
        error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name,
 | 
					        error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name,
 | 
				
			||||||
@ -211,9 +202,6 @@ void parse_option_size(const char *name, const char *value,
 | 
				
			|||||||
                          "kilobytes, megabytes, gigabytes and terabytes.\n");
 | 
					                          "kilobytes, megabytes, gigabytes and terabytes.\n");
 | 
				
			||||||
        return;
 | 
					        return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    } else {
 | 
					 | 
				
			||||||
        error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name, "a size");
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
bool has_help_option(const char *param)
 | 
					bool has_help_option(const char *param)
 | 
				
			||||||
@ -566,6 +554,7 @@ static void opt_set(QemuOpts *opts, const char *name, const char *value,
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
    opt->desc = desc;
 | 
					    opt->desc = desc;
 | 
				
			||||||
    opt->str = g_strdup(value);
 | 
					    opt->str = g_strdup(value);
 | 
				
			||||||
 | 
					    assert(opt->str);
 | 
				
			||||||
    qemu_opt_parse(opt, &local_err);
 | 
					    qemu_opt_parse(opt, &local_err);
 | 
				
			||||||
    if (local_err) {
 | 
					    if (local_err) {
 | 
				
			||||||
        error_propagate(errp, local_err);
 | 
					        error_propagate(errp, local_err);
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user