Skip to content

Commit cbb6540

Browse files
elmarcoMarkus Armbruster
authored and
Markus Armbruster
committed
qlit: Tighten QLit list vs QList comparison
We check that all members of the QLit list are also in the QList. We neglect to check the other direction. Fix that. While there, use QLIST_FOREACH_ENTRY() to simplify the code and break the loop on the first mismatch. Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com> Message-Id: <20170825105913.4060-13-marcandre.lureau@redhat.com> [Commit message improved] Signed-off-by: Markus Armbruster <armbru@redhat.com>
1 parent 6da8a7a commit cbb6540

File tree

2 files changed

+22
-34
lines changed

2 files changed

+22
-34
lines changed

qobject/qlit.c

+19-34
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,6 @@
1818
#include "qapi/qmp/qlit.h"
1919
#include "qapi/qmp/types.h"
2020

21-
typedef struct QListCompareHelper {
22-
int index;
23-
QLitObject *objs;
24-
bool result;
25-
} QListCompareHelper;
26-
27-
static void compare_helper(QObject *obj, void *opaque)
28-
{
29-
QListCompareHelper *helper = opaque;
30-
31-
if (!helper->result) {
32-
return;
33-
}
34-
35-
if (helper->objs[helper->index].type == QTYPE_NONE) {
36-
helper->result = false;
37-
return;
38-
}
39-
40-
helper->result =
41-
qlit_equal_qobject(&helper->objs[helper->index++], obj);
42-
}
43-
4421
static bool qlit_equal_qdict(const QLitObject *lhs, const QDict *qdict)
4522
{
4623
int i;
@@ -62,6 +39,23 @@ static bool qlit_equal_qdict(const QLitObject *lhs, const QDict *qdict)
6239
return true;
6340
}
6441

42+
static bool qlit_equal_qlist(const QLitObject *lhs, const QList *qlist)
43+
{
44+
QListEntry *e;
45+
int i = 0;
46+
47+
QLIST_FOREACH_ENTRY(qlist, e) {
48+
QObject *obj = qlist_entry_obj(e);
49+
50+
if (!qlit_equal_qobject(&lhs->value.qlist[i], obj)) {
51+
return false;
52+
}
53+
i++;
54+
}
55+
56+
return !e && lhs->value.qlist[i].type == QTYPE_NONE;
57+
}
58+
6559
bool qlit_equal_qobject(const QLitObject *lhs, const QObject *rhs)
6660
{
6761
if (!rhs || lhs->type != qobject_type(rhs)) {
@@ -78,17 +72,8 @@ bool qlit_equal_qobject(const QLitObject *lhs, const QObject *rhs)
7872
qstring_get_str(qobject_to_qstring(rhs))) == 0);
7973
case QTYPE_QDICT:
8074
return qlit_equal_qdict(lhs, qobject_to_qdict(rhs));
81-
case QTYPE_QLIST: {
82-
QListCompareHelper helper;
83-
84-
helper.index = 0;
85-
helper.objs = lhs->value.qlist;
86-
helper.result = true;
87-
88-
qlist_iter(qobject_to_qlist(rhs), compare_helper, &helper);
89-
90-
return helper.result;
91-
}
75+
case QTYPE_QLIST:
76+
return qlit_equal_qlist(lhs, qobject_to_qlist(rhs));
9277
case QTYPE_QNULL:
9378
return true;
9479
default:

tests/check-qlit.c

+3
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ static void qlit_equal_qobject_test(void)
5858

5959
g_assert(!qlit_equal_qobject(&qlit_foo, qobj));
6060

61+
qdict_put(qobject_to_qdict(qobj), "bee", qlist_new());
62+
g_assert(!qlit_equal_qobject(&qlit, qobj));
63+
6164
qobject_decref(qobj);
6265
}
6366

0 commit comments

Comments
 (0)