Skip to contentSkip to conversation

block: Don't compare strings in bdrv_reopen_prepare()

PATCH Superseded Max Reitz 3 participants 28 messages Jul 3, 2017 – Jul 5, 2017

v3 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Max Reitz

author
#0 lore raw

v3 cover block: Don't compare strings in bdrv_reopen_prepare()

bdrv_reopen_prepare() assumes that all BDS options are strings, which is not necessarily correct. This series introduces a new qobject_is_equal() function which can be used to test whether any options have changed, independently of their type.

v3:
- Patch 1:
- Fix copyright header [Markus]
- Drop qobject.h include from qnull.c [Markus]
- Replacing all QObject includes by qmp/types.h in target/i386/cpu.c became unnecessary due to 01b2ffcedd94ad7b42bc870e4c6936c87ad03429 (kept R-b anyway, because v2 contained exactly the same change as that commit)
- Patch 2:
- Move the code for QInt and QFloat to QNum
- Write function descriptions in imperative mood [Markus]
- Break comment lines after 70 characters [Markus]
- Style changes [Markus]
- Patch 3: Add a comment on what will go wrong when using e.g. -blockdev and the qemu-io reopen command together [Markus]
- Patch 5: Added some test cases for qobject_is_equal() [Markus]

git-backport-diff against v2:

Key:
[----] : patches are identical
[####] : number of functional differences between upstream/downstream patch [down] : patch is downstream-only
The flags [FC] indicate (F)unctional and (C)ontextual differences, respectively

001/5:[0017] [FC] 'qapi/qnull: Add own header'
002/5:[0120] [FC] 'qapi: Add qobject_is_equal()' 003/5:[0018] [FC] 'block: qobject_is_equal() in bdrv_reopen_prepare()' 004/5:[----] [--] 'iotests: Add test for non-string option reopening' 005/5:[down] 'tests: Add check-qobject for equality tests'

Max Reitz (5):
qapi/qnull: Add own header
qapi: Add qobject_is_equal()
block: qobject_is_equal() in bdrv_reopen_prepare()
iotests: Add test for non-string option reopening
tests: Add check-qobject for equality tests

 tests/Makefile.include     |   4 +-
 include/qapi/qmp/qbool.h   |   1 +
 include/qapi/qmp/qdict.h   |   1 +
 include/qapi/qmp/qlist.h   |   1 +
 include/qapi/qmp/qnull.h   |  28 ++++
 include/qapi/qmp/qnum.h    |   1 +
 include/qapi/qmp/qobject.h |  17 +--
 include/qapi/qmp/qstring.h |   1 +
 include/qapi/qmp/types.h   |   1 +
 block.c                    |  31 +++--
 qobject/qbool.c            |   8 ++
 qobject/qdict.c            |  29 +++++
 qobject/qlist.c            |  32 +++++
 qobject/qnull.c            |  11 +-
 qobject/qnum.c             |  53 ++++++++
 qobject/qobject.c          |  29 +++++
 qobject/qstring.c          |   9 ++
 tests/check-qnull.c        |   2 +-
 tests/check-qobject.c      | 312 +++++++++++++++++++++++++++++++++++++++++++++
 tests/qemu-iotests/133     |   9 ++
 tests/qemu-iotests/133.out |   5 +
 21 files changed, 561 insertions(+), 24 deletions(-)
 create mode 100644 include/qapi/qmp/qnull.h
 create mode 100644 tests/check-qobject.c
Signature · 1 line
2.9.4
author
#1 lore raw

PATCH v3 1/5 qapi/qnull: Add own header

---
 include/qapi/qmp/qnull.h   | 26 ++++++++++++++++++++++++++
 include/qapi/qmp/qobject.h |  8 --------
 include/qapi/qmp/types.h   |  1 +
 qobject/qnull.c            |  2 +-
 tests/check-qnull.c        |  2 +-
 5 files changed, 29 insertions(+), 10 deletions(-)
 create mode 100644 include/qapi/qmp/qnull.h
include/qapi/qmp/qnull.h new file +26 -0
new file mode 100644
index 0000000..48edad4
@@ -0,0 +1,26 @@
+/*
+ * QNull
+ *
+ * Copyright (C) 2015 Red Hat, Inc.
+ *
+ * Authors:
+ *  Markus Armbruster <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU LGPL, version 2.1
+ * or later.  See the COPYING.LIB file in the top-level directory.
+ */
+
+#ifndef QNULL_H
+#define QNULL_H
+
+#include "qapi/qmp/qobject.h"
+
+extern QObject qnull_;
+
+static inline QObject *qnull(void)
+{
+    qobject_incref(&qnull_);
+    return &qnull_;
+}
+
+#endif /* QNULL_H */
include/qapi/qmp/qobject.h +0 -8
index b8ddbca..ef1d1a9 100644
@@ -93,12 +93,4 @@ static inline QType qobject_type(const QObject *obj)
     return obj->type;
 }
 
-extern QObject qnull_;
-
-static inline QObject *qnull(void)
-{
-    qobject_incref(&qnull_);
-    return &qnull_;
-}
-
 #endif /* QOBJECT_H */
include/qapi/qmp/types.h +1 -0
index a4bc662..749ac44 100644
@@ -19,5 +19,6 @@
 #include "qapi/qmp/qstring.h"
 #include "qapi/qmp/qdict.h"
 #include "qapi/qmp/qlist.h"
+#include "qapi/qmp/qnull.h"
 
 #endif /* QAPI_QMP_TYPES_H */
qobject/qnull.c +1 -1
index c124d05..43918f1 100644
@@ -12,7 +12,7 @@
 
 #include "qemu/osdep.h"
 #include "qemu-common.h"
-#include "qapi/qmp/qobject.h"
+#include "qapi/qmp/qnull.h"
 
 QObject qnull_ = {
     .type = QTYPE_QNULL,
tests/check-qnull.c +1 -1
index 8dd1c96..4a67b9a 100644
@@ -8,7 +8,7 @@
  */
 #include "qemu/osdep.h"
 
-#include "qapi/qmp/qobject.h"
+#include "qapi/qmp/qnull.h"
 #include "qemu-common.h"
 #include "qapi/qobject-input-visitor.h"
 #include "qapi/qobject-output-visitor.h"
Signature · 1 line
2.9.4
Reviewed-by on v3 1/5
#2 lore raw

Re: [Qemu-devel] [PATCH v3 1/5] qapi/qnull: Add own header

Quoted 17 lines · Max Reitz
---
 include/qapi/qmp/qnull.h   | 26 ++++++++++++++++++++++++++
 include/qapi/qmp/qobject.h |  8 --------
 include/qapi/qmp/types.h   |  1 +
 qobject/qnull.c            |  2 +-
 tests/check-qnull.c        |  2 +-
 5 files changed, 29 insertions(+), 10 deletions(-)
 create mode 100644 include/qapi/qmp/qnull.h
include/qapi/qmp/qnull.h new file +0 -0
new file mode 100644
index 0000000..48edad4

include/qapi/qmp/qnull.h line 4

Quoted 4 lines
include/qapi/qmp/qnull.h new file +4 -0
@@ -0,0 +1,26 @@
+/*
+ * QNull
+ *
+ * Copyright (C) 2015 Red Hat, Inc.

The date makes sense based on when the bulk of the content for this file was first introduced elsewhere, but I'm okay if you also want to add 2017.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
author
#3 lore raw

PATCH v3 2/5 qapi: Add qobject_is_equal()

In reply to Max Reitz

This generic function (along with its implementations for different types) determines whether two QObjects are equal.

---
 include/qapi/qmp/qbool.h   |  1 +
 include/qapi/qmp/qdict.h   |  1 +
 include/qapi/qmp/qlist.h   |  1 +
 include/qapi/qmp/qnull.h   |  2 ++
 include/qapi/qmp/qnum.h    |  1 +
 include/qapi/qmp/qobject.h |  9 ++++++++
 include/qapi/qmp/qstring.h |  1 +
 qobject/qbool.c            |  8 +++++++
 qobject/qdict.c            | 29 +++++++++++++++++++++++++
 qobject/qlist.c            | 32 ++++++++++++++++++++++++++++
 qobject/qnull.c            |  9 ++++++++
 qobject/qnum.c             | 53 ++++++++++++++++++++++++++++++++++++++++++++++
 qobject/qobject.c          | 29 +++++++++++++++++++++++++
 qobject/qstring.c          |  9 ++++++++
 14 files changed, 185 insertions(+)
include/qapi/qmp/qbool.h +1 -0
index a41111c..f77ea86 100644
@@ -24,6 +24,7 @@ typedef struct QBool {
 QBool *qbool_from_bool(bool value);
 bool qbool_get_bool(const QBool *qb);
 QBool *qobject_to_qbool(const QObject *obj);
+bool qbool_is_equal(const QObject *x, const QObject *y);
 void qbool_destroy_obj(QObject *obj);
 
 #endif /* QBOOL_H */
include/qapi/qmp/qdict.h +1 -0
index 363e431..84f8ea7 100644
@@ -42,6 +42,7 @@ void qdict_del(QDict *qdict, const char *key);
 int qdict_haskey(const QDict *qdict, const char *key);
 QObject *qdict_get(const QDict *qdict, const char *key);
 QDict *qobject_to_qdict(const QObject *obj);
+bool qdict_is_equal(const QObject *x, const QObject *y);
 void qdict_iter(const QDict *qdict,
                 void (*iter)(const char *key, QObject *obj, void *opaque),
                 void *opaque);
include/qapi/qmp/qlist.h +1 -0
index c4b5fda..24e1e9f 100644
@@ -58,6 +58,7 @@ QObject *qlist_peek(QList *qlist);
 int qlist_empty(const QList *qlist);
 size_t qlist_size(const QList *qlist);
 QList *qobject_to_qlist(const QObject *obj);
+bool qlist_is_equal(const QObject *x, const QObject *y);
 void qlist_destroy_obj(QObject *obj);
 
 static inline const QListEntry *qlist_first(const QList *qlist)
include/qapi/qmp/qnull.h +2 -0
index 48edad4..f4fbcae 100644
@@ -23,4 +23,6 @@ static inline QObject *qnull(void)
     return &qnull_;
 }
 
+bool qnull_is_equal(const QObject *x, const QObject *y);
+
 #endif /* QNULL_H */
include/qapi/qmp/qnum.h +1 -0
index 09d745c..237d01b 100644
@@ -48,6 +48,7 @@ double qnum_get_double(QNum *qn);
 char *qnum_to_string(QNum *qn);
 
 QNum *qobject_to_qnum(const QObject *obj);
+bool qnum_is_equal(const QObject *x, const QObject *y);
 void qnum_destroy_obj(QObject *obj);
 
 #endif /* QNUM_H */
include/qapi/qmp/qobject.h +9 -0
index ef1d1a9..38ac688 100644
@@ -68,6 +68,15 @@ static inline void qobject_incref(QObject *obj)
 }
 
 /**
+ * qobject_is_equal(): Return whether the two objects are equal.
+ *
+ * Any of the pointers may be NULL; return true if both are.  Always
+ * return false if only one is (therefore a QNull object is not
+ * considered equal to a NULL pointer).
+ */
+bool qobject_is_equal(const QObject *x, const QObject *y);
+
+/**
  * qobject_destroy(): Free resources used by the object
  */
 void qobject_destroy(QObject *obj);
include/qapi/qmp/qstring.h +1 -0
index 10076b7..65c05a9 100644
@@ -31,6 +31,7 @@ void qstring_append_int(QString *qstring, int64_t value);
 void qstring_append(QString *qstring, const char *str);
 void qstring_append_chr(QString *qstring, int c);
 QString *qobject_to_qstring(const QObject *obj);
+bool qstring_is_equal(const QObject *x, const QObject *y);
 void qstring_destroy_obj(QObject *obj);
 
 #endif /* QSTRING_H */
qobject/qbool.c +8 -0
index 0606bbd..ac825fc 100644
@@ -52,6 +52,14 @@ QBool *qobject_to_qbool(const QObject *obj)
 }
 
 /**
+ * qbool_is_equal(): Test whether the two QBools are equal
+ */
+bool qbool_is_equal(const QObject *x, const QObject *y)
+{
+    return qobject_to_qbool(x)->value == qobject_to_qbool(y)->value;
+}
+
+/**
  * qbool_destroy_obj(): Free all memory allocated by a
  * QBool object
  */
qobject/qdict.c +29 -0
index 576018e..e8f15f1 100644
@@ -403,6 +403,35 @@ void qdict_del(QDict *qdict, const char *key)
 }
 
 /**
+ * qdict_is_equal(): Test whether the two QDicts are equal
+ *
+ * Here, equality means whether they contain the same keys and whether
+ * the respective values are in turn equal (i.e. invoking
+ * qobject_is_equal() on them yields true).
+ */
+bool qdict_is_equal(const QObject *x, const QObject *y)
+{
+    const QDict *dict_x = qobject_to_qdict(x);
+    const QDict *dict_y = qobject_to_qdict(y);
+    const QDictEntry *e;
+
+    if (qdict_size(dict_x) != qdict_size(dict_y)) {
+        return false;
+    }
+
+    for (e = qdict_first(dict_x); e; e = qdict_next(dict_x, e)) {
+        const QObject *obj_x = qdict_entry_value(e);
+        const QObject *obj_y = qdict_get(dict_y, qdict_entry_key(e));
+
+        if (!qobject_is_equal(obj_x, obj_y)) {
+            return false;
+        }
+    }
+
+    return true;
+}
+
+/**
  * qdict_destroy_obj(): Free all the memory allocated by a QDict
  */
 void qdict_destroy_obj(QObject *obj)
qobject/qlist.c +32 -0
index 86b60cb..3ef57d3 100644
@@ -140,6 +140,38 @@ QList *qobject_to_qlist(const QObject *obj)
 }
 
 /**
+ * qlist_is_equal(): Test whether the two QLists are equal
+ *
+ * In order to be considered equal, the respective two objects at each
+ * index of the two lists have to compare equal (regarding
+ * qobject_is_equal()), and both lists have to have the same number of
+ * elements.
+ * That means both lists have to contain equal objects in equal order.
+ */
+bool qlist_is_equal(const QObject *x, const QObject *y)
+{
+    const QList *list_x = qobject_to_qlist(x);
+    const QList *list_y = qobject_to_qlist(y);
+    const QListEntry *entry_x, *entry_y;
+
+    entry_x = qlist_first(list_x);
+    entry_y = qlist_first(list_y);
+
+    while (entry_x && entry_y) {
+        if (!qobject_is_equal(qlist_entry_obj(entry_x),
+                              qlist_entry_obj(entry_y)))
+        {
+            return false;
+        }
+
+        entry_x = qlist_next(entry_x);
+        entry_y = qlist_next(entry_y);
+    }
+
+    return !entry_x && !entry_y;
+}
+
+/**
  * qlist_destroy_obj(): Free all the memory allocated by a QList
  */
 void qlist_destroy_obj(QObject *obj)
qobject/qnull.c +9 -0
index 43918f1..4b9cdbc 100644
@@ -18,3 +18,12 @@ QObject qnull_ = {
     .type = QTYPE_QNULL,
     .refcnt = 1,
 };
+
+/**
+ * qnull_is_equal(): Always return true because any two QNull objects
+ * are equal.
+ */
+bool qnull_is_equal(const QObject *x, const QObject *y)
+{
+    return true;
+}
qobject/qnum.c +53 -0

59 lines not on this page · raw message

qobject/qobject.c +29 -0
index b0cafb6..b2a5360 100644
@@ -27,3 +27,32 @@ void qobject_destroy(QObject *obj)
     assert(QTYPE_QNULL < obj->type && obj->type < QTYPE__MAX);
     qdestroy[obj->type](obj);
 }
+
+
+static bool (*qis_equal[QTYPE__MAX])(const QObject *, const QObject *) = {
+    [QTYPE_NONE] = NULL,               /* No such object exists */
+    [QTYPE_QNULL] = qnull_is_equal,
+    [QTYPE_QNUM] = qnum_is_equal,
+    [QTYPE_QSTRING] = qstring_is_equal,
+    [QTYPE_QDICT] = qdict_is_equal,
+    [QTYPE_QLIST] = qlist_is_equal,
+    [QTYPE_QBOOL] = qbool_is_equal,
+};
+
+bool qobject_is_equal(const QObject *x, const QObject *y)
+{
+    /* We cannot test x == y because an object does not need to be
+     * equal to itself (e.g. NaN floats are not). */
+
+    if (!x && !y) {
+        return true;
+    }
+
+    if (!x || !y || x->type != y->type) {
+        return false;
+    }
+
+    assert(QTYPE_NONE < x->type && x->type < QTYPE__MAX);
+
+    return qis_equal[x->type](x, y);
+}
qobject/qstring.c +9 -0
index 5da7b5f..74182a1 100644
@@ -129,6 +129,15 @@ const char *qstring_get_str(const QString *qstring)
 }
 
 /**
+ * qstring_is_equal(): Test whether the two QStrings are equal
+ */
+bool qstring_is_equal(const QObject *x, const QObject *y)
+{
+    return !strcmp(qobject_to_qstring(x)->string,
+                   qobject_to_qstring(y)->string);
+}
+
+/**
  * qstring_destroy_obj(): Free all memory allocated by a QString
  * object
  */
Signature · 1 line
2.9.4
Reviewed-by on v3 2/5
#4 lore raw

Re: [Qemu-devel] [PATCH v3 2/5] qapi: Add qobject_is_equal()

Quoted 8 lines · Max Reitz

This generic function (along with its implementations for different types) determines whether two QObjects are equal.

---

+++ b/qobject/qnum.c

qobject/qnum.c QNum *qobject_to_qnum(const QObject *obj) · line 260

Quoted 9 lines
qobject/qnum.c +9 -0
+    case QNUM_DOUBLE:
+        switch (num_y->kind) {
+        case QNUM_I64:
+            return qnum_is_equal(y, x);
+        case QNUM_U64:
+            return qnum_is_equal(y, x);
+        case QNUM_DOUBLE:
+            /* Comparison in native double type */
+            return num_x->u.dbl == num_y->u.dbl;

Which returns false if we allow NaN (in general, JSON does not, so I'm not sure if we allow NaN in a QNum)

qobject/qobject.c void qobject_destroy(QObject *obj) · line 45

Quoted 6 lines

+++ b/qobject/qobject.c

qobject/qobject.c +4 -0
+bool qobject_is_equal(const QObject *x, const QObject *y)
+{
+    /* We cannot test x == y because an object does not need to be
+     * equal to itself (e.g. NaN floats are not). */

But this comment matches the code above in QNum.

qobject/qobject.c void qobject_destroy(QObject *obj) · line 53

Quoted 8 lines
qobject/qobject.c +8 -0
+
+    if (!x && !y) {
+        return true;
+    }
+
+    if (!x || !y || x->type != y->type) {
+        return false;
+    }

I like that there is no implicit conversion between bool and int; the only conversions that still allow equality are within the subtypes of QNum.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#5 lore raw

Re: [Qemu-devel] [PATCH v3 2/5] qapi: Add qobject_is_equal()

In reply to Max Reitz
Quoted 4 lines · Max Reitz

This generic function (along with its implementations for different types) determines whether two QObjects are equal.

[...]

Quoted 19 lines
qobject/qnum.c +11 -0
index 476e81c..784d061 100644
@@ -213,6 +213,59 @@ QNum *qobject_to_qnum(const QObject *obj)
 }
 
 /**
+ * qnum_is_equal(): Test whether the two QNums are equal
+ */
+bool qnum_is_equal(const QObject *x, const QObject *y)
+{
+    QNum *num_x = qobject_to_qnum(x);
+    QNum *num_y = qobject_to_qnum(y);
+
+    switch (num_x->kind) {
+    case QNUM_I64:
+        switch (num_y->kind) {
+        case QNUM_I64:

qobject/qnum.c QNum *qobject_to_qnum(const QObject *obj) · line 236

Quoted 10 lines
qobject/qnum.c +10 -0
+            /* Comparison in native int64_t type */
+            return num_x->u.i64 == num_y->u.i64;
+        case QNUM_U64:
+            /* Implicit conversion of x to uin64_t, so we have to
+             * check its sign before */
+            return num_x->u.i64 >= 0 && num_x->u.i64 == num_y->u.u64;
+        case QNUM_DOUBLE:
+            /* Implicit conversion of x to double; no overflow
+             * possible */
+            return num_x->u.i64 == num_y->u.dbl;

Overflow is impossible, but loss of precision is possible:

    (double)9007199254740993ull == 9007199254740992.0

yields true. Is this what we want?

qobject/qnum.c QNum *qobject_to_qnum(const QObject *obj) · line 249

Quoted 13 lines
qobject/qnum.c +13 -0
+        }
+        abort();
+    case QNUM_U64:
+        switch (num_y->kind) {
+        case QNUM_I64:
+            return qnum_is_equal(y, x);
+        case QNUM_U64:
+            /* Comparison in native uint64_t type */
+            return num_x->u.u64 == num_y->u.u64;
+        case QNUM_DOUBLE:
+            /* Implicit conversion of x to double; no overflow
+             * possible */
+            return num_x->u.u64 == num_y->u.dbl;

Similar loss of precision.

Quoted 7 lines
qobject/qnum.c +7 -0
+        }
+        abort();
+    case QNUM_DOUBLE:
+        switch (num_y->kind) {
+        case QNUM_I64:
+            return qnum_is_equal(y, x);
+        case QNUM_U64:

qobject/qnum.c QNum *qobject_to_qnum(const QObject *obj) · line 266

Quoted 10 lines
qobject/qnum.c +10 -0
+            return qnum_is_equal(y, x);
+        case QNUM_DOUBLE:
+            /* Comparison in native double type */
+            return num_x->u.dbl == num_y->u.dbl;
+        }
+        abort();
+    }
+
+    abort();
+}

I think there's more than one sane interpretations of "is equal", including:

* The mathematical numbers represented by @x and @y are equal.

* @x and @y have the same contents, i.e. same kind and u.

* @x and @y are the same object (listed for completeness; we don't need a function to compare pointers).

Your patch implements yet another one. Which one do we want, and why?

The second is easier to implement than the first.

If we really want the first, you need to fix the loss of precision bugs. I guess the obvious fix is

    return (double)x == x && x == y;

Note that this is what you do for mixed signedness: first check @x is exactly representable in @y's type, then compare in @y's type.

Regardless of which one we pick, the function comment needs to explain.

qobject/qnum.c QNum *qobject_to_qnum(const QObject *obj) · line 271

Quoted 5 lines
qobject/qnum.c +2 -0
+
+/**
  * qnum_destroy_obj(): Free all memory allocated by a
  * QNum object
  */

[...]

Remainder of the patch looks good to me.

author
#6 lore raw

Re: [Qemu-devel] [PATCH v3 2/5] qapi: Add qobject_is_equal()

Quoted 39 lines · Markus Armbruster
Quoted 4 lines · Max Reitz

This generic function (along with its implementations for different types) determines whether two QObjects are equal.

[...]

Quoted 19 lines
qobject/qnum.c +11 -0
index 476e81c..784d061 100644
@@ -213,6 +213,59 @@ QNum *qobject_to_qnum(const QObject *obj)
 }
 
 /**
+ * qnum_is_equal(): Test whether the two QNums are equal
+ */
+bool qnum_is_equal(const QObject *x, const QObject *y)
+{
+    QNum *num_x = qobject_to_qnum(x);
+    QNum *num_y = qobject_to_qnum(y);
+
+    switch (num_x->kind) {
+    case QNUM_I64:
+        switch (num_y->kind) {
+        case QNUM_I64:

qobject/qnum.c QNum *qobject_to_qnum(const QObject *obj) · line 236

Quoted 10 lines
qobject/qnum.c +10 -0
+            /* Comparison in native int64_t type */
+            return num_x->u.i64 == num_y->u.i64;
+        case QNUM_U64:
+            /* Implicit conversion of x to uin64_t, so we have to
+             * check its sign before */
+            return num_x->u.i64 >= 0 && num_x->u.i64 == num_y->u.u64;
+        case QNUM_DOUBLE:
+            /* Implicit conversion of x to double; no overflow
+             * possible */
+            return num_x->u.i64 == num_y->u.dbl;

Overflow is impossible, but loss of precision is possible:

Quoted 3 lines
    (double)9007199254740993ull == 9007199254740992.0

yields true. Is this what we want?

I'd argue that yes, because the floating point value represents basically all of the values which are "equal" to it.

But I don't have a string opinion. I guess the alternative would be to convert the double to an integer instead and check for overflows before?

Quoted 37 lines

qobject/qnum.c QNum *qobject_to_qnum(const QObject *obj) · line 249

Quoted 13 lines
qobject/qnum.c +13 -0
+        }
+        abort();
+    case QNUM_U64:
+        switch (num_y->kind) {
+        case QNUM_I64:
+            return qnum_is_equal(y, x);
+        case QNUM_U64:
+            /* Comparison in native uint64_t type */
+            return num_x->u.u64 == num_y->u.u64;
+        case QNUM_DOUBLE:
+            /* Implicit conversion of x to double; no overflow
+             * possible */
+            return num_x->u.u64 == num_y->u.dbl;

Similar loss of precision.

Quoted 7 lines
qobject/qnum.c +7 -0
+        }
+        abort();
+    case QNUM_DOUBLE:
+        switch (num_y->kind) {
+        case QNUM_I64:
+            return qnum_is_equal(y, x);
+        case QNUM_U64:

qobject/qnum.c QNum *qobject_to_qnum(const QObject *obj) · line 266

Quoted 10 lines
qobject/qnum.c +10 -0
+            return qnum_is_equal(y, x);
+        case QNUM_DOUBLE:
+            /* Comparison in native double type */
+            return num_x->u.dbl == num_y->u.dbl;
+        }
+        abort();
+    }
+
+    abort();
+}

I think there's more than one sane interpretations of "is equal", including:

Quoted 8 lines

* The mathematical numbers represented by @x and @y are equal.

* @x and @y have the same contents, i.e. same kind and u.

* @x and @y are the same object (listed for completeness; we don't need a function to compare pointers).

Your patch implements yet another one. Which one do we want, and why?

Mine is the first one, except that I think that a floating point value does not represent a single number but just some number in a range.

Quoted 1 line

The second is easier to implement than the first.

It seems much less useful, though.

Quoted 1 line

If we really want the first, you need to fix the loss of precision bugs.

I'm not sure, but I don't mind either, so...

Quoted 3 lines

I guess the obvious fix is

    return (double)x == x && x == y;

Yes, that would do, too; and spares me of having to think about how well comparing an arbitrary double to UINT64_MAX actually works. :-)

Quoted 4 lines

Note that this is what you do for mixed signedness: first check @x is exactly representable in @y's type, then compare in @y's type.

Regardless of which one we pick, the function comment needs to explain.

OK, will do.

Max

Quoted 9 lines

qobject/qnum.c QNum *qobject_to_qnum(const QObject *obj) · line 271

Quoted 5 lines
qobject/qnum.c +2 -0
+
+/**
  * qnum_destroy_obj(): Free all memory allocated by a
  * QNum object
  */

[...]

Remainder of the patch looks good to me.

#7 lore raw

Re: [Qemu-devel] [PATCH v3 2/5] qapi: Add qobject_is_equal()

Quoted 25 lines · Max Reitz
Quoted 25 lines
Quoted 12 lines

/**
+ * qnum_is_equal(): Test whether the two QNums are equal
+ */

qobject/qnum.c +9 -0
+bool qnum_is_equal(const QObject *x, const QObject *y)
+{
+    QNum *num_x = qobject_to_qnum(x);
+    QNum *num_y = qobject_to_qnum(y);
+
+    switch (num_x->kind) {
+    case QNUM_I64:
+        switch (num_y->kind) {
+        case QNUM_I64:

qobject/qnum.c QNum *qobject_to_qnum(const QObject *obj) · line 236

Quoted 10 lines
qobject/qnum.c +10 -0
+            /* Comparison in native int64_t type */
+            return num_x->u.i64 == num_y->u.i64;
+        case QNUM_U64:
+            /* Implicit conversion of x to uin64_t, so we have to
+             * check its sign before */
+            return num_x->u.i64 >= 0 && num_x->u.i64 == num_y->u.u64;
+        case QNUM_DOUBLE:
+            /* Implicit conversion of x to double; no overflow
+             * possible */
+            return num_x->u.i64 == num_y->u.dbl;

Overflow is impossible, but loss of precision is possible:

Quoted 6 lines
Quoted 3 lines
    (double)9007199254740993ull == 9007199254740992.0

yields true. Is this what we want?

I'd argue that yes, because the floating point value represents basically all of the values which are "equal" to it.

But the problem is that we CAN represent the fully-precise number as an integer, so having multiple distinct integers that compare differently against each other, but equal to the same double, is awkward.

Quoted 3 lines

But I don't have a string opinion. I guess the alternative would be to convert the double to an integer instead and check for overflows before?

That's the solution Markus gave, and I'm in favor of the tighter check:

Quoted 7 lines
Quoted 4 lines

I guess the obvious fix is

    return (double)x == x && x == y;

Yes, that would do, too; and spares me of having to think about how well comparing an arbitrary double to UINT64_MAX actually works. :-)

It basically says that we are unwilling to declare an integer equivalent to the double if the double loses precision when trying to store the integer.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#8 lore raw

Re: [Qemu-devel] [PATCH v3 2/5] qapi: Add qobject_is_equal()

Quoted 28 lines · Eric Blake
Quoted 27 lines · Max Reitz
Quoted 27 lines
Quoted 12 lines

/**
+ * qnum_is_equal(): Test whether the two QNums are equal
+ */

qobject/qnum.c +9 -0
+bool qnum_is_equal(const QObject *x, const QObject *y)
+{
+    QNum *num_x = qobject_to_qnum(x);
+    QNum *num_y = qobject_to_qnum(y);
+
+    switch (num_x->kind) {
+    case QNUM_I64:
+        switch (num_y->kind) {
+        case QNUM_I64:

qobject/qnum.c QNum *qobject_to_qnum(const QObject *obj) · line 236

Quoted 10 lines
qobject/qnum.c +10 -0
+            /* Comparison in native int64_t type */
+            return num_x->u.i64 == num_y->u.i64;
+        case QNUM_U64:
+            /* Implicit conversion of x to uin64_t, so we have to
+             * check its sign before */
+            return num_x->u.i64 >= 0 && num_x->u.i64 == num_y->u.u64;
+        case QNUM_DOUBLE:
+            /* Implicit conversion of x to double; no overflow
+             * possible */
+            return num_x->u.i64 == num_y->u.dbl;

Overflow is impossible, but loss of precision is possible:

    (double)9007199254740993ull == 9007199254740992.0
Quoted 8 lines
Quoted 4 lines
Quoted 1 line

yields true. Is this what we want?

I'd argue that yes, because the floating point value represents basically all of the values which are "equal" to it.

But the problem is that we CAN represent the fully-precise number as an integer, so having multiple distinct integers that compare differently against each other, but equal to the same double, is awkward.

Yup.

Quoted 5 lines
Quoted 2 lines

But I don't have a string opinion. I guess the alternative would be to convert the double to an integer instead and check for overflows before?

That's the solution Markus gave, and I'm in favor of the tighter check:

[...]

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

I think there's more than one sane interpretations of "is equal", including:

* The mathematical numbers represented by @x and @y are equal.

* @x and @y have the same contents, i.e. same kind and u.

* @x and @y are the same object (listed for completeness; we don't need a function to compare pointers).

Quoted 8 lines
Quoted 8 lines
Quoted 1 line

Your patch implements yet another one. Which one do we want, and why?

Mine is the first one, except that I think that a floating point value does not represent a single number but just some number in a range.

Quoted 1 line

The second is easier to implement than the first.

It seems much less useful, though.

Depends on what for.

Common Lisp has

* eq: same object
* eql: eq, or both numbers with same type and value, or both characters that represent the same character
* =: mathematically equal (arguments must be numbers)
* equal: like eql, but it recursively descends into lists (I'm simplifying)

Decades of use back the assertion that eq, eql and = are all useful.

Quoted 3 lines
Quoted 3 lines
Quoted 1 line

If we really want the first, you need to fix the loss of precision bugs.

I'm not sure, but I don't mind either, so...

For what it's worth, your v2 had QInt compare not equal to QFloat. Makes me suspect that eql is good enough for the problem at hand.

Quoted 15 lines
Quoted 6 lines
Quoted 3 lines

I guess the obvious fix is

    return (double)x == x && x == y;

Yes, that would do, too; and spares me of having to think about how well comparing an arbitrary double to UINT64_MAX actually works. :-)

It basically says that we are unwilling to declare an integer equivalent to the double if the double loses precision when trying to store the integer.

Quoted 4 lines
Quoted 4 lines

Note that this is what you do for mixed signedness: first check @x is exactly representable in @y's type, then compare in @y's type.

Regardless of which one we pick, the function comment needs to explain.

author
#9 lore raw

Re: [Qemu-devel] [PATCH v3 2/5] qapi: Add qobject_is_equal()

In reply to Max Reitz
Quoted 105 lines · Max Reitz
Quoted 39 lines · Markus Armbruster
Quoted 4 lines · Max Reitz

This generic function (along with its implementations for different types) determines whether two QObjects are equal.

[...]

Quoted 19 lines
qobject/qnum.c +11 -0
index 476e81c..784d061 100644
@@ -213,6 +213,59 @@ QNum *qobject_to_qnum(const QObject *obj)
 }
 
 /**
+ * qnum_is_equal(): Test whether the two QNums are equal
+ */
+bool qnum_is_equal(const QObject *x, const QObject *y)
+{
+    QNum *num_x = qobject_to_qnum(x);
+    QNum *num_y = qobject_to_qnum(y);
+
+    switch (num_x->kind) {
+    case QNUM_I64:
+        switch (num_y->kind) {
+        case QNUM_I64:

qobject/qnum.c QNum *qobject_to_qnum(const QObject *obj) · line 236

Quoted 10 lines
qobject/qnum.c +10 -0
+            /* Comparison in native int64_t type */
+            return num_x->u.i64 == num_y->u.i64;
+        case QNUM_U64:
+            /* Implicit conversion of x to uin64_t, so we have to
+             * check its sign before */
+            return num_x->u.i64 >= 0 && num_x->u.i64 == num_y->u.u64;
+        case QNUM_DOUBLE:
+            /* Implicit conversion of x to double; no overflow
+             * possible */
+            return num_x->u.i64 == num_y->u.dbl;

Overflow is impossible, but loss of precision is possible:

Quoted 3 lines
    (double)9007199254740993ull == 9007199254740992.0

yields true. Is this what we want?

I'd argue that yes, because the floating point value represents basically all of the values which are "equal" to it.

But I don't have a string opinion. I guess the alternative would be to convert the double to an integer instead and check for overflows before?

Quoted 37 lines

qobject/qnum.c QNum *qobject_to_qnum(const QObject *obj) · line 249

Quoted 13 lines
qobject/qnum.c +13 -0
+        }
+        abort();
+    case QNUM_U64:
+        switch (num_y->kind) {
+        case QNUM_I64:
+            return qnum_is_equal(y, x);
+        case QNUM_U64:
+            /* Comparison in native uint64_t type */
+            return num_x->u.u64 == num_y->u.u64;
+        case QNUM_DOUBLE:
+            /* Implicit conversion of x to double; no overflow
+             * possible */
+            return num_x->u.u64 == num_y->u.dbl;

Similar loss of precision.

Quoted 7 lines
qobject/qnum.c +7 -0
+        }
+        abort();
+    case QNUM_DOUBLE:
+        switch (num_y->kind) {
+        case QNUM_I64:
+            return qnum_is_equal(y, x);
+        case QNUM_U64:

qobject/qnum.c QNum *qobject_to_qnum(const QObject *obj) · line 266

Quoted 10 lines
qobject/qnum.c +10 -0
+            return qnum_is_equal(y, x);
+        case QNUM_DOUBLE:
+            /* Comparison in native double type */
+            return num_x->u.dbl == num_y->u.dbl;
+        }
+        abort();
+    }
+
+    abort();
+}

I think there's more than one sane interpretations of "is equal", including:

Quoted 8 lines

* The mathematical numbers represented by @x and @y are equal.

* @x and @y have the same contents, i.e. same kind and u.

* @x and @y are the same object (listed for completeness; we don't need a function to compare pointers).

Your patch implements yet another one. Which one do we want, and why?

Mine is the first one, except that I think that a floating point value does not represent a single number but just some number in a range.

Quoted 1 line

The second is easier to implement than the first.

It seems much less useful, though.

Quoted 1 line

If we really want the first, you need to fix the loss of precision bugs.

Quoted 8 lines

I'm not sure, but I don't mind either, so...

Quoted 3 lines

I guess the obvious fix is

    return (double)x == x && x == y;

Yes, that would do, too; and spares me of having to think about how well comparing an arbitrary double to UINT64_MAX actually works. :-)

On second thought, this won't do, because (double)x == x is always true if x is an integer (because this will implicitly cast the second x to a double, too). However, (uint64_t)(double)x == x should work.

Max

Quoted 21 lines
Quoted 5 lines

Note that this is what you do for mixed signedness: first check @x is exactly representable in @y's type, then compare in @y's type.

Regardless of which one we pick, the function comment needs to explain.

OK, will do.

Max

Quoted 9 lines

qobject/qnum.c QNum *qobject_to_qnum(const QObject *obj) · line 271

Quoted 5 lines
qobject/qnum.c +2 -0
+
+/**
  * qnum_destroy_obj(): Free all memory allocated by a
  * QNum object
  */

[...]

Remainder of the patch looks good to me.

author
#10 lore raw

Re: [Qemu-devel] [PATCH v3 2/5] qapi: Add qobject_is_equal()

Quoted 110 lines · Max Reitz
Quoted 109 lines · Max Reitz
Quoted 39 lines · Markus Armbruster
Quoted 4 lines · Max Reitz

This generic function (along with its implementations for different types) determines whether two QObjects are equal.

[...]

Quoted 19 lines
qobject/qnum.c +11 -0
index 476e81c..784d061 100644
@@ -213,6 +213,59 @@ QNum *qobject_to_qnum(const QObject *obj)
 }
 
 /**
+ * qnum_is_equal(): Test whether the two QNums are equal
+ */
+bool qnum_is_equal(const QObject *x, const QObject *y)
+{
+    QNum *num_x = qobject_to_qnum(x);
+    QNum *num_y = qobject_to_qnum(y);
+
+    switch (num_x->kind) {
+    case QNUM_I64:
+        switch (num_y->kind) {
+        case QNUM_I64:

qobject/qnum.c QNum *qobject_to_qnum(const QObject *obj) · line 236

Quoted 10 lines
qobject/qnum.c +10 -0
+            /* Comparison in native int64_t type */
+            return num_x->u.i64 == num_y->u.i64;
+        case QNUM_U64:
+            /* Implicit conversion of x to uin64_t, so we have to
+             * check its sign before */
+            return num_x->u.i64 >= 0 && num_x->u.i64 == num_y->u.u64;
+        case QNUM_DOUBLE:
+            /* Implicit conversion of x to double; no overflow
+             * possible */
+            return num_x->u.i64 == num_y->u.dbl;

Overflow is impossible, but loss of precision is possible:

Quoted 3 lines
    (double)9007199254740993ull == 9007199254740992.0

yields true. Is this what we want?

I'd argue that yes, because the floating point value represents basically all of the values which are "equal" to it.

But I don't have a string opinion. I guess the alternative would be to convert the double to an integer instead and check for overflows before?

Quoted 37 lines

qobject/qnum.c QNum *qobject_to_qnum(const QObject *obj) · line 249

Quoted 13 lines
qobject/qnum.c +13 -0
+        }
+        abort();
+    case QNUM_U64:
+        switch (num_y->kind) {
+        case QNUM_I64:
+            return qnum_is_equal(y, x);
+        case QNUM_U64:
+            /* Comparison in native uint64_t type */
+            return num_x->u.u64 == num_y->u.u64;
+        case QNUM_DOUBLE:
+            /* Implicit conversion of x to double; no overflow
+             * possible */
+            return num_x->u.u64 == num_y->u.dbl;

Similar loss of precision.

Quoted 7 lines
qobject/qnum.c +7 -0
+        }
+        abort();
+    case QNUM_DOUBLE:
+        switch (num_y->kind) {
+        case QNUM_I64:
+            return qnum_is_equal(y, x);
+        case QNUM_U64:

qobject/qnum.c QNum *qobject_to_qnum(const QObject *obj) · line 266

Quoted 10 lines
qobject/qnum.c +10 -0
+            return qnum_is_equal(y, x);
+        case QNUM_DOUBLE:
+            /* Comparison in native double type */
+            return num_x->u.dbl == num_y->u.dbl;
+        }
+        abort();
+    }
+
+    abort();
+}

I think there's more than one sane interpretations of "is equal", including:

Quoted 8 lines

* The mathematical numbers represented by @x and @y are equal.

* @x and @y have the same contents, i.e. same kind and u.

* @x and @y are the same object (listed for completeness; we don't need a function to compare pointers).

Your patch implements yet another one. Which one do we want, and why?

Mine is the first one, except that I think that a floating point value does not represent a single number but just some number in a range.

Quoted 1 line

The second is easier to implement than the first.

It seems much less useful, though.

Quoted 1 line

If we really want the first, you need to fix the loss of precision bugs.

I'm not sure, but I don't mind either, so...

Quoted 2 lines

I guess the obvious fix is

Quoted 8 lines
Quoted 4 lines
Quoted 1 line
    return (double)x == x && x == y;

Yes, that would do, too; and spares me of having to think about how well comparing an arbitrary double to UINT64_MAX actually works. :-)

On second thought, this won't do, because (double)x == x is always true if x is an integer (because this will implicitly cast the second x to a double, too). However, (uint64_t)(double)x == x should work.

Hm, well, the nice thing with this is that (double)UINT64_MAX is actually UINT64_MAX + 1, and now (uint64_t)(UINT64_MAX + 1) is undefined... Urgs.

So I guess one thing that isn't very obvious but that should *always* work (and is always well-defined) is this:

For uint64_t: y < 0x1p64 && (uint64_t)y == x

For int64_t: y >= -0x1p63 && y < 0x1p63 && (int64_t)y == x

I hope. :-/

(But finally a chance to use binary exponents! Yay!)

Max

#11 lore raw

Re: [Qemu-devel] [PATCH v3 2/5] qapi: Add qobject_is_equal()

Quoted 5 lines · Max Reitz
Quoted 5 lines
Quoted 4 lines
Quoted 1 line
    return (double)x == x && x == y;

Yes, that would do, too; and spares me of having to think about how well comparing an arbitrary double to UINT64_MAX actually works. :-)

Quoted 7 lines
Quoted 3 lines

On second thought, this won't do, because (double)x == x is always true if x is an integer (because this will implicitly cast the second x to a double, too). However, (uint64_t)(double)x == x should work.

Hm, well, the nice thing with this is that (double)UINT64_MAX is actually UINT64_MAX + 1, and now (uint64_t)(UINT64_MAX + 1) is undefined... Urgs.

(uint64_t)(UINT64_MAX + 1) is well-defined - it is 0.

(Adding in unsigned integers is always well-defined - it wraps around on mathematical overflow modulo the integer size. You're thinking of overflow addition on signed integers, which is indeed undefined)

Quoted 7 lines

So I guess one thing that isn't very obvious but that should *always* work (and is always well-defined) is this:

For uint64_t: y < 0x1p64 && (uint64_t)y == x

For int64_t: y >= -0x1p63 && y < 0x1p63 && (int64_t)y == x

That's harder to read, compared to the double-cast method which is well-defined after all.

Quoted 4 lines

I hope. :-/

(But finally a chance to use binary exponents! Yay!)

Justifying the use of binary exponents is going to be harder than that, and would need a really good comment in the code, compared to just using a safe double-cast.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
author
#12 lore raw

Re: [Qemu-devel] [PATCH v3 2/5] qapi: Add qobject_is_equal()

Quoted 15 lines · Eric Blake
Quoted 5 lines · Max Reitz
Quoted 5 lines
Quoted 4 lines
Quoted 1 line
    return (double)x == x && x == y;

Yes, that would do, too; and spares me of having to think about how well comparing an arbitrary double to UINT64_MAX actually works. :-)

Quoted 7 lines
Quoted 3 lines

On second thought, this won't do, because (double)x == x is always true if x is an integer (because this will implicitly cast the second x to a double, too). However, (uint64_t)(double)x == x should work.

Hm, well, the nice thing with this is that (double)UINT64_MAX is actually UINT64_MAX + 1, and now (uint64_t)(UINT64_MAX + 1) is undefined... Urgs.

Quoted 5 lines

(uint64_t)(UINT64_MAX + 1) is well-defined - it is 0.

(Adding in unsigned integers is always well-defined - it wraps around on mathematical overflow modulo the integer size. You're thinking of overflow addition on signed integers, which is indeed undefined)

It's not. See the standard:

When a finite value of real floating type is converted to an integer type other than _Bool, the fractional part is discarded (i.e., the value is truncated toward zero). If the value of the integral part cannot be represented by the integer type, the behavior is undefined. [61]

[61] The remaindering operation performed when a value of integer type is converted to unsigned type need not be performed when a value of real floating type is converted to unsigned type. Thus, the range of portable real floating values is (−1, Utype_MAX+1).

See for yourself:

printf("%i\n", (uint64_t)(double)UINT64_MAX == UINT64_MAX);

This yields 1 with gcc and -O3.

Quoted 12 lines
Quoted 7 lines

So I guess one thing that isn't very obvious but that should *always* work (and is always well-defined) is this:

For uint64_t: y < 0x1p64 && (uint64_t)y == x

For int64_t: y >= -0x1p63 && y < 0x1p63 && (int64_t)y == x

That's harder to read, compared to the double-cast method which is well-defined after all.

Quoted 1 line
Quoted 7 lines
Quoted 3 lines

I hope. :-/

(But finally a chance to use binary exponents! Yay!)

Justifying the use of binary exponents is going to be harder than that, and would need a really good comment in the code, compared to just using a safe double-cast.

It's not safe.

Max

author
#13 lore raw

Re: [Qemu-devel] [PATCH v3 2/5] qapi: Add qobject_is_equal()

Quoted 16 lines · Max Reitz
Quoted 15 lines · Eric Blake
Quoted 12 lines · Max Reitz
Quoted 8 lines
Quoted 4 lines
Quoted 1 line
    return (double)x == x && x == y;

Yes, that would do, too; and spares me of having to think about how well comparing an arbitrary double to UINT64_MAX actually works. :-)

On second thought, this won't do, because (double)x == x is always true if x is an integer (because this will implicitly cast the second x to a double, too). However, (uint64_t)(double)x == x should work.

Hm, well, the nice thing with this is that (double)UINT64_MAX is actually UINT64_MAX + 1, and now (uint64_t)(UINT64_MAX + 1) is undefined... Urgs.

Quoted 7 lines
Quoted 5 lines

(uint64_t)(UINT64_MAX + 1) is well-defined - it is 0.

(Adding in unsigned integers is always well-defined - it wraps around on mathematical overflow modulo the integer size. You're thinking of overflow addition on signed integers, which is indeed undefined)

It's not. See the standard:

Or, well, yes, it is in this case, but I meant literally UINT64_MAX + 1, not the uint64_t value. I meant the natural number 2^64.

Because the issue is that (double)UINT64_MAX will (or may, depending on the environment and such) give us 2.0^64 == 0x1p64.

And this is where the quotation I gave below comes in. When casting an integer to a double we may end up with a value that is not representable in the original integer type, so we cannot easily cast it back.

Max

Quoted 40 lines

When a finite value of real floating type is converted to an integer type other than _Bool, the fractional part is discarded (i.e., the value is truncated toward zero). If the value of the integral part cannot be represented by the integer type, the behavior is undefined. [61]

[61] The remaindering operation performed when a value of integer type is converted to unsigned type need not be performed when a value of real floating type is converted to unsigned type. Thus, the range of portable real floating values is (−1, Utype_MAX+1).

See for yourself:

printf("%i\n", (uint64_t)(double)UINT64_MAX == UINT64_MAX);

This yields 1 with gcc and -O3.

Quoted 12 lines
Quoted 7 lines

So I guess one thing that isn't very obvious but that should *always* work (and is always well-defined) is this:

For uint64_t: y < 0x1p64 && (uint64_t)y == x

For int64_t: y >= -0x1p63 && y < 0x1p63 && (int64_t)y == x

That's harder to read, compared to the double-cast method which is well-defined after all.

Quoted 1 line
Quoted 7 lines
Quoted 3 lines

I hope. :-/

(But finally a chance to use binary exponents! Yay!)

Justifying the use of binary exponents is going to be harder than that, and would need a really good comment in the code, compared to just using a safe double-cast.

It's not safe.

Max

#14 lore raw

Re: [Qemu-devel] [PATCH v3 2/5] qapi: Add qobject_is_equal()

Quoted 9 lines · Max Reitz

Or, well, yes, it is in this case, but I meant literally UINT64_MAX + 1, not the uint64_t value. I meant the natural number 2^64.

Because the issue is that (double)UINT64_MAX will (or may, depending on the environment and such) give us 2.0^64 == 0x1p64.

And this is where the quotation I gave below comes in. When casting an integer to a double we may end up with a value that is not representable in the original integer type, so we cannot easily cast it back.

And it's more than just UINT64_MAX that rounds to the double value of 0x1p64. Okay, I'm starting to be convinced that using a floating-point comparison to 0x1p64 is going to be easier than an integer comparison to the rounding point (what point is that, anyways: where the low-order 11 bits that don't fit in 53 bits of precision cause us to round up instead of down?)

Quoted 10 lines
Quoted 8 lines
Quoted 3 lines

Justifying the use of binary exponents is going to be harder than that, and would need a really good comment in the code, compared to just using a safe double-cast.

It's not safe.

Max

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#15 lore raw

Re: [Qemu-devel] [PATCH v3 2/5] qapi: Add qobject_is_equal()

In reply to Max Reitz
Quoted 9 lines · Max Reitz
Quoted 5 lines

(uint64_t)(UINT64_MAX + 1) is well-defined - it is 0.

(Adding in unsigned integers is always well-defined - it wraps around on mathematical overflow modulo the integer size. You're thinking of overflow addition on signed integers, which is indeed undefined)

It's not. See the standard:

When a finite value of real floating type is converted to an integer

Ah, you're talking:

(uint64_t)(double)(UINT64_MAX + 1), which is indeed undefined.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
author
#16 lore raw

Re: [Qemu-devel] [PATCH v3 2/5] qapi: Add qobject_is_equal()

In reply to Max Reitz
Quoted 120 lines · Max Reitz
Quoted 110 lines · Max Reitz
Quoted 109 lines · Max Reitz
Quoted 39 lines · Markus Armbruster
Quoted 4 lines · Max Reitz

This generic function (along with its implementations for different types) determines whether two QObjects are equal.

[...]

Quoted 19 lines
qobject/qnum.c +11 -0
index 476e81c..784d061 100644
@@ -213,6 +213,59 @@ QNum *qobject_to_qnum(const QObject *obj)
 }
 
 /**
+ * qnum_is_equal(): Test whether the two QNums are equal
+ */
+bool qnum_is_equal(const QObject *x, const QObject *y)
+{
+    QNum *num_x = qobject_to_qnum(x);
+    QNum *num_y = qobject_to_qnum(y);
+
+    switch (num_x->kind) {
+    case QNUM_I64:
+        switch (num_y->kind) {
+        case QNUM_I64:

qobject/qnum.c QNum *qobject_to_qnum(const QObject *obj) · line 236

Quoted 10 lines
qobject/qnum.c +10 -0
+            /* Comparison in native int64_t type */
+            return num_x->u.i64 == num_y->u.i64;
+        case QNUM_U64:
+            /* Implicit conversion of x to uin64_t, so we have to
+             * check its sign before */
+            return num_x->u.i64 >= 0 && num_x->u.i64 == num_y->u.u64;
+        case QNUM_DOUBLE:
+            /* Implicit conversion of x to double; no overflow
+             * possible */
+            return num_x->u.i64 == num_y->u.dbl;

Overflow is impossible, but loss of precision is possible:

Quoted 3 lines
    (double)9007199254740993ull == 9007199254740992.0

yields true. Is this what we want?

I'd argue that yes, because the floating point value represents basically all of the values which are "equal" to it.

But I don't have a string opinion. I guess the alternative would be to convert the double to an integer instead and check for overflows before?

Quoted 37 lines

qobject/qnum.c QNum *qobject_to_qnum(const QObject *obj) · line 249

Quoted 13 lines
qobject/qnum.c +13 -0
+        }
+        abort();
+    case QNUM_U64:
+        switch (num_y->kind) {
+        case QNUM_I64:
+            return qnum_is_equal(y, x);
+        case QNUM_U64:
+            /* Comparison in native uint64_t type */
+            return num_x->u.u64 == num_y->u.u64;
+        case QNUM_DOUBLE:
+            /* Implicit conversion of x to double; no overflow
+             * possible */
+            return num_x->u.u64 == num_y->u.dbl;

Similar loss of precision.

Quoted 7 lines
qobject/qnum.c +7 -0
+        }
+        abort();
+    case QNUM_DOUBLE:
+        switch (num_y->kind) {
+        case QNUM_I64:
+            return qnum_is_equal(y, x);
+        case QNUM_U64:

qobject/qnum.c QNum *qobject_to_qnum(const QObject *obj) · line 266

Quoted 10 lines
qobject/qnum.c +10 -0
+            return qnum_is_equal(y, x);
+        case QNUM_DOUBLE:
+            /* Comparison in native double type */
+            return num_x->u.dbl == num_y->u.dbl;
+        }
+        abort();
+    }
+
+    abort();
+}

I think there's more than one sane interpretations of "is equal", including:

Quoted 8 lines

* The mathematical numbers represented by @x and @y are equal.

* @x and @y have the same contents, i.e. same kind and u.

* @x and @y are the same object (listed for completeness; we don't need a function to compare pointers).

Your patch implements yet another one. Which one do we want, and why?

Mine is the first one, except that I think that a floating point value does not represent a single number but just some number in a range.

Quoted 1 line

The second is easier to implement than the first.

It seems much less useful, though.

Quoted 1 line

If we really want the first, you need to fix the loss of precision bugs.

I'm not sure, but I don't mind either, so...

Quoted 2 lines

I guess the obvious fix is

Quoted 8 lines
Quoted 4 lines
Quoted 1 line
    return (double)x == x && x == y;

Yes, that would do, too; and spares me of having to think about how well comparing an arbitrary double to UINT64_MAX actually works. :-)

On second thought, this won't do, because (double)x == x is always true if x is an integer (because this will implicitly cast the second x to a double, too). However, (uint64_t)(double)x == x should work.

Quoted 8 lines

Hm, well, the nice thing with this is that (double)UINT64_MAX is actually UINT64_MAX + 1, and now (uint64_t)(UINT64_MAX + 1) is undefined... Urgs.

So I guess one thing that isn't very obvious but that should *always* work (and is always well-defined) is this:

For uint64_t: y < 0x1p64 && (uint64_t)y == x

Here comes iteration number 4: Forgot the y >= 0 check.

Quoted 1 line

For int64_t: y >= -0x1p63 && y < 0x1p63 && (int64_t)y == x

Also, I should check that the fractional part of y is 0 (through modf(y, &_) == 0.0).

Floating point numbers are so much fun!

(And all of this gives me such great ideas for tests to add to patch 5!)

Max

Quoted 6 lines

I hope. :-/

(But finally a chance to use binary exponents! Yay!)

Max

author
#17 lore raw

PATCH v3 3/5 block: qobject_is_equal() in bdrv_reopen_prepare()

In reply to Max Reitz

Currently, bdrv_reopen_prepare() assumes that all BDS options are strings. However, this is not the case if the BDS has been created through the json: pseudo-protocol or blockdev-add.

Note that the user-invokable reopen command is an HMP command, so you can only specify strings there. Therefore, specifying a non-string option with the "same" value as it was when originally created will now return an error because the values are supposedly similar (and there is no way for the user to circumvent this but to just not specify the option again -- however, this is still strictly better than just crashing).

---
 block.c | 31 ++++++++++++++++++-------------
 1 file changed, 18 insertions(+), 13 deletions(-)
block.c +18 -13
index 913bb43..45eb248 100644
@@ -2947,19 +2947,24 @@ int bdrv_reopen_prepare(BDRVReopenState *reopen_state, BlockReopenQueue *queue,
         const QDictEntry *entry = qdict_first(reopen_state->options);
 
         do {
-            QString *new_obj = qobject_to_qstring(entry->value);
-            const char *new = qstring_get_str(new_obj);
-            /*
-             * Caution: while qdict_get_try_str() is fine, getting
-             * non-string types would require more care.  When
-             * bs->options come from -blockdev or blockdev_add, its
-             * members are typed according to the QAPI schema, but
-             * when they come from -drive, they're all QString.
-             */
-            const char *old = qdict_get_try_str(reopen_state->bs->options,
-                                                entry->key);
-
-            if (!old || strcmp(new, old)) {
+            QObject *new = entry->value;
+            QObject *old = qdict_get(reopen_state->bs->options, entry->key);
+
+            /* TODO: When using -drive to specify blockdev options, all values
+             * will be strings; however, when using -blockdev, blockdev-add or
+             * filenames using the json:{} pseudo-protocol, they will be
+             * correctly typed.
+             * In contrast, reopening options are (currently) always strings
+             * (because you can only specify them through qemu-io; all other
+             * callers do not specify any options).
+             * Therefore, when using anything other than -drive to create a BDS,
+             * this cannot detect non-string options as unchanged, because
+             * qobject_is_equal() always returns false for objects of different
+             * type.  In the future, this should be remedied by correctly typing
+             * all options.  For now, this is not too big of an issue because
+             * the user simply can not specify options which cannot be changed
+             * anyway, so they will stay unchanged. */
+            if (!qobject_is_equal(new, old)) {
                 error_setg(errp, "Cannot change the option '%s'", entry->key);
                 ret = -EINVAL;
                 goto error;
Signature · 1 line
2.9.4
Reviewed-by on v3 3/5
#18 lore raw

Re: [Qemu-devel] [PATCH v3 3/5] block: qobject_is_equal() in bdrv_reopen_prepare()

Quoted 5 lines · Max Reitz

Currently, bdrv_reopen_prepare() assumes that all BDS options are strings. However, this is not the case if the BDS has been created through the json: pseudo-protocol or blockdev-add.

Note that the user-invokable reopen command is an HMP command, so you

s/invokable/invocable/

Quoted 16 lines

can only specify strings there. Therefore, specifying a non-string option with the "same" value as it was when originally created will now return an error because the values are supposedly similar (and there is no way for the user to circumvent this but to just not specify the option again -- however, this is still strictly better than just crashing).

---
 block.c | 31 ++++++++++++++++++-------------
 1 file changed, 18 insertions(+), 13 deletions(-)
block.c +3 -0
+            /* TODO: When using -drive to specify blockdev options, all values
+             * will be strings; however, when using -blockdev, blockdev-add or
+             * filenames using the json:{} pseudo-protocol, they will be

block.c int bdrv_reopen_prepare(BDRVReopenState *reopen_state, BlockReopenQueue *queue, · line 2965

Quoted 10 lines
block.c +10 -0
+             * correctly typed.
+             * In contrast, reopening options are (currently) always strings
+             * (because you can only specify them through qemu-io; all other
+             * callers do not specify any options).
+             * Therefore, when using anything other than -drive to create a BDS,
+             * this cannot detect non-string options as unchanged, because
+             * qobject_is_equal() always returns false for objects of different
+             * type.  In the future, this should be remedied by correctly typing
+             * all options.  For now, this is not too big of an issue because
+             * the user simply can not specify options which cannot be changed

Seeing "can not" usually looks wrong for "cannot"; but here, it is grammatically correct. But better would be "the user can simply not specify" or "the user can simply avoid specifying options".

Quoted 1 line

+ * anyway, so they will stay unchanged. */

The grammar tweak is minor, so:

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
author
#19 lore raw

Re: [Qemu-devel] [PATCH v3 3/5] block: qobject_is_equal() in bdrv_reopen_prepare()

Quoted 8 lines · Eric Blake
Quoted 5 lines · Max Reitz

Currently, bdrv_reopen_prepare() assumes that all BDS options are strings. However, this is not the case if the BDS has been created through the json: pseudo-protocol or blockdev-add.

Note that the user-invokable reopen command is an HMP command, so you

s/invokable/invocable/

I was asking this myself when writing this commit message and actually consulted Wiktionary: https://en.wiktionary.org/wiki/invokable

Quoted 27 lines
Quoted 16 lines

can only specify strings there. Therefore, specifying a non-string option with the "same" value as it was when originally created will now return an error because the values are supposedly similar (and there is no way for the user to circumvent this but to just not specify the option again -- however, this is still strictly better than just crashing).

---
 block.c | 31 ++++++++++++++++++-------------
 1 file changed, 18 insertions(+), 13 deletions(-)
block.c +3 -0
+            /* TODO: When using -drive to specify blockdev options, all values
+             * will be strings; however, when using -blockdev, blockdev-add or
+             * filenames using the json:{} pseudo-protocol, they will be

block.c int bdrv_reopen_prepare(BDRVReopenState *reopen_state, BlockReopenQueue *queue, · line 2965

Quoted 10 lines
block.c +10 -0
+             * correctly typed.
+             * In contrast, reopening options are (currently) always strings
+             * (because you can only specify them through qemu-io; all other
+             * callers do not specify any options).
+             * Therefore, when using anything other than -drive to create a BDS,
+             * this cannot detect non-string options as unchanged, because
+             * qobject_is_equal() always returns false for objects of different
+             * type.  In the future, this should be remedied by correctly typing
+             * all options.  For now, this is not too big of an issue because
+             * the user simply can not specify options which cannot be changed
Quoted 3 lines

Seeing "can not" usually looks wrong for "cannot"; but here, it is grammatically correct. But better would be "the user can simply not specify" or "the user can simply avoid specifying options".

Awww, I deliberately designed the sentence so I could use "can not". :-)

(and even contrast it with the following "cannot"!)

Quoted 4 lines
Quoted 1 line

+ * anyway, so they will stay unchanged. */

The grammar tweak is minor, so:

Please let me keep it :-)

Max

#20 lore raw

Re: [Qemu-devel] [PATCH v3 3/5] block: qobject_is_equal() in bdrv_reopen_prepare()

Quoted 6 lines · Max Reitz
Quoted 5 lines · Eric Blake
Quoted 4 lines · Max Reitz

Currently, bdrv_reopen_prepare() assumes that all BDS options are strings. However, this is not the case if the BDS has been created through the json: pseudo-protocol or blockdev-add.

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Note that the user-invokable reopen command is an HMP command, so you

s/invokable/invocable/

I was asking this myself when writing this commit message and actually consulted Wiktionary: https://en.wiktionary.org/wiki/invokable

My intuitive reasoning for favoring 'c' is that we spell it "invocation", not "invokation", even if the short form is "invoke".

But I also consulted dictionary.com before writing my original reply:
http://www.dictionary.com/browse/invocable
which pulls up "invoke" as the primary, and only lists "invocable" (and not "invokable") as the related form adjective.

Meanwhile, you are correct that wiktionary does list both forms as alternates of one another, so I can live with the 'k' even though it looks odd.

Quoted 10 lines
Quoted 6 lines

block.c int bdrv_reopen_prepare(BDRVReopenState *reopen_state, BlockReopenQueue *queue, · line 2965

Quoted 2 lines
block.c +2 -0
+             * all options.  For now, this is not too big of an issue because
+             * the user simply can not specify options which cannot be changed

Seeing "can not" usually looks wrong for "cannot"; but here, it is grammatically correct. But better would be "the user can simply not specify" or "the user can simply avoid specifying options".

Awww, I deliberately designed the sentence so I could use "can not". :-)

(and even contrast it with the following "cannot"!)

Maybe it's just that I'm being thrown off by the placement of 'simply'; as a native speaker, I have an easier time parsing "can simply not" than I do "simply can not", maybe because of where the emphasis of "simply" is falling. Which is perhaps weird, because in the infinitive form (coming up with a construct that uses "to" instead of "can"), I note that "it is possible to simply not specify options" is a split infinitive (which style guides tell you to avoid, even though I think it is acceptable); while "it is possible simply to not specify options" satisfies more style guides.

But since you are intentionally doing it for the play on words,

Quoted 6 lines
Quoted 5 lines
Quoted 2 lines

+ * anyway, so they will stay unchanged. */

The grammar tweak is minor, so:

Please let me keep it :-)

you can keep it.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#21 lore raw

Re: [Qemu-devel] [PATCH v3 3/5] block: qobject_is_equal() in bdrv_reopen_prepare()

In reply to Max Reitz
Quoted 45 lines · Max Reitz

Currently, bdrv_reopen_prepare() assumes that all BDS options are strings. However, this is not the case if the BDS has been created through the json: pseudo-protocol or blockdev-add.

Note that the user-invokable reopen command is an HMP command, so you can only specify strings there. Therefore, specifying a non-string option with the "same" value as it was when originally created will now return an error because the values are supposedly similar (and there is no way for the user to circumvent this but to just not specify the option again -- however, this is still strictly better than just crashing).

---
 block.c | 31 ++++++++++++++++++-------------
 1 file changed, 18 insertions(+), 13 deletions(-)
block.c +7 -13
index 913bb43..45eb248 100644
@@ -2947,19 +2947,24 @@ int bdrv_reopen_prepare(BDRVReopenState *reopen_state, BlockReopenQueue *queue,
         const QDictEntry *entry = qdict_first(reopen_state->options);
 
         do {
-            QString *new_obj = qobject_to_qstring(entry->value);
-            const char *new = qstring_get_str(new_obj);
-            /*
-             * Caution: while qdict_get_try_str() is fine, getting
-             * non-string types would require more care.  When
-             * bs->options come from -blockdev or blockdev_add, its
-             * members are typed according to the QAPI schema, but
-             * when they come from -drive, they're all QString.
-             */
-            const char *old = qdict_get_try_str(reopen_state->bs->options,
-                                                entry->key);
-
-            if (!old || strcmp(new, old)) {
+            QObject *new = entry->value;
+            QObject *old = qdict_get(reopen_state->bs->options, entry->key);
+
+            /* TODO: When using -drive to specify blockdev options, all values
+             * will be strings; however, when using -blockdev, blockdev-add or
+             * filenames using the json:{} pseudo-protocol, they will be
+             * correctly typed.

block.c int bdrv_reopen_prepare(BDRVReopenState *reopen_state, BlockReopenQueue *queue, · line 2966

Quoted 10 lines
block.c +10 -0
+             * In contrast, reopening options are (currently) always strings
+             * (because you can only specify them through qemu-io; all other
+             * callers do not specify any options).
+             * Therefore, when using anything other than -drive to create a BDS,
+             * this cannot detect non-string options as unchanged, because
+             * qobject_is_equal() always returns false for objects of different
+             * type.  In the future, this should be remedied by correctly typing
+             * all options.  For now, this is not too big of an issue because
+             * the user simply can not specify options which cannot be changed
+             * anyway, so they will stay unchanged. */

I'm not the maintainer, and this is not a demand: consider winging this comment and wrapping lines around column 70.

As much as I fancy word play (see your reply to Eric), I have to admit that I had to read "the user simply can not specify options" about three times to make sense of it. Please consider a wording that's easier to grasp, perhaps "the user can simply refrain from specifying options that cannot be changed".

Quoted 4 lines
block.c +1 -0
+            if (!qobject_is_equal(new, old)) {
                 error_setg(errp, "Cannot change the option '%s'", entry->key);
                 ret = -EINVAL;
                 goto error;
author
#22 lore raw

Re: [Qemu-devel] [PATCH v3 3/5] block: qobject_is_equal() in bdrv_reopen_prepare()

Quoted 52 lines · Markus Armbruster
Quoted 50 lines · Max Reitz

Currently, bdrv_reopen_prepare() assumes that all BDS options are strings. However, this is not the case if the BDS has been created through the json: pseudo-protocol or blockdev-add.

Note that the user-invokable reopen command is an HMP command, so you can only specify strings there. Therefore, specifying a non-string option with the "same" value as it was when originally created will now return an error because the values are supposedly similar (and there is no way for the user to circumvent this but to just not specify the option again -- however, this is still strictly better than just crashing).

---
 block.c | 31 ++++++++++++++++++-------------
 1 file changed, 18 insertions(+), 13 deletions(-)
block.c +12 -13
index 913bb43..45eb248 100644
@@ -2947,19 +2947,24 @@ int bdrv_reopen_prepare(BDRVReopenState *reopen_state, BlockReopenQueue *queue,
         const QDictEntry *entry = qdict_first(reopen_state->options);
 
         do {
-            QString *new_obj = qobject_to_qstring(entry->value);
-            const char *new = qstring_get_str(new_obj);
-            /*
-             * Caution: while qdict_get_try_str() is fine, getting
-             * non-string types would require more care.  When
-             * bs->options come from -blockdev or blockdev_add, its
-             * members are typed according to the QAPI schema, but
-             * when they come from -drive, they're all QString.
-             */
-            const char *old = qdict_get_try_str(reopen_state->bs->options,
-                                                entry->key);
-
-            if (!old || strcmp(new, old)) {
+            QObject *new = entry->value;
+            QObject *old = qdict_get(reopen_state->bs->options, entry->key);
+
+            /* TODO: When using -drive to specify blockdev options, all values
+             * will be strings; however, when using -blockdev, blockdev-add or
+             * filenames using the json:{} pseudo-protocol, they will be
+             * correctly typed.
+             * In contrast, reopening options are (currently) always strings
+             * (because you can only specify them through qemu-io; all other
+             * callers do not specify any options).
+             * Therefore, when using anything other than -drive to create a BDS,
+             * this cannot detect non-string options as unchanged, because
Quoted 8 lines

block.c int bdrv_reopen_prepare(BDRVReopenState *reopen_state, BlockReopenQueue *queue, · line 2966

Quoted 5 lines
block.c +5 -0
+             * qobject_is_equal() always returns false for objects of different
+             * type.  In the future, this should be remedied by correctly typing
+             * all options.  For now, this is not too big of an issue because
+             * the user simply can not specify options which cannot be changed
+             * anyway, so they will stay unchanged. */

I'm not the maintainer, and this is not a demand: consider winging this comment and wrapping lines around column 70.

I actually did consider wrapping around column 70 and decided against it because this comment is already indented by 12 characters, so none of the lines exceed 65 characters (80 - (12 + strlen(" * "))).

About winging... For some reason I don't quite like it here. But it probably is better because the comment is not immediately related to the qobject_is_equal() call below, so I'll do it.

Quoted 5 lines

As much as I fancy word play (see your reply to Eric), I have to admit that I had to read "the user simply can not specify options" about three times to make sense of it. Please consider a wording that's easier to grasp, perhaps "the user can simply refrain from specifying options that cannot be changed".

Aw. I've wanted this to be an example I could point people to to educate them about the difference. Now, alas, none shall learn. :-(

Max

author
#23 lore raw

PATCH v3 4/5 iotests: Add test for non-string option reopening

In reply to Max Reitz
---
 tests/qemu-iotests/133     | 9 +++++++++
 tests/qemu-iotests/133.out | 5 +++++
 2 files changed, 14 insertions(+)
tests/qemu-iotests/133 +9 -0
index 9d35a6a..af6b3e1 100755
@@ -83,6 +83,15 @@ $QEMU_IO -c 'reopen -o driver=qcow2' $TEST_IMG
 $QEMU_IO -c 'reopen -o file.driver=file' $TEST_IMG
 $QEMU_IO -c 'reopen -o backing.driver=qcow2' $TEST_IMG
 
+echo
+echo "=== Check that reopening works with non-string options ==="
+echo
+
+# Using the json: pseudo-protocol we can create non-string options
+# (Invoke 'info' just so we get some output afterwards)
+IMGOPTSSYNTAX=false $QEMU_IO -f null-co -c 'reopen' -c 'info' \
+    "json:{'driver': 'null-co', 'size': 65536}"
+
 # success, all done
 echo "*** done"
 rm -f $seq.full
tests/qemu-iotests/133.out +5 -0
index cc86b94..f4a85ae 100644
@@ -19,4 +19,9 @@ Cannot change the option 'driver'
 
 === Check that unchanged driver is okay ===
 
+
+=== Check that reopening works with non-string options ===
+
+format name: null-co
+format name: null-co
 *** done
Signature · 1 line
2.9.4
author
#24 lore raw

PATCH v3 5/5 tests: Add check-qobject for equality tests

In reply to Max Reitz

Add a new test file (check-qobject.c) for unit tests that concern QObjects as a whole.

Its only purpose for now is to test the qobject_is_equal() function.

---
 tests/Makefile.include |   4 +-
 tests/check-qobject.c  | 312 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 315 insertions(+), 1 deletion(-)
 create mode 100644 tests/check-qobject.c
tests/Makefile.include +3 -1
index c738e92..ff0022d 100644
@@ -18,6 +18,7 @@ check-unit-y += tests/check-qlist$(EXESUF)
 gcov-files-check-qlist-y = qobject/qlist.c
 check-unit-y += tests/check-qnull$(EXESUF)
 gcov-files-check-qnull-y = qobject/qnull.c
+check-unit-y += tests/check-qobject$(EXESUF)
 check-unit-y += tests/check-qjson$(EXESUF)
 gcov-files-check-qjson-y = qobject/qjson.c
 check-unit-y += tests/test-qobject-output-visitor$(EXESUF)
@@ -507,7 +508,7 @@ GENERATED_FILES += tests/test-qapi-types.h tests/test-qapi-visit.h \
 	tests/test-qmp-introspect.h
 
 test-obj-y = tests/check-qnum.o tests/check-qstring.o tests/check-qdict.o \
-	tests/check-qlist.o tests/check-qnull.o \
+	tests/check-qlist.o tests/check-qnull.o tests/check-qobject.o \
 	tests/check-qjson.o \
 	tests/test-coroutine.o tests/test-string-output-visitor.o \
 	tests/test-string-input-visitor.o tests/test-qobject-output-visitor.o \
@@ -540,6 +541,7 @@ tests/check-qstring$(EXESUF): tests/check-qstring.o $(test-util-obj-y)
 tests/check-qdict$(EXESUF): tests/check-qdict.o $(test-util-obj-y)
 tests/check-qlist$(EXESUF): tests/check-qlist.o $(test-util-obj-y)
 tests/check-qnull$(EXESUF): tests/check-qnull.o $(test-util-obj-y)
+tests/check-qobject$(EXESUF): tests/check-qobject.o $(test-util-obj-y)
 tests/check-qjson$(EXESUF): tests/check-qjson.o $(test-util-obj-y)
 tests/check-qom-interface$(EXESUF): tests/check-qom-interface.o $(test-qom-obj-y)
 tests/check-qom-proplist$(EXESUF): tests/check-qom-proplist.o $(test-qom-obj-y)
tests/check-qobject.c new file +312 -0

312 lines not on this page · raw message

Signature · 1 line
2.9.4
Reviewed-by on v3 5/5
#25 lore raw

Re: [Qemu-devel] [PATCH v3 5/5] tests: Add check-qobject for equality tests

Quoted 13 lines · Max Reitz

Add a new test file (check-qobject.c) for unit tests that concern QObjects as a whole.

Its only purpose for now is to test the qobject_is_equal() function.

---
 tests/Makefile.include |   4 +-
 tests/check-qobject.c  | 312 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 315 insertions(+), 1 deletion(-)
 create mode 100644 tests/check-qobject.c
Quoted 3 lines

+ * Note that qobject_is_equal() is not really an equivalence relation,
+ * so this function may not be used for all objects (reflexivity is
+ * not guaranteed).

May be worth expanding the comment to mention NaN in QNum as the culprit for this fact.

tests/check-qobject.c line 39

Quoted 10 lines
tests/check-qobject.c new file +10 -0
+static void do_test_equality(bool expected, ...)
+{
+    va_list ap_count, ap_extract;
+    QObject **args;
+    int arg_count = 0;
+    int i, j;
+
+    va_start(ap_count, expected);
+    va_copy(ap_extract, ap_count);
+    while (va_arg(ap_count, QObject *) != &_test_equality_end_of_arguments) {

Here, you're careful to allow a NULL argument,

tests/check-qobject.c line 70

Quoted 9 lines
tests/check-qobject.c new file +9 -0
+static void do_free_all(int _, ...)
+{
+    va_list ap;
+    QObject *obj;
+
+    va_start(ap, _);
+    while ((obj = va_arg(ap, QObject *)) != NULL) {
+        qobject_decref(obj);
+    }

Here, you stop on the first NULL, and aren't checking for the special sentinel that can't be freed.

tests/check-qobject.c line 75

Quoted 5 lines
tests/check-qobject.c new file +5 -0
+    va_end(ap);
+}
+
+#define free_all(...) \
+    do_free_all(0, __VA_ARGS__, NULL)

Then again, you don't pass the special sentinel. So as long as NULL is the last argument(s) on any test that passes NULL (rather than an intermediate argument), you don't need to use the sentinel, and stopping iteration on the first NULL is okay. A bit magic, but I can live with it.

tests/check-qobject.c line 80

Quoted 5 lines
tests/check-qobject.c new file +5 -0
+
+static void qobject_is_equal_null_test(void)
+{
+    test_equality(false, qnull(), NULL);
+}

Where do you test_equality(true, NULL, NULL) ?

tests/check-qobject.c line 92

Quoted 12 lines
tests/check-qobject.c new file +12 -0
+
+static void qobject_is_equal_num_test(void)
+{
+    QNum *u0, *i0, *d0, *d0p25, *dnan, *um42, *im42, *dm42;
+    QString *s0, *s_empty;
+    QBool *bfalse;
+
+    u0 = qnum_from_uint(0u);
+    i0 = qnum_from_int(0);
+    d0 = qnum_from_double(0.0);
+    d0p25 = qnum_from_double(0.25);
+    dnan = qnum_from_double(0.0 / 0.0);

Ah, so you ARE testing NaN as a QNum, even though it can't occur in JSON. Might be worth a comment.

tests/check-qobject.c line 285

Quoted 2 lines
tests/check-qobject.c new file +2 -0
+
+    /* Containing an NaN value will make this dict compare unequal to

s/an/a/ (if you pronounce it "nan" or "not-a-number") (but I guess no change if you pronounce it "en-a-en")

There might be some improvements to add, but as written the test is reasonable, and some testing is better than none, so:

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
author
#26 lore raw

Re: [Qemu-devel] [PATCH v3 5/5] tests: Add check-qobject for equality tests

Quoted 14 lines · Eric Blake
Quoted 12 lines · Max Reitz

Add a new test file (check-qobject.c) for unit tests that concern QObjects as a whole.

Its only purpose for now is to test the qobject_is_equal() function.

---
 tests/Makefile.include |   4 +-
 tests/check-qobject.c  | 312 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 315 insertions(+), 1 deletion(-)
 create mode 100644 tests/check-qobject.c
Quoted 6 lines
Quoted 3 lines

+ * Note that qobject_is_equal() is not really an equivalence relation,
+ * so this function may not be used for all objects (reflexivity is
+ * not guaranteed).

May be worth expanding the comment to mention NaN in QNum as the culprit for this fact.

True.

Quoted 27 lines

tests/check-qobject.c line 39

Quoted 10 lines
tests/check-qobject.c new file +10 -0
+static void do_test_equality(bool expected, ...)
+{
+    va_list ap_count, ap_extract;
+    QObject **args;
+    int arg_count = 0;
+    int i, j;
+
+    va_start(ap_count, expected);
+    va_copy(ap_extract, ap_count);
+    while (va_arg(ap_count, QObject *) != &_test_equality_end_of_arguments) {

Here, you're careful to allow a NULL argument,

tests/check-qobject.c line 70

Quoted 9 lines
tests/check-qobject.c new file +9 -0
+static void do_free_all(int _, ...)
+{
+    va_list ap;
+    QObject *obj;
+
+    va_start(ap, _);
+    while ((obj = va_arg(ap, QObject *)) != NULL) {
+        qobject_decref(obj);
+    }

Here, you stop on the first NULL, and aren't checking for the special sentinel that can't be freed.

Quoted 10 lines

tests/check-qobject.c line 75

Quoted 5 lines
tests/check-qobject.c new file +5 -0
+    va_end(ap);
+}
+
+#define free_all(...) \
+    do_free_all(0, __VA_ARGS__, NULL)

Then again, you don't pass the special sentinel. So as long as NULL is the last argument(s) on any test that passes NULL (rather than an intermediate argument), you don't need to use the sentinel, and stopping iteration on the first NULL is okay. A bit magic, but I can live with it.

I don't mind using the sentinel here, too, but passing NULL doesn't make much sense here. It does for test_equality(), though.

Quoted 7 lines

tests/check-qobject.c line 80

Quoted 5 lines
tests/check-qobject.c new file +5 -0
+
+static void qobject_is_equal_null_test(void)
+{
+    test_equality(false, qnull(), NULL);
+}

Where do you test_equality(true, NULL, NULL) ?

Automatically in do_test_equality().

(It automatically tests whether all arguments are equal to itself -- which is why I can't use it to test NaN.)

Quoted 7 lines
Quoted 7 lines
tests/check-qobject.c new file +7 -0
+
+static void qobject_is_equal_num_test(void)
+{
+    QNum *u0, *i0, *d0, *d0p25, *dnan, *um42, *im42, *dm42;
+    QString *s0, *s_empty;
+    QBool *bfalse;
+
Quoted 8 lines

tests/check-qobject.c line 92

Quoted 5 lines
tests/check-qobject.c new file +5 -0
+    u0 = qnum_from_uint(0u);
+    i0 = qnum_from_int(0);
+    d0 = qnum_from_double(0.0);
+    d0p25 = qnum_from_double(0.25);
+    dnan = qnum_from_double(0.0 / 0.0);

Ah, so you ARE testing NaN as a QNum, even though it can't occur in JSON. Might be worth a comment.

Sure, why not.

Quoted 5 lines

tests/check-qobject.c line 285

Quoted 2 lines
tests/check-qobject.c new file +2 -0
+
+    /* Containing an NaN value will make this dict compare unequal to

s/an/a/ (if you pronounce it "nan" or "not-a-number") (but I guess no change if you pronounce it "en-a-en")

I pronounce it en-a-en, but I don't know whether that's the usual way. ;-)

Quoted 3 lines

There might be some improvements to add, but as written the test is reasonable, and some testing is better than none, so:

Thanks!

Max

#27 lore raw

Re: [Qemu-devel] [PATCH v3 5/5] tests: Add check-qobject for equality tests

In reply to Max Reitz
Quoted 6 lines · Max Reitz

Add a new test file (check-qobject.c) for unit tests that concern QObjects as a whole.

Its only purpose for now is to test the qobject_is_equal() function.

I'll review this one as soon as we made up our minds on what exactly qnum_is_equal() should do.