From nobody Mon Feb 9 03:13:01 2026 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.zoho.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; Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 14940151852941023.901513484115; Fri, 5 May 2017 13:13:05 -0700 (PDT) Received: from localhost ([::1]:48827 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d6jb4-0004eI-Nm for importer@patchew.org; Fri, 05 May 2017 16:13:02 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50472) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d6jZl-0003mm-9d for qemu-devel@nongnu.org; Fri, 05 May 2017 16:11:42 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d6jZj-0001m9-I7 for qemu-devel@nongnu.org; Fri, 05 May 2017 16:11:41 -0400 Received: from mx1.redhat.com ([209.132.183.28]:51114) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1d6jZj-0001lY-8N for qemu-devel@nongnu.org; Fri, 05 May 2017 16:11:39 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 493163D97B; Fri, 5 May 2017 20:11:38 +0000 (UTC) Received: from localhost (ovpn-116-28.gru2.redhat.com [10.97.116.28]) by smtp.corp.redhat.com (Postfix) with ESMTP id A019E79821; Fri, 5 May 2017 20:11:37 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 493163D97B Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx06.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=ehabkost@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 493163D97B From: Eduardo Habkost To: qemu-devel@nongnu.org Date: Fri, 5 May 2017 17:11:28 -0300 Message-Id: <20170505201128.12099-4-ehabkost@redhat.com> In-Reply-To: <20170505201128.12099-1-ehabkost@redhat.com> References: <20170505201128.12099-1-ehabkost@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Fri, 05 May 2017 20:11:38 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v2 3/3] string-input-visitor: Support alternate types 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: Markus Armbruster , Michael Roth Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" When parsing alternates from a string, there are some limitations in what we can do, but it is a valid use case in some situations. We can support booleans, integer types, and enums. This will be used to support 'feature=3Dforce' in -cpu options, while keeping 'feature=3Don|off|true|false' represented as boolean values. Signed-off-by: Eduardo Habkost --- Changes v1 -> v2: * Updated string_input_visitor_new() documentation to mention alternate support (Markus Armbruster) * Detect ambiguous alternates at runtime. Test case included. * Removed support for integers. We don't need it yet, and it would require sorting out the parse_str() mess. * Change supported_qtypes to uint32_t (Eric Blake) * Update tests/qapi-schema/qapi-schema-test.out to match qapi-schema-test.json updates (Eric Blake) * Code indentation fix (Markus Armbruster) * Use &error_abort on test cases instead of g_assert(!err) (Markus Armbruster) --- include/qapi/string-input-visitor.h | 6 +- qapi/string-input-visitor.c | 99 +++++++++++++++++++++++++++++= ---- tests/test-string-input-visitor.c | 76 +++++++++++++++++++++++++ tests/qapi-schema/qapi-schema-test.json | 8 +++ tests/qapi-schema/qapi-schema-test.out | 9 +++ 5 files changed, 187 insertions(+), 11 deletions(-) diff --git a/include/qapi/string-input-visitor.h b/include/qapi/string-inpu= t-visitor.h index 33551340e3..e7f359f225 100644 --- a/include/qapi/string-input-visitor.h +++ b/include/qapi/string-input-visitor.h @@ -19,8 +19,12 @@ typedef struct StringInputVisitor StringInputVisitor; =20 /* * The string input visitor does not implement support for visiting - * QAPI structs, alternates, null, or arbitrary QTypes. It also + * QAPI structs, null, or arbitrary QTypes. It also * requires a non-null list argument to visit_start_list(). + * + * Support for alternates is very limited: only bool and enum + * members are supported, and only when the enum members' + * representations can't be confused with a bool value. */ Visitor *string_input_visitor_new(const char *str); =20 diff --git a/qapi/string-input-visitor.c b/qapi/string-input-visitor.c index c089491c24..e339b88192 100644 --- a/qapi/string-input-visitor.c +++ b/qapi/string-input-visitor.c @@ -19,6 +19,7 @@ #include "qemu/option.h" #include "qemu/queue.h" #include "qemu/range.h" +#include "qemu/host-utils.h" =20 =20 struct StringInputVisitor @@ -278,21 +279,34 @@ static void parse_type_size(Visitor *v, const char *n= ame, uint64_t *obj, *obj =3D val; } =20 +static int try_parse_bool(const char *s, bool *result) +{ + if (!strcasecmp(s, "on") || + !strcasecmp(s, "yes") || + !strcasecmp(s, "true")) { + if (result) { + *result =3D true; + } + return 0; + } + if (!strcasecmp(s, "off") || + !strcasecmp(s, "no") || + !strcasecmp(s, "false")) { + if (result) { + *result =3D false; + } + return 0; + } + + return -1; +} + static void parse_type_bool(Visitor *v, const char *name, bool *obj, Error **errp) { StringInputVisitor *siv =3D to_siv(v); =20 - if (!strcasecmp(siv->string, "on") || - !strcasecmp(siv->string, "yes") || - !strcasecmp(siv->string, "true")) { - *obj =3D true; - return; - } - if (!strcasecmp(siv->string, "off") || - !strcasecmp(siv->string, "no") || - !strcasecmp(siv->string, "false")) { - *obj =3D false; + if (try_parse_bool(siv->string, obj) =3D=3D 0) { return; } =20 @@ -326,6 +340,70 @@ static void parse_type_number(Visitor *v, const char *= name, double *obj, *obj =3D val; } =20 +/* + * Check if alternate type string representation is ambiguous and + * can't be parsed by StringInputVisitor + */ +static bool ambiguous_alternate(uint32_t qtypes, const char *const enum_ta= ble[]) +{ + uint32_t non_str_qtypes =3D qtypes & ~(1U << QTYPE_QSTRING); + + if ((qtypes & (1U << QTYPE_QSTRING)) && !enum_table && non_str_qtypes)= { + return true; + } + + if (qtypes & (1U << QTYPE_QBOOL)) { + const char *const *e; + /* + * If the string representation of enum members can be parsed as + * booleans, the alternate string representation is ambiguous. + */ + for (e =3D enum_table; e && *e; e++) { + if (try_parse_bool(*e, NULL) =3D=3D 0) { + return true; + } + } + } + + return false; +} + +static void start_alternate(Visitor *v, const char *name, + GenericAlternate **obj, size_t size, + uint32_t qtypes, const char *const enum_table[= ], + Error **errp) +{ + /* + * Enum types are represented as QTYPE_QSTRING, so this is + * the default. Actual parsing of the string as an enum is + * done by visit_type_(), which is called just + * after visit_start_alternate(). + */ + QType qtype =3D QTYPE_QSTRING; + uint32_t unsupported_qtypes =3D qtypes & ~((1U << QTYPE_QSTRING) | + (1U << QTYPE_QBOOL)); + StringInputVisitor *siv =3D to_siv(v); + + if (ambiguous_alternate(qtypes, enum_table)) { + error_setg(errp, "Can't parse ambiguous alternate type"); + return; + } + + if (unsupported_qtypes) { + error_setg(errp, "Can't parse %s' alternate member", + QType_lookup[ctz32(unsupported_qtypes)]); + return; + } + + if ((qtypes & (1U << QTYPE_QBOOL)) && + try_parse_bool(siv->string, NULL) =3D=3D 0) { + qtype =3D QTYPE_QBOOL; + } + + *obj =3D g_malloc0(size); + (*obj)->type =3D qtype; +} + static void string_input_free(Visitor *v) { StringInputVisitor *siv =3D to_siv(v); @@ -353,6 +431,7 @@ Visitor *string_input_visitor_new(const char *str) v->visitor.next_list =3D next_list; v->visitor.check_list =3D check_list; v->visitor.end_list =3D end_list; + v->visitor.start_alternate =3D start_alternate; v->visitor.free =3D string_input_free; =20 v->string =3D str; diff --git a/tests/test-string-input-visitor.c b/tests/test-string-input-vi= sitor.c index 79313a7f7a..263fcc2b8c 100644 --- a/tests/test-string-input-visitor.c +++ b/tests/test-string-input-visitor.c @@ -290,6 +290,76 @@ static void test_visitor_in_enum(TestInputVisitorData = *data, } } =20 +static void test_visitor_in_alt_bool_enum(TestInputVisitorData *data, + const void *unused) +{ + Error *err =3D NULL; + Visitor *v; + AltBoolEnum *be =3D NULL; + + v =3D visitor_input_test_init(data, "true"); + visit_type_AltBoolEnum(v, NULL, &be, &error_abort); + g_assert_cmpint(be->type, =3D=3D, QTYPE_QBOOL); + g_assert(be->u.b); + qapi_free_AltBoolEnum(be); + + v =3D visitor_input_test_init(data, "off"); + visit_type_AltBoolEnum(v, NULL, &be, &error_abort); + g_assert_cmpint(be->type, =3D=3D, QTYPE_QBOOL); + g_assert(!be->u.b); + qapi_free_AltBoolEnum(be); + + v =3D visitor_input_test_init(data, "value2"); + visit_type_AltBoolEnum(v, NULL, &be, &error_abort); + g_assert_cmpint(be->type, =3D=3D, QTYPE_QSTRING); + g_assert_cmpint(be->u.e, =3D=3D, ENUM_ONE_VALUE2); + qapi_free_AltBoolEnum(be); + + v =3D visitor_input_test_init(data, "value100"); + visit_type_AltBoolEnum(v, NULL, &be, &err); + error_free_or_abort(&err); + qapi_free_AltBoolEnum(be); + + v =3D visitor_input_test_init(data, "10"); + visit_type_AltBoolEnum(v, NULL, &be, &err); + error_free_or_abort(&err); + qapi_free_AltBoolEnum(be); +} + +static void test_visitor_in_alt_ambig_str(TestInputVisitorData *data, + const void *unused) +{ + Error *err =3D NULL; + Visitor *v; + AltStrBool *sb =3D NULL; + + /* + * AltStrBool has an ambiguous string representation, and + * can't be handled by string-input-visitor: + */ + v =3D visitor_input_test_init(data, "s"); + visit_type_AltStrBool(v, NULL, &sb, &err); + error_free_or_abort(&err); + qapi_free_AltStrBool(sb); +} + +static void test_visitor_in_alt_ambig_enum(TestInputVisitorData *data, + const void *unused) +{ + Error *err =3D NULL; + Visitor *v; + AltOnOffBool *ob =3D NULL; + + /* + * AltOnOffBool has an ambiguous string representation, and + * can't be handled by string-input-visitor: + */ + v =3D visitor_input_test_init(data, "on"); + visit_type_AltOnOffBool(v, NULL, &ob, &err); + error_free_or_abort(&err); + qapi_free_AltOnOffBool(ob); +} + /* Try to crash the visitors */ static void test_visitor_in_fuzz(TestInputVisitorData *data, const void *unused) @@ -366,6 +436,12 @@ int main(int argc, char **argv) &in_visitor_data, test_visitor_in_string); input_visitor_test_add("/string-visitor/input/enum", &in_visitor_data, test_visitor_in_enum); + input_visitor_test_add("/string-visitor/input/alternate/bool_enum", + &in_visitor_data, test_visitor_in_alt_bool_enu= m); + input_visitor_test_add("/string-visitor/input/alternate/ambig_enum", + &in_visitor_data, test_visitor_in_alt_ambig_en= um); + input_visitor_test_add("/string-visitor/input/alternate/ambig_str", + &in_visitor_data, test_visitor_in_alt_ambig_st= r); input_visitor_test_add("/string-visitor/input/fuzz", &in_visitor_data, test_visitor_in_fuzz); =20 diff --git a/tests/qapi-schema/qapi-schema-test.json b/tests/qapi-schema/qa= pi-schema-test.json index 842ea3c5e3..d602f3d40b 100644 --- a/tests/qapi-schema/qapi-schema-test.json +++ b/tests/qapi-schema/qapi-schema-test.json @@ -106,6 +106,14 @@ { 'alternate': 'AltIntNum', 'data': { 'i': 'int', 'n': 'number' } } { 'alternate': 'AltNumInt', 'data': { 'n': 'number', 'i': 'int' } } =20 +# for testing string-input-visitor handling of alternates: +{ 'alternate': 'AltBoolEnum', 'data': { 'b': 'bool', 'e': 'EnumOne' } } + +# this alternate type will be detected as ambiguous by string-input-visito= r: +{ 'enum': 'TestOnOffAuto', + 'data': [ 'on', 'off', 'auto' ] } +{ 'alternate': 'AltOnOffBool', 'data': { 'o': 'TestOnOffAuto', 'b': 'bool'= } } + # for testing native lists { 'union': 'UserDefNativeListUnion', 'data': { 'integer': ['int'], diff --git a/tests/qapi-schema/qapi-schema-test.out b/tests/qapi-schema/qap= i-schema-test.out index 9d99c4eebb..17cd276295 100644 --- a/tests/qapi-schema/qapi-schema-test.out +++ b/tests/qapi-schema/qapi-schema-test.out @@ -1,3 +1,7 @@ +alternate AltBoolEnum + tag type + case b: bool + case e: EnumOne alternate AltIntNum tag type case i: int @@ -10,6 +14,10 @@ alternate AltNumStr tag type case n: number case s: str +alternate AltOnOffBool + tag type + case o: TestOnOffAuto + case b: bool alternate AltStrBool tag type case s: str @@ -56,6 +64,7 @@ enum QEnumTwo ['value1', 'value2'] prefix QENUM_TWO enum QType ['none', 'qnull', 'qint', 'qstring', 'qdict', 'qlist', 'qfloat'= , 'qbool'] prefix QTYPE +enum TestOnOffAuto ['on', 'off', 'auto'] object TestStruct member integer: int optional=3DFalse member boolean: bool optional=3DFalse --=20 2.11.0.259.g40922b1