Patchwork qemu-option: Reject anti-social IDs

login
register
mail settings
Submitter Markus Armbruster
Date June 8, 2010, 11:54 a.m.
Message ID <1275998066-2796-1-git-send-email-armbru@redhat.com>
Download mbox | patch
Permalink /patch/54962/
State New
Headers show

Comments

Markus Armbruster - June 8, 2010, 11:54 a.m.
Restrict IDs to letters, digits, '-', '.', '_', starting with a
letter.

This takes care of '/' in qdev IDs breaking qbus_find().

Signed-off-by: Markus Armbruster <armbru@redhat.com>
---
 qemu-option.c |   20 ++++++++++++++++++++
 1 files changed, 20 insertions(+), 0 deletions(-)
Anthony Liguori - June 14, 2010, 8:57 p.m.
On 06/08/2010 06:54 AM, Markus Armbruster wrote:
> Restrict IDs to letters, digits, '-', '.', '_', starting with a
> letter.
>
> This takes care of '/' in qdev IDs breaking qbus_find().
>
> Signed-off-by: Markus Armbruster<armbru@redhat.com>
>    

Applied.  Thanks.

Regards,

Anthony Liguori
> ---
>   qemu-option.c |   20 ++++++++++++++++++++
>   1 files changed, 20 insertions(+), 0 deletions(-)
>
> diff --git a/qemu-option.c b/qemu-option.c
> index acd74f9..74fb882 100644
> --- a/qemu-option.c
> +++ b/qemu-option.c
> @@ -672,11 +672,31 @@ QemuOpts *qemu_opts_find(QemuOptsList *list, const char *id)
>       return NULL;
>   }
>
> +static int id_wellformed(const char *id)
> +{
> +    int i;
> +
> +    if (!qemu_isalpha(id[0])) {
> +        return 0;
> +    }
> +    for (i = 1; id[i]; i++) {
> +        if (!qemu_isalnum(id[i])&&  !strchr("-._", id[i])) {
> +            return 0;
> +        }
> +    }
> +    return 1;
> +}
> +
>   QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id, int fail_if_exists)
>   {
>       QemuOpts *opts = NULL;
>
>       if (id) {
> +        if (!id_wellformed(id)) {
> +            qerror_report(QERR_INVALID_PARAMETER_VALUE, "id", "an identifier");
> +            error_printf_unless_qmp("Identifiers consist of letters, digits, '-', '.', '_', starting with a letter.\n");
> +            return NULL;
> +        }
>           opts = qemu_opts_find(list, id);
>           if (opts != NULL) {
>               if (fail_if_exists) {
>
Avi Kivity - June 22, 2010, 9:33 a.m.
On 06/08/2010 02:54 PM, Markus Armbruster wrote:
> Restrict IDs to letters, digits, '-', '.', '_', starting with a
> letter.
>
> This takes care of '/' in qdev IDs breaking qbus_find().
>
>    

Broke autotest (used leading numeric char).  Patch posted.

Patch

diff --git a/qemu-option.c b/qemu-option.c
index acd74f9..74fb882 100644
--- a/qemu-option.c
+++ b/qemu-option.c
@@ -672,11 +672,31 @@  QemuOpts *qemu_opts_find(QemuOptsList *list, const char *id)
     return NULL;
 }
 
+static int id_wellformed(const char *id)
+{
+    int i;
+
+    if (!qemu_isalpha(id[0])) {
+        return 0;
+    }
+    for (i = 1; id[i]; i++) {
+        if (!qemu_isalnum(id[i]) && !strchr("-._", id[i])) {
+            return 0;
+        }
+    }
+    return 1;
+}
+
 QemuOpts *qemu_opts_create(QemuOptsList *list, const char *id, int fail_if_exists)
 {
     QemuOpts *opts = NULL;
 
     if (id) {
+        if (!id_wellformed(id)) {
+            qerror_report(QERR_INVALID_PARAMETER_VALUE, "id", "an identifier");
+            error_printf_unless_qmp("Identifiers consist of letters, digits, '-', '.', '_', starting with a letter.\n");
+            return NULL;
+        }
         opts = qemu_opts_find(list, id);
         if (opts != NULL) {
             if (fail_if_exists) {