From patchwork Sun Jun 10 18:02:11 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Keno Fischer X-Patchwork-Id: 927393 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=nongnu.org (client-ip=2001:4830:134:3::11; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=juliacomputing.com Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=juliacomputing-com.20150623.gappssmtp.com header.i=@juliacomputing-com.20150623.gappssmtp.com header.b="TFqXblVx"; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 413kXK5B50z9ry1 for ; Mon, 11 Jun 2018 04:03:12 +1000 (AEST) Received: from localhost ([::1]:44863 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fS4gH-0003op-9Y for incoming@patchwork.ozlabs.org; Sun, 10 Jun 2018 14:03:09 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59184) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fS4fl-0003oY-2T for qemu-devel@nongnu.org; Sun, 10 Jun 2018 14:02:38 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fS4fh-0004Gw-W3 for qemu-devel@nongnu.org; Sun, 10 Jun 2018 14:02:37 -0400 Received: from mail-qt0-x242.google.com ([2607:f8b0:400d:c0d::242]:38241) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1fS4fh-0004G0-QY for qemu-devel@nongnu.org; Sun, 10 Jun 2018 14:02:33 -0400 Received: by mail-qt0-x242.google.com with SMTP id x34-v6so18295005qtk.5 for ; Sun, 10 Jun 2018 11:02:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=juliacomputing-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id; bh=MrD5wwNEy69k0b/gYj+TIh7z/jCuVGo4T/bNvJlcxYE=; b=TFqXblVxvAll/iHeypo8HHPjyuMQQP3VPbFeuyrx+b/r93A7wxgJ2neicKIZ9s0lN9 UM++dCGMGR7zrDi/+kx6htRiiFhJdfTpokjg7OR/3gxMPvVFSxNiVfKsU57Rreem/DQ6 wFDyedsxApbDArPgy06VshfYbQyKziG9e1GXE8KFt7p8Pkvd8WXrS3Q9Lc0IQGXF0jh8 m/mnyiim4NJvQs8gF7N7/VBd989CCWhcVrVEbzcELnK9kCDWWR1X8LTzdONZ/Inwkdv9 jfK1A/9S+MF1s9Ngx7GwNcOYr+eQxXOF3VBUpfUAS4KjD5vsaof2K2DJ/LF5YSYM/6yj +MSA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=MrD5wwNEy69k0b/gYj+TIh7z/jCuVGo4T/bNvJlcxYE=; b=O2LzOFzjDFTBxEZqyJjyy/5OHlTNQDT1li+LHojo3ukbXili1unMZ++qvoltWOgEq9 1oCq+siqMy0JX49HRIcylWLPoqQoJAdd5s+gtiWLjKuQUaFaU5pHQTEjj1s/RqACBb+t HVlgmTv/CQEUhlqC3gxtkZ/SO+9w6WKZ/Xucej9XAuj0c2IrHAh8kGDAx7xEZNy4vr7f aE6WJIvu8KVr3zXhPkw4UqyCeCVLVLdO3hiDcJ3mIkdlxSKaprXqv1ULuzJf2wFaE6CQ A5bV7Ymr5wZsFyGhWDiTNSGStNZcI//sc8Os8+mUg20UF/ZHUJW9IQ9EyaBbBhD+d0rK DFMg== X-Gm-Message-State: APt69E3Awnf0W8FpoClwH4KT1G/gcaFWU96KHXtbpJyQwRZGa79mkSNe H4ftTN61/Khh33sbRJ5Gk6o5QcJhx8g= X-Google-Smtp-Source: ADUXVKJFHtBK0eO+XwFnr1+qgwlYCCH/liBIl9vjgogzTeZU+S9gzQMcJ33oL6SpAbJ6//sVtAnqxA== X-Received: by 2002:ac8:368:: with SMTP id w40-v6mr13776337qtg.191.1528653752641; Sun, 10 Jun 2018 11:02:32 -0700 (PDT) Received: from localhost.localdomain (96-86-104-61-static.hfc.comcastbusiness.net. [96.86.104.61]) by smtp.gmail.com with ESMTPSA id g18-v6sm14454542qkm.68.2018.06.10.11.02.31 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 10 Jun 2018 11:02:31 -0700 (PDT) From: Keno Fischer To: qemu-devel@nongnu.org Date: Sun, 10 Jun 2018 14:02:11 -0400 Message-Id: <1528653731-4920-1-git-send-email-keno@juliacomputing.com> X-Mailer: git-send-email 2.8.1 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400d:c0d::242 Subject: [Qemu-devel] [PATCH v4] cutils: Provide strchrnul X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Keno Fischer , groug@kaod.org, "Dr. David Alan Gilbert" , Markus Armbruster Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" 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 --- Changes since v3: - Fix bug in configure check 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 14b1113..f5ca850 100755 --- a/configure +++ b/configure @@ -4747,6 +4747,18 @@ if compile_prog "" "" ; then fi ########################################## +# check if we have strchrnul + +strchrnul=no +cat > $TMPC << EOF +#include +int main(void) { (void)strchrnul("Hello World", 'W'); return 0; } +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 +6241,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 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..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 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 = qemu_strchrnul(p, '|'); if ((p - pstart) == len && !memcmp(pstart, name, len)) return 1; if (*p == '\0') @@ -3400,9 +3398,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. */