aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKeno Fischer <keno@juliacomputing.com>2018-06-29 12:32:10 +0200
committerGreg Kurz <groug@kaod.org>2018-06-29 12:32:10 +0200
commit5c99fa375da1c7cc4a42a93e002e98b9fb34754b (patch)
tree96dd1387a3a911dfb5ef5d97a4775028b87ba560
parent609ef9f451759151d0bfe7c3843410ab94d68f18 (diff)
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 <keno@juliacomputing.com> Acked-by: Greg Kurz <groug@kaod.org> Reviewed-by: Markus Armbruster <armbru@redhat.com> Signed-off-by: Greg Kurz <groug@kaod.org>
-rwxr-xr-xconfigure18
-rw-r--r--hmp.c8
-rw-r--r--hw/9pfs/9p-local.c2
-rw-r--r--include/qemu/cutils.h8
-rw-r--r--monitor.c8
-rw-r--r--util/cutils.c15
-rw-r--r--util/qemu-option.c6
-rw-r--r--util/uri.c6
8 files changed, 51 insertions, 20 deletions
diff --git a/configure b/configure
index 4d12cfbe3f..26944a74be 100755
--- a/configure
+++ b/configure
@@ -4794,6 +4794,21 @@ if compile_prog "" "" ; then
fi
##########################################
+# check if we have strchrnul
+
+strchrnul=no
+cat > $TMPC << EOF
+#include <string.h>
+int main(void);
+// Use a haystack that the compiler shouldn't be able to constant fold
+char *haystack = (char*)&main;
+int main(void) { return strchrnul(haystack, 'x') != &haystack[6]; }
+EOF
+if compile_prog "" "" ; then
+ strchrnul=yes
+fi
+
+##########################################
# check if trace backend exists
$python "$source_path/scripts/tracetool.py" "--backends=$trace_backends" --check-backends > /dev/null 2> /dev/null
@@ -6276,6 +6291,9 @@ fi
if test "$sem_timedwait" = "yes" ; then
echo "CONFIG_SEM_TIMEDWAIT=y" >> $config_host_mak
fi
+if test "$strchrnul" = "yes" ; then
+ echo "HAVE_STRCHRNUL=y" >> $config_host_mak
+fi
if test "$byteswap_h" = "yes" ; then
echo "CONFIG_BYTESWAP_H=y" >> $config_host_mak
fi
diff --git a/hmp.c b/hmp.c
index 0da0b0ac33..933230d8a7 100644
--- a/hmp.c
+++ b/hmp.c
@@ -2140,12 +2140,12 @@ void hmp_sendkey(Monitor *mon, const QDict *qdict)
int has_hold_time = qdict_haskey(qdict, "hold-time");
int hold_time = qdict_get_try_int(qdict, "hold-time", -1);
Error *err = NULL;
- char *separator;
+ const char *separator;
int keyname_len;
while (1) {
- separator = strchr(keys, '-');
- keyname_len = separator ? separator - keys : strlen(keys);
+ separator = qemu_strchrnul(keys, '-');
+ keyname_len = separator - keys;
/* Be compatible with old interface, convert user inputted "<" */
if (keys[0] == '<' && keyname_len == 1) {
@@ -2182,7 +2182,7 @@ void hmp_sendkey(Monitor *mon, const QDict *qdict)
keylist->value->u.qcode.data = idx;
}
- if (!separator) {
+ if (!*separator) {
break;
}
keys = separator + 1;
diff --git a/hw/9pfs/9p-local.c b/hw/9pfs/9p-local.c
index 5721eff1e1..828e8d6e56 100644
--- a/hw/9pfs/9p-local.c
+++ b/hw/9pfs/9p-local.c
@@ -65,7 +65,7 @@ int local_open_nofollow(FsContext *fs_ctx, const char *path, int flags,
assert(*path != '/');
head = g_strdup(path);
- c = strchrnul(path, '/');
+ c = qemu_strchrnul(path, '/');
if (*c) {
/* Intermediate path element */
head[c - path] = 0;
diff --git a/include/qemu/cutils.h b/include/qemu/cutils.h
index a663340b23..274d419bb7 100644
--- a/include/qemu/cutils.h
+++ b/include/qemu/cutils.h
@@ -122,6 +122,14 @@ int qemu_strnlen(const char *s, int max_len);
* Returns: the pointer originally in @input.
*/
char *qemu_strsep(char **input, const char *delim);
+#ifdef HAVE_STRCHRNUL
+static inline const char *qemu_strchrnul(const char *s, int c)
+{
+ return strchrnul(s, c);
+}
+#else
+const char *qemu_strchrnul(const char *s, int c);
+#endif
time_t mktimegm(struct tm *tm);
int qemu_fdatasync(int fd);
int fcntl_setfl(int fd, int flag);
diff --git a/monitor.c b/monitor.c
index 0730a27172..80af6a9390 100644
--- a/monitor.c
+++ b/monitor.c
@@ -820,9 +820,7 @@ static int compare_cmd(const char *name, const char *list)
p = list;
for(;;) {
pstart = p;
- p = strchr(p, '|');
- if (!p)
- p = pstart + strlen(pstart);
+ p = qemu_strchrnul(p, '|');
if ((p - pstart) == len && !memcmp(pstart, name, len))
return 1;
if (*p == '\0')
@@ -3489,9 +3487,7 @@ static void cmd_completion(Monitor *mon, const char *name, const char *list)
p = list;
for(;;) {
pstart = p;
- p = strchr(p, '|');
- if (!p)
- p = pstart + strlen(pstart);
+ p = qemu_strchrnul(p, '|');
len = p - pstart;
if (len > sizeof(cmd) - 2)
len = sizeof(cmd) - 2;
diff --git a/util/cutils.c b/util/cutils.c
index 0de69e6db4..9205e09031 100644
--- a/util/cutils.c
+++ b/util/cutils.c
@@ -545,6 +545,21 @@ int qemu_strtou64(const char *nptr, const char **endptr, int base,
}
/**
+ * 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:
*
* @s: String to parse
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. */