From a0c7ca37736336c6b0ab0c2bf0523bbf05313298 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Thu, 15 May 2025 07:23:44 -0700 Subject: [PATCH] QTest::CrashHandler: use string_view for writeToStderr() for the inputs For regular strings, it'll call strlen() for us, removing the need of our doing it. We just have to remove the one case where we were passing iovec into it: make asyncSafeToString() return a string_view too. Change-Id: I7d17e5c4525637df23f1fffd5e5c381632386f49 Reviewed-by: Edward Welbourne Reviewed-by: Ahmad Samir --- src/testlib/qtestcrashhandler_unix.cpp | 27 +++++++++----------------- 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/src/testlib/qtestcrashhandler_unix.cpp b/src/testlib/qtestcrashhandler_unix.cpp index 92ad16ceb97..8a24b96e3dc 100644 --- a/src/testlib/qtestcrashhandler_unix.cpp +++ b/src/testlib/qtestcrashhandler_unix.cpp @@ -17,6 +17,7 @@ // Broken implementation, causes link failures just by #include'ing! # undef __cpp_lib_to_chars // in case was included #endif +#include #include #include @@ -118,15 +119,11 @@ using OldActionsArray = std::array; template static ssize_t writeToStderr(Args &&... args) { - auto makeIovec = [](auto &&arg) { - if constexpr (std::is_same_v, iovec>) { - return arg; - } else { - struct iovec r = {}; - r.iov_base = const_cast(arg); - r.iov_len = strlen(arg); - return r; - } + auto makeIovec = [](std::string_view arg) { + struct iovec r = {}; + r.iov_base = const_cast(arg.data()); + r.iov_len = arg.size(); + return r; }; struct iovec vec[] = { makeIovec(std::forward(args))... }; return ::writev(STDERR_FILENO, vec, std::size(vec)); @@ -145,7 +142,7 @@ struct AsyncSafeIntBuffer AsyncSafeIntBuffer(Qt::Initialization) {} // leaves array uninitialized }; -struct iovec asyncSafeToString(int n, AsyncSafeIntBuffer &&result = Qt::Uninitialized) +std::string_view asyncSafeToString(int n, AsyncSafeIntBuffer &&result = Qt::Uninitialized) { char *ptr = result.array.data(); if (false) { @@ -185,10 +182,7 @@ struct iovec asyncSafeToString(int n, AsyncSafeIntBuffer &&result = Qt::Uninitia // this isn't necessary, it just helps in the debugger *ptr = '\0'; #endif - struct iovec r; - r.iov_base = result.array.data(); - r.iov_len = ptr - result.array.data(); - return r; + return std::string_view(result.array.data(), ptr - result.array.data()); }; } // unnamed namespace @@ -433,10 +427,7 @@ printCrashingSignalInfo(T *info) int shift = sizeof(quintptr) * 8 - 4; for (size_t i = 0; i < sizeof(quintptr) * 2; ++i, shift -= 4) r[i] = QtMiscUtils::toHexLower(u >> shift); - struct iovec vec; - vec.iov_base = r.data(); - vec.iov_len = r.size(); - return vec; + return std::string_view(r.data(), r.size()); }; writeToStderr(", code ", asyncSafeToString(info->si_code), ", for address 0x", toHexString(quintptr(info->si_addr)));