From 4d655da69dc4ba5ca5dcea112e25ddada6763e5c Mon Sep 17 00:00:00 2001 From: Paul Hollinsky Date: Fri, 29 Jan 2021 17:32:07 -0500 Subject: [PATCH] Decoder: 64-bit shift to avoid UB Shifting a value by more than the size of its type is UB. This was actually causing mangled serial numbers with optimization on using Clang 12 on Linux. --- communication/decoder.cpp | 4 ++-- include/icsneo/communication/decoder.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/communication/decoder.cpp b/communication/decoder.cpp index bead06c..cd9b5cd 100644 --- a/communication/decoder.cpp +++ b/communication/decoder.cpp @@ -13,10 +13,10 @@ using namespace icsneo; -uint64_t Decoder::GetUInt64FromLEBytes(uint8_t* bytes) { +uint64_t Decoder::GetUInt64FromLEBytes(const uint8_t* bytes) { uint64_t ret = 0; for(int i = 0; i < 8; i++) - ret |= (bytes[i] << (i * 8)); + ret |= (uint64_t(bytes[i]) << (i * 8)); return ret; } diff --git a/include/icsneo/communication/decoder.h b/include/icsneo/communication/decoder.h index ca5a651..dd5792e 100644 --- a/include/icsneo/communication/decoder.h +++ b/include/icsneo/communication/decoder.h @@ -16,7 +16,7 @@ namespace icsneo { class Decoder { public: - static uint64_t GetUInt64FromLEBytes(uint8_t* bytes); + static uint64_t GetUInt64FromLEBytes(const uint8_t* bytes); Decoder(device_eventhandler_t report) : report(report) {} bool decode(std::shared_ptr& result, const std::shared_ptr& packet);