From 65e13b70345b546c55b561ef490cce652f5534a3 Mon Sep 17 00:00:00 2001
From: Friedemann Kleint <Friedemann.Kleint@nokia.com>
Date: Tue, 27 Oct 2009 12:25:11 +0100
Subject: [PATCH] Debugger: Fix autotests on Windows

Format pointers consistently using 0x%[l]x in printf/scanf.
Remove %p formatting as the existence of the prefix 0x
is platform-dependent (missing with MSVC), which caused
a scanf error and thus dumper crash for QAbstractItem.
Reviewed-by: ck <qt-info@nokia.com>
---
 share/qtcreator/gdbmacros/gdbmacros.cpp | 27 +++++++++++++------------
 tests/auto/debugger/tst_dumpers.cpp     | 12 ++++++++---
 2 files changed, 23 insertions(+), 16 deletions(-)

diff --git a/share/qtcreator/gdbmacros/gdbmacros.cpp b/share/qtcreator/gdbmacros/gdbmacros.cpp
index 648c79e95d3..b49a7fcceac 100644
--- a/share/qtcreator/gdbmacros/gdbmacros.cpp
+++ b/share/qtcreator/gdbmacros/gdbmacros.cpp
@@ -57,6 +57,12 @@
 #include <QtCore/QTextStream>
 #include <QtCore/QVector>
 
+#if (QT_POINTER_SIZE==4) // How to printf/scanf a pointer (uintptr_t)
+#    define POINTER_PRINTFORMAT "0x%x"
+#else
+#    define POINTER_PRINTFORMAT "0x%lx"
+#endif
+
 #ifndef QT_BOOTSTRAPPED
 
 #include <QtCore/QModelIndex>
@@ -630,18 +636,10 @@ QDumper &QDumper::put(int i)
 
 QDumper &QDumper::put(const void *p)
 {
-    static char buf[100];
     if (p) {
-        sprintf(buf, "%p", p);
-        // we get a '0x' prefix only on some implementations.
-        // if it isn't there, write it out manually.
-        if (buf[1] != 'x') {
-            put('0');
-            put('x');
-        }
-        put(buf);
+        pos += sprintf(outBuffer + pos, POINTER_PRINTFORMAT, reinterpret_cast<uintptr_t>(p));
     } else {
-        put("<null>");
+        pos += sprintf(outBuffer + pos, "<null>");
     }
     return *this;
 }
@@ -1070,7 +1068,10 @@ static void qDumpQAbstractItem(QDumper &d)
     QModelIndex mi;
     {
        ModelIndex *mm = reinterpret_cast<ModelIndex *>(&mi);
-       sscanf(d.templateParameters[0], "%d,%d,%p,%p", &mm->r, &mm->c, &mm->p, &mm->m);
+       mm->r = mm->c = 0;
+       mm->p = mm->m = 0;
+       sscanf(d.templateParameters[0], "%d,%d,"POINTER_PRINTFORMAT","POINTER_PRINTFORMAT, &mm->r, &mm->c, 
+	      reinterpret_cast<uintptr_t*>(&mm->p), reinterpret_cast<uintptr_t*>(&mm->m));
     }
     const QAbstractItemModel *m = mi.model();
     const int rowCount = m->rowCount(mi);
@@ -2149,8 +2150,8 @@ static void qDumpQVariantHelper(const QVariant *v, QString *value,
     default: {
         char buf[1000];
         const char *format = (v->typeName()[0] == 'Q')
-            ?  "'"NS"%s "NS"qVariantValue<"NS"%s >'(*('"NS"QVariant'*)%p)"
-            :  "'%s "NS"qVariantValue<%s >'(*('"NS"QVariant'*)%p)";
+            ?  "'"NS"%s "NS"qVariantValue<"NS"%s >'(*('"NS"QVariant'*)"POINTER_PRINTFORMAT")"
+            :  "'%s "NS"qVariantValue<%s >'(*('"NS"QVariant'*)"POINTER_PRINTFORMAT")";
         qsnprintf(buf, sizeof(buf) - 1, format, v->typeName(), v->typeName(), v);
         *exp = QLatin1String(buf);
         *numchild = 1;
diff --git a/tests/auto/debugger/tst_dumpers.cpp b/tests/auto/debugger/tst_dumpers.cpp
index 7dc603f664a..eff6f97dbd2 100644
--- a/tests/auto/debugger/tst_dumpers.cpp
+++ b/tests/auto/debugger/tst_dumpers.cpp
@@ -10,6 +10,12 @@
 #include <QtTest/QtTest>
 //#include <QtTest/qtest_gui.h>
 
+#if (QT_POINTER_SIZE==4)
+#    define POINTER_PRINTFORMAT "0x%x"
+#else
+#    define POINTER_PRINTFORMAT "0x%lx"
+#endif
+
 #undef NS
 #ifdef QT_NAMESPACE
 #   define STRINGIFY0(s) #s
@@ -387,7 +393,7 @@ static void testDumper(QByteArray expected0, const void *data, QByteArray outert
         extraInt0, extraInt1, extraInt2, extraInt3);
     QString expected(expected0);
     char buf[100];
-    sprintf(buf, "%p", data);
+    sprintf(buf, POINTER_PRINTFORMAT, (uintptr_t)data);
     if ((!expected.startsWith('t') && !expected.startsWith('f'))
             || expected.startsWith("type"))
         expected = "tiname='$I',addr='$A'," + expected;
@@ -417,7 +423,7 @@ static void testDumper(QByteArray expected0, const void *data, QByteArray outert
 QByteArray str(const void *p)
 {
     char buf[100];
-    sprintf(buf, "%p", p);
+    sprintf(buf, POINTER_PRINTFORMAT, (uintptr_t)p);
     return buf;
 }
 
@@ -880,7 +886,7 @@ void tst_Debugger::dumpQByteArray()
         &ba, NS"QByteArray", true);
 
     // Case 5: Regular and special characters and the replacement character.
-    ba = QByteArray("abc\a\n\r\e\'\"?");
+    ba = QByteArray("abc\a\n\r\033\'\"?");
     testDumper("value='YWJjBwoNGyciPw==',valueencoded='1',type='"NS"QByteArray',"
             "numchild='10',childtype='char',childnumchild='0',children=["
             "{value='61  (97 'a')'},{value='62  (98 'b')'},"
-- 
GitLab