From 85c8e289735d9d2f1d8ab3605522bf2cf346cd96 Mon Sep 17 00:00:00 2001 From: Tushar Maheshwari Date: Wed, 16 Jun 2021 01:17:56 +0530 Subject: [PATCH 1/2] Enable generator tests on Windows CI - Reduce included windows headers - Reduce scope of time-generator dependencies --- CMakeLists.txt | 6 ++++++ appveyor.yml | 3 ++- include/uuid.h | 31 ++++++++++--------------------- 3 files changed, 18 insertions(+), 22 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 65df943..fffb92c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,6 +4,7 @@ set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake") option(UUID_BUILD_TESTS "Build the unit tests" ON) option(UUID_SYSTEM_GENERATOR "Enable operating system uuid generator" OFF) +option(UUID_TIME_GENERATOR "Enable experimental time-based uuid generator" OFF) option(UUID_USING_CXX20_SPAN "Using span from std instead of gsl" OFF) # Library target @@ -29,6 +30,11 @@ if (UUID_SYSTEM_GENERATOR) endif () endif () +# Using time-based generator +if (UUID_TIME_GENERATOR) + target_compile_definitions(${PROJECT_NAME} INTERFACE UUID_TIME_GENERATOR) +endif() + # Using span from std if (NOT UUID_USING_CXX20_SPAN) target_include_directories(${PROJECT_NAME} INTERFACE diff --git a/appveyor.yml b/appveyor.yml index 32525d3..b156d56 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -9,13 +9,14 @@ environment: - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017 CMAKE_GENERATOR: Visual Studio 15 2017 CMAKE_GENERATOR_PLATFORM: x64 + CMAKE_CLI_FLAGS: -DUUID_SYSTEM_GENERATOR=ON -DUUID_TIME_GENERATOR=ON init: - cmake --version - msbuild /version before_build: - - cmake -S . -B build + - cmake %CMAKE_CLI_FLAGS% -S . -B build - cd build build_script: diff --git a/include/uuid.h b/include/uuid.h index 3eb90c8..5adfdeb 100644 --- a/include/uuid.h +++ b/include/uuid.h @@ -17,23 +17,16 @@ #include #include -#if defined(UUID_TIME_GENERATOR) || defined(UUID_SYSTEM_GENERATOR) #ifdef _WIN32 -#ifndef WIN32_LEAN_AND_MEAN -#define WIN32_LEAN_AND_MEAN -#endif -#ifndef NOMINMAX -#define NOMINMAX -#endif #ifdef UUID_SYSTEM_GENERATOR #include #endif -#include -#include +#ifdef UUID_TIME_GENERATOR #include #pragma comment(lib, "IPHLPAPI.lib") +#endif #elif defined(__linux__) || defined(__unix__) @@ -47,7 +40,6 @@ #include #endif -#endif #endif namespace uuids @@ -278,10 +270,6 @@ namespace uuids size_t m_blockByteIndex; size_t m_byteCount; }; - - static std::mt19937 clock_gen(std::random_device{}()); - static std::uniform_int_distribution clock_dis{ -32768, 32767 }; - static std::atomic_short clock_sequence = clock_dis(clock_gen); } // -------------------------------------------------------------------------------------------------------------------------- @@ -847,11 +835,15 @@ namespace uuids return ns / 100; } - public: - uuid_time_generator() + static short get_clock_sequence() { + static std::mt19937 clock_gen(std::random_device{}()); + static std::uniform_int_distribution clock_dis{ -32768, 32767 }; + static std::atomic_short clock_sequence = clock_dis(clock_gen); + return clock_sequence++; } + public: uuid operator()() { if (get_mac_address()) @@ -860,12 +852,9 @@ namespace uuids auto tm = get_time_intervals(); - short clock_seq = detail::clock_sequence++; - - clock_seq &= 0x3FFF; + short clock_seq = get_clock_sequence(); auto ptm = reinterpret_cast(&tm); - ptm[0] &= 0x0F; memcpy(&data[0], ptm + 4, 4); memcpy(&data[4], ptm + 2, 2); @@ -878,7 +867,7 @@ namespace uuids data[8] |= 0x80; // version must be 0b0001xxxx - data[6] &= 0x5F; + data[6] &= 0x1F; data[6] |= 0x10; memcpy(&data[10], &device_address.value()[0], 6); From 1562616dab012e71d12b10a0046e240de50504fe Mon Sep 17 00:00:00 2001 From: Tushar Maheshwari Date: Fri, 25 Jun 2021 01:10:48 +0530 Subject: [PATCH 2/2] fix short overflow UB --- include/uuid.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/include/uuid.h b/include/uuid.h index 5adfdeb..0752e7f 100644 --- a/include/uuid.h +++ b/include/uuid.h @@ -835,11 +835,11 @@ namespace uuids return ns / 100; } - static short get_clock_sequence() + static unsigned short get_clock_sequence() { static std::mt19937 clock_gen(std::random_device{}()); - static std::uniform_int_distribution clock_dis{ -32768, 32767 }; - static std::atomic_short clock_sequence = clock_dis(clock_gen); + static std::uniform_int_distribution clock_dis; + static std::atomic_ushort clock_sequence = clock_dis(clock_gen); return clock_sequence++; } @@ -852,7 +852,7 @@ namespace uuids auto tm = get_time_intervals(); - short clock_seq = get_clock_sequence(); + auto clock_seq = get_clock_sequence(); auto ptm = reinterpret_cast(&tm); @@ -860,7 +860,7 @@ namespace uuids memcpy(&data[4], ptm + 2, 2); memcpy(&data[6], ptm, 2); - memcpy(&data[8], reinterpret_cast(&clock_seq), 2); + memcpy(&data[8], &clock_seq, 2); // variant must be 0b10xxxxxx data[8] &= 0xBF;