From: Greg Kurz <groug@kaod.org>
To: Keno Fischer <keno@juliacomputing.com>
Cc: qemu-devel@nongnu.org, Markus Armbruster <armbru@redhat.com>,
"Dr. David Alan Gilbert" <dgilbert@redhat.com>,
Eric Blake <eblake@redhat.com>
Subject: Re: [Qemu-devel] [PATCH v3 1/5] cutils: Provide strchrnul
Date: Tue, 5 Jun 2018 13:48:02 +0200 [thread overview]
Message-ID: <20180605134802.4672d12c@bahia.lan> (raw)
In-Reply-To: <51be4c123dbf92a45184e386bd7a62614430ba29.1527973524.git.keno@juliacomputing.com>
On Sat, 2 Jun 2018 17:29:35 -0400
Keno Fischer <keno@juliacomputing.com> wrote:
> 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>
> ---
>
> Changes since v2:
> * Add configure check as suggested by Greg Kurz <groug@kaod.org>,
> and requested by Eric Blake <eblake@redhat.com>
> * Use qemu_strchrnul in hmp_sendkey as suggested by
> Dr. David Alan Gilbert <dgilbert@redhat.com>
>
My Acked-by stands with the new changes.
If everyone is okay, maybe better for this to go through my tree
since it is required for the "9p: Add support for Darwin" series.
> configure | 15 +++++++++++++++
> hmp.c | 8 ++++----
> hw/9pfs/9p-local.c | 2 +-
> include/qemu/cutils.h | 8 ++++++++
> monitor.c | 8 ++------
> util/cutils.c | 15 +++++++++++++++
> util/qemu-option.c | 6 +-----
> util/uri.c | 6 ++----
> 8 files changed, 48 insertions(+), 20 deletions(-)
>
> diff --git a/configure b/configure
> index a6a4616..1b3ca4e 100755
> --- a/configure
> +++ b/configure
> @@ -4754,6 +4754,18 @@ if compile_prog "" "" ; then
> fi
>
> ##########################################
> +# check if we have strchrnul
> +
> +strchrnul=no
> +cat > $TMPC << EOF
> +#include <string.h>
> +int main(void) { (void)strchrnul("Hello World", 'W'); }
> +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
> @@ -6210,6 +6222,9 @@ fi
> if test "$sem_timedwait" = "yes" ; then
> echo "CONFIG_SEM_TIMEDWAIT=y" >> $config_host_mak
> fi
> +if test "$strchrnul" = "yes" ; then
> + echo "CONFIG_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 ef93f48..416d4c9 100644
> --- a/hmp.c
> +++ b/hmp.c
> @@ -2123,12 +2123,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) {
> @@ -2165,7 +2165,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 7758c38..304ef72 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 a663340..809090c 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 CONFIG_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 922cfc0..e1f01c4 100644
> --- a/monitor.c
> +++ b/monitor.c
> @@ -798,9 +798,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')
> @@ -3401,9 +3399,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 0de69e6..c365ddb 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 CONFIG_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 58d1c23..54eca12 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 8624a7a..8bdef84 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. */
next prev parent reply other threads:[~2018-06-05 11:48 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-06-02 21:29 [Qemu-devel] [PATCH v3 0/5] Prepratory cleanup for 9p darwin support Keno Fischer
2018-06-02 21:29 ` [Qemu-devel] [PATCH v3 1/5] cutils: Provide strchrnul Keno Fischer
2018-06-05 11:48 ` Greg Kurz [this message]
2018-06-05 13:15 ` Greg Kurz
2018-06-02 21:29 ` [Qemu-devel] [PATCH v3 2/5] 9p: xattr: Fix crashes due to free of uninitialized value Keno Fischer
2018-06-02 21:29 ` [Qemu-devel] [PATCH v3 3/5] 9p: local: Avoid warning if FS_IOC_GETVERSION is not defined Keno Fischer
2018-06-02 21:29 ` [Qemu-devel] [PATCH v3 4/5] 9p: Properly check/translate flags in unlinkat Keno Fischer
2018-06-02 21:29 ` [Qemu-devel] [PATCH v3 5/5] 9p: xattr: Properly translate xattrcreate flags Keno Fischer
2018-06-05 13:37 ` [Qemu-devel] [PATCH v3 0/5] Prepratory cleanup for 9p darwin support Greg Kurz
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20180605134802.4672d12c@bahia.lan \
--to=groug@kaod.org \
--cc=armbru@redhat.com \
--cc=dgilbert@redhat.com \
--cc=eblake@redhat.com \
--cc=keno@juliacomputing.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.