util: require command args to be non-NULL

The virCommand APIs do not expect to be given a NULL value for an arg
name or value. Such a mistake can lead to execution of the wrong
command, as the NULL may prematurely terminate the list of args.
Detect this and report suitable error messages.

This identified a flaw in the storage test which was passing a NULL
instead of the volume path. This flaw was then validated by an incorrect
set of qemu-img args as expected data.

Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
This commit is contained in:
Daniel P. Berrangé 2018-12-17 13:18:31 +00:00
parent f8f525ff86
commit 912c6b22fc
3 changed files with 12 additions and 1 deletions

View File

@ -1498,6 +1498,12 @@ virCommandAddArg(virCommandPtr cmd, const char *val)
if (!cmd || cmd->has_error) if (!cmd || cmd->has_error)
return; return;
if (val == NULL) {
cmd->has_error = EINVAL;
abort();
return;
}
if (VIR_STRDUP_QUIET(arg, val) < 0) { if (VIR_STRDUP_QUIET(arg, val) < 0) {
cmd->has_error = ENOMEM; cmd->has_error = ENOMEM;
return; return;
@ -1595,6 +1601,10 @@ virCommandAddArgFormat(virCommandPtr cmd, const char *format, ...)
void void
virCommandAddArgPair(virCommandPtr cmd, const char *name, const char *val) virCommandAddArgPair(virCommandPtr cmd, const char *name, const char *val)
{ {
if (name == NULL || val == NULL) {
cmd->has_error = EINVAL;
return;
}
virCommandAddArgFormat(cmd, "%s=%s", name, val); virCommandAddArgFormat(cmd, "%s=%s", name, val);
} }

View File

@ -1 +1 @@
qemu-img create -f qcow2 -o compat=0.10 0K qemu-img create -f qcow2 -o compat=0.10 /var/lib/libvirt/images/OtherDemo.img 0K

View File

@ -1,6 +1,7 @@
<volume> <volume>
<name>OtherDemo.img</name> <name>OtherDemo.img</name>
<target> <target>
<path>/var/lib/libvirt/images/OtherDemo.img</path>
<format type="qcow2"/> <format type="qcow2"/>
</target> </target>
<capacity>0</capacity> <capacity>0</capacity>