From 5c99fa375da1c7cc4a42a93e002e98b9fb34754b Mon Sep 17 00:00:00 2001 From: Keno Fischer Date: Fri, 29 Jun 2018 12:32:10 +0200 Subject: cutils: Provide strchrnul strchrnul is a GNU extension and thus unavailable on a number of targets. In the review for a commit removing strchrnul from 9p, I was asked to create a qemu_strchrnul helper to factor out this functionality. Do so, and use it in a number of other places in the code base that inlined the replacement pattern in a place where strchrnul could be used. Signed-off-by: Keno Fischer Acked-by: Greg Kurz Reviewed-by: Markus Armbruster Signed-off-by: Greg Kurz --- util/cutils.c | 15 +++++++++++++++ util/qemu-option.c | 6 +----- util/uri.c | 6 ++---- 3 files changed, 18 insertions(+), 9 deletions(-) (limited to 'util') diff --git a/util/cutils.c b/util/cutils.c index 0de69e6db4..9205e09031 100644 --- a/util/cutils.c +++ b/util/cutils.c @@ -544,6 +544,21 @@ int qemu_strtou64(const char *nptr, const char **endptr, int base, return check_strtox_error(nptr, ep, endptr, errno); } +/** + * Searches for the first occurrence of 'c' in 's', and returns a pointer + * to the trailing null byte if none was found. + */ +#ifndef HAVE_STRCHRNUL +const char *qemu_strchrnul(const char *s, int c) +{ + const char *e = strchr(s, c); + if (!e) { + e = s + strlen(s); + } + return e; +} +#endif + /** * parse_uint: * diff --git a/util/qemu-option.c b/util/qemu-option.c index ba44a0895c..19761e3eaf 100644 --- a/util/qemu-option.c +++ b/util/qemu-option.c @@ -77,11 +77,7 @@ const char *get_opt_value(const char *p, char **value) *value = NULL; while (1) { - offset = strchr(p, ','); - if (!offset) { - offset = p + strlen(p); - } - + offset = qemu_strchrnul(p, ','); length = offset - p; if (*offset != '\0' && *(offset + 1) == ',') { length++; diff --git a/util/uri.c b/util/uri.c index 8624a7ac23..8bdef84120 100644 --- a/util/uri.c +++ b/util/uri.c @@ -52,6 +52,7 @@ */ #include "qemu/osdep.h" +#include "qemu/cutils.h" #include "qemu/uri.h" @@ -2266,10 +2267,7 @@ struct QueryParams *query_params_parse(const char *query) /* Find the next separator, or end of the string. */ end = strchr(query, '&'); if (!end) { - end = strchr(query, ';'); - } - if (!end) { - end = query + strlen(query); + end = qemu_strchrnul(query, ';'); } /* Find the first '=' character between here and end. */ -- cgit v1.2.3