diff mbox series

[v3,05/19] qlit: Use qnum_value_is_equal() when comparing QNums

Message ID 20201123194818.2773508-6-ehabkost@redhat.com
State New
Headers show
Series qom: Use qlit to represent property defaults | expand

Commit Message

Eduardo Habkost Nov. 23, 2020, 7:48 p.m. UTC
Currently, qlit_equal_qobject() crashes if getting a QNum that
can't be represented as int64.  Fix this by using
qnum_value_is_equal().

Signed-off-by: Eduardo Habkost <ehabkost@redhat.com>
---
This is a new patch added in v3 of the series.
---
 qobject/qlit.c     |  3 ++-
 tests/check-qlit.c | 19 +++++++++++++++++++
 2 files changed, 21 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/qobject/qlit.c b/qobject/qlit.c
index be8332136c..67126b25d5 100644
--- a/qobject/qlit.c
+++ b/qobject/qlit.c
@@ -71,7 +71,8 @@  bool qlit_equal_qobject(const QLitObject *lhs, const QObject *rhs)
     case QTYPE_QBOOL:
         return lhs->value.qbool == qbool_get_bool(qobject_to(QBool, rhs));
     case QTYPE_QNUM:
-        return lhs->value.qnum ==  qnum_get_int(qobject_to(QNum, rhs));
+        return qnum_value_is_equal(&(QNumValue)QNUM_VAL_INT(lhs->value.qnum),
+                                   &qobject_to(QNum, rhs)->value);
     case QTYPE_QSTRING:
         return (strcmp(lhs->value.qstr,
                        qstring_get_str(qobject_to(QString, rhs))) == 0);
diff --git a/tests/check-qlit.c b/tests/check-qlit.c
index bd6798d912..58ceaae5a3 100644
--- a/tests/check-qlit.c
+++ b/tests/check-qlit.c
@@ -65,6 +65,24 @@  static void qlit_equal_qobject_test(void)
     qobject_unref(qobj);
 }
 
+static void qlit_equal_large_qnum_test(void)
+{
+    /* 2^32-1 */
+    QNum *large = qnum_from_uint(9223372036854775807LL);
+    /* 2^32 */
+    QNum *too_large = qnum_from_uint(9223372036854775808ULL);
+    QNum *dbl = qnum_from_double(9223372036854775808.0);
+    QLitObject qlit_large = QLIT_QNUM(9223372036854775807LL);
+
+    g_assert(qlit_equal_qobject(&qlit_large, QOBJECT(large)));
+    g_assert(!qlit_equal_qobject(&qlit_large, QOBJECT(too_large)));
+    g_assert(!qlit_equal_qobject(&qlit_large, QOBJECT(dbl)));
+
+    qobject_unref(dbl);
+    qobject_unref(large);
+    qobject_unref(too_large);
+}
+
 static void qobject_from_qlit_test(void)
 {
     QObject *obj, *qobj = qobject_from_qlit(&qlit);
@@ -95,6 +113,7 @@  int main(int argc, char **argv)
     g_test_init(&argc, &argv, NULL);
 
     g_test_add_func("/qlit/equal_qobject", qlit_equal_qobject_test);
+    g_test_add_func("/qlit/equal_large_qnum", qlit_equal_large_qnum_test);
     g_test_add_func("/qlit/qobject_from_qlit", qobject_from_qlit_test);
 
     return g_test_run();