On Tue, 12 Jun 2018 23:48:31 -0400
Keno Fischer 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
> Acked-by: Greg Kurz
> ---
>
Applied to 9p-next. You can rebase your patches against:
https://github.com/gkurz/qemu/commits/9p-next
Thanks!
> Changes since v4:
> - `CONFIG_STRCHRNUL` -> `HAVE_STRCHRNUL` name change
> - In the strchrnul configure check
>- Return the value of strchrnul to avoid it being optimized away
>- Use `` (i.e. the assembly of the generated function as
> the haystack) to avoid concerns about constant folding. `main`
> seemed like the simplest symbol guaranteed to exist, but with
> non-foldable content.
>
> configure | 18 ++
> 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, 51 insertions(+), 20 deletions(-)
>
> diff --git a/configure b/configure
> index 14b1113..8a75745 100755
> --- a/configure
> +++ b/configure
> @@ -4747,6 +4747,21 @@ if compile_prog "" "" ; then
> fi
>
> ##
> +# check if we have strchrnul
> +
> +strchrnul=no
> +cat > $TMPC << EOF
> +#include
> +int main(void);
> +// Use a haystack that the compiler shouldn't be able to constant fold
> +char *haystack = (char*)
> +int main(void) { return strchrnul(haystack, 'x') != [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
> @@ -6229,6 +6244,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 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 5721eff..828e8d6 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..274d419 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 6d0cec5..4484d74 100644
> --- a/monitor.c
> +++ b/monitor.c
> @@ -797,9 +797,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 =