From 10de7f77f4ff37899db57d08fbd2a2b0702d1ab0 Mon Sep 17 00:00:00 2001 From: Kent Hansen Date: Sun, 5 Feb 2012 22:38:56 +0100 Subject: qmetaobjectbuilder: Add asserts to confirm validity MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit buildMetaObject() can operate in two "modes", based on whether a buffer to write the meta-object into is passed or not. Add asserts to make sure that the intermediate meta-data indexes are correct in both "modes", and that the final size in "write mode" matches the size that was computed in the preceding non-writing pass. The asserts make it easier to catch obvious problems when changing buildMetaObject() to generate a new meta-object revision. Change-Id: Ief7c74e6f6fca836587e831b06072d6aa98c7193 Reviewed-by: Bradley T. Hughes Reviewed-by: Jędrzej Nowacki Reviewed-by: João Abecasis Reviewed-by: Kent Hansen --- src/corelib/kernel/qmetaobjectbuilder.cpp | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/corelib/kernel/qmetaobjectbuilder.cpp b/src/corelib/kernel/qmetaobjectbuilder.cpp index c84c95cb6f..a19f1fde80 100644 --- a/src/corelib/kernel/qmetaobjectbuilder.cpp +++ b/src/corelib/kernel/qmetaobjectbuilder.cpp @@ -1132,8 +1132,9 @@ static QByteArray buildParameterNames // build the QMetaObject. Returns -1 if the metaobject if // relocatable is set, but the metaobject contains extradata. static int buildMetaObject(QMetaObjectBuilderPrivate *d, char *buf, - bool relocatable) + int expectedSize, bool relocatable) { + Q_UNUSED(expectedSize); // Avoid warning in release mode int size = 0; int dataIndex; int enumIndex; @@ -1248,6 +1249,7 @@ static int buildMetaObject(QMetaObjectBuilderPrivate *d, char *buf, int empty = buildString(buf, str, &offset, QByteArray(), -1); // Output the class infos, + Q_ASSERT(!buf || dataIndex == pmeta->classInfoData); for (index = 0; index < d->classInfoNames.size(); ++index) { int name = buildString(buf, str, &offset, d->classInfoNames[index], empty); int value = buildString(buf, str, &offset, d->classInfoValues[index], empty); @@ -1259,6 +1261,7 @@ static int buildMetaObject(QMetaObjectBuilderPrivate *d, char *buf, } // Output the methods in the class. + Q_ASSERT(!buf || dataIndex == pmeta->methodData); for (index = 0; index < d->methods.size(); ++index) { QMetaMethodBuilderPrivate *method = &(d->methods[index]); int sig = buildString(buf, str, &offset, method->signature, empty); @@ -1290,6 +1293,7 @@ static int buildMetaObject(QMetaObjectBuilderPrivate *d, char *buf, } // Output the properties in the class. + Q_ASSERT(!buf || dataIndex == pmeta->propertyData); for (index = 0; index < d->properties.size(); ++index) { QMetaPropertyBuilderPrivate *prop = &(d->properties[index]); int name = buildString(buf, str, &offset, prop->name, empty); @@ -1331,6 +1335,7 @@ static int buildMetaObject(QMetaObjectBuilderPrivate *d, char *buf, } // Output the enumerators in the class. + Q_ASSERT(!buf || dataIndex == pmeta->enumeratorData); for (index = 0; index < d->enumerators.size(); ++index) { QMetaEnumBuilderPrivate *enumerator = &(d->enumerators[index]); int name = buildString(buf, str, &offset, enumerator->name, empty); @@ -1355,6 +1360,7 @@ static int buildMetaObject(QMetaObjectBuilderPrivate *d, char *buf, } // Output the constructors in the class. + Q_ASSERT(!buf || dataIndex == pmeta->constructorData); for (index = 0; index < d->constructors.size(); ++index) { QMetaMethodBuilderPrivate *method = &(d->constructors[index]); int sig = buildString(buf, str, &offset, method->signature, empty); @@ -1411,6 +1417,7 @@ static int buildMetaObject(QMetaObjectBuilderPrivate *d, char *buf, // Align the final size and return it. ALIGN(size, void *); + Q_ASSERT(!buf || size == expectedSize); return size; } @@ -1426,10 +1433,10 @@ static int buildMetaObject(QMetaObjectBuilderPrivate *d, char *buf, */ QMetaObject *QMetaObjectBuilder::toMetaObject() const { - int size = buildMetaObject(d, 0, false); + int size = buildMetaObject(d, 0, 0, false); char *buf = reinterpret_cast(malloc(size)); memset(buf, 0, size); - buildMetaObject(d, buf, false); + buildMetaObject(d, buf, size, false); return reinterpret_cast(buf); } @@ -1449,7 +1456,7 @@ QMetaObject *QMetaObjectBuilder::toMetaObject() const */ QByteArray QMetaObjectBuilder::toRelocatableData(bool *ok) const { - int size = buildMetaObject(d, 0, true); + int size = buildMetaObject(d, 0, 0, true); if (size == -1) { if (ok) *ok = false; return QByteArray(); @@ -1459,7 +1466,7 @@ QByteArray QMetaObjectBuilder::toRelocatableData(bool *ok) const data.resize(size); char *buf = data.data(); memset(buf, 0, size); - buildMetaObject(d, buf, true); + buildMetaObject(d, buf, size, true); if (ok) *ok = true; return data; } -- cgit v1.2.3