ipctestcommon.h: port away from qsnprintf()

Use std::snprintf() instead. Also use qToUnderlying() instead of
explicit cast to unsigned (so the compiler warns us should the
underlying type of he QNativeIpcKey::Type enum ever change) and
applyRAII to temporary toString() results.

Amends 32a06e983073080d939b4742996a3dc9cd75bb83.

Pick-to: 6.7
Change-Id: I4dd00672382d377285d722a47d998bdf12422eb4
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
(cherry picked from commit ce7b8fc91d82402cbed230e83c00cab3a54f9916)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Marc Mutz 2024-07-22 14:59:18 +02:00 committed by Qt Cherry-pick Bot
parent 37dc5b91fd
commit a228f1f399

View File

@ -5,6 +5,9 @@
#include <QtTest/QTest> #include <QtTest/QTest>
#include <QtCore/QNativeIpcKey> #include <QtCore/QNativeIpcKey>
#include <cstdio>
#include <memory>
namespace IpcTestCommon { namespace IpcTestCommon {
static QList<QNativeIpcKey::Type> supportedKeyTypes; static QList<QNativeIpcKey::Type> supportedKeyTypes;
@ -64,7 +67,7 @@ template<> inline char *toString(const QNativeIpcKey::Type &type)
return qstrdup("Invalid"); return qstrdup("Invalid");
char buf[32]; char buf[32];
qsnprintf(buf, sizeof(buf), "%u", unsigned(type)); std::snprintf(buf, sizeof(buf), "%u", qToUnderlying(type));
return qstrdup(buf); return qstrdup(buf);
} }
@ -73,12 +76,11 @@ template<> inline char *toString(const QNativeIpcKey &key)
if (!key.isValid()) if (!key.isValid())
return qstrdup("<invalid>"); return qstrdup("<invalid>");
const char *type = toString(key.type()); using UP = std::unique_ptr<char[]>;
const char *text = toString(key.nativeKey()); const auto type = UP{toString(key.type())};
const auto text = UP{toString(key.nativeKey())};
char buf[256]; char buf[256];
qsnprintf(buf, sizeof(buf), "QNativeIpcKey(%s, %s)", text, type); std::snprintf(buf, sizeof(buf), "QNativeIpcKey(%s, %s)", text.get(), type.get());
delete[] type;
delete[] text;
return qstrdup(buf); return qstrdup(buf);
} }
} // namespace QTest } // namespace QTest