Pino Toscano
2018-May-04 13:55 UTC
[Libguestfs] [PATCH] common/qemuopts: ensure arg lists are never empty
Since it does not make much sense, then forbid this situation outright: - change qemuopts_end_arg_list() to return an error if the current arg list has no elements - when creating the argv array, assert that each arg list is not empty --- common/qemuopts/qemuopts.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/common/qemuopts/qemuopts.c b/common/qemuopts/qemuopts.c index b3e69e306..49550bb21 100644 --- a/common/qemuopts/qemuopts.c +++ b/common/qemuopts/qemuopts.c @@ -453,7 +453,15 @@ qemuopts_append_arg_list_format (struct qemuopts *qopts, int qemuopts_end_arg_list (struct qemuopts *qopts) { - /* Nothing to do, the list is already well-formed. */ + struct qopt *qopt; + size_t len; + + qopt = last_option (qopts); + assert (qopt->type == QOPT_ARG_LIST); + len = count_strings (qopt->values); + if (len == 0) + return -1; + return 0; } @@ -816,7 +824,9 @@ qemuopts_to_argv (struct qemuopts *qopts) case QOPT_ARG_LIST: /* We only have to do comma-quoting here. */ values = qopts->options[i].values; - len = count_strings (values) - 1 /* one for each comma */; + len = count_strings (values); + assert (len > 0); + len -= 1 /* one for each comma */; for (j = 0; values[j] != NULL; ++j) { for (k = 0; k < strlen (values[j]); ++k) { if (values[j][k] == ',') len++; -- 2.14.3
Richard W.M. Jones
2018-May-04 14:11 UTC
Re: [Libguestfs] [PATCH] common/qemuopts: ensure arg lists are never empty
On Fri, May 04, 2018 at 03:55:36PM +0200, Pino Toscano wrote:> Since it does not make much sense, then forbid this situation outright: > - change qemuopts_end_arg_list() to return an error if the current arg > list has no elements > - when creating the argv array, assert that each arg list is not empty > --- > common/qemuopts/qemuopts.c | 14 ++++++++++++-- > 1 file changed, 12 insertions(+), 2 deletions(-) > > diff --git a/common/qemuopts/qemuopts.c b/common/qemuopts/qemuopts.c > index b3e69e306..49550bb21 100644 > --- a/common/qemuopts/qemuopts.c > +++ b/common/qemuopts/qemuopts.c > @@ -453,7 +453,15 @@ qemuopts_append_arg_list_format (struct qemuopts *qopts, > int > qemuopts_end_arg_list (struct qemuopts *qopts) > { > - /* Nothing to do, the list is already well-formed. */ > + struct qopt *qopt; > + size_t len; > + > + qopt = last_option (qopts); > + assert (qopt->type == QOPT_ARG_LIST); > + len = count_strings (qopt->values); > + if (len == 0) > + return -1; > + > return 0; > } > > @@ -816,7 +824,9 @@ qemuopts_to_argv (struct qemuopts *qopts) > case QOPT_ARG_LIST: > /* We only have to do comma-quoting here. */ > values = qopts->options[i].values; > - len = count_strings (values) - 1 /* one for each comma */; > + len = count_strings (values); > + assert (len > 0); > + len -= 1 /* one for each comma */; > for (j = 0; values[j] != NULL; ++j) { > for (k = 0; k < strlen (values[j]); ++k) { > if (values[j][k] == ',') len++; > --ACK Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com Fedora Windows cross-compiler. Compile Windows programs, test, and build Windows installers. Over 100 libraries supported. http://fedoraproject.org/wiki/MinGW
Reasonably Related Threads
- [PATCH 0/4] common: Add a simple mini-library for handling qemu command and config files.
- [PATCH 3/4] common/qemuopts: use the old pointer as realloc pointer
- [PATCH] Replace -nodefconfig with -no-user-config.
- [PATCH 2.6] update to network emulation QOS scheduler
- [PATCH] packet delay scheduler