diff options
author | Robert Adam <dev@robert-adam.de> | 2021-04-05 21:45:26 +0300 |
---|---|---|
committer | Robert Adam <dev@robert-adam.de> | 2021-04-06 10:43:57 +0300 |
commit | 44d8d92fab23442865d1fd12b826464da8b4250d (patch) | |
tree | e331b8af3515a4b593904800b92b16dfe003880c /src/murmur/UnixMurmur.cpp | |
parent | 7e64683554f42f1af14ff19252e5e77cdda48083 (diff) |
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
Diffstat (limited to 'src/murmur/UnixMurmur.cpp')
-rw-r--r-- | src/murmur/UnixMurmur.cpp | 9 |
1 files changed, 8 insertions, 1 deletions
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 <sys/stat.h> #include <unistd.h> +#include <limits> + 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."); |