Skip to content

Commit 6da8a7a

Browse files
elmarcoMarkus Armbruster
authored and
Markus Armbruster
committed
qlit: Tighten QLit dict vs QDict comparison
We check that all members of the QLit dictionary are also in the QDict. We neglect to check the other direction. Comparing the number of members suffices, because QDict can't contain duplicate members, and putting duplicates in a QLit is a programming error. Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com> Message-Id: <20170825105913.4060-12-marcandre.lureau@redhat.com> [Commit message improved] Signed-off-by: Markus Armbruster <armbru@redhat.com>
1 parent 382176b commit 6da8a7a

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

qobject/qlit.c

+23-14
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,27 @@ static void compare_helper(QObject *obj, void *opaque)
4141
qlit_equal_qobject(&helper->objs[helper->index++], obj);
4242
}
4343

44+
static bool qlit_equal_qdict(const QLitObject *lhs, const QDict *qdict)
45+
{
46+
int i;
47+
48+
for (i = 0; lhs->value.qdict[i].key; i++) {
49+
QObject *obj = qdict_get(qdict, lhs->value.qdict[i].key);
50+
51+
if (!qlit_equal_qobject(&lhs->value.qdict[i].value, obj)) {
52+
return false;
53+
}
54+
}
55+
56+
/* Note: the literal qdict must not contain duplicates, this is
57+
* considered a programming error and it isn't checked here. */
58+
if (qdict_size(qdict) != i) {
59+
return false;
60+
}
61+
62+
return true;
63+
}
64+
4465
bool qlit_equal_qobject(const QLitObject *lhs, const QObject *rhs)
4566
{
4667
if (!rhs || lhs->type != qobject_type(rhs)) {
@@ -55,20 +76,8 @@ bool qlit_equal_qobject(const QLitObject *lhs, const QObject *rhs)
5576
case QTYPE_QSTRING:
5677
return (strcmp(lhs->value.qstr,
5778
qstring_get_str(qobject_to_qstring(rhs))) == 0);
58-
case QTYPE_QDICT: {
59-
int i;
60-
61-
for (i = 0; lhs->value.qdict[i].key; i++) {
62-
QObject *obj = qdict_get(qobject_to_qdict(rhs),
63-
lhs->value.qdict[i].key);
64-
65-
if (!qlit_equal_qobject(&lhs->value.qdict[i].value, obj)) {
66-
return false;
67-
}
68-
}
69-
70-
return true;
71-
}
79+
case QTYPE_QDICT:
80+
return qlit_equal_qdict(lhs, qobject_to_qdict(rhs));
7281
case QTYPE_QLIST: {
7382
QListCompareHelper helper;
7483

tests/check-qlit.c

+7
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ static QLitObject qlit = QLIT_QDICT(((QLitDictEntry[]) {
2828
{ },
2929
}));
3030

31+
static QLitObject qlit_foo = QLIT_QDICT(((QLitDictEntry[]) {
32+
{ "foo", QLIT_QNUM(42) },
33+
{ },
34+
}));
35+
3136
static QObject *make_qobject(void)
3237
{
3338
QDict *qdict = qdict_new();
@@ -51,6 +56,8 @@ static void qlit_equal_qobject_test(void)
5156

5257
g_assert(qlit_equal_qobject(&qlit, qobj));
5358

59+
g_assert(!qlit_equal_qobject(&qlit_foo, qobj));
60+
5461
qobject_decref(qobj);
5562
}
5663

0 commit comments

Comments
 (0)