From aff547b738b274bd7189731ef6bde390b9834583 Mon Sep 17 00:00:00 2001 From: David Rebbe Date: Tue, 21 Jul 2026 17:07:06 +0000 Subject: [PATCH] C2: Report copied buffer lengths --- api/icsneoc2/icsneoc2.cpp | 22 +++++++++++++--------- test/unit/icsneoc2.cpp | 22 ++++++++++++++++++++++ 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/api/icsneoc2/icsneoc2.cpp b/api/icsneoc2/icsneoc2.cpp index 91915fd..defe87d 100644 --- a/api/icsneoc2/icsneoc2.cpp +++ b/api/icsneoc2/icsneoc2.cpp @@ -465,11 +465,13 @@ icsneoc2_error_t icsneoc2_device_pcb_serial_get(const icsneoc2_device_t* device, return icsneoc2_error_invalid_type; } const auto& data = *pcbSerial; - if(value) { - size_t copyLen = std::min(*value_length, data.size()); - std::copy(data.begin(), data.begin() + copyLen, value); + if(!value) { + *value_length = data.size(); + return icsneoc2_error_success; } - *value_length = data.size(); + size_t copyLen = std::min(*value_length, data.size()); + std::copy(data.begin(), data.begin() + copyLen, value); + *value_length = copyLen; return icsneoc2_error_success; } @@ -515,14 +517,16 @@ icsneoc2_error_t icsneoc2_mac_network_id_get(const icsneoc2_mac_addr_entry_t* ma } icsneoc2_error_t icsneoc2_mac_address_get(const icsneoc2_mac_addr_entry_t* mac_address, uint8_t* value, size_t* value_length) { - if(!mac_address || !value || !value_length) { + if(!mac_address || !value_length) { return icsneoc2_error_invalid_parameters; } - if(value) { - size_t copyLen = std::min(*value_length, static_cast(ICSNEO_MAC_ADDRESS_LEN)); - std::copy(mac_address->address, mac_address->address + copyLen, value); + if(!value) { + *value_length = static_cast(ICSNEO_MAC_ADDRESS_LEN); + return icsneoc2_error_success; } - *value_length = static_cast(ICSNEO_MAC_ADDRESS_LEN); + size_t copyLen = std::min(*value_length, static_cast(ICSNEO_MAC_ADDRESS_LEN)); + std::copy(mac_address->address, mac_address->address + copyLen, value); + *value_length = copyLen; return icsneoc2_error_success; } diff --git a/test/unit/icsneoc2.cpp b/test/unit/icsneoc2.cpp index 04d39ab..55f7e1b 100644 --- a/test/unit/icsneoc2.cpp +++ b/test/unit/icsneoc2.cpp @@ -127,6 +127,28 @@ TEST(icsneoc2, test_icsneoc2_device_is_valid) ASSERT_EQ(icsneoc2_error_invalid_parameters, icsneoc2_device_is_valid(NULL)); } +TEST(icsneoc2, test_icsneoc2_mac_address_get_query_length) +{ + icsneoc2_mac_addr_entry_t mac_address = {}; + size_t value_length = 0; + + ASSERT_EQ(icsneoc2_error_success, icsneoc2_mac_address_get(&mac_address, NULL, &value_length)); + ASSERT_EQ(ICSNEO_MAC_ADDRESS_LEN, value_length); +} + +TEST(icsneoc2, test_icsneoc2_mac_address_get_truncated_length) +{ + icsneoc2_mac_addr_entry_t mac_address = {}; + const uint8_t expected[ICSNEO_MAC_ADDRESS_LEN] = {0x00, 0xFC, 0x70, 0x1E, 0x18, 0x70}; + std::copy(std::begin(expected), std::end(expected), mac_address.address); + + uint8_t value[3] = {}; + size_t value_length = sizeof(value); + ASSERT_EQ(icsneoc2_error_success, icsneoc2_mac_address_get(&mac_address, value, &value_length)); + ASSERT_EQ(sizeof(value), value_length); + ASSERT_EQ(0, memcmp(expected, value, sizeof(value))); +} + TEST(icsneoc2, test_icsneoc2_error_invalid_parameters_and_invalid_device) { bool placeholderBool = false;