Make clang-tidy changes
clang-tidy has a number of checks it recommends. These checks are
documented in the next commit, but make the code pass our coding
standard.
Tested:
Minor changes made by the robot.
Signed-off-by: Ed Tanous <edtanous@google.com>
Change-Id: I6cfaab92211af9c4c1eccd981ba9fe7b8c523457
diff --git a/include/fru_reader.hpp b/include/fru_reader.hpp
index b1d21d8..f54d900 100644
--- a/include/fru_reader.hpp
+++ b/include/fru_reader.hpp
@@ -39,7 +39,7 @@
class FRUReader
{
public:
- FRUReader(ReadBlockFunc readFunc) : readFunc(std::move(readFunc))
+ explicit FRUReader(ReadBlockFunc readFunc) : readFunc(std::move(readFunc))
{}
// The ::read() operation here is analogous to ReadBlockFunc (with the same
// return value semantics), but is not subject to SMBus block size
diff --git a/include/fru_utils.hpp b/include/fru_utils.hpp
index ebb2b19..e50b580 100644
--- a/include/fru_utils.hpp
+++ b/include/fru_utils.hpp
@@ -17,6 +17,7 @@
#pragma once
#include "fru_reader.hpp"
+
#include <boost/container/flat_map.hpp>
#include <cstdint>
@@ -94,7 +95,7 @@
char sixBitToChar(uint8_t val);
/* 0xd - 0xf are reserved values, but not fatal; use a placeholder char. */
-const char bcdHighChars[] = {
+constexpr std::array<char, 6> bcdHighChars = {
' ', '-', '.', 'X', 'X', 'X',
};
diff --git a/include/utils.hpp b/include/utils.hpp
index bc8ac1b..99e2f85 100644
--- a/include/utils.hpp
+++ b/include/utils.hpp
@@ -132,22 +132,20 @@
}
std::optional<std::string> templateCharReplace(
- nlohmann::json::iterator& keyPair, const DBusObject& object,
- const size_t index,
+ nlohmann::json::iterator& keyPair, const DBusObject& object, size_t index,
const std::optional<std::string>& replaceStr = std::nullopt);
std::optional<std::string> templateCharReplace(
nlohmann::json::iterator& keyPair, const DBusInterface& interface,
- const size_t index,
- const std::optional<std::string>& replaceStr = std::nullopt);
+ size_t index, const std::optional<std::string>& replaceStr = std::nullopt);
inline bool deviceHasLogging(const nlohmann::json& json)
{
auto logging = json.find("Logging");
if (logging != json.end())
{
- auto ptr = logging->get_ptr<const std::string*>();
- if (ptr)
+ const auto* ptr = logging->get_ptr<const std::string*>();
+ if (ptr != nullptr)
{
if (*ptr == "Off")
{
diff --git a/src/entity_manager.cpp b/src/entity_manager.cpp
index 4a730b0..bfda895 100644
--- a/src/entity_manager.cpp
+++ b/src/entity_manager.cpp
@@ -200,7 +200,7 @@
}
template <typename PropertyType>
-void addProperty(const std::string& propertyName, const PropertyType& value,
+void addProperty(const std::string& name, const PropertyType& value,
sdbusplus::asio::dbus_interface* iface,
nlohmann::json& systemConfiguration,
const std::string& jsonPointerString,
@@ -208,11 +208,11 @@
{
if (permission == sdbusplus::asio::PropertyPermission::readOnly)
{
- iface->register_property(propertyName, value);
+ iface->register_property(name, value);
return;
}
iface->register_property(
- propertyName, value,
+ name, value,
[&systemConfiguration,
jsonPointerString{std::string(jsonPointerString)}](
const PropertyType& newVal, PropertyType& val) {
@@ -282,7 +282,7 @@
if (value.type() == nlohmann::json::value_t::array)
{
array = true;
- if (!value.size())
+ if (value.empty())
{
continue;
}
@@ -461,8 +461,11 @@
for (const auto& item : data)
{
nlohmann::json& newJson = newData[item.first];
- std::visit([&newJson](auto&& val) { newJson = std::move(val); },
- item.second);
+ std::visit(
+ [&newJson](auto&& val) {
+ newJson = std::forward<decltype(val)>(val);
+ },
+ item.second);
}
auto findName = newData.find("Name");
@@ -708,7 +711,7 @@
else if (config.type() == nlohmann::json::value_t::array)
{
size_t index = 0;
- if (!config.size())
+ if (config.empty())
{
continue;
}
@@ -838,8 +841,8 @@
return false;
}
- auto ptr = powerState->get_ptr<const std::string*>();
- if (!ptr)
+ const auto* ptr = powerState->get_ptr<const std::string*>();
+ if (ptr == nullptr)
{
return false;
}
diff --git a/src/fru_device.cpp b/src/fru_device.cpp
index b503d80..9d07e8e 100644
--- a/src/fru_device.cpp
+++ b/src/fru_device.cpp
@@ -32,6 +32,7 @@
#include <array>
#include <cerrno>
+#include <charconv>
#include <chrono>
#include <ctime>
#include <filesystem>
@@ -81,10 +82,8 @@
boost::asio::io_service io;
-uint8_t calculateChecksum(std::vector<uint8_t>::const_iterator iter,
- std::vector<uint8_t>::const_iterator end);
bool updateFRUProperty(
- const std::string& assetTag, uint32_t bus, uint32_t address,
+ const std::string& updatePropertyReq, uint32_t bus, uint32_t address,
const std::string& propertyName,
boost::container::flat_map<
std::pair<size_t, size_t>,
@@ -128,14 +127,17 @@
return read(fd, buf, len);
}
-static int busStrToInt(const std::string& busName)
+static int busStrToInt(const std::string_view busName)
{
auto findBus = busName.rfind('-');
if (findBus == std::string::npos)
{
return -1;
}
- return std::stoi(busName.substr(findBus + 1));
+ std::string_view num = busName.substr(findBus + 1);
+ int val = 0;
+ std::from_chars(num.data(), num.data() + num.size(), val);
+ return val;
}
static int getRootBus(size_t bus)
@@ -233,9 +235,10 @@
return -1;
}
-#define SMBUS_IOCTL_WRITE_THEN_READ_MSG_COUNT 2
- struct i2c_msg msgs[SMBUS_IOCTL_WRITE_THEN_READ_MSG_COUNT];
- struct i2c_rdwr_ioctl_data rdwr;
+ constexpr size_t smbusWriteThenReadMsgCount = 2;
+ std::array<struct i2c_msg, smbusWriteThenReadMsgCount> msgs{};
+ struct i2c_rdwr_ioctl_data rdwr
+ {};
msgs[0].addr = address;
msgs[0].flags = 0;
@@ -246,12 +249,12 @@
msgs[1].len = fromSlaveBufLen;
msgs[1].buf = fromSlaveBuf;
- rdwr.msgs = msgs;
- rdwr.nmsgs = SMBUS_IOCTL_WRITE_THEN_READ_MSG_COUNT;
+ rdwr.msgs = msgs.data();
+ rdwr.nmsgs = msgs.size();
int ret = ioctl(file, I2C_RDWR, &rdwr);
- return (ret == SMBUS_IOCTL_WRITE_THEN_READ_MSG_COUNT) ? msgs[1].len : -1;
+ return (ret == msgs.size()) ? msgs[1].len : -1;
}
static int64_t readBlockData(bool is16bit, int file, uint16_t address,
@@ -264,8 +267,9 @@
}
offset = htobe16(offset);
- return i2cSmbusWriteThenRead(
- file, address, reinterpret_cast<uint8_t*>(&offset), 2, buf, len);
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
+ uint8_t* u8Offset = reinterpret_cast<uint8_t*>(&offset);
+ return i2cSmbusWriteThenRead(file, address, u8Offset, 2, buf, len);
}
// TODO: This code is very similar to the non-eeprom version and can be merged
@@ -323,7 +327,7 @@
std::ssub_match subMatch = m[1];
std::string addressString = subMatch.str();
- std::size_t ignored;
+ std::size_t ignored = 0;
const int hexBase = 16;
int address = std::stoi(addressString, &ignored, hexBase);
@@ -535,17 +539,15 @@
std::exit(EXIT_FAILURE);
}
}
-
- return;
}
static void findI2CDevices(const std::vector<fs::path>& i2cBuses,
BusMap& busmap, const bool& powerIsOn,
sdbusplus::asio::object_server& objServer)
{
- for (auto& i2cBus : i2cBuses)
+ for (const auto& i2cBus : i2cBuses)
{
- int bus = busStrToInt(i2cBus);
+ int bus = busStrToInt(i2cBus.string());
if (bus < 0)
{
@@ -580,8 +582,8 @@
close(file);
continue;
}
- if (!(funcs & I2C_FUNC_SMBUS_READ_BYTE) ||
- !(I2C_FUNC_SMBUS_READ_I2C_BLOCK))
+ if (((funcs & I2C_FUNC_SMBUS_READ_BYTE) == 0U) ||
+ ((I2C_FUNC_SMBUS_READ_I2C_BLOCK) == 0))
{
std::cerr << "Error: Can't use SMBus Receive Byte command bus "
<< bus << "\n";
@@ -804,8 +806,9 @@
size_t fileSize = static_cast<size_t>(baseboardFRUFile.tellg());
baseboardFRU.resize(fileSize);
baseboardFRUFile.seekg(0, std::ios_base::beg);
- baseboardFRUFile.read(reinterpret_cast<char*>(baseboardFRU.data()),
- fileSize);
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
+ char* charOffset = reinterpret_cast<char*>(baseboardFRU.data());
+ baseboardFRUFile.read(charOffset, fileSize);
}
else
{
@@ -838,7 +841,9 @@
throw DBusInternalError();
return false;
}
- file.write(reinterpret_cast<const char*>(fru.data()), fru.size());
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
+ const char* charOffset = reinterpret_cast<const char*>(fru.data());
+ file.write(charOffset, fru.size());
return file.good();
}
@@ -888,7 +893,7 @@
size_t retries = retryMax;
while (index < fru.size())
{
- if ((index && ((index % (maxEepromPageIndex + 1)) == 0)) &&
+ if (((index != 0U) && ((index % (maxEepromPageIndex + 1)) == 0)) &&
(retries == retryMax))
{
// The 4K EEPROM only uses the A2 and A1 device address bits
@@ -905,7 +910,7 @@
if (i2c_smbus_write_byte_data(file, static_cast<uint8_t>(index),
fru[index]) < 0)
{
- if (!retries--)
+ if ((retries--) == 0U)
{
std::cerr << "error writing fru: " << strerror(errno) << "\n";
close(file);
@@ -1103,7 +1108,7 @@
return false;
}
- const std::vector<std::string>* fruAreaFieldNames;
+ const std::vector<std::string>* fruAreaFieldNames = nullptr;
uint8_t fruAreaOffsetFieldValue = 0;
size_t offset = 0;
@@ -1151,10 +1156,10 @@
size_t fruAreaEnd = fruAreaStart + fruAreaSize;
size_t fruDataIter = fruAreaStart + offset;
size_t skipToFRUUpdateField = 0;
- ssize_t fieldLength;
+ ssize_t fieldLength = 0;
bool found = false;
- for (auto& field : *fruAreaFieldNames)
+ for (const auto& field : *fruAreaFieldNames)
{
skipToFRUUpdateField++;
if (propertyName == propertyNamePrefix + field)
@@ -1235,7 +1240,7 @@
}
}
std::vector<uint8_t> restFRUAreasData;
- if (nextFRUAreaLoc)
+ if (nextFRUAreaLoc != 0U)
{
std::copy_n(fruData.begin() + nextFRUAreaLoc,
fruData.size() - nextFRUAreaLoc,
@@ -1429,7 +1434,7 @@
int fd = inotify_init();
inotify_add_watch(fd, i2CDevLocation, IN_CREATE | IN_MOVED_TO | IN_DELETE);
- std::array<char, 4096> readBuffer;
+ std::array<char, 4096> readBuffer{};
// monitor for new i2c devices
boost::asio::posix::stream_descriptor dirWatch(io, fd);
std::function<void(const boost::system::error_code, std::size_t)>
@@ -1443,14 +1448,15 @@
size_t index = 0;
while ((index + sizeof(inotify_event)) <= bytesTransferred)
{
- const inotify_event* iEvent =
- reinterpret_cast<const inotify_event*>(&readBuffer[index]);
+ const char* p = &readBuffer[index];
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
+ const auto* iEvent = reinterpret_cast<const inotify_event*>(p);
switch (iEvent->mask)
{
case IN_CREATE:
case IN_MOVED_TO:
case IN_DELETE:
- std::string name(iEvent->name);
+ std::string_view name(&iEvent->name[0], iEvent->len);
if (boost::starts_with(name, "i2c"))
{
int bus = busStrToInt(name);
diff --git a/src/fru_reader.cpp b/src/fru_reader.cpp
index e381e7e..61d4020 100644
--- a/src/fru_reader.cpp
+++ b/src/fru_reader.cpp
@@ -30,8 +30,8 @@
break;
}
- const uint8_t* blkData;
- size_t available;
+ const uint8_t* blkData = nullptr;
+ size_t available = 0;
size_t blk = cursor / cacheBlockSize;
size_t blkOffset = cursor % cacheBlockSize;
auto findBlk = cache.find(blk);
@@ -52,7 +52,7 @@
{
// don't leave empty blocks in the cache
cache.erase(blk);
- return done ? done : ret;
+ return done != 0U ? done : ret;
}
blkData = newData;
@@ -80,6 +80,7 @@
? 0
: std::min(available - blkOffset, remaining);
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
memcpy(outbuf + done, blkData + blkOffset, toCopy);
cursor += toCopy;
done += toCopy;
diff --git a/src/fru_utils.cpp b/src/fru_utils.cpp
index 99f4aa9..bc59af8 100644
--- a/src/fru_utils.cpp
+++ b/src/fru_utils.cpp
@@ -18,6 +18,7 @@
#include "fru_utils.hpp"
#include <array>
+#include <cstddef>
#include <cstdint>
#include <filesystem>
#include <iostream>
@@ -75,7 +76,7 @@
bool isLangEng)
{
std::string value;
- unsigned int i;
+ unsigned int i = 0;
/* we need at least one byte to decode the type/len header */
if (iter == end)
@@ -171,7 +172,7 @@
{
// If Lang is not English then the encoding is defined as 2-byte UNICODE,
// but we don't support that.
- if (lang && lang != 25)
+ if ((lang != 0U) && lang != 25)
{
std::cerr << "Warning: languages other than English is not "
"supported\n";
@@ -269,7 +270,7 @@
result["Common_Format_Version"] =
std::to_string(static_cast<int>(*fruBytes.begin()));
- const std::vector<std::string>* fruAreaFieldNames;
+ const std::vector<std::string>* fruAreaFieldNames = nullptr;
// Don't parse Internal and Multirecord areas
for (fruAreas area = fruAreas::fruAreaChassis;
@@ -353,12 +354,12 @@
*(fruBytesIter + 2) << 16;
std::tm fruTime = intelEpoch();
std::time_t timeValue = std::mktime(&fruTime);
- timeValue += minutes * 60;
+ timeValue += static_cast<long>(minutes) * 60;
fruTime = *std::gmtime(&timeValue);
// Tue Nov 20 23:08:00 2018
- char timeString[32] = {0};
- auto bytes = std::strftime(timeString, sizeof(timeString),
+ std::array<char, 32> timeString = {};
+ auto bytes = std::strftime(timeString.data(), timeString.size(),
"%Y-%m-%d - %H:%M:%S", &fruTime);
if (bytes == 0)
{
@@ -366,7 +367,8 @@
return resCodes::resErr;
}
- result["BOARD_MANUFACTURE_DATE"] = std::string(timeString);
+ result["BOARD_MANUFACTURE_DATE"] =
+ std::string_view(timeString.data(), bytes);
fruBytesIter += 3;
fruAreaFieldNames = &boardFruAreas;
break;
@@ -389,7 +391,7 @@
}
}
size_t fieldIndex = 0;
- DecodeState state;
+ DecodeState state = DecodeState::ok;
do
{
auto res =
@@ -447,7 +449,7 @@
for (; fruBytesIter < fruBytesIterEndArea; fruBytesIter++)
{
uint8_t c = *fruBytesIter;
- if (c)
+ if (c != 0U)
{
std::cerr << "Non-zero byte after EndOfFields in FRU area "
<< getFruAreaName(area) << "\n";
@@ -489,8 +491,8 @@
fruData.begin() + fruAreaEndOffset, 0);
size_t mod = traverseFRUAreaIndex % fruBlockSize;
- size_t checksumLoc;
- if (!mod)
+ size_t checksumLoc = 0;
+ if (mod == 0U)
{
traverseFRUAreaIndex += (fruBlockSize);
checksumLoc = fruAreaEndOfFieldsOffset + (fruBlockSize - 1);
@@ -501,8 +503,9 @@
checksumLoc = fruAreaEndOfFieldsOffset + (fruBlockSize - mod - 1);
}
- size_t newFRUAreaLen = (traverseFRUAreaIndex / fruBlockSize) +
- ((traverseFRUAreaIndex % fruBlockSize) != 0);
+ size_t newFRUAreaLen =
+ (traverseFRUAreaIndex / fruBlockSize) +
+ static_cast<unsigned long>((traverseFRUAreaIndex % fruBlockSize) != 0);
size_t fruAreaLengthLoc = fruAreaStart + 1;
fruData[fruAreaLengthLoc] = static_cast<uint8_t>(newFRUAreaLen);
@@ -634,7 +637,7 @@
std::vector<uint8_t> readFRUContents(FRUReader& reader,
const std::string& errorHelp)
{
- std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> blockData;
+ std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> blockData{};
off_t baseOffset = 0x0;
if (!findFRUHeader(reader, errorHelp, blockData, baseOffset))
@@ -725,7 +728,7 @@
fruLength = (areaOffset > fruLength) ? areaOffset : fruLength;
// If this is the end of the list bail.
- if ((blockData[1] & multiRecordEndOfListMask))
+ if ((blockData[1] & multiRecordEndOfListMask) != 0)
{
break;
}
diff --git a/src/overlay.cpp b/src/overlay.cpp
index 966bfd1..9dfb6b1 100644
--- a/src/overlay.cpp
+++ b/src/overlay.cpp
@@ -17,8 +17,8 @@
#include "overlay.hpp"
-#include "utils.hpp"
#include "devices.hpp"
+#include "utils.hpp"
#include <boost/algorithm/string/predicate.hpp>
#include <boost/asio/io_context.hpp>
@@ -187,7 +187,7 @@
const std::string& destructor, const bool createsHWMon,
const size_t retries = 5)
{
- if (!retries)
+ if (retries == 0U)
{
return -1;
}
@@ -282,7 +282,8 @@
int err = buildDevice(busPath, parameters, bus, address, constructor,
destructor, createsHWMon);
- if (!err && boost::ends_with(type, "Mux") && bus && address && channels)
+ if ((err == 0) && boost::ends_with(type, "Mux") && bus && address &&
+ (channels != nullptr))
{
linkMux(name, static_cast<size_t>(*bus), static_cast<size_t>(*address),
*channels);
@@ -302,7 +303,7 @@
continue;
}
- for (auto& configuration : *findExposes)
+ for (const auto& configuration : *findExposes)
{
auto findStatus = configuration.find("Status");
// status missing is assumed to be 'okay'
diff --git a/src/perform_probe.cpp b/src/perform_probe.cpp
index d5b7f3b..04ba87d 100644
--- a/src/perform_probe.cpp
+++ b/src/perform_probe.cpp
@@ -92,7 +92,7 @@
probe_type_codes lastCommand = probe_type_codes::FALSE_T;
bool first = true;
- for (auto& probe : probeCommand)
+ for (const auto& probe : probeCommand)
{
FoundProbeTypeT probeType = findProbeType(probe);
if (probeType)
@@ -198,7 +198,7 @@
}
// probe passed, but empty device
- if (ret && foundDevs.size() == 0)
+ if (ret && foundDevs.empty())
{
// Use emplace back when clang implements
// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p0960r3.html
diff --git a/src/perform_scan.cpp b/src/perform_scan.cpp
index bf3c750..559b822 100644
--- a/src/perform_scan.cpp
+++ b/src/perform_scan.cpp
@@ -50,7 +50,7 @@
const std::vector<std::shared_ptr<PerformProbe>>& probeVector,
const std::shared_ptr<PerformScan>& scan, size_t retries = 5)
{
- if (!retries)
+ if (retries == 0U)
{
std::cerr << "retries exhausted on " << instance.busName << " "
<< instance.path << " " << instance.interface << "\n";
@@ -83,7 +83,7 @@
if constexpr (debug)
{
- std::cerr << __func__ << " " << __LINE__ << "\n";
+ std::cerr << __LINE__ << "\n";
}
}
@@ -172,7 +172,7 @@
}
std::cerr << "Error communicating to mapper.\n";
- if (!retries)
+ if (retries == 0U)
{
// if we can't communicate to the mapper something is very
// wrong
@@ -202,7 +202,7 @@
if constexpr (debug)
{
- std::cerr << __func__ << " " << __LINE__ << "\n";
+ std::cerr << __LINE__ << "\n";
}
}
@@ -216,7 +216,7 @@
// use an array so alphabetical order from the flat_map is maintained
auto device = nlohmann::json::array();
- for (auto& devPair : probe)
+ for (const auto& devPair : probe)
{
device.push_back(devPair.first);
std::visit([&device](auto&& v) { device.push_back(v); },
@@ -283,7 +283,9 @@
int index = 0;
auto str = nameIt->get<std::string>().substr(indexIdx);
- auto [p, ec] = std::from_chars(str.data(), str.data() + str.size(), index);
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
+ const char* endPtr = str.data() + str.size();
+ auto [p, ec] = std::from_chars(str.data(), endPtr, index);
if (ec != std::errc())
{
return; // non-numeric replacement
@@ -638,7 +640,7 @@
std::cerr << "Probe statement wasn't a string, can't parse";
continue;
}
- if (findProbeType(probe->c_str()))
+ if (findProbeType(*probe))
{
continue;
}
@@ -657,7 +659,7 @@
std::move(dbusProbeInterfaces), shared_from_this());
if constexpr (debug)
{
- std::cerr << __func__ << " " << __LINE__ << "\n";
+ std::cerr << __LINE__ << "\n";
}
}
@@ -674,7 +676,7 @@
if constexpr (debug)
{
- std::cerr << __func__ << " " << __LINE__ << "\n";
+ std::cerr << __LINE__ << "\n";
}
}
else
@@ -683,7 +685,7 @@
if constexpr (debug)
{
- std::cerr << __func__ << " " << __LINE__ << "\n";
+ std::cerr << __LINE__ << "\n";
}
}
}
diff --git a/src/utils.cpp b/src/utils.cpp
index ecd108a..2b77755 100644
--- a/src/utils.cpp
+++ b/src/utils.cpp
@@ -136,11 +136,7 @@
parser.populateSchema(schemaAdapter, schema);
valijson::Validator validator;
valijson::adapters::NlohmannJsonAdapter targetAdapter(input);
- if (!validator.validate(schema, targetAdapter, nullptr))
- {
- return false;
- }
- return true;
+ return validator.validate(schema, targetAdapter, nullptr);
}
bool isPowerOn(void)
@@ -239,7 +235,7 @@
boost::replace_all(*strPtr, *replaceStr, std::to_string(index));
}
- for (auto& [propName, propValue] : interface)
+ for (const auto& [propName, propValue] : interface)
{
std::string templateName = templateChar + propName;
boost::iterator_range<std::string::const_iterator> find =
@@ -252,7 +248,7 @@
size_t start = find.begin() - strPtr->begin();
// check for additional operations
- if (!start && find.end() == strPtr->end())
+ if ((start == 0U) && find.end() == strPtr->end())
{
std::visit([&](auto&& val) { keyPair.value() = val; }, propValue);
return ret;
diff --git a/test/test_fru-utils.cpp b/test/test_fru-utils.cpp
index 02abc34..9470e4c 100644
--- a/test/test_fru-utils.cpp
+++ b/test/test_fru-utils.cpp
@@ -155,9 +155,9 @@
return 0;
}
- uint16_t idx;
- for (idx = offset; idx < data.size() && idx < offset + length;
- ++idx, ++outBuf)
+ uint16_t idx = offset;
+ // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
+ for (; idx < std::min(data.size(), offset + length); ++idx, ++outBuf)
{
*outBuf = data[idx];
}
@@ -172,7 +172,7 @@
{
data.push_back(i);
}
- std::array<uint8_t, blockSize * 2> rdbuf;
+ std::array<uint8_t, blockSize * 2> rdbuf{};
auto getData = [&data](auto o, auto l, auto* b) {
return getDataTempl(data, o, l, b);
};
@@ -206,7 +206,7 @@
{
std::vector<uint8_t> data = {};
data.resize(blockSize / 2);
- std::array<uint8_t, blockSize> blockData;
+ std::array<uint8_t, blockSize> blockData{};
auto getData = [&data](auto o, auto l, auto* b) {
return getDataTempl(data, o, l, b);
};
@@ -238,7 +238,8 @@
TEST(FRUReaderTest, CacheHit)
{
std::vector<uint8_t> data = {'X'};
- std::array<uint8_t, blockSize> read1, read2;
+ std::array<uint8_t, blockSize> read1{};
+ std::array<uint8_t, blockSize> read2{};
auto getData = [&data](auto o, auto l, auto* b) {
return getDataTempl(data, o, l, b);
};
@@ -255,7 +256,7 @@
TEST(FRUReaderTest, ReadPastKnownEnd)
{
const std::vector<uint8_t> data = {'X', 'Y'};
- std::array<uint8_t, blockSize> rdbuf;
+ std::array<uint8_t, blockSize> rdbuf{};
auto getData = [&data](auto o, auto l, auto* b) {
return getDataTempl(data, o, l, b);
};
@@ -275,7 +276,7 @@
std::vector<uint8_t> data = {};
data.resize(blockSize, 'X');
data.resize(2 * blockSize, 'Y');
- std::array<uint8_t, 2 * blockSize> rdbuf;
+ std::array<uint8_t, 2 * blockSize> rdbuf{};
auto getData = [&data](auto o, auto l, auto* b) {
return getDataTempl(data, o, l, b);
};
@@ -290,7 +291,7 @@
{
std::vector<uint8_t> data = {};
data.resize(3 * blockSize, 'X');
- std::array<uint8_t, blockSize> rdbuf;
+ std::array<uint8_t, blockSize> rdbuf{};
auto getData = [&data](auto o, auto l, auto* b) {
return getDataTempl(data, o, l, b);
};
@@ -305,7 +306,7 @@
{
const std::vector<uint8_t> data = {255, 16};
off_t offset = 0;
- std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> blockData;
+ std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> blockData{};
auto getData = [&data](auto o, auto l, auto* b) {
return getDataTempl(data, o, l, b);
};
@@ -318,7 +319,7 @@
{
const std::vector<uint8_t> data = {};
off_t offset = 0;
- std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> blockData;
+ std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> blockData{};
auto getData = [&data](auto o, auto l, auto* b) {
return getDataTempl(data, o, l, b);
};
@@ -332,7 +333,7 @@
const std::vector<uint8_t> data = {0x01, 0x00, 0x01, 0x02,
0x03, 0x04, 0x00, 0xf5};
off_t offset = 0;
- std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> blockData;
+ std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> blockData{};
auto getData = [&data](auto o, auto l, auto* b) {
return getDataTempl(data, o, l, b);
};
@@ -347,7 +348,7 @@
std::vector<uint8_t> data = {'$', 'T', 'Y', 'A', 'N', '$', 0, 0};
data.resize(0x6000 + I2C_SMBUS_BLOCK_MAX);
off_t offset = 0;
- std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> blockData;
+ std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> blockData{};
auto getData = [&data](auto o, auto l, auto* b) {
return getDataTempl(data, o, l, b);
};
@@ -360,7 +361,7 @@
{
const std::vector<uint8_t> data = {'$', 'T', 'Y', 'A', 'N', '$', 0, 0};
off_t offset = 0;
- std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> blockData;
+ std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> blockData{};
auto getData = [&data](auto o, auto l, auto* b) {
return getDataTempl(data, o, l, b);
};
@@ -378,7 +379,7 @@
copy(fruHeader.begin(), fruHeader.end(), back_inserter(data));
off_t offset = 0;
- std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> blockData;
+ std::array<uint8_t, I2C_SMBUS_BLOCK_MAX> blockData{};
auto getData = [&data](auto o, auto l, auto* b) {
return getDataTempl(data, o, l, b);
};