From nobody Wed Apr 24 13:42:43 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1528861822204636.2114664312445; Tue, 12 Jun 2018 20:50:22 -0700 (PDT) Received: from localhost ([::1]:59655 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fSwnd-00028A-FL for importer@patchew.org; Tue, 12 Jun 2018 23:50:21 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:37478) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fSwmD-0001Pw-Mw for qemu-devel@nongnu.org; Tue, 12 Jun 2018 23:48:55 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fSwmA-0004JG-Ie for qemu-devel@nongnu.org; Tue, 12 Jun 2018 23:48:53 -0400 Received: from mail-qt0-x244.google.com ([2607:f8b0:400d:c0d::244]:36361) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1fSwmA-0004Ib-7U for qemu-devel@nongnu.org; Tue, 12 Jun 2018 23:48:50 -0400 Received: by mail-qt0-x244.google.com with SMTP id o9-v6so1191762qtp.3 for ; Tue, 12 Jun 2018 20:48:48 -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 x134-v6sm1285225qka.58.2018.06.12.20.48.46 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 12 Jun 2018 20:48:47 -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=rjKCkj/gFoybvRU2vb3w4q/kAUa86Bw320d3puk4e+0=; b=RjUATeFU5B8lpQVCTndoUeOIAw/8IkdLQZs2qPB7Tk0DXJokiL0wPHwiibjtYKzfC+ Z4JL9iNLN3ExqUmAcikDF6mZV5P8U/49dHRqw6YdQe0B5SxtW3rt7nuV8XF3p4qE/NEU XHOwHEEs9FtkYAar1eNWw8zUlHpG4ujVbDzjNzApUAvYjcRqWG0kmSwQ8quO+dDMUSQY 76k9ptpl1akrIzK4V7pcu9m9X/3eJDKZhUm3Vc8VNMsjJDhniEvVNttLjF2xv6qx7gAq 054C9f4H4e9+jk8ngIA5l0nNCMLTzjG4Q0EEy2g7ZTaB5rJ29ITxvSBYZVVLIavDl5Sb spiQ== 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=rjKCkj/gFoybvRU2vb3w4q/kAUa86Bw320d3puk4e+0=; b=j4JZ3M3bUtxF78fNNcIQxRpNdIOvJQ+qMTbY9YdEI2UnqzLtmcu68wMHIKwrLxi3KZ Cw6ENW7NzrZEjlB0qpJ1h0LaQijToLLJPhi234i8hXFv2/khb7KF6BcPfTcU9ltJSfpt iXA8++hHa8zqXHCzDZ/s84Y+wCqtN2Yc672dkOr1sYXW4lorfj4d2JfxjREbBefTh8gU w6E7/JnJM66KsNId8lVolN9p2dA+8c23sP0tGEVGe/p7Ic5dTrGjLCw5LLa+z13CQWth NLSGVDJOaHm8/ruUYsAdlmFhXWaJPTxzj3XOq7S0DIpnxTLu6VkeChRr2546l5YEES5L HI2w== X-Gm-Message-State: APt69E2yMjhLmmiiDWW+5/mR2XqQFQX2yPSOlIYVOUMJqlMl8KyIYcmj wJlf5yZ2enUrjynle2UxGOn3Ra9MPgM= X-Google-Smtp-Source: ADUXVKLTvujZ9p6RBhNMTn7iMlGSgs0SsHeB5Aq8LfzNkLr0Dfpwlwrs7IBPdiP1yH9JylPUrK8NGg== X-Received: by 2002:a0c:ad69:: with SMTP id v38-v6mr2974272qvc.120.1528861727960; Tue, 12 Jun 2018 20:48:47 -0700 (PDT) From: Keno Fischer To: qemu-devel@nongnu.org Date: Tue, 12 Jun 2018 23:48:31 -0400 Message-Id: <1528861711-23200-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::244 Subject: [Qemu-devel] [PATCH v5] 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+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" 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 --- 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 `&main` (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 =20 ########################################## +# check if we have strchrnul + +strchrnul=3Dno +cat > $TMPC << EOF +#include +int main(void); +// Use a haystack that the compiler shouldn't be able to constant fold +char *haystack =3D (char*)&main; +int main(void) { return strchrnul(haystack, 'x') !=3D &haystack[6]; } +EOF +if compile_prog "" "" ; then + strchrnul=3Dyes +fi + +########################################## # check if trace backend exists =20 $python "$source_path/scripts/tracetool.py" "--backends=3D$trace_backends"= --check-backends > /dev/null 2> /dev/null @@ -6229,6 +6244,9 @@ fi if test "$sem_timedwait" =3D "yes" ; then echo "CONFIG_SEM_TIMEDWAIT=3Dy" >> $config_host_mak fi +if test "$strchrnul" =3D "yes" ; then + echo "HAVE_STRCHRNUL=3Dy" >> $config_host_mak +fi if test "$byteswap_h" =3D "yes" ; then echo "CONFIG_BYTESWAP_H=3Dy" >> $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 =3D qdict_haskey(qdict, "hold-time"); int hold_time =3D qdict_get_try_int(qdict, "hold-time", -1); Error *err =3D NULL; - char *separator; + const char *separator; int keyname_len; =20 while (1) { - separator =3D strchr(keys, '-'); - keyname_len =3D separator ? separator - keys : strlen(keys); + separator =3D qemu_strchrnul(keys, '-'); + keyname_len =3D separator - keys; =20 /* Be compatible with old interface, convert user inputted "<" */ if (keys[0] =3D=3D '<' && keyname_len =3D=3D 1) { @@ -2165,7 +2165,7 @@ void hmp_sendkey(Monitor *mon, const QDict *qdict) keylist->value->u.qcode.data =3D idx; } =20 - if (!separator) { + if (!*separator) { break; } keys =3D 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 *pa= th, int flags, assert(*path !=3D '/'); =20 head =3D g_strdup(path); - c =3D strchrnul(path, '/'); + c =3D qemu_strchrnul(path, '/'); if (*c) { /* Intermediate path element */ head[c - path] =3D 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 *li= st) p =3D list; for(;;) { pstart =3D p; - p =3D strchr(p, '|'); - if (!p) - p =3D pstart + strlen(pstart); + p =3D qemu_strchrnul(p, '|'); if ((p - pstart) =3D=3D len && !memcmp(pstart, name, len)) return 1; if (*p =3D=3D '\0') @@ -3400,9 +3398,7 @@ static void cmd_completion(Monitor *mon, const char *= name, const char *list) p =3D list; for(;;) { pstart =3D p; - p =3D strchr(p, '|'); - if (!p) - p =3D pstart + strlen(pstart); + p =3D qemu_strchrnul(p, '|'); len =3D p - pstart; if (len > sizeof(cmd) - 2) len =3D sizeof(cmd) - 2; diff --git a/util/cutils.c b/util/cutils.c index 0de69e6..9205e09 100644 --- a/util/cutils.c +++ b/util/cutils.c @@ -545,6 +545,21 @@ int qemu_strtou64(const char *nptr, const char **endpt= r, int base, } =20 /** + * 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 =3D strchr(s, c); + if (!e) { + e =3D 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) =20 *value =3D NULL; while (1) { - offset =3D strchr(p, ','); - if (!offset) { - offset =3D p + strlen(p); - } - + offset =3D qemu_strchrnul(p, ','); length =3D offset - p; if (*offset !=3D '\0' && *(offset + 1) =3D=3D ',') { 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 @@ */ =20 #include "qemu/osdep.h" +#include "qemu/cutils.h" =20 #include "qemu/uri.h" =20 @@ -2266,10 +2267,7 @@ struct QueryParams *query_params_parse(const char *q= uery) /* Find the next separator, or end of the string. */ end =3D strchr(query, '&'); if (!end) { - end =3D strchr(query, ';'); - } - if (!end) { - end =3D query + strlen(query); + end =3D qemu_strchrnul(query, ';'); } =20 /* Find the first '=3D' character between here and end. */ --=20 2.8.1