diff mbox series

[2/2] tools/xl: rework p9 config parsing

Message ID 20230317111546.18061-3-jgross@suse.com (mailing list archive)
State Superseded
Headers show
Series tools/xl: small cleanup of parsing code | expand

Commit Message

Jürgen Groß March 17, 2023, 11:15 a.m. UTC
Rework the config parsing of a p9 device to use the
split_string_into_pair() function instead of open coding it.

Signed-off-by: Juergen Gross <jgross@suse.com>
---
 tools/xl/xl_parse.c | 72 ++++++++++++++++++++++-----------------------
 1 file changed, 35 insertions(+), 37 deletions(-)

Comments

Jason Andryuk March 20, 2023, 5:12 p.m. UTC | #1
On Fri, Mar 17, 2023 at 7:16 AM Juergen Gross <jgross@suse.com> wrote:
>
> Rework the config parsing of a p9 device to use the
> split_string_into_pair() function instead of open coding it.
>
> Signed-off-by: Juergen Gross <jgross@suse.com>
> ---
>  tools/xl/xl_parse.c | 72 ++++++++++++++++++++++-----------------------
>  1 file changed, 35 insertions(+), 37 deletions(-)
>
> diff --git a/tools/xl/xl_parse.c b/tools/xl/xl_parse.c
> index 2f9dfea05c..715e14f95f 100644
> --- a/tools/xl/xl_parse.c
> +++ b/tools/xl/xl_parse.c
> @@ -2111,54 +2111,52 @@ void parse_config_data(const char *config_source,
>
>      if (!xlu_cfg_get_list(config, "p9", &p9devs, 0, 0)) {
>          libxl_device_p9 *p9;
> -        char *security_model = NULL;
> -        char *path = NULL;
> -        char *tag = NULL;
> -        char *backend = NULL;
> -        char *p, *p2, *buf2;
>
>          d_config->num_p9s = 0;
>          d_config->p9s = NULL;
>          while ((buf = xlu_cfg_get_listitem (p9devs, d_config->num_p9s)) != NULL) {
> +            libxl_string_list pairs;
> +            int len;
> +
>              p9 = ARRAY_EXTEND_INIT(d_config->p9s,
>                                     d_config->num_p9s,
>                                     libxl_device_p9_init);
>              libxl_device_p9_init(p9);
>
> -            buf2 = strdup(buf);
> -            p = strtok(buf2, ",");
> -            if(p) {
> -               do {
> -                  while(*p == ' ')
> -                     ++p;
> -                  if ((p2 = strchr(p, '=')) == NULL)
> -                     break;
> -                  *p2 = '\0';
> -                  if (!strcmp(p, "security_model")) {
> -                     security_model = strdup(p2 + 1);
> -                  } else if(!strcmp(p, "path")) {
> -                     path = strdup(p2 + 1);
> -                  } else if(!strcmp(p, "tag")) {
> -                     tag = strdup(p2 + 1);
> -                  } else if(!strcmp(p, "backend")) {
> -                     backend = strdup(p2 + 1);
> -                  } else {
> -                     fprintf(stderr, "Unknown string `%s' in 9pfs spec\n", p);
> -                     exit(1);
> -                  }
> -               } while ((p = strtok(NULL, ",")) != NULL);
> -            }
> -            if (!path || !security_model || !tag) {
> -               fprintf(stderr, "9pfs spec missing required field!\n");
> -               exit(1);
> +            split_string_into_string_list(buf, ",", &pairs);
> +            len = libxl_string_list_length(&pairs);
> +            for (i = 0; i < len; i++) {
> +                char *key, *value;
> +                int rc;
> +
> +                rc = split_string_into_pair(pairs[i], "=", &key, &value,
> +                                            isspace);
> +                if (rc != 0) {
> +                    fprintf(stderr, "failed to parse 9pfs configuration: %s",
> +                            pairs[i]);
> +                    exit(1);
> +                }
> +
> +                if (!strcmp(key, "security_model")) {
> +                    replace_string(&p9->security_model, value);
> +                } else if (!strcmp(key, "path")) {
> +                    replace_string(&p9->path, value);
> +                } else if (!strcmp(key, "tag")) {
> +                    replace_string(&p9->tag, value);
> +                } else if (!strcmp(key, "backend")) {
> +                    replace_string(&p9->backend_domname, value);
> +                } else {
> +                    fprintf(stderr, "Unknown 9pfs parameter '%s'\n", key);
> +                    exit(1);
> +                }
> +                free(key);
> +                free(value);
>              }
> -            free(buf2);

I think you need libxl_string_list_dispose(&pairs); somewhere around here?

The rest looks good.

Regards,
Jason
Jürgen Groß March 21, 2023, 5:45 a.m. UTC | #2
On 20.03.23 18:12, Jason Andryuk wrote:
> On Fri, Mar 17, 2023 at 7:16 AM Juergen Gross <jgross@suse.com> wrote:
>>
>> Rework the config parsing of a p9 device to use the
>> split_string_into_pair() function instead of open coding it.
>>
>> Signed-off-by: Juergen Gross <jgross@suse.com>
>> ---
>>   tools/xl/xl_parse.c | 72 ++++++++++++++++++++++-----------------------
>>   1 file changed, 35 insertions(+), 37 deletions(-)
>>
>> diff --git a/tools/xl/xl_parse.c b/tools/xl/xl_parse.c
>> index 2f9dfea05c..715e14f95f 100644
>> --- a/tools/xl/xl_parse.c
>> +++ b/tools/xl/xl_parse.c
>> @@ -2111,54 +2111,52 @@ void parse_config_data(const char *config_source,
>>
>>       if (!xlu_cfg_get_list(config, "p9", &p9devs, 0, 0)) {
>>           libxl_device_p9 *p9;
>> -        char *security_model = NULL;
>> -        char *path = NULL;
>> -        char *tag = NULL;
>> -        char *backend = NULL;
>> -        char *p, *p2, *buf2;
>>
>>           d_config->num_p9s = 0;
>>           d_config->p9s = NULL;
>>           while ((buf = xlu_cfg_get_listitem (p9devs, d_config->num_p9s)) != NULL) {
>> +            libxl_string_list pairs;
>> +            int len;
>> +
>>               p9 = ARRAY_EXTEND_INIT(d_config->p9s,
>>                                      d_config->num_p9s,
>>                                      libxl_device_p9_init);
>>               libxl_device_p9_init(p9);
>>
>> -            buf2 = strdup(buf);
>> -            p = strtok(buf2, ",");
>> -            if(p) {
>> -               do {
>> -                  while(*p == ' ')
>> -                     ++p;
>> -                  if ((p2 = strchr(p, '=')) == NULL)
>> -                     break;
>> -                  *p2 = '\0';
>> -                  if (!strcmp(p, "security_model")) {
>> -                     security_model = strdup(p2 + 1);
>> -                  } else if(!strcmp(p, "path")) {
>> -                     path = strdup(p2 + 1);
>> -                  } else if(!strcmp(p, "tag")) {
>> -                     tag = strdup(p2 + 1);
>> -                  } else if(!strcmp(p, "backend")) {
>> -                     backend = strdup(p2 + 1);
>> -                  } else {
>> -                     fprintf(stderr, "Unknown string `%s' in 9pfs spec\n", p);
>> -                     exit(1);
>> -                  }
>> -               } while ((p = strtok(NULL, ",")) != NULL);
>> -            }
>> -            if (!path || !security_model || !tag) {
>> -               fprintf(stderr, "9pfs spec missing required field!\n");
>> -               exit(1);
>> +            split_string_into_string_list(buf, ",", &pairs);
>> +            len = libxl_string_list_length(&pairs);
>> +            for (i = 0; i < len; i++) {
>> +                char *key, *value;
>> +                int rc;
>> +
>> +                rc = split_string_into_pair(pairs[i], "=", &key, &value,
>> +                                            isspace);
>> +                if (rc != 0) {
>> +                    fprintf(stderr, "failed to parse 9pfs configuration: %s",
>> +                            pairs[i]);
>> +                    exit(1);
>> +                }
>> +
>> +                if (!strcmp(key, "security_model")) {
>> +                    replace_string(&p9->security_model, value);
>> +                } else if (!strcmp(key, "path")) {
>> +                    replace_string(&p9->path, value);
>> +                } else if (!strcmp(key, "tag")) {
>> +                    replace_string(&p9->tag, value);
>> +                } else if (!strcmp(key, "backend")) {
>> +                    replace_string(&p9->backend_domname, value);
>> +                } else {
>> +                    fprintf(stderr, "Unknown 9pfs parameter '%s'\n", key);
>> +                    exit(1);
>> +                }
>> +                free(key);
>> +                free(value);
>>               }
>> -            free(buf2);
> 
> I think you need libxl_string_list_dispose(&pairs); somewhere around here?

Ah yes, thanks for noticing.


Juergen
Anthony PERARD March 21, 2023, 10:44 a.m. UTC | #3
On Fri, Mar 17, 2023 at 12:15:46PM +0100, Juergen Gross wrote:
> Rework the config parsing of a p9 device to use the
> split_string_into_pair() function instead of open coding it.

But that wasn't an open codded version of split_string_into_pair(). Now
if a value contains a '=', everything after it is ignored.

split_string_into_pair() would split the string "foo=bar=void" into just
"foo" and "bar".

As the man page doesn't say that VALUE can't contains '=', this patch
looks like a regression.

I start to think that split_string_into_pair() is broken. I've notice
the same issue when reviewing the "smbios" addition, and did proposed to
"open code" split_string_into_pair(). But maybe that function needs
fixing instead.

Thanks,
Jürgen Groß March 21, 2023, 11:17 a.m. UTC | #4
On 21.03.23 11:44, Anthony PERARD wrote:
> On Fri, Mar 17, 2023 at 12:15:46PM +0100, Juergen Gross wrote:
>> Rework the config parsing of a p9 device to use the
>> split_string_into_pair() function instead of open coding it.
> 
> But that wasn't an open codded version of split_string_into_pair(). Now
> if a value contains a '=', everything after it is ignored.
> 
> split_string_into_pair() would split the string "foo=bar=void" into just
> "foo" and "bar".
> 
> As the man page doesn't say that VALUE can't contains '=', this patch
> looks like a regression.
> 
> I start to think that split_string_into_pair() is broken. I've notice
> the same issue when reviewing the "smbios" addition, and did proposed to
> "open code" split_string_into_pair(). But maybe that function needs
> fixing instead.

Yes, I'll add a patch fixing split_string_into_pair().


Juergen
diff mbox series

Patch

diff --git a/tools/xl/xl_parse.c b/tools/xl/xl_parse.c
index 2f9dfea05c..715e14f95f 100644
--- a/tools/xl/xl_parse.c
+++ b/tools/xl/xl_parse.c
@@ -2111,54 +2111,52 @@  void parse_config_data(const char *config_source,
 
     if (!xlu_cfg_get_list(config, "p9", &p9devs, 0, 0)) {
         libxl_device_p9 *p9;
-        char *security_model = NULL;
-        char *path = NULL;
-        char *tag = NULL;
-        char *backend = NULL;
-        char *p, *p2, *buf2;
 
         d_config->num_p9s = 0;
         d_config->p9s = NULL;
         while ((buf = xlu_cfg_get_listitem (p9devs, d_config->num_p9s)) != NULL) {
+            libxl_string_list pairs;
+            int len;
+
             p9 = ARRAY_EXTEND_INIT(d_config->p9s,
                                    d_config->num_p9s,
                                    libxl_device_p9_init);
             libxl_device_p9_init(p9);
 
-            buf2 = strdup(buf);
-            p = strtok(buf2, ",");
-            if(p) {
-               do {
-                  while(*p == ' ')
-                     ++p;
-                  if ((p2 = strchr(p, '=')) == NULL)
-                     break;
-                  *p2 = '\0';
-                  if (!strcmp(p, "security_model")) {
-                     security_model = strdup(p2 + 1);
-                  } else if(!strcmp(p, "path")) {
-                     path = strdup(p2 + 1);
-                  } else if(!strcmp(p, "tag")) {
-                     tag = strdup(p2 + 1);
-                  } else if(!strcmp(p, "backend")) {
-                     backend = strdup(p2 + 1);
-                  } else {
-                     fprintf(stderr, "Unknown string `%s' in 9pfs spec\n", p);
-                     exit(1);
-                  }
-               } while ((p = strtok(NULL, ",")) != NULL);
-            }
-            if (!path || !security_model || !tag) {
-               fprintf(stderr, "9pfs spec missing required field!\n");
-               exit(1);
+            split_string_into_string_list(buf, ",", &pairs);
+            len = libxl_string_list_length(&pairs);
+            for (i = 0; i < len; i++) {
+                char *key, *value;
+                int rc;
+
+                rc = split_string_into_pair(pairs[i], "=", &key, &value,
+                                            isspace);
+                if (rc != 0) {
+                    fprintf(stderr, "failed to parse 9pfs configuration: %s",
+                            pairs[i]);
+                    exit(1);
+                }
+
+                if (!strcmp(key, "security_model")) {
+                    replace_string(&p9->security_model, value);
+                } else if (!strcmp(key, "path")) {
+                    replace_string(&p9->path, value);
+                } else if (!strcmp(key, "tag")) {
+                    replace_string(&p9->tag, value);
+                } else if (!strcmp(key, "backend")) {
+                    replace_string(&p9->backend_domname, value);
+                } else {
+                    fprintf(stderr, "Unknown 9pfs parameter '%s'\n", key);
+                    exit(1);
+                }
+                free(key);
+                free(value);
             }
-            free(buf2);
 
-            replace_string(&p9->tag, tag);
-            replace_string(&p9->security_model, security_model);
-            replace_string(&p9->path, path);
-            if (backend)
-                    replace_string(&p9->backend_domname, backend);
+            if (!p9->path || !p9->security_model || !p9->tag) {
+                fprintf(stderr, "9pfs spec missing required field!\n");
+                exit(1);
+            }
         }
     }