From 44d8d92fab23442865d1fd12b826464da8b4250d Mon Sep 17 00:00:00 2001 From: Robert Adam Date: Mon, 5 Apr 2021 20:45:26 +0200 Subject: FIX(server): Fix undefined behvior on Linux hosts The fields of the rlimit struct were assumed to be of type long int when printing via the qWarning function. This assumption is correct for glibc (used by e.g. Ubuntu) but it is not true for musl (used by e.g. Alpine Linux). In that case the wrong format specifiers are being used for printing these values, which causes undefined behavior. The fix is to always copy the values into variables of type long long unsigned int and then printing these. The code also adds an assertion making sure that within this copy operation the values won't overflow the variable's size. Fixes #4897 --- src/murmur/UnixMurmur.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'src/murmur/UnixMurmur.cpp') diff --git a/src/murmur/UnixMurmur.cpp b/src/murmur/UnixMurmur.cpp index 09ebd6cde..472539789 100644 --- a/src/murmur/UnixMurmur.cpp +++ b/src/murmur/UnixMurmur.cpp @@ -27,6 +27,8 @@ #include #include +#include + QMutex *LimitTest::qm; QWaitCondition *LimitTest::qw; QWaitCondition *LimitTest::qstartw; @@ -340,7 +342,12 @@ void UnixMurmur::finalcap() { if (getrlimit(RLIMIT_RTPRIO, &r) != 0) { qCritical("Failed to get priority limits."); } else { - qWarning("Resource limits were %ld %ld", r.rlim_cur, r.rlim_max); + using ulong_t = unsigned long long int; + static_assert(std::numeric_limits< ulong_t >::max() >= std::numeric_limits< rlim_t >::max(), "rlim_t is unexpectedly large"); + ulong_t current = r.rlim_cur; + ulong_t max = r.rlim_max; + qWarning("Resource limits were %llu %llu", current, max); + r.rlim_cur = r.rlim_max = 1; if (setrlimit(RLIMIT_RTPRIO, &r) != 0) { qCritical("Failed to set priority limits."); -- cgit v1.2.3