From 04889fef4d62c9c8f300c25041e29570116823d8 Mon Sep 17 00:00:00 2001 From: Uwe Seimet Date: Sun, 27 Aug 2023 19:18:22 +0200 Subject: [PATCH] Fix SonarQube issue --- cpp/devices/ctapdriver.cpp | 10 +++++----- cpp/devices/ctapdriver.h | 3 ++- cpp/test/ctapdriver_test.cpp | 16 ++++++++-------- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/cpp/devices/ctapdriver.cpp b/cpp/devices/ctapdriver.cpp index b2b3342a22..fea3b951fc 100644 --- a/cpp/devices/ctapdriver.cpp +++ b/cpp/devices/ctapdriver.cpp @@ -433,11 +433,11 @@ bool CTapDriver::PendingPackets() const } // See https://stackoverflow.com/questions/21001659/crc32-algorithm-implementation-in-c-without-a-look-up-table-and-with-a-public-li -uint32_t CTapDriver::Crc32(const uint8_t *buf, int length) { +uint32_t CTapDriver::Crc32(span data) { uint32_t crc = 0xffffffff; - for (int i = 0; i < length; i++) { - crc ^= buf[i]; - for (int j = 0; j < 8; j++) { + for (const auto d: data) { + crc ^= d; + for (int i = 0; i < 8; i++) { const uint32_t mask = -(static_cast(crc) & 1); crc = (crc >> 1) ^ (0xEDB88320 & mask); } @@ -466,7 +466,7 @@ int CTapDriver::Receive(uint8_t *buf) // We need to add the Frame Check Status (FCS) CRC back onto the end of the packet. // The Linux network subsystem removes it, since most software apps shouldn't ever // need it. - const int crc = Crc32(buf, dwReceived); + const int crc = Crc32(span(buf, dwReceived)); buf[dwReceived + 0] = (uint8_t)((crc >> 0) & 0xFF); buf[dwReceived + 1] = (uint8_t)((crc >> 8) & 0xFF); diff --git a/cpp/devices/ctapdriver.h b/cpp/devices/ctapdriver.h index bb3bc37105..5f027cbbed 100644 --- a/cpp/devices/ctapdriver.h +++ b/cpp/devices/ctapdriver.h @@ -17,6 +17,7 @@ #include #include #include +#include using namespace std; @@ -41,7 +42,7 @@ class CTapDriver bool Disable() const; // Disable the piscsi0 interface void Flush(); // Purge all of the packets that are waiting to be processed - static uint32_t Crc32(const uint8_t *, int); + static uint32_t Crc32(span); private: array m_MacAddr; // MAC Address diff --git a/cpp/test/ctapdriver_test.cpp b/cpp/test/ctapdriver_test.cpp index 031e1accc1..f7d9dba0cd 100644 --- a/cpp/test/ctapdriver_test.cpp +++ b/cpp/test/ctapdriver_test.cpp @@ -3,7 +3,7 @@ // SCSI Target Emulator PiSCSI // for Raspberry Pi // -// Copyright (C) 2022 Uwe Seimet +// Copyright (C) 2022-2023 Uwe Seimet // //--------------------------------------------------------------------------- @@ -15,27 +15,27 @@ TEST(CTapDriverTest, Crc32) array buf; buf.fill(0x00); - EXPECT_EQ(0xe3d887bb, CTapDriver::Crc32(buf.data(), ETH_FRAME_LEN)); + EXPECT_EQ(0xe3d887bb, CTapDriver::Crc32(span(buf.data(), ETH_FRAME_LEN))); buf.fill(0xff); - EXPECT_EQ(0x814765f4, CTapDriver::Crc32(buf.data(), ETH_FRAME_LEN)); + EXPECT_EQ(0x814765f4, CTapDriver::Crc32(span(buf.data(), ETH_FRAME_LEN))); buf.fill(0x10); - EXPECT_EQ(0xb7288Cd3, CTapDriver::Crc32(buf.data(), ETH_FRAME_LEN)); + EXPECT_EQ(0xb7288Cd3, CTapDriver::Crc32(span(buf.data(), ETH_FRAME_LEN))); buf.fill(0x7f); - EXPECT_EQ(0x4b543477, CTapDriver::Crc32(buf.data(), ETH_FRAME_LEN)); + EXPECT_EQ(0x4b543477, CTapDriver::Crc32(span(buf.data(), ETH_FRAME_LEN))); buf.fill(0x80); - EXPECT_EQ(0x29cbd638, CTapDriver::Crc32(buf.data(), ETH_FRAME_LEN)); + EXPECT_EQ(0x29cbd638, CTapDriver::Crc32(span(buf.data(), ETH_FRAME_LEN))); for (size_t i = 0; i < buf.size(); i++) { buf[i] = (uint8_t)i; } - EXPECT_EQ(0xe7870705, CTapDriver::Crc32(buf.data(), ETH_FRAME_LEN)); + EXPECT_EQ(0xe7870705, CTapDriver::Crc32(span(buf.data(), ETH_FRAME_LEN))); for (size_t i = buf.size() - 1; i > 0; i--) { buf[i] = (uint8_t)i; } - EXPECT_EQ(0xe7870705, CTapDriver::Crc32(buf.data(), ETH_FRAME_LEN)); + EXPECT_EQ(0xe7870705, CTapDriver::Crc32(span(buf.data(), ETH_FRAME_LEN))); }