aboutsummaryrefslogtreecommitdiff
path: root/qobject/json-parser.c
diff options
context:
space:
mode:
authorPeter Maydell <peter.maydell@linaro.org>2017-06-22 11:34:38 +0100
committerPeter Maydell <peter.maydell@linaro.org>2017-06-22 11:34:39 +0100
commit84e3d0725b06bdf8c6985788caa7776d6b7353ce (patch)
treee4b273590132a034fe693debb52e7037bb7730f2 /qobject/json-parser.c
parentdb7a99cdc1d0f4d8cbf7c41ce9e570dce04f0a11 (diff)
parent269c20b2bbd2aa8531e0cdc741fb166f290d7a2b (diff)
Merge remote-tracking branch 'remotes/armbru/tags/pull-qapi-2017-06-09-v2' into staging
QAPI patches for 2017-06-09 # gpg: Signature made Tue 20 Jun 2017 13:31:39 BST # gpg: using RSA key 0x3870B400EB918653 # gpg: Good signature from "Markus Armbruster <armbru@redhat.com>" # gpg: aka "Markus Armbruster <armbru@pond.sub.org>" # Primary key fingerprint: 354B C8B3 D7EB 2A6B 6867 4E5F 3870 B400 EB91 8653 * remotes/armbru/tags/pull-qapi-2017-06-09-v2: (41 commits) tests/qdict: check more get_try_int() cases console: use get_uint() for "head" property i386/cpu: use get_uint() for "min-level"/"min-xlevel" properties numa: use get_uint() for "size" property pnv-core: use get_uint() for "core-pir" property pvpanic: use get_uint() for "ioport" property auxbus: use get_uint() for "addr" property arm: use get_uint() for "mp-affinity" property xen: use get_uint() for "max-ram-below-4g" property pc: use get_uint() for "hpet-intcap" property pc: use get_uint() for "apic-id" property pc: use get_uint() for "iobase" property acpi: use get_uint() for "pci-hole*" properties acpi: use get_uint() for various acpi properties acpi: use get_uint() for "acpi-pcihp-io*" properties platform-bus: use get_uint() for "addr" property bcm2835_fb: use {get, set}_uint() for "vcram-size" and "vcram-base" aspeed: use {set, get}_uint() for "ram-size" property pcihp: use get_uint() for "bsel" property pc-dimm: make "size" property uint64 ... Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
Diffstat (limited to 'qobject/json-parser.c')
-rw-r--r--qobject/json-parser.c56
1 files changed, 37 insertions, 19 deletions
diff --git a/qobject/json-parser.c b/qobject/json-parser.c
index c18e48ab94..7a417f20cd 100644
--- a/qobject/json-parser.c
+++ b/qobject/json-parser.c
@@ -12,6 +12,7 @@
*/
#include "qemu/osdep.h"
+#include "qemu/cutils.h"
#include "qapi/error.h"
#include "qemu-common.h"
#include "qapi/qmp/types.h"
@@ -466,16 +467,23 @@ static QObject *parse_escape(JSONParserContext *ctxt, va_list *ap)
} else if (!strcmp(token->str, "%i")) {
return QOBJECT(qbool_from_bool(va_arg(*ap, int)));
} else if (!strcmp(token->str, "%d")) {
- return QOBJECT(qint_from_int(va_arg(*ap, int)));
+ return QOBJECT(qnum_from_int(va_arg(*ap, int)));
} else if (!strcmp(token->str, "%ld")) {
- return QOBJECT(qint_from_int(va_arg(*ap, long)));
+ return QOBJECT(qnum_from_int(va_arg(*ap, long)));
} else if (!strcmp(token->str, "%lld") ||
!strcmp(token->str, "%I64d")) {
- return QOBJECT(qint_from_int(va_arg(*ap, long long)));
+ return QOBJECT(qnum_from_int(va_arg(*ap, long long)));
+ } else if (!strcmp(token->str, "%u")) {
+ return QOBJECT(qnum_from_uint(va_arg(*ap, unsigned int)));
+ } else if (!strcmp(token->str, "%lu")) {
+ return QOBJECT(qnum_from_uint(va_arg(*ap, unsigned long)));
+ } else if (!strcmp(token->str, "%llu") ||
+ !strcmp(token->str, "%I64u")) {
+ return QOBJECT(qnum_from_uint(va_arg(*ap, unsigned long long)));
} else if (!strcmp(token->str, "%s")) {
return QOBJECT(qstring_from_str(va_arg(*ap, const char *)));
} else if (!strcmp(token->str, "%f")) {
- return QOBJECT(qfloat_from_double(va_arg(*ap, double)));
+ return QOBJECT(qnum_from_double(va_arg(*ap, double)));
}
return NULL;
}
@@ -491,24 +499,34 @@ static QObject *parse_literal(JSONParserContext *ctxt)
case JSON_STRING:
return QOBJECT(qstring_from_escaped_str(ctxt, token));
case JSON_INTEGER: {
- /* A possibility exists that this is a whole-valued float where the
- * fractional part was left out due to being 0 (.0). It's not a big
- * deal to treat these as ints in the parser, so long as users of the
- * resulting QObject know to expect a QInt in place of a QFloat in
- * cases like these.
+ /*
+ * Represent JSON_INTEGER as QNUM_I64 if possible, else as
+ * QNUM_U64, else as QNUM_DOUBLE. Note that qemu_strtoi64()
+ * and qemu_strtou64() fail with ERANGE when it's not
+ * possible.
*
- * However, in some cases these values will overflow/underflow a
- * QInt/int64 container, thus we should assume these are to be handled
- * as QFloats/doubles rather than silently changing their values.
- *
- * strtoll() indicates these instances by setting errno to ERANGE
+ * qnum_get_int() will then work for any signed 64-bit
+ * JSON_INTEGER, qnum_get_uint() for any unsigned 64-bit
+ * integer, and qnum_get_double() both for any JSON_INTEGER
+ * and any JSON_FLOAT (with precision loss for integers beyond
+ * 53 bits)
*/
+ int ret;
int64_t value;
+ uint64_t uvalue;
+
+ ret = qemu_strtoi64(token->str, NULL, 10, &value);
+ if (!ret) {
+ return QOBJECT(qnum_from_int(value));
+ }
+ assert(ret == -ERANGE);
- errno = 0; /* strtoll doesn't set errno on success */
- value = strtoll(token->str, NULL, 10);
- if (errno != ERANGE) {
- return QOBJECT(qint_from_int(value));
+ if (token->str[0] != '-') {
+ ret = qemu_strtou64(token->str, NULL, 10, &uvalue);
+ if (!ret) {
+ return QOBJECT(qnum_from_uint(uvalue));
+ }
+ assert(ret == -ERANGE);
}
/* fall through to JSON_FLOAT */
}
@@ -516,7 +534,7 @@ static QObject *parse_literal(JSONParserContext *ctxt)
/* FIXME dependent on locale; a pervasive issue in QEMU */
/* FIXME our lexer matches RFC 7159 in forbidding Inf or NaN,
* but those might be useful extensions beyond JSON */
- return QOBJECT(qfloat_from_double(strtod(token->str, NULL)));
+ return QOBJECT(qnum_from_double(strtod(token->str, NULL)));
default:
abort();
}