From nobody Sun May 19 03:02:47 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; 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; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 15427060812991008.0626597845974; Tue, 20 Nov 2018 01:28:01 -0800 (PST) Received: from localhost ([::1]:60928 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gP2Jm-0005fx-Am for importer@patchew.org; Tue, 20 Nov 2018 04:27:38 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:46532) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gP2I5-0004pJ-LK for qemu-devel@nongnu.org; Tue, 20 Nov 2018 04:25:54 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gP2I2-0001tX-HO for qemu-devel@nongnu.org; Tue, 20 Nov 2018 04:25:53 -0500 Received: from mx1.redhat.com ([209.132.183.28]:37488) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gP2I2-0001od-9M for qemu-devel@nongnu.org; Tue, 20 Nov 2018 04:25:50 -0500 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 8B4DB16972A; Tue, 20 Nov 2018 09:25:49 +0000 (UTC) Received: from t460s.redhat.com (ovpn-117-138.ams2.redhat.com [10.36.117.138]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2BE306013B; Tue, 20 Nov 2018 09:25:48 +0000 (UTC) From: David Hildenbrand To: qemu-devel@nongnu.org Date: Tue, 20 Nov 2018 10:25:34 +0100 Message-Id: <20181120092542.13102-2-david@redhat.com> In-Reply-To: <20181120092542.13102-1-david@redhat.com> References: <20181120092542.13102-1-david@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Tue, 20 Nov 2018 09:25:49 +0000 (UTC) Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v2 1/9] cutils: Add qemu_strtod() and qemu_strtod_finite() 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: Paolo Bonzini , David Hildenbrand , Markus Armbruster , Michael Roth Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" Let's provide a wrapper for strtod(). Reviewed-by: Eric Blake Signed-off-by: David Hildenbrand Reviewed-by: Markus Armbruster --- include/qemu/cutils.h | 2 ++ util/cutils.c | 65 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/include/qemu/cutils.h b/include/qemu/cutils.h index 7071bfe2d4..756b41c193 100644 --- a/include/qemu/cutils.h +++ b/include/qemu/cutils.h @@ -146,6 +146,8 @@ int qemu_strtoi64(const char *nptr, const char **endptr= , int base, int64_t *result); int qemu_strtou64(const char *nptr, const char **endptr, int base, uint64_t *result); +int qemu_strtod(const char *nptr, const char **endptr, double *result); +int qemu_strtod_finite(const char *nptr, const char **endptr, double *resu= lt); =20 int parse_uint(const char *s, unsigned long long *value, char **endptr, int base); diff --git a/util/cutils.c b/util/cutils.c index 698bd315bd..c965dbfcad 100644 --- a/util/cutils.c +++ b/util/cutils.c @@ -544,6 +544,71 @@ int qemu_strtou64(const char *nptr, const char **endpt= r, int base, return check_strtox_error(nptr, ep, endptr, errno); } =20 +/** + * Convert string @nptr to a double. + * + * This is a wrapper around strtod() that is harder to misuse. + * Semantics of @nptr and @endptr match strtod() with differences + * noted below. + * + * @nptr may be null, and no conversion is performed then. + * + * If no conversion is performed, store @nptr in *@endptr and return + * -EINVAL. + * + * If @endptr is null, and the string isn't fully converted, return + * -EINVAL. This is the case when the pointer that would be stored in + * a non-null @endptr points to a character other than '\0'. + * + * If the conversion overflows, store +/-HUGE_VAL in @result, depending + * on the sign, and return -ERANGE. + * + * If the conversion underflows, store =C2=B10.0 in @result, depending on = the + * sign, and return -ERANGE. + * + * Else store the converted value in @result, and return zero. + */ +int qemu_strtod(const char *nptr, const char **endptr, double *result) +{ + char *ep; + + if (!nptr) { + if (endptr) { + *endptr =3D nptr; + } + return -EINVAL; + } + + errno =3D 0; + *result =3D strtod(nptr, &ep); + return check_strtox_error(nptr, ep, endptr, errno); +} + +/** + * Convert string @nptr to a finite double. + * + * Works like qemu_strtod(), except that "NaN" and "inf" are rejected + * with -EINVAL and no conversion is performed. + */ +int qemu_strtod_finite(const char *nptr, const char **endptr, double *resu= lt) +{ + double tmp; + int ret; + + ret =3D qemu_strtod(nptr, endptr, &tmp); + if (ret) { + return ret; + } else if (!isfinite(tmp)) { + if (endptr) { + *endptr =3D nptr; + } + return -EINVAL; + } + + *result =3D tmp; + return ret; +} + /** * Searches for the first occurrence of 'c' in 's', and returns a pointer * to the trailing null byte if none was found. --=20 2.17.2 From nobody Sun May 19 03:02:47 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; 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; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1542706350156599.4326460024563; Tue, 20 Nov 2018 01:32:30 -0800 (PST) Received: from localhost ([::1]:60955 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gP2OT-0001E2-11 for importer@patchew.org; Tue, 20 Nov 2018 04:32:29 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:46531) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gP2I5-0004pI-LI for qemu-devel@nongnu.org; Tue, 20 Nov 2018 04:25:54 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gP2I4-00023U-4B for qemu-devel@nongnu.org; Tue, 20 Nov 2018 04:25:53 -0500 Received: from mx1.redhat.com ([209.132.183.28]:37500) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gP2I3-0001yB-So for qemu-devel@nongnu.org; Tue, 20 Nov 2018 04:25:52 -0500 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 37F6B16974A; Tue, 20 Nov 2018 09:25:51 +0000 (UTC) Received: from t460s.redhat.com (ovpn-117-138.ams2.redhat.com [10.36.117.138]) by smtp.corp.redhat.com (Postfix) with ESMTP id D11C160141; Tue, 20 Nov 2018 09:25:49 +0000 (UTC) From: David Hildenbrand To: qemu-devel@nongnu.org Date: Tue, 20 Nov 2018 10:25:35 +0100 Message-Id: <20181120092542.13102-3-david@redhat.com> In-Reply-To: <20181120092542.13102-1-david@redhat.com> References: <20181120092542.13102-1-david@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Tue, 20 Nov 2018 09:25:51 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v2 2/9] cutils: Fix qemu_strtosz() & friends to reject non-finite sizes 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: Paolo Bonzini , David Hildenbrand , Markus Armbruster , Michael Roth Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" qemu_strtosz() & friends reject NaNs, but happily accept inifities. They shouldn't. Fix that. The fix makes use of qemu_strtod_finite(). To avoid ugly casts, change the @end parameter of qemu_strtosz() & friends from char ** to const char **. Also, add two test cases, testing that "inf" and "NaN" are properly rejected. Signed-off-by: David Hildenbrand Reviewed-by: Eric Blake Reviewed-by: Markus Armbruster --- include/qemu/cutils.h | 6 +++--- monitor.c | 2 +- tests/test-cutils.c | 24 +++++++++++++++++------- util/cutils.c | 16 +++++++--------- 4 files changed, 28 insertions(+), 20 deletions(-) diff --git a/include/qemu/cutils.h b/include/qemu/cutils.h index 756b41c193..d2dad3057c 100644 --- a/include/qemu/cutils.h +++ b/include/qemu/cutils.h @@ -153,9 +153,9 @@ int parse_uint(const char *s, unsigned long long *value= , char **endptr, int base); int parse_uint_full(const char *s, unsigned long long *value, int base); =20 -int qemu_strtosz(const char *nptr, char **end, uint64_t *result); -int qemu_strtosz_MiB(const char *nptr, char **end, uint64_t *result); -int qemu_strtosz_metric(const char *nptr, char **end, uint64_t *result); +int qemu_strtosz(const char *nptr, const char **end, uint64_t *result); +int qemu_strtosz_MiB(const char *nptr, const char **end, uint64_t *result); +int qemu_strtosz_metric(const char *nptr, const char **end, uint64_t *resu= lt); =20 /* used to print char* safely */ #define STR_OR_NULL(str) ((str) ? (str) : "null") diff --git a/monitor.c b/monitor.c index d39390c2f2..ee9893c785 100644 --- a/monitor.c +++ b/monitor.c @@ -3231,7 +3231,7 @@ static QDict *monitor_parse_arguments(Monitor *mon, { int ret; uint64_t val; - char *end; + const char *end; =20 while (qemu_isspace(*p)) { p++; diff --git a/tests/test-cutils.c b/tests/test-cutils.c index d85c3e0f6d..1aa8351520 100644 --- a/tests/test-cutils.c +++ b/tests/test-cutils.c @@ -1950,7 +1950,7 @@ static void test_qemu_strtou64_full_max(void) static void test_qemu_strtosz_simple(void) { const char *str; - char *endptr =3D NULL; + const char *endptr; int err; uint64_t res =3D 0xbaadf00d; =20 @@ -2017,7 +2017,7 @@ static void test_qemu_strtosz_units(void) const char *p =3D "1P"; const char *e =3D "1E"; int err; - char *endptr =3D NULL; + const char *endptr; uint64_t res =3D 0xbaadf00d; =20 /* default is M */ @@ -2066,7 +2066,7 @@ static void test_qemu_strtosz_float(void) { const char *str =3D "12.345M"; int err; - char *endptr =3D NULL; + const char *endptr; uint64_t res =3D 0xbaadf00d; =20 err =3D qemu_strtosz(str, &endptr, &res); @@ -2078,7 +2078,7 @@ static void test_qemu_strtosz_float(void) static void test_qemu_strtosz_invalid(void) { const char *str; - char *endptr =3D NULL; + const char *endptr; int err; uint64_t res =3D 0xbaadf00d; =20 @@ -2096,12 +2096,22 @@ static void test_qemu_strtosz_invalid(void) err =3D qemu_strtosz(str, &endptr, &res); g_assert_cmpint(err, =3D=3D, -EINVAL); g_assert(endptr =3D=3D str); + + str =3D "inf"; + err =3D qemu_strtosz(str, &endptr, &res); + g_assert_cmpint(err, =3D=3D, -EINVAL); + g_assert(endptr =3D=3D str); + + str =3D "NaN"; + err =3D qemu_strtosz(str, &endptr, &res); + g_assert_cmpint(err, =3D=3D, -EINVAL); + g_assert(endptr =3D=3D str); } =20 static void test_qemu_strtosz_trailing(void) { const char *str; - char *endptr =3D NULL; + const char *endptr; int err; uint64_t res =3D 0xbaadf00d; =20 @@ -2126,7 +2136,7 @@ static void test_qemu_strtosz_trailing(void) static void test_qemu_strtosz_erange(void) { const char *str; - char *endptr =3D NULL; + const char *endptr; int err; uint64_t res =3D 0xbaadf00d; =20 @@ -2160,7 +2170,7 @@ static void test_qemu_strtosz_metric(void) { const char *str =3D "12345k"; int err; - char *endptr =3D NULL; + const char *endptr; uint64_t res =3D 0xbaadf00d; =20 err =3D qemu_strtosz_metric(str, &endptr, &res); diff --git a/util/cutils.c b/util/cutils.c index c965dbfcad..98732ff5b2 100644 --- a/util/cutils.c +++ b/util/cutils.c @@ -206,20 +206,18 @@ static int64_t suffix_mul(char suffix, int64_t unit) * in *end, if not NULL. Return -ERANGE on overflow, Return -EINVAL on * other error. */ -static int do_strtosz(const char *nptr, char **end, +static int do_strtosz(const char *nptr, const char **end, const char default_suffix, int64_t unit, uint64_t *result) { int retval; - char *endptr; + const char *endptr; unsigned char c; int mul_required =3D 0; double val, mul, integral, fraction; =20 - errno =3D 0; - val =3D strtod(nptr, &endptr); - if (isnan(val) || endptr =3D=3D nptr || errno !=3D 0) { - retval =3D -EINVAL; + retval =3D qemu_strtod_finite(nptr, &endptr, &val); + if (retval) { goto out; } fraction =3D modf(val, &integral); @@ -259,17 +257,17 @@ out: return retval; } =20 -int qemu_strtosz(const char *nptr, char **end, uint64_t *result) +int qemu_strtosz(const char *nptr, const char **end, uint64_t *result) { return do_strtosz(nptr, end, 'B', 1024, result); } =20 -int qemu_strtosz_MiB(const char *nptr, char **end, uint64_t *result) +int qemu_strtosz_MiB(const char *nptr, const char **end, uint64_t *result) { return do_strtosz(nptr, end, 'M', 1024, result); } =20 -int qemu_strtosz_metric(const char *nptr, char **end, uint64_t *result) +int qemu_strtosz_metric(const char *nptr, const char **end, uint64_t *resu= lt) { return do_strtosz(nptr, end, 'B', 1000, result); } --=20 2.17.2 From nobody Sun May 19 03:02:47 2024 Delivered-To: importer@patchew.org Received-SPF: temperror (zoho.com: Error in retrieving data from DNS) 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; spf=temperror (zoho.com: Error in retrieving data from DNS) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1542706464125349.8592787775573; Tue, 20 Nov 2018 01:34:24 -0800 (PST) Received: from localhost ([::1]:60968 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gP2Q9-0002zf-5C for importer@patchew.org; Tue, 20 Nov 2018 04:34:13 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:46556) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gP2I6-0004pe-S4 for qemu-devel@nongnu.org; Tue, 20 Nov 2018 04:25:55 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gP2I6-0002Ek-0T for qemu-devel@nongnu.org; Tue, 20 Nov 2018 04:25:54 -0500 Received: from mx1.redhat.com ([209.132.183.28]:52434) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gP2I5-0002AL-QT for qemu-devel@nongnu.org; Tue, 20 Nov 2018 04:25:53 -0500 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 2D1E176215; Tue, 20 Nov 2018 09:25:53 +0000 (UTC) Received: from t460s.redhat.com (ovpn-117-138.ams2.redhat.com [10.36.117.138]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7EB9F60141; Tue, 20 Nov 2018 09:25:51 +0000 (UTC) From: David Hildenbrand To: qemu-devel@nongnu.org Date: Tue, 20 Nov 2018 10:25:36 +0100 Message-Id: <20181120092542.13102-4-david@redhat.com> In-Reply-To: <20181120092542.13102-1-david@redhat.com> References: <20181120092542.13102-1-david@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Tue, 20 Nov 2018 09:25:53 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v2 3/9] qapi: Fix string-input-visitor to reject NaN and infinities 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: Paolo Bonzini , David Hildenbrand , Markus Armbruster , Michael Roth Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" The string-input-visitor happily accepts NaN and inifities when parsing numbers (doubles). They shouldn't. Fix that. Also, add two test cases, testing if "NaN" and "inf" is properly rejected. Reviewed-by: Eric Blake Signed-off-by: David Hildenbrand Reviewed-by: Markus Armbruster --- qapi/string-input-visitor.c | 6 ++---- tests/test-string-input-visitor.c | 13 +++++++++++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/qapi/string-input-visitor.c b/qapi/string-input-visitor.c index b3fdd0827d..b89c6c4e06 100644 --- a/qapi/string-input-visitor.c +++ b/qapi/string-input-visitor.c @@ -20,6 +20,7 @@ #include "qemu/option.h" #include "qemu/queue.h" #include "qemu/range.h" +#include "qemu/cutils.h" =20 =20 struct StringInputVisitor @@ -313,12 +314,9 @@ static void parse_type_number(Visitor *v, const char *= name, double *obj, Error **errp) { StringInputVisitor *siv =3D to_siv(v); - char *endp =3D (char *) siv->string; double val; =20 - errno =3D 0; - val =3D strtod(siv->string, &endp); - if (errno || endp =3D=3D siv->string || *endp) { + if (qemu_strtod_finite(siv->string, NULL, &val)) { error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null", "number"); return; diff --git a/tests/test-string-input-visitor.c b/tests/test-string-input-vi= sitor.c index 88e0e1aa9a..1efba06948 100644 --- a/tests/test-string-input-visitor.c +++ b/tests/test-string-input-visitor.c @@ -252,6 +252,19 @@ static void test_visitor_in_number(TestInputVisitorDat= a *data, visit_type_number(v, NULL, &res, &err); g_assert(!err); g_assert_cmpfloat(res, =3D=3D, value); + + /* NaN and infinity has to be rejected */ + + v =3D visitor_input_test_init(data, "NaN"); + + visit_type_number(v, NULL, &res, &err); + error_free_or_abort(&err); + + v =3D visitor_input_test_init(data, "inf"); + + visit_type_number(v, NULL, &res, &err); + error_free_or_abort(&err); + } =20 static void test_visitor_in_string(TestInputVisitorData *data, --=20 2.17.2 From nobody Sun May 19 03:02:47 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; 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; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1542706079924894.3127757675057; Tue, 20 Nov 2018 01:27:59 -0800 (PST) Received: from localhost ([::1]:60929 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gP2Ju-0005ko-D9 for importer@patchew.org; Tue, 20 Nov 2018 04:27:46 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:46573) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gP2I8-0004qP-AC for qemu-devel@nongnu.org; Tue, 20 Nov 2018 04:25:59 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gP2I7-0002Mz-Lj for qemu-devel@nongnu.org; Tue, 20 Nov 2018 04:25:56 -0500 Received: from mx1.redhat.com ([209.132.183.28]:51362) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gP2I7-0002M0-Gg for qemu-devel@nongnu.org; Tue, 20 Nov 2018 04:25:55 -0500 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id D61DAB4CA; Tue, 20 Nov 2018 09:25:54 +0000 (UTC) Received: from t460s.redhat.com (ovpn-117-138.ams2.redhat.com [10.36.117.138]) by smtp.corp.redhat.com (Postfix) with ESMTP id 738056013B; Tue, 20 Nov 2018 09:25:53 +0000 (UTC) From: David Hildenbrand To: qemu-devel@nongnu.org Date: Tue, 20 Nov 2018 10:25:37 +0100 Message-Id: <20181120092542.13102-5-david@redhat.com> In-Reply-To: <20181120092542.13102-1-david@redhat.com> References: <20181120092542.13102-1-david@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Tue, 20 Nov 2018 09:25:54 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v2 4/9] qapi: Use qemu_strtod_finite() in qobject-input-visitor 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: Paolo Bonzini , David Hildenbrand , Markus Armbruster , Michael Roth Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Let's use the new function. Just as current behavior, we have to consume the whole string (now it's just way clearer what's going on). Reviewed-by: Eric Blake Reviewed-by: Markus Armbruster Signed-off-by: David Hildenbrand --- qapi/qobject-input-visitor.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/qapi/qobject-input-visitor.c b/qapi/qobject-input-visitor.c index 3e88b27f9e..07465f9947 100644 --- a/qapi/qobject-input-visitor.c +++ b/qapi/qobject-input-visitor.c @@ -562,19 +562,20 @@ static void qobject_input_type_number_keyval(Visitor = *v, const char *name, { QObjectInputVisitor *qiv =3D to_qiv(v); const char *str =3D qobject_input_get_keyval(qiv, name, errp); - char *endp; + double val; =20 if (!str) { return; } =20 - errno =3D 0; - *obj =3D strtod(str, &endp); - if (errno || endp =3D=3D str || *endp || !isfinite(*obj)) { + if (qemu_strtod_finite(str, NULL, &val)) { /* TODO report -ERANGE more nicely */ error_setg(errp, QERR_INVALID_PARAMETER_TYPE, full_name(qiv, name), "number"); + return; } + + *obj =3D val; } =20 static void qobject_input_type_any(Visitor *v, const char *name, QObject *= *obj, --=20 2.17.2 From nobody Sun May 19 03:02:47 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; 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; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 154270607757968.1993299058554; Tue, 20 Nov 2018 01:27:57 -0800 (PST) Received: from localhost ([::1]:60930 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gP2Ju-0005mB-7d for importer@patchew.org; Tue, 20 Nov 2018 04:27:46 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:46585) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gP2IA-0004ql-1l for qemu-devel@nongnu.org; Tue, 20 Nov 2018 04:25:59 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gP2I9-0002P4-Bu for qemu-devel@nongnu.org; Tue, 20 Nov 2018 04:25:58 -0500 Received: from mx1.redhat.com ([209.132.183.28]:43094) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gP2I9-0002OJ-6g for qemu-devel@nongnu.org; Tue, 20 Nov 2018 04:25:57 -0500 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 855C0A4D29; Tue, 20 Nov 2018 09:25:56 +0000 (UTC) Received: from t460s.redhat.com (ovpn-117-138.ams2.redhat.com [10.36.117.138]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1F6C460145; Tue, 20 Nov 2018 09:25:54 +0000 (UTC) From: David Hildenbrand To: qemu-devel@nongnu.org Date: Tue, 20 Nov 2018 10:25:38 +0100 Message-Id: <20181120092542.13102-6-david@redhat.com> In-Reply-To: <20181120092542.13102-1-david@redhat.com> References: <20181120092542.13102-1-david@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Tue, 20 Nov 2018 09:25:56 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v2 5/9] test-string-input-visitor: Add more tests 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: Paolo Bonzini , David Hildenbrand , Markus Armbruster , Michael Roth Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Test that very big/small values are not accepted and that ranges with only one element work. Also test that ranges are ascending and cannot have more than 65536 elements. Rename expect4 to expect5, as we will be moving that to a separate ulist test after the rework. Signed-off-by: David Hildenbrand Reviewed-by: Eric Blake Reviewed-by: Markus Armbruster --- tests/test-string-input-visitor.c | 41 +++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/tests/test-string-input-visitor.c b/tests/test-string-input-vi= sitor.c index 1efba06948..8ee0d1b284 100644 --- a/tests/test-string-input-visitor.c +++ b/tests/test-string-input-visitor.c @@ -121,7 +121,8 @@ static void test_visitor_in_intList(TestInputVisitorDat= a *data, int64_t expect1[] =3D { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 20 }; int64_t expect2[] =3D { 32767, -32768, -32767 }; int64_t expect3[] =3D { INT64_MAX, INT64_MIN }; - uint64_t expect4[] =3D { UINT64_MAX }; + int64_t expect4[] =3D { 1 }; + uint64_t expect5[] =3D { UINT64_MAX }; Error *err =3D NULL; int64List *res =3D NULL; int64List *tail; @@ -140,8 +141,44 @@ static void test_visitor_in_intList(TestInputVisitorDa= ta *data, "-9223372036854775808,9223372036854775807"= ); check_ilist(v, expect3, ARRAY_SIZE(expect3)); =20 + v =3D visitor_input_test_init(data, "1-1"); + check_ilist(v, expect4, ARRAY_SIZE(expect4)); + v =3D visitor_input_test_init(data, "18446744073709551615"); - check_ulist(v, expect4, ARRAY_SIZE(expect4)); + check_ulist(v, expect5, ARRAY_SIZE(expect5)); + + /* Value too large */ + + v =3D visitor_input_test_init(data, "9223372036854775808"); + visit_type_int64List(v, NULL, &res, &err); + error_free_or_abort(&err); + g_assert(!res); + + /* Value too small */ + + v =3D visitor_input_test_init(data, "-9223372036854775809"); + visit_type_int64List(v, NULL, &res, &err); + error_free_or_abort(&err); + g_assert(!res); + + /* Range not ascending */ + + v =3D visitor_input_test_init(data, "3-1"); + visit_type_int64List(v, NULL, &res, &err); + error_free_or_abort(&err); + g_assert(!res); + + v =3D visitor_input_test_init(data, "9223372036854775807-0"); + visit_type_int64List(v, NULL, &res, &err); + error_free_or_abort(&err); + g_assert(!res); + + /* Range too big (65536 is the limit against DOS attacks) */ + + v =3D visitor_input_test_init(data, "0-65536"); + visit_type_int64List(v, NULL, &res, &err); + error_free_or_abort(&err); + g_assert(!res); =20 /* Empty list */ =20 --=20 2.17.2 From nobody Sun May 19 03:02:47 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; 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; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1542706216956953.0647985292242; Tue, 20 Nov 2018 01:30:16 -0800 (PST) Received: from localhost ([::1]:60943 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gP2MJ-0008Jp-Ly for importer@patchew.org; Tue, 20 Nov 2018 04:30:15 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:46607) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gP2IF-0004to-99 for qemu-devel@nongnu.org; Tue, 20 Nov 2018 04:26:05 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gP2IB-0002RS-9G for qemu-devel@nongnu.org; Tue, 20 Nov 2018 04:26:03 -0500 Received: from mx1.redhat.com ([209.132.183.28]:51406) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gP2IB-0002QX-1b for qemu-devel@nongnu.org; Tue, 20 Nov 2018 04:25:59 -0500 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 5DE2F256A; Tue, 20 Nov 2018 09:25:58 +0000 (UTC) Received: from t460s.redhat.com (ovpn-117-138.ams2.redhat.com [10.36.117.138]) by smtp.corp.redhat.com (Postfix) with ESMTP id C4A5860141; Tue, 20 Nov 2018 09:25:56 +0000 (UTC) From: David Hildenbrand To: qemu-devel@nongnu.org Date: Tue, 20 Nov 2018 10:25:39 +0100 Message-Id: <20181120092542.13102-7-david@redhat.com> In-Reply-To: <20181120092542.13102-1-david@redhat.com> References: <20181120092542.13102-1-david@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Tue, 20 Nov 2018 09:25:58 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v2 6/9] qapi: Rewrite string-input-visitor 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: Paolo Bonzini , David Hildenbrand , Markus Armbruster , Michael Roth Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" The input visitor has some problems right now, especially - unsigned type "Range" is used to process signed ranges, resulting in inconsistent behavior and ugly/magical code - uint64_t are parsed like int64_t, so big uint64_t values are not supported and error messages are misleading - lists/ranges of int64_t are accepted although no list is parsed and we should rather report an error - lists/ranges are preparsed using int64_t, making it hard to implement uint64_t values or uint64_t lists - types that don't support lists don't bail out - visiting beyond the end of a list is not handled properly - we don't actually parse lists, we parse *sets*: members are sorted, and duplicates eliminated So let's rewrite it by getting rid of usage of the type "Range" and properly supporting lists of int64_t and uint64_t (including ranges of both types), fixing the above mentioned issues. Lists of other types are not supported and will properly report an error. Virtual walks are now supported. Tests have to be fixed up: - Two BUGs were hardcoded that are fixed now - The string-input-visitor now actually returns a parsed list and not an ordered set. Please note that no users/callers have to be fixed up. Candiates using visit_type_uint16List() and friends are: - backends/hostmem.c:host_memory_backend_set_host_nodes() -- Code can deal with dupilcates/unsorted lists - numa.c::query_memdev() -- via object_property_get_uint16List(), the list will still be sorted and without duplicates (via host_memory_backend_get_host_nodes()) - qapi-visit.c::visit_type_Memdev_members() - qapi-visit.c::visit_type_NumaNodeOptions_members() - qapi-visit.c::visit_type_RockerOfDpaGroup_members - qapi-visit.c::visit_type_RxFilterInfo_members() -- Not used with string-input-visitor. Signed-off-by: David Hildenbrand Reviewed-by: Eric Blake Reviewed-by: Markus Armbruster --- include/qapi/string-input-visitor.h | 4 +- qapi/string-input-visitor.c | 405 ++++++++++++++++------------ tests/test-string-input-visitor.c | 18 +- 3 files changed, 234 insertions(+), 193 deletions(-) diff --git a/include/qapi/string-input-visitor.h b/include/qapi/string-inpu= t-visitor.h index 33551340e3..921f3875b9 100644 --- a/include/qapi/string-input-visitor.h +++ b/include/qapi/string-input-visitor.h @@ -19,8 +19,8 @@ typedef struct StringInputVisitor StringInputVisitor; =20 /* * The string input visitor does not implement support for visiting - * QAPI structs, alternates, null, or arbitrary QTypes. It also - * requires a non-null list argument to visit_start_list(). + * QAPI structs, alternates, null, or arbitrary QTypes. Only flat lists + * of integers (except type "size") are supported. */ Visitor *string_input_visitor_new(const char *str); =20 diff --git a/qapi/string-input-visitor.c b/qapi/string-input-visitor.c index b89c6c4e06..bd92080667 100644 --- a/qapi/string-input-visitor.c +++ b/qapi/string-input-visitor.c @@ -4,10 +4,10 @@ * Copyright Red Hat, Inc. 2012-2016 * * Author: Paolo Bonzini + * David Hildenbrand * * This work is licensed under the terms of the GNU LGPL, version 2.1 or l= ater. * See the COPYING.LIB file in the top-level directory. - * */ =20 #include "qemu/osdep.h" @@ -18,21 +18,42 @@ #include "qapi/qmp/qerror.h" #include "qapi/qmp/qnull.h" #include "qemu/option.h" -#include "qemu/queue.h" -#include "qemu/range.h" #include "qemu/cutils.h" =20 +typedef enum ListMode { + /* no list parsing active / no list expected */ + LM_NONE, + /* we have an unparsed string remaining */ + LM_UNPARSED, + /* we have an unfinished int64 range */ + LM_INT64_RANGE, + /* we have an unfinished uint64 range */ + LM_UINT64_RANGE, + /* we have parsed the string completely and no range is remaining */ + LM_END, +} ListMode; + +/* protect against DOS attacks, limit the amount of elements per range */ +#define RANGE_MAX_ELEMENTS 65536 + +typedef union RangeElement { + int64_t i64; + uint64_t u64; +} RangeElement; =20 struct StringInputVisitor { Visitor visitor; =20 - GList *ranges; - GList *cur_range; - int64_t cur; + /* List parsing state */ + ListMode lm; + RangeElement rangeNext; + RangeElement rangeEnd; + const char *unparsed_string; + void *list; =20 + /* The original string to parse */ const char *string; - void *list; /* Only needed for sanity checking the caller */ }; =20 static StringInputVisitor *to_siv(Visitor *v) @@ -40,136 +61,42 @@ static StringInputVisitor *to_siv(Visitor *v) return container_of(v, StringInputVisitor, visitor); } =20 -static void free_range(void *range, void *dummy) -{ - g_free(range); -} - -static int parse_str(StringInputVisitor *siv, const char *name, Error **er= rp) -{ - char *str =3D (char *) siv->string; - long long start, end; - Range *cur; - char *endptr; - - if (siv->ranges) { - return 0; - } - - if (!*str) { - return 0; - } - - do { - errno =3D 0; - start =3D strtoll(str, &endptr, 0); - if (errno =3D=3D 0 && endptr > str) { - if (*endptr =3D=3D '\0') { - cur =3D g_malloc0(sizeof(*cur)); - range_set_bounds(cur, start, start); - siv->ranges =3D range_list_insert(siv->ranges, cur); - cur =3D NULL; - str =3D NULL; - } else if (*endptr =3D=3D '-') { - str =3D endptr + 1; - errno =3D 0; - end =3D strtoll(str, &endptr, 0); - if (errno =3D=3D 0 && endptr > str && start <=3D end && - (start > INT64_MAX - 65536 || - end < start + 65536)) { - if (*endptr =3D=3D '\0') { - cur =3D g_malloc0(sizeof(*cur)); - range_set_bounds(cur, start, end); - siv->ranges =3D range_list_insert(siv->ranges, cur= ); - cur =3D NULL; - str =3D NULL; - } else if (*endptr =3D=3D ',') { - str =3D endptr + 1; - cur =3D g_malloc0(sizeof(*cur)); - range_set_bounds(cur, start, end); - siv->ranges =3D range_list_insert(siv->ranges, cur= ); - cur =3D NULL; - } else { - goto error; - } - } else { - goto error; - } - } else if (*endptr =3D=3D ',') { - str =3D endptr + 1; - cur =3D g_malloc0(sizeof(*cur)); - range_set_bounds(cur, start, start); - siv->ranges =3D range_list_insert(siv->ranges, cur); - cur =3D NULL; - } else { - goto error; - } - } else { - goto error; - } - } while (str); - - return 0; -error: - g_list_foreach(siv->ranges, free_range, NULL); - g_list_free(siv->ranges); - siv->ranges =3D NULL; - error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name ? name : "null", - "an int64 value or range"); - return -1; -} - -static void -start_list(Visitor *v, const char *name, GenericList **list, size_t size, - Error **errp) +static void start_list(Visitor *v, const char *name, GenericList **list, + size_t size, Error **errp) { StringInputVisitor *siv =3D to_siv(v); =20 - /* We don't support visits without a list */ - assert(list); + assert(siv->lm =3D=3D LM_NONE); siv->list =3D list; + siv->unparsed_string =3D siv->string; =20 - if (parse_str(siv, name, errp) < 0) { - *list =3D NULL; - return; - } - - siv->cur_range =3D g_list_first(siv->ranges); - if (siv->cur_range) { - Range *r =3D siv->cur_range->data; - if (r) { - siv->cur =3D range_lob(r); + if (!siv->string[0]) { + if (list) { + *list =3D NULL; } - *list =3D g_malloc0(size); + siv->lm =3D LM_END; } else { - *list =3D NULL; + if (list) { + *list =3D g_malloc0(size); + } + siv->lm =3D LM_UNPARSED; } } =20 static GenericList *next_list(Visitor *v, GenericList *tail, size_t size) { StringInputVisitor *siv =3D to_siv(v); - Range *r; - - if (!siv->ranges || !siv->cur_range) { - return NULL; - } =20 - r =3D siv->cur_range->data; - if (!r) { + switch (siv->lm) { + case LM_END: return NULL; - } - - if (!range_contains(r, siv->cur)) { - siv->cur_range =3D g_list_next(siv->cur_range); - if (!siv->cur_range) { - return NULL; - } - r =3D siv->cur_range->data; - if (!r) { - return NULL; - } - siv->cur =3D range_lob(r); + case LM_INT64_RANGE: + case LM_UINT64_RANGE: + case LM_UNPARSED: + /* we have an unparsed string or something left in a range */ + break; + default: + abort(); } =20 tail->next =3D g_malloc0(size); @@ -179,88 +106,208 @@ static GenericList *next_list(Visitor *v, GenericLis= t *tail, size_t size) static void check_list(Visitor *v, Error **errp) { const StringInputVisitor *siv =3D to_siv(v); - Range *r; - GList *cur_range; =20 - if (!siv->ranges || !siv->cur_range) { + switch (siv->lm) { + case LM_INT64_RANGE: + case LM_UINT64_RANGE: + case LM_UNPARSED: + error_setg(errp, "Fewer list elements expected"); return; - } - - r =3D siv->cur_range->data; - if (!r) { + case LM_END: return; + default: + abort(); } - - if (!range_contains(r, siv->cur)) { - cur_range =3D g_list_next(siv->cur_range); - if (!cur_range) { - return; - } - r =3D cur_range->data; - if (!r) { - return; - } - } - - error_setg(errp, "Range contains too many values"); } =20 static void end_list(Visitor *v, void **obj) { StringInputVisitor *siv =3D to_siv(v); =20 + assert(siv->lm !=3D LM_NONE); assert(siv->list =3D=3D obj); + siv->list =3D NULL; + siv->unparsed_string =3D NULL; + siv->lm =3D LM_NONE; +} + +static int try_parse_int64_list_entry(StringInputVisitor *siv, int64_t *ob= j) +{ + const char *endptr; + int64_t start, end; + + /* parse a simple int64 or range */ + if (qemu_strtoi64(siv->unparsed_string, &endptr, 0, &start)) { + return -EINVAL; + } + end =3D start; + + switch (endptr[0]) { + case '\0': + siv->unparsed_string =3D endptr; + break; + case ',': + siv->unparsed_string =3D endptr + 1; + break; + case '-': + /* parse the end of the range */ + if (qemu_strtoi64(endptr + 1, &endptr, 0, &end)) { + return -EINVAL; + } + if (start > end || end - start >=3D RANGE_MAX_ELEMENTS) { + return -EINVAL; + } + switch (endptr[0]) { + case '\0': + siv->unparsed_string =3D endptr; + break; + case ',': + siv->unparsed_string =3D endptr + 1; + break; + default: + return -EINVAL; + } + break; + default: + return -EINVAL; + } + + /* we have a proper range (with maybe only one element) */ + siv->lm =3D LM_INT64_RANGE; + siv->rangeNext.i64 =3D start; + siv->rangeEnd.i64 =3D end; + return 0; } =20 static void parse_type_int64(Visitor *v, const char *name, int64_t *obj, Error **errp) { StringInputVisitor *siv =3D to_siv(v); - - if (parse_str(siv, name, errp) < 0) { + int64_t val; + + switch (siv->lm) { + case LM_NONE: + /* just parse a simple int64, bail out if not completely consumed = */ + if (qemu_strtoi64(siv->string, NULL, 0, &val)) { + error_setg(errp, QERR_INVALID_PARAMETER_VALUE, + name ? name : "null", "int64"); + return; + } + *obj =3D val; return; + case LM_UNPARSED: + if (try_parse_int64_list_entry(siv, obj)) { + error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name ? name : "= null", + "list of int64 values or ranges"); + return; + } + assert(siv->lm =3D=3D LM_INT64_RANGE); + /* fall through */ + case LM_INT64_RANGE: + /* return the next element in the range */ + assert(siv->rangeNext.i64 <=3D siv->rangeEnd.i64); + *obj =3D siv->rangeNext.i64++; + + if (siv->rangeNext.i64 > siv->rangeEnd.i64 || *obj =3D=3D INT64_MA= X) { + /* end of range, check if there is more to parse */ + siv->lm =3D siv->unparsed_string[0] ? LM_UNPARSED : LM_END; + } + return; + case LM_END: + error_setg(errp, "Fewer list elements expected"); + return; + default: + abort(); } +} =20 - if (!siv->ranges) { - goto error; - } - - if (!siv->cur_range) { - Range *r; +static int try_parse_uint64_list_entry(StringInputVisitor *siv, uint64_t *= obj) +{ + const char *endptr; + uint64_t start, end; =20 - siv->cur_range =3D g_list_first(siv->ranges); - if (!siv->cur_range) { - goto error; + /* parse a simple uint64 or range */ + if (qemu_strtou64(siv->unparsed_string, &endptr, 0, &start)) { + return -EINVAL; + } + end =3D start; + + switch (endptr[0]) { + case '\0': + siv->unparsed_string =3D endptr; + break; + case ',': + siv->unparsed_string =3D endptr + 1; + break; + case '-': + /* parse the end of the range */ + if (qemu_strtou64(endptr + 1, &endptr, 0, &end)) { + return -EINVAL; } - - r =3D siv->cur_range->data; - if (!r) { - goto error; + if (start > end || end - start >=3D RANGE_MAX_ELEMENTS) { + return -EINVAL; } - - siv->cur =3D range_lob(r); + switch (endptr[0]) { + case '\0': + siv->unparsed_string =3D endptr; + break; + case ',': + siv->unparsed_string =3D endptr + 1; + break; + default: + return -EINVAL; + } + break; + default: + return -EINVAL; } =20 - *obj =3D siv->cur; - siv->cur++; - return; - -error: - error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name ? name : "null", - "an int64 value or range"); + /* we have a proper range (with maybe only one element) */ + siv->lm =3D LM_UINT64_RANGE; + siv->rangeNext.u64 =3D start; + siv->rangeEnd.u64 =3D end; + return 0; } =20 static void parse_type_uint64(Visitor *v, const char *name, uint64_t *obj, Error **errp) { - /* FIXME: parse_type_int64 mishandles values over INT64_MAX */ - int64_t i; - Error *err =3D NULL; - parse_type_int64(v, name, &i, &err); - if (err) { - error_propagate(errp, err); - } else { - *obj =3D i; + StringInputVisitor *siv =3D to_siv(v); + uint64_t val; + + switch (siv->lm) { + case LM_NONE: + /* just parse a simple uint64, bail out if not completely consumed= */ + if (qemu_strtou64(siv->string, NULL, 0, &val)) { + error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name ? name : "= null", + "uint64"); + return; + } + *obj =3D val; + return; + case LM_UNPARSED: + if (try_parse_uint64_list_entry(siv, obj)) { + error_setg(errp, QERR_INVALID_PARAMETER_VALUE, name ? name : "= null", + "list of uint64 values or ranges"); + return; + } + assert(siv->lm =3D=3D LM_UINT64_RANGE); + /* fall through */ + case LM_UINT64_RANGE: + /* return the next element in the range */ + assert(siv->rangeNext.u64 <=3D siv->rangeEnd.u64); + *obj =3D siv->rangeNext.u64++; + + if (siv->rangeNext.u64 > siv->rangeEnd.u64 || *obj =3D=3D UINT64_M= AX) { + /* end of range, check if there is more to parse */ + siv->lm =3D siv->unparsed_string[0] ? LM_UNPARSED : LM_END; + } + return; + case LM_END: + error_setg(errp, "Fewer list elements expected"); + return; + default: + abort(); } } =20 @@ -271,6 +318,7 @@ static void parse_type_size(Visitor *v, const char *nam= e, uint64_t *obj, Error *err =3D NULL; uint64_t val; =20 + assert(siv->lm =3D=3D LM_NONE); parse_option_size(name, siv->string, &val, &err); if (err) { error_propagate(errp, err); @@ -285,6 +333,7 @@ static void parse_type_bool(Visitor *v, const char *nam= e, bool *obj, { StringInputVisitor *siv =3D to_siv(v); =20 + assert(siv->lm =3D=3D LM_NONE); if (!strcasecmp(siv->string, "on") || !strcasecmp(siv->string, "yes") || !strcasecmp(siv->string, "true")) { @@ -307,6 +356,7 @@ static void parse_type_str(Visitor *v, const char *name= , char **obj, { StringInputVisitor *siv =3D to_siv(v); =20 + assert(siv->lm =3D=3D LM_NONE); *obj =3D g_strdup(siv->string); } =20 @@ -316,6 +366,7 @@ static void parse_type_number(Visitor *v, const char *n= ame, double *obj, StringInputVisitor *siv =3D to_siv(v); double val; =20 + assert(siv->lm =3D=3D LM_NONE); if (qemu_strtod_finite(siv->string, NULL, &val)) { error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null", "number"); @@ -330,9 +381,10 @@ static void parse_type_null(Visitor *v, const char *na= me, QNull **obj, { StringInputVisitor *siv =3D to_siv(v); =20 + assert(siv->lm =3D=3D LM_NONE); *obj =3D NULL; =20 - if (!siv->string || siv->string[0]) { + if (siv->string[0]) { error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name ? name : "null", "null"); return; @@ -345,8 +397,6 @@ static void string_input_free(Visitor *v) { StringInputVisitor *siv =3D to_siv(v); =20 - g_list_foreach(siv->ranges, free_range, NULL); - g_list_free(siv->ranges); g_free(siv); } =20 @@ -372,5 +422,6 @@ Visitor *string_input_visitor_new(const char *str) v->visitor.free =3D string_input_free; =20 v->string =3D str; + v->lm =3D LM_NONE; return &v->visitor; } diff --git a/tests/test-string-input-visitor.c b/tests/test-string-input-vi= sitor.c index 8ee0d1b284..c5379365a6 100644 --- a/tests/test-string-input-visitor.c +++ b/tests/test-string-input-visitor.c @@ -92,16 +92,6 @@ static void check_ulist(Visitor *v, uint64_t *expected, = size_t n) uint64List *tail; int i; =20 - /* BUG: unsigned numbers above INT64_MAX don't work */ - for (i =3D 0; i < n; i++) { - if (expected[i] > INT64_MAX) { - Error *err =3D NULL; - visit_type_uint64List(v, NULL, &res, &err); - error_free_or_abort(&err); - return; - } - } - visit_type_uint64List(v, NULL, &res, &error_abort); tail =3D res; for (i =3D 0; i < n; i++) { @@ -117,10 +107,10 @@ static void check_ulist(Visitor *v, uint64_t *expecte= d, size_t n) static void test_visitor_in_intList(TestInputVisitorData *data, const void *unused) { - /* Note: the visitor *sorts* ranges *unsigned* */ - int64_t expect1[] =3D { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 20 }; + int64_t expect1[] =3D { 1, 2, 0, 2, 3, 4, 20, 5, 6, 7, + 8, 9, 1, 2, 3, 4, 5, 6, 7, 8 }; int64_t expect2[] =3D { 32767, -32768, -32767 }; - int64_t expect3[] =3D { INT64_MAX, INT64_MIN }; + int64_t expect3[] =3D { INT64_MIN, INT64_MAX }; int64_t expect4[] =3D { 1 }; uint64_t expect5[] =3D { UINT64_MAX }; Error *err =3D NULL; @@ -226,7 +216,7 @@ static void test_visitor_in_intList(TestInputVisitorDat= a *data, visit_type_int64(v, NULL, &tail->value, &err); g_assert_cmpint(tail->value, =3D=3D, 0); visit_type_int64(v, NULL, &val, &err); - g_assert_cmpint(val, =3D=3D, 1); /* BUG */ + error_free_or_abort(&err); visit_check_list(v, &error_abort); visit_end_list(v, (void **)&res); =20 --=20 2.17.2 From nobody Sun May 19 03:02:47 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; 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; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1542706534113859.0744077756912; Tue, 20 Nov 2018 01:35:34 -0800 (PST) Received: from localhost ([::1]:60979 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gP2RI-0003dF-Na for importer@patchew.org; Tue, 20 Nov 2018 04:35:24 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:46613) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gP2IF-0004uK-T0 for qemu-devel@nongnu.org; Tue, 20 Nov 2018 04:26:04 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gP2IF-0002V8-5h for qemu-devel@nongnu.org; Tue, 20 Nov 2018 04:26:03 -0500 Received: from mx1.redhat.com ([209.132.183.28]:34120) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gP2IF-0002Ud-10 for qemu-devel@nongnu.org; Tue, 20 Nov 2018 04:26:03 -0500 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 4E9A6307EA9F; Tue, 20 Nov 2018 09:26:02 +0000 (UTC) Received: from t460s.redhat.com (ovpn-117-138.ams2.redhat.com [10.36.117.138]) by smtp.corp.redhat.com (Postfix) with ESMTP id A72E46013D; Tue, 20 Nov 2018 09:25:58 +0000 (UTC) From: David Hildenbrand To: qemu-devel@nongnu.org Date: Tue, 20 Nov 2018 10:25:40 +0100 Message-Id: <20181120092542.13102-8-david@redhat.com> In-Reply-To: <20181120092542.13102-1-david@redhat.com> References: <20181120092542.13102-1-david@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.44]); Tue, 20 Nov 2018 09:26:02 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v2 7/9] test-string-input-visitor: Use virtual walk 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: Paolo Bonzini , David Hildenbrand , Markus Armbruster , Michael Roth Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" We now support virtual walks, so use that instead. Reviewed-by: Markus Armbruster Signed-off-by: David Hildenbrand Reviewed-by: Eric Blake --- tests/test-string-input-visitor.c | 36 +++++++++++-------------------- 1 file changed, 12 insertions(+), 24 deletions(-) diff --git a/tests/test-string-input-visitor.c b/tests/test-string-input-vi= sitor.c index c5379365a6..718d9a03c3 100644 --- a/tests/test-string-input-visitor.c +++ b/tests/test-string-input-visitor.c @@ -115,7 +115,6 @@ static void test_visitor_in_intList(TestInputVisitorDat= a *data, uint64_t expect5[] =3D { UINT64_MAX }; Error *err =3D NULL; int64List *res =3D NULL; - int64List *tail; Visitor *v; int64_t val; =20 @@ -188,39 +187,28 @@ static void test_visitor_in_intList(TestInputVisitorD= ata *data, =20 v =3D visitor_input_test_init(data, "0,2-3"); =20 - /* Would be simpler if the visitor genuinely supported virtual walks */ - visit_start_list(v, NULL, (GenericList **)&res, sizeof(*res), - &error_abort); - tail =3D res; - visit_type_int64(v, NULL, &tail->value, &error_abort); - g_assert_cmpint(tail->value, =3D=3D, 0); - tail =3D (int64List *)visit_next_list(v, (GenericList *)tail, sizeof(*= res)); - g_assert(tail); - visit_type_int64(v, NULL, &tail->value, &error_abort); - g_assert_cmpint(tail->value, =3D=3D, 2); - tail =3D (int64List *)visit_next_list(v, (GenericList *)tail, sizeof(*= res)); - g_assert(tail); + visit_start_list(v, NULL, NULL, 0, &error_abort); + visit_type_int64(v, NULL, &val, &error_abort); + g_assert_cmpint(val, =3D=3D, 0); + visit_type_int64(v, NULL, &val, &error_abort); + g_assert_cmpint(val, =3D=3D, 2); =20 visit_check_list(v, &err); error_free_or_abort(&err); - visit_end_list(v, (void **)&res); - - qapi_free_int64List(res); + visit_end_list(v, NULL); =20 /* Visit beyond end of list */ + v =3D visitor_input_test_init(data, "0"); =20 - visit_start_list(v, NULL, (GenericList **)&res, sizeof(*res), - &error_abort); - tail =3D res; - visit_type_int64(v, NULL, &tail->value, &err); - g_assert_cmpint(tail->value, =3D=3D, 0); + visit_start_list(v, NULL, NULL, 0, &error_abort); + visit_type_int64(v, NULL, &val, &err); + g_assert_cmpint(val, =3D=3D, 0); visit_type_int64(v, NULL, &val, &err); error_free_or_abort(&err); - visit_check_list(v, &error_abort); - visit_end_list(v, (void **)&res); =20 - qapi_free_int64List(res); + visit_check_list(v, &error_abort); + visit_end_list(v, NULL); } =20 static void test_visitor_in_bool(TestInputVisitorData *data, --=20 2.17.2 From nobody Sun May 19 03:02:47 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; 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; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1542706357068279.6011065542658; Tue, 20 Nov 2018 01:32:37 -0800 (PST) Received: from localhost ([::1]:60956 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gP2Oa-0001IY-2U for importer@patchew.org; Tue, 20 Nov 2018 04:32:36 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:46655) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gP2IT-00053I-0l for qemu-devel@nongnu.org; Tue, 20 Nov 2018 04:26:17 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gP2IO-0002bd-W4 for qemu-devel@nongnu.org; Tue, 20 Nov 2018 04:26:16 -0500 Received: from mx1.redhat.com ([209.132.183.28]:59770) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gP2IM-0002a0-Ba for qemu-devel@nongnu.org; Tue, 20 Nov 2018 04:26:11 -0500 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id AF75F309706E; Tue, 20 Nov 2018 09:26:09 +0000 (UTC) Received: from t460s.redhat.com (ovpn-117-138.ams2.redhat.com [10.36.117.138]) by smtp.corp.redhat.com (Postfix) with ESMTP id 955C96014C; Tue, 20 Nov 2018 09:26:02 +0000 (UTC) From: David Hildenbrand To: qemu-devel@nongnu.org Date: Tue, 20 Nov 2018 10:25:41 +0100 Message-Id: <20181120092542.13102-9-david@redhat.com> In-Reply-To: <20181120092542.13102-1-david@redhat.com> References: <20181120092542.13102-1-david@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.43]); Tue, 20 Nov 2018 09:26:09 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v2 8/9] test-string-input-visitor: Split off uint64 list tests 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: Paolo Bonzini , David Hildenbrand , Markus Armbruster , Michael Roth Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Basically copy all int64 list tests but adapt them to work on uint64 instead. The values for very big/very small values have to be adapted. Reviewed-by: Markus Armbruster Signed-off-by: David Hildenbrand --- tests/test-string-input-visitor.c | 113 ++++++++++++++++++++++++++++-- 1 file changed, 109 insertions(+), 4 deletions(-) diff --git a/tests/test-string-input-visitor.c b/tests/test-string-input-vi= sitor.c index 718d9a03c3..9b1dd44b2d 100644 --- a/tests/test-string-input-visitor.c +++ b/tests/test-string-input-visitor.c @@ -112,7 +112,6 @@ static void test_visitor_in_intList(TestInputVisitorDat= a *data, int64_t expect2[] =3D { 32767, -32768, -32767 }; int64_t expect3[] =3D { INT64_MIN, INT64_MAX }; int64_t expect4[] =3D { 1 }; - uint64_t expect5[] =3D { UINT64_MAX }; Error *err =3D NULL; int64List *res =3D NULL; Visitor *v; @@ -133,9 +132,6 @@ static void test_visitor_in_intList(TestInputVisitorDat= a *data, v =3D visitor_input_test_init(data, "1-1"); check_ilist(v, expect4, ARRAY_SIZE(expect4)); =20 - v =3D visitor_input_test_init(data, "18446744073709551615"); - check_ulist(v, expect5, ARRAY_SIZE(expect5)); - /* Value too large */ =20 v =3D visitor_input_test_init(data, "9223372036854775808"); @@ -211,6 +207,113 @@ static void test_visitor_in_intList(TestInputVisitorD= ata *data, visit_end_list(v, NULL); } =20 +static void test_visitor_in_uintList(TestInputVisitorData *data, + const void *unused) +{ + uint64_t expect1[] =3D { 1, 2, 0, 2, 3, 4, 20, 5, 6, 7, + 8, 9, 1, 2, 3, 4, 5, 6, 7, 8 }; + uint64_t expect2[] =3D { 32767, -32768, -32767 }; + uint64_t expect3[] =3D { INT64_MIN, INT64_MAX }; + uint64_t expect4[] =3D { 1 }; + uint64_t expect5[] =3D { UINT64_MAX }; + Error *err =3D NULL; + uint64List *res =3D NULL; + Visitor *v; + uint64_t val; + + /* Valid lists */ + + v =3D visitor_input_test_init(data, "1,2,0,2-4,20,5-9,1-8"); + check_ulist(v, expect1, ARRAY_SIZE(expect1)); + + v =3D visitor_input_test_init(data, "32767,-32768--32767"); + check_ulist(v, expect2, ARRAY_SIZE(expect2)); + + v =3D visitor_input_test_init(data, + "-9223372036854775808,9223372036854775807"= ); + check_ulist(v, expect3, ARRAY_SIZE(expect3)); + + v =3D visitor_input_test_init(data, "1-1"); + check_ulist(v, expect4, ARRAY_SIZE(expect4)); + + v =3D visitor_input_test_init(data, "18446744073709551615"); + check_ulist(v, expect5, ARRAY_SIZE(expect5)); + + /* Value too large */ + + v =3D visitor_input_test_init(data, "18446744073709551616"); + visit_type_uint64List(v, NULL, &res, &err); + error_free_or_abort(&err); + g_assert(!res); + + /* Value too small */ + + v =3D visitor_input_test_init(data, "-18446744073709551616"); + visit_type_uint64List(v, NULL, &res, &err); + error_free_or_abort(&err); + g_assert(!res); + + /* Range not ascending */ + + v =3D visitor_input_test_init(data, "3-1"); + visit_type_uint64List(v, NULL, &res, &err); + error_free_or_abort(&err); + g_assert(!res); + + v =3D visitor_input_test_init(data, "18446744073709551615-0"); + visit_type_uint64List(v, NULL, &res, &err); + error_free_or_abort(&err); + g_assert(!res); + + /* Range too big (65536 is the limit against DOS attacks) */ + + v =3D visitor_input_test_init(data, "0-65536"); + visit_type_uint64List(v, NULL, &res, &err); + error_free_or_abort(&err); + g_assert(!res); + + /* Empty list */ + + v =3D visitor_input_test_init(data, ""); + visit_type_uint64List(v, NULL, &res, &error_abort); + g_assert(!res); + + /* Not a list */ + + v =3D visitor_input_test_init(data, "not an uint list"); + + visit_type_uint64List(v, NULL, &res, &err); + error_free_or_abort(&err); + g_assert(!res); + + /* Unvisited list tail */ + + v =3D visitor_input_test_init(data, "0,2-3"); + + visit_start_list(v, NULL, NULL, 0, &error_abort); + visit_type_uint64(v, NULL, &val, &error_abort); + g_assert_cmpuint(val, =3D=3D, 0); + visit_type_uint64(v, NULL, &val, &error_abort); + g_assert_cmpuint(val, =3D=3D, 2); + + visit_check_list(v, &err); + error_free_or_abort(&err); + visit_end_list(v, NULL); + + /* Visit beyond end of list */ + + v =3D visitor_input_test_init(data, "0"); + + visit_start_list(v, NULL, NULL, 0, &error_abort); + visit_type_uint64(v, NULL, &val, &err); + g_assert_cmpuint(val, =3D=3D, 0); + visit_type_uint64(v, NULL, &val, &err); + error_free_or_abort(&err); + + visit_check_list(v, &error_abort); + visit_end_list(v, NULL); +} + static void test_visitor_in_bool(TestInputVisitorData *data, const void *unused) { @@ -384,6 +487,8 @@ int main(int argc, char **argv) &in_visitor_data, test_visitor_in_int); input_visitor_test_add("/string-visitor/input/intList", &in_visitor_data, test_visitor_in_intList); + input_visitor_test_add("/string-visitor/input/uintList", + &in_visitor_data, test_visitor_in_uintList); input_visitor_test_add("/string-visitor/input/bool", &in_visitor_data, test_visitor_in_bool); input_visitor_test_add("/string-visitor/input/number", --=20 2.17.2 From nobody Sun May 19 03:02:47 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; 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; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1542706608057771.6154748973698; Tue, 20 Nov 2018 01:36:48 -0800 (PST) Received: from localhost ([::1]:60982 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gP2SV-0004II-2Y for importer@patchew.org; Tue, 20 Nov 2018 04:36:39 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:46654) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gP2IT-00053H-0P for qemu-devel@nongnu.org; Tue, 20 Nov 2018 04:26:17 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gP2IP-0002bs-4A for qemu-devel@nongnu.org; Tue, 20 Nov 2018 04:26:16 -0500 Received: from mx1.redhat.com ([209.132.183.28]:34986) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gP2IO-0002ak-Va for qemu-devel@nongnu.org; Tue, 20 Nov 2018 04:26:13 -0500 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 5BD23307DAB6; Tue, 20 Nov 2018 09:26:11 +0000 (UTC) Received: from t460s.redhat.com (ovpn-117-138.ams2.redhat.com [10.36.117.138]) by smtp.corp.redhat.com (Postfix) with ESMTP id EF70560141; Tue, 20 Nov 2018 09:26:09 +0000 (UTC) From: David Hildenbrand To: qemu-devel@nongnu.org Date: Tue, 20 Nov 2018 10:25:42 +0100 Message-Id: <20181120092542.13102-10-david@redhat.com> In-Reply-To: <20181120092542.13102-1-david@redhat.com> References: <20181120092542.13102-1-david@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.42]); Tue, 20 Nov 2018 09:26:11 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v2 9/9] test-string-input-visitor: Add range overflow tests 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: Paolo Bonzini , David Hildenbrand , Markus Armbruster , Michael Roth Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Let's make sure that the range handling code can properly deal with ranges that end at the biggest possible number. Reviewed-by: Markus Armbruster Signed-off-by: David Hildenbrand --- tests/test-string-input-visitor.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/test-string-input-visitor.c b/tests/test-string-input-vi= sitor.c index 9b1dd44b2d..34b54dfc89 100644 --- a/tests/test-string-input-visitor.c +++ b/tests/test-string-input-visitor.c @@ -112,6 +112,7 @@ static void test_visitor_in_intList(TestInputVisitorDat= a *data, int64_t expect2[] =3D { 32767, -32768, -32767 }; int64_t expect3[] =3D { INT64_MIN, INT64_MAX }; int64_t expect4[] =3D { 1 }; + int64_t expect5[] =3D { INT64_MAX - 2, INT64_MAX - 1, INT64_MAX }; Error *err =3D NULL; int64List *res =3D NULL; Visitor *v; @@ -132,6 +133,10 @@ static void test_visitor_in_intList(TestInputVisitorDa= ta *data, v =3D visitor_input_test_init(data, "1-1"); check_ilist(v, expect4, ARRAY_SIZE(expect4)); =20 + v =3D visitor_input_test_init(data, + "9223372036854775805-9223372036854775807"); + check_ilist(v, expect5, ARRAY_SIZE(expect5)); + /* Value too large */ =20 v =3D visitor_input_test_init(data, "9223372036854775808"); @@ -216,6 +221,7 @@ static void test_visitor_in_uintList(TestInputVisitorDa= ta *data, uint64_t expect3[] =3D { INT64_MIN, INT64_MAX }; uint64_t expect4[] =3D { 1 }; uint64_t expect5[] =3D { UINT64_MAX }; + uint64_t expect6[] =3D { UINT64_MAX - 2, UINT64_MAX - 1, UINT64_MAX }; Error *err =3D NULL; uint64List *res =3D NULL; Visitor *v; @@ -239,6 +245,10 @@ static void test_visitor_in_uintList(TestInputVisitorD= ata *data, v =3D visitor_input_test_init(data, "18446744073709551615"); check_ulist(v, expect5, ARRAY_SIZE(expect5)); =20 + v =3D visitor_input_test_init(data, + "18446744073709551613-18446744073709551615= "); + check_ulist(v, expect6, ARRAY_SIZE(expect6)); + /* Value too large */ =20 v =3D visitor_input_test_init(data, "18446744073709551616"); --=20 2.17.2