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 <edward.welbourne@qt.io> Reviewed-by: Ahmad Samir <a.samirh78@gmail.com>
This commit is contained in:
parent
2ef4ea3319
commit
a0c7ca3773
@ -17,6 +17,7 @@
|
|||||||
// Broken implementation, causes link failures just by #include'ing!
|
// Broken implementation, causes link failures just by #include'ing!
|
||||||
# undef __cpp_lib_to_chars // in case <version> was included
|
# undef __cpp_lib_to_chars // in case <version> was included
|
||||||
#endif
|
#endif
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@ -118,15 +119,11 @@ using OldActionsArray = std::array<struct sigaction, fatalSignals.size()>;
|
|||||||
|
|
||||||
template <typename... Args> static ssize_t writeToStderr(Args &&... args)
|
template <typename... Args> static ssize_t writeToStderr(Args &&... args)
|
||||||
{
|
{
|
||||||
auto makeIovec = [](auto &&arg) {
|
auto makeIovec = [](std::string_view arg) {
|
||||||
if constexpr (std::is_same_v<std::decay_t<decltype(arg)>, iovec>) {
|
struct iovec r = {};
|
||||||
return arg;
|
r.iov_base = const_cast<char *>(arg.data());
|
||||||
} else {
|
r.iov_len = arg.size();
|
||||||
struct iovec r = {};
|
return r;
|
||||||
r.iov_base = const_cast<char *>(arg);
|
|
||||||
r.iov_len = strlen(arg);
|
|
||||||
return r;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
struct iovec vec[] = { makeIovec(std::forward<Args>(args))... };
|
struct iovec vec[] = { makeIovec(std::forward<Args>(args))... };
|
||||||
return ::writev(STDERR_FILENO, vec, std::size(vec));
|
return ::writev(STDERR_FILENO, vec, std::size(vec));
|
||||||
@ -145,7 +142,7 @@ struct AsyncSafeIntBuffer
|
|||||||
AsyncSafeIntBuffer(Qt::Initialization) {} // leaves array uninitialized
|
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();
|
char *ptr = result.array.data();
|
||||||
if (false) {
|
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
|
// this isn't necessary, it just helps in the debugger
|
||||||
*ptr = '\0';
|
*ptr = '\0';
|
||||||
#endif
|
#endif
|
||||||
struct iovec r;
|
return std::string_view(result.array.data(), ptr - result.array.data());
|
||||||
r.iov_base = result.array.data();
|
|
||||||
r.iov_len = ptr - result.array.data();
|
|
||||||
return r;
|
|
||||||
};
|
};
|
||||||
} // unnamed namespace
|
} // unnamed namespace
|
||||||
|
|
||||||
@ -433,10 +427,7 @@ printCrashingSignalInfo(T *info)
|
|||||||
int shift = sizeof(quintptr) * 8 - 4;
|
int shift = sizeof(quintptr) * 8 - 4;
|
||||||
for (size_t i = 0; i < sizeof(quintptr) * 2; ++i, shift -= 4)
|
for (size_t i = 0; i < sizeof(quintptr) * 2; ++i, shift -= 4)
|
||||||
r[i] = QtMiscUtils::toHexLower(u >> shift);
|
r[i] = QtMiscUtils::toHexLower(u >> shift);
|
||||||
struct iovec vec;
|
return std::string_view(r.data(), r.size());
|
||||||
vec.iov_base = r.data();
|
|
||||||
vec.iov_len = r.size();
|
|
||||||
return vec;
|
|
||||||
};
|
};
|
||||||
writeToStderr(", code ", asyncSafeToString(info->si_code),
|
writeToStderr(", code ", asyncSafeToString(info->si_code),
|
||||||
", for address 0x", toHexString(quintptr(info->si_addr)));
|
", for address 0x", toHexString(quintptr(info->si_addr)));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user