Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/compression/HttpDeflateCompression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace MAT_NS_BEGIN {

int result = deflateInit2(&stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, m_windowBits, 8 /*DEF_MEM_LEVEL*/, Z_DEFAULT_STRATEGY);
if (result != Z_OK) {
LOG_WARN("HTTP request compressing failed, error=%u/%u (%s)", 1, result, stream.msg);
LOG_WARN("HTTP request compressing failed, error=%d/%d (%s)", 1, result, (stream.msg ? stream.msg : "(null)"));
compressionFailed(ctx);
return false;
}
Expand Down Expand Up @@ -80,7 +80,7 @@ namespace MAT_NS_BEGIN {
deflateEnd(&stream);

if (result != Z_STREAM_END) {
LOG_WARN("HTTP request compressing failed, error=%u/%u (%s)", 2, result, stream.msg);
LOG_WARN("HTTP request compressing failed, error=%d/%d (%s)", 2, result, (stream.msg ? stream.msg : "(null)"));
compressionFailed(ctx);
return false;
}
Expand Down
35 changes: 24 additions & 11 deletions lib/pal/PAL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,19 +369,32 @@ namespace PAL_NS_BEGIN {
std::transform(uuidStr.begin(), uuidStr.end(), uuidStr.begin(), ::tolower);
return uuidStr;
#else
static std::once_flag flag;
std::call_once(flag, [](){
auto now = std::chrono::high_resolution_clock::now();
auto nanos = std::chrono::duration_cast<std::chrono::nanoseconds>(now.time_since_epoch()).count();
std::srand(static_cast<unsigned int>(std::time(0) ^ nanos));
});
// Use std::random_device -- a non-deterministic, CSPRNG-backed source on
// the platforms we target (glibc/bionic/libc++ draw from getrandom or
// /dev/urandom) -- instead of std::rand()/srand(time(0)), so the session
// and event identifiers built from it are not predictable. It is
// thread_local so the backing source is opened once per thread rather than
// on every call (generateUuidString is on the event logging hot path), and
// the 128 bits are drawn with four operator() calls instead of eleven
// (random_device::max() is guaranteed to span the full unsigned int range).
thread_local std::random_device rd;

GUID_t uuid;
uuid.Data1 = (static_cast<uint16_t>(std::rand()) << 16) | static_cast<uint16_t>(std::rand());
uuid.Data2 = static_cast<uint16_t>(std::rand());
uuid.Data3 = static_cast<uint16_t>(std::rand());
for (size_t i = 0; i < sizeof(uuid.Data4); i++)
uuid.Data4[i] = static_cast<uint8_t>(std::rand());
const uint32_t r0 = rd();
const uint32_t r1 = rd();
const uint32_t r2 = rd();
const uint32_t r3 = rd();
uuid.Data1 = r0;
uuid.Data2 = static_cast<uint16_t>(r1);
uuid.Data3 = static_cast<uint16_t>(r1 >> 16);
uuid.Data4[0] = static_cast<uint8_t>(r2);
uuid.Data4[1] = static_cast<uint8_t>(r2 >> 8);
uuid.Data4[2] = static_cast<uint8_t>(r2 >> 16);
uuid.Data4[3] = static_cast<uint8_t>(r2 >> 24);
uuid.Data4[4] = static_cast<uint8_t>(r3);
uuid.Data4[5] = static_cast<uint8_t>(r3 >> 8);
uuid.Data4[6] = static_cast<uint8_t>(r3 >> 16);
uuid.Data4[7] = static_cast<uint8_t>(r3 >> 24);

// TODO: [MG] - replace this sprintf by more robust GUID to string converter
char buf[40] = { 0 };
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/ZlibUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ namespace MAT_NS_BEGIN
} while (ret == Z_OK);
if (ret != Z_STREAM_END)
{
LOG_WARN("Inflate failed, error=%u/%u (%s)", 2, ret, zs.msg);
LOG_WARN("Inflate failed, error=%d/%d (%s)", 2, ret, (zs.msg ? zs.msg : "(null)"));
result = false;
}
inflateEnd(&zs);
Expand Down
11 changes: 11 additions & 0 deletions tests/unittests/PalTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <cstdint>
#include <string>
#include <set>

#ifdef HAVE_MAT_LOGGING
#include "pal/PAL.hpp"
Expand Down Expand Up @@ -68,6 +69,16 @@ TEST_F(PalTests, UuidGeneration)
diff += (uuid0[i] != uuid1[i]);
}
EXPECT_THAT(diff, Gt(20u));

// A batch of generated UUIDs must all be distinct (guards against a stuck
// or low-entropy generator).
std::set<std::string> uuids;
for (size_t i = 0; i < 1000; i++) {
std::string u = PAL::generateUuidString();
EXPECT_THAT(u.length(), 36u);
uuids.insert(u);
}
EXPECT_THAT(uuids.size(), 1000u);
}

TEST_F(PalTests, PseudoRandomGenerator)
Expand Down
Loading