Message ID | 20230322073453.7853-3-jgross@suse.com (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
Series | tools/xl: small cleanup of parsing code | expand |
On Wed, Mar 22, 2023 at 3:35 AM Juergen Gross <jgross@suse.com> wrote: > > Today split_string_into_pair() will not really do what its name is > suggesting: instead of splitting a string into a pair of strings using > a delimiter, it will return the first two strings of the initial string > by using the delimiter. > > This is never what the callers want, so modify split_string_into_pair() > to split the string only at the first delimiter found, resulting in > something like "x=a=b" to be split into "x" and "a=b" when being called > with "=" as the delimiter. Today the returned strings would be "x" and > "a". > > At the same time switch the delimiter from "const char *" (allowing > multiple delimiter characters) to "char" (a single character only), as > this makes the function more simple without breaking any use cases. > > Suggested-by: Anthony PERARD <anthony.perard@citrix.com> > Signed-off-by: Juergen Gross <jgross@suse.com> Reviewed-by: Jason Andryuk <jandryuk@gmail.com>
On Wed, Mar 22, 2023 at 08:29:51AM -0400, Jason Andryuk wrote: > On Wed, Mar 22, 2023 at 3:35 AM Juergen Gross <jgross@suse.com> wrote: > > > > Today split_string_into_pair() will not really do what its name is > > suggesting: instead of splitting a string into a pair of strings using > > a delimiter, it will return the first two strings of the initial string > > by using the delimiter. > > > > This is never what the callers want, so modify split_string_into_pair() > > to split the string only at the first delimiter found, resulting in > > something like "x=a=b" to be split into "x" and "a=b" when being called > > with "=" as the delimiter. Today the returned strings would be "x" and > > "a". > > > > At the same time switch the delimiter from "const char *" (allowing > > multiple delimiter characters) to "char" (a single character only), as > > this makes the function more simple without breaking any use cases. > > > > Suggested-by: Anthony PERARD <anthony.perard@citrix.com> > > Signed-off-by: Juergen Gross <jgross@suse.com> > > Reviewed-by: Jason Andryuk <jandryuk@gmail.com> Acked-by: Anthony PERARD <anthony.perard@citrix.com> Thanks,
diff --git a/tools/xl/xl_parse.c b/tools/xl/xl_parse.c index 0e1b6907fa..09cabd2732 100644 --- a/tools/xl/xl_parse.c +++ b/tools/xl/xl_parse.c @@ -654,7 +654,7 @@ static void parse_vnuma_config(const XLU_Config *config, if (!buf) continue; - if (split_string_into_pair(buf, "=", &option, &value, + if (split_string_into_pair(buf, '=', &option, &value, isspace)) { fprintf(stderr, "xl: failed to split \"%s\" into pair\n", buf); @@ -831,7 +831,7 @@ int parse_vdispl_config(libxl_device_vdispl *vdispl, char *token) { char *resolution; - rc = split_string_into_pair(connectors[i], ":", + rc = split_string_into_pair(connectors[i], ':', &vdispl->connectors[i].unique_id, &resolution, NULL); @@ -2358,7 +2358,7 @@ void parse_config_data(const char *config_source, for (i = 0; i < len; i++) { char *key, *value; int rc; - rc = split_string_into_pair(pairs[i], "=", &key, &value, + rc = split_string_into_pair(pairs[i], '=', &key, &value, isspace); if (rc != 0) { fprintf(stderr, "failed to parse channel configuration: %s", @@ -3011,26 +3011,27 @@ void trim(char_predicate_t predicate, const char *input, char **output) *output = result; } -int split_string_into_pair(const char *str, const char *delim, - char **a, char **b, char_predicate_t predicate) +int split_string_into_pair(const char *str, char delim, char **a, char **b, + char_predicate_t predicate) { - char *s, *p, *saveptr, *aa = NULL, *bb = NULL; + char *s, *p, *aa = NULL, *bb = NULL; int rc = 0; s = xstrdup(str); - p = strtok_r(s, delim, &saveptr); + p = strchr(s, delim); if (p == NULL) { rc = ERROR_INVAL; goto out; } + *p = 0; if (predicate) { - trim(predicate, p, &aa); + trim(predicate, s, &aa); } else { - aa = xstrdup(p); + aa = xstrdup(s); } - p = strtok_r(NULL, delim, &saveptr); - if (p == NULL) { + p++; + if (!*p) { rc = ERROR_INVAL; goto out; } diff --git a/tools/xl/xl_parse.h b/tools/xl/xl_parse.h index ab35c68545..fe0d586cdd 100644 --- a/tools/xl/xl_parse.h +++ b/tools/xl/xl_parse.h @@ -51,8 +51,8 @@ void replace_string(char **str, const char *val); and look for CTYPE in libxl_internal.h */ typedef int (*char_predicate_t)(const int c); void trim(char_predicate_t predicate, const char *input, char **output); -int split_string_into_pair(const char *str, const char *delim, - char **a, char **b, char_predicate_t predicate); +int split_string_into_pair(const char *str, char delim, char **a, char **b, + char_predicate_t predicate); const char *get_action_on_shutdown_name(libxl_action_on_shutdown a);
Today split_string_into_pair() will not really do what its name is suggesting: instead of splitting a string into a pair of strings using a delimiter, it will return the first two strings of the initial string by using the delimiter. This is never what the callers want, so modify split_string_into_pair() to split the string only at the first delimiter found, resulting in something like "x=a=b" to be split into "x" and "a=b" when being called with "=" as the delimiter. Today the returned strings would be "x" and "a". At the same time switch the delimiter from "const char *" (allowing multiple delimiter characters) to "char" (a single character only), as this makes the function more simple without breaking any use cases. Suggested-by: Anthony PERARD <anthony.perard@citrix.com> Signed-off-by: Juergen Gross <jgross@suse.com> --- V2: - new patch --- tools/xl/xl_parse.c | 23 ++++++++++++----------- tools/xl/xl_parse.h | 4 ++-- 2 files changed, 14 insertions(+), 13 deletions(-)