Skip to content

Commit ce9ea2d

Browse files
committed
apply static analysis suggestions
1 parent 252cd21 commit ce9ea2d

22 files changed

Lines changed: 174 additions & 181 deletions

src/BinaryFile.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@
1212

1313
namespace tivars
1414
{
15-
/**
16-
* @param null filePath
17-
* @throws \Exception
18-
*/
1915
BinaryFile::BinaryFile(const std::string& filePath)
2016
{
2117
if (!filePath.empty())
@@ -46,7 +42,7 @@ namespace tivars
4642
* @return uint8_t
4743
* @throws runtime_error
4844
*/
49-
uint8_t BinaryFile::get_raw_byte()
45+
uint8_t BinaryFile::get_raw_byte() const
5046
{
5147
if (file)
5248
{
@@ -65,11 +61,11 @@ namespace tivars
6561
/**
6662
* Returns an array of bytes bytes read from the file
6763
*
68-
* @param size_t bytes
64+
* @param bytes The number of bytes to read
6965
* @return data_t
7066
* @throws runtime_error
7167
*/
72-
data_t BinaryFile::get_raw_bytes(size_t bytes)
68+
data_t BinaryFile::get_raw_bytes(size_t bytes) const
7369
{
7470
if (file)
7571
{
@@ -88,11 +84,11 @@ namespace tivars
8884
/**
8985
* Returns a string of bytes bytes read from the file (doesn't stop at NUL)
9086
*
91-
* @param size_t bytes The number of bytes to read
87+
* @param bytes The number of bytes to read
9288
* @return string
9389
* @throws runtime_error
9490
*/
95-
std::string BinaryFile::get_string_bytes(size_t bytes)
91+
std::string BinaryFile::get_string_bytes(size_t bytes) const
9692
{
9793
if (file)
9894
{

src/BinaryFile.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ namespace tivars
3030
close();
3131
}
3232

33-
uint8_t get_raw_byte();
34-
data_t get_raw_bytes(size_t bytes);
35-
std::string get_string_bytes(size_t bytes);
33+
uint8_t get_raw_byte() const;
34+
data_t get_raw_bytes(size_t bytes) const;
35+
std::string get_string_bytes(size_t bytes) const;
3636
size_t size() const;
3737
void close();
3838

src/TIFlashFile.cpp

Lines changed: 47 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ namespace tivars
133133

134134
std::string format_unix_timestamp(uint32_t timestamp)
135135
{
136-
const std::time_t raw = static_cast<std::time_t>(timestamp);
136+
const std::time_t raw = timestamp;
137137
std::tm tm{};
138138
#ifdef _WIN32
139139
gmtime_s(&tm, &raw);
@@ -161,8 +161,7 @@ namespace tivars
161161
const size_t infoStart = start;
162162
const uint32_t mainOffset = read_le24(data, infoStart + 18);
163163
out["name"] = std::string(data.begin() + static_cast<ptrdiff_t>(infoStart + 3), data.begin() + static_cast<ptrdiff_t>(infoStart + 12));
164-
const size_t nulPos = out["name"].get<std::string>().find('\0');
165-
if (nulPos != std::string::npos)
164+
if (const size_t nulPos = out["name"].get<std::string>().find('\0'); nulPos != std::string::npos)
166165
{
167166
out["name"] = out["name"].get<std::string>().substr(0, nulPos);
168167
}
@@ -179,8 +178,7 @@ namespace tivars
179178
if (copyrightOffset < size)
180179
{
181180
out["copyright"] = std::string(data.begin() + static_cast<ptrdiff_t>(start + copyrightOffset), data.begin() + static_cast<ptrdiff_t>(start + size));
182-
const size_t nulPos = out["copyright"].get<std::string>().find('\0');
183-
if (nulPos != std::string::npos)
181+
if (const size_t nulPos = out["copyright"].get<std::string>().find('\0'); nulPos != std::string::npos)
184182
{
185183
out["copyright"] = out["copyright"].get<std::string>().substr(0, nulPos);
186184
}
@@ -402,11 +400,11 @@ namespace tivars
402400
j["hasChecksum"] = header.hasChecksum;
403401

404402
j["devices"] = json::array();
405-
for (const auto& device : header.devices)
403+
for (const auto& [devType, typeId] : header.devices)
406404
{
407405
j["devices"].push_back({
408-
{"deviceType", device.first},
409-
{"typeId", device.second},
406+
{"deviceType", devType},
407+
{"typeId", typeId},
410408
});
411409
}
412410

@@ -429,12 +427,12 @@ namespace tivars
429427
else
430428
{
431429
j["blocks"] = json::array();
432-
for (const auto& block : parseIntelBlocks(header.calcData))
430+
for (const auto& [address, blockType, data] : parseIntelBlocks(header.calcData))
433431
{
434432
j["blocks"].push_back({
435-
{"address", toHex({static_cast<uint8_t>((block.address >> 8) & 0xFF), static_cast<uint8_t>(block.address & 0xFF)})},
436-
{"blockType", dechex(block.blockType)},
437-
{"dataHex", toHex(block.data)},
433+
{"address", toHex({static_cast<uint8_t>((address >> 8) & 0xFF), static_cast<uint8_t>(address & 0xFF)})},
434+
{"blockType", dechex(blockType)},
435+
{"dataHex", toHex(data)},
438436
});
439437
}
440438
}
@@ -449,7 +447,7 @@ namespace tivars
449447
throw std::out_of_range("Invalid flash header index");
450448
}
451449

452-
flash_header_t& header = headers[headerIdx];
450+
auto& [magic, revision, binaryFlag, objectType, date, name, devices, productId, calcData, hasChecksum, type, model] = headers[headerIdx];
453451
const json j = json::parse(str);
454452
if (!j.is_object())
455453
{
@@ -458,58 +456,58 @@ namespace tivars
458456

459457
if (j.contains("typeName"))
460458
{
461-
header.type = TIVarType{j.at("typeName").get<std::string>()};
459+
type = TIVarType{j.at("typeName").get<std::string>()};
462460
}
463461
if (j.contains("magic"))
464462
{
465-
header.magic = j.at("magic").get<std::string>();
463+
magic = j.at("magic").get<std::string>();
466464
}
467465
if (j.contains("revision"))
468466
{
469-
header.revision = j.at("revision").get<std::string>();
467+
revision = j.at("revision").get<std::string>();
470468
}
471469
if (j.contains("binaryFlag"))
472470
{
473-
header.binaryFlag = static_cast<uint8_t>(j.at("binaryFlag").get<int>());
471+
binaryFlag = static_cast<uint8_t>(j.at("binaryFlag").get<int>());
474472
}
475473
if (j.contains("objectType"))
476474
{
477-
header.objectType = static_cast<uint8_t>(j.at("objectType").get<int>());
475+
objectType = static_cast<uint8_t>(j.at("objectType").get<int>());
478476
}
479477
if (j.contains("date"))
480478
{
481-
const json& date = j.at("date");
482-
if (!date.is_array() || date.size() != 3)
479+
const json& jsonDate = j.at("date");
480+
if (!jsonDate.is_array() || jsonDate.size() != 3)
483481
{
484482
throw std::invalid_argument("Flash header date must be a [day, month, year] array");
485483
}
486-
header.date.day = static_cast<uint8_t>(date[0].get<int>());
487-
header.date.month = static_cast<uint8_t>(date[1].get<int>());
488-
header.date.year = static_cast<uint16_t>(date[2].get<int>());
484+
date.day = static_cast<uint8_t>(jsonDate[0].get<int>());
485+
date.month = static_cast<uint8_t>(jsonDate[1].get<int>());
486+
date.year = static_cast<uint16_t>(jsonDate[2].get<int>());
489487
}
490488
if (j.contains("name"))
491489
{
492-
header.name = j.at("name").get<std::string>();
490+
name = j.at("name").get<std::string>();
493491
}
494492
if (j.contains("devices"))
495493
{
496-
header.devices.clear();
494+
devices.clear();
497495
for (const json& device : j.at("devices"))
498496
{
499-
header.devices.emplace_back(
497+
devices.emplace_back(
500498
static_cast<uint8_t>(device.at("deviceType").get<int>()),
501499
static_cast<uint8_t>(device.at("typeId").get<int>())
502500
);
503501
}
504502
}
505503
if (j.contains("productId"))
506504
{
507-
header.productId = static_cast<uint8_t>(j.at("productId").get<int>());
508-
header.model = modelFromProductId(header.productId);
505+
productId = static_cast<uint8_t>(j.at("productId").get<int>());
506+
model = modelFromProductId(productId);
509507
}
510508
if (j.contains("hasChecksum"))
511509
{
512-
header.hasChecksum = j.at("hasChecksum").get<bool>();
510+
hasChecksum = j.at("hasChecksum").get<bool>();
513511
}
514512

515513
if (j.contains("blocks"))
@@ -522,27 +520,27 @@ namespace tivars
522520
{
523521
throw std::invalid_argument("Flash block address must be a two-byte hex string");
524522
}
525-
flash_block_t block{};
526-
block.address = static_cast<uint16_t>((addressBytes[0] << 8) | addressBytes[1]);
527-
block.blockType = static_cast<uint8_t>(hexdec(item.at("blockType").get<std::string>()));
528-
block.data = parseHex(item.at("dataHex").get<std::string>());
529-
blocks.push_back(block);
523+
blocks.emplace_back(
524+
static_cast<uint16_t>((addressBytes[0] << 8) | addressBytes[1]),
525+
hexdec(item.at("blockType").get<std::string>()),
526+
parseHex(item.at("dataHex").get<std::string>())
527+
);
530528
}
531-
header.calcData = makeIntelData(blocks);
529+
calcData = makeIntelData(blocks);
532530
}
533531
else if (j.contains("calcDataHex"))
534532
{
535-
header.calcData = parseHex(j.at("calcDataHex").get<std::string>());
533+
calcData = parseHex(j.at("calcDataHex").get<std::string>());
536534
}
537-
else if (header.type.getName() == "FlashLicense" && j.contains("license"))
535+
else if (type.getName() == "FlashLicense" && j.contains("license"))
538536
{
539537
const std::string license = j.at("license").get<std::string>();
540-
header.calcData.assign(license.begin(), license.end());
538+
calcData.assign(license.begin(), license.end());
541539
}
542540

543-
if (!header.devices.empty())
541+
if (!devices.empty())
544542
{
545-
header.type = TIVarType{header.devices.front().second};
543+
type = TIVarType{devices.front().second};
546544
}
547545
}
548546

@@ -737,7 +735,7 @@ namespace tivars
737735
uint16_t TIFlashFile::computeChecksum(const data_t& data)
738736
{
739737
uint32_t sum = 0;
740-
for (uint8_t byte : data)
738+
for (const uint8_t byte : data)
741739
{
742740
sum += byte;
743741
}
@@ -791,7 +789,7 @@ namespace tivars
791789
<< std::setw(2) << static_cast<int>(block.data.size())
792790
<< std::setw(4) << static_cast<int>(block.address)
793791
<< std::setw(2) << static_cast<int>(block.blockType);
794-
for (uint8_t byte : block.data)
792+
for (const uint8_t byte : block.data)
795793
{
796794
out << std::setw(2) << static_cast<int>(byte);
797795
}
@@ -800,11 +798,11 @@ namespace tivars
800798
+ ((block.address >> 8) & 0xFF)
801799
+ (block.address & 0xFF)
802800
+ block.blockType;
803-
for (uint8_t byte : block.data)
801+
for (const uint8_t byte : block.data)
804802
{
805803
checksumBase += byte;
806804
}
807-
out << std::setw(2) << static_cast<int>((-static_cast<int>(checksumBase)) & 0xFF);
805+
out << std::setw(2) << (-static_cast<int>(checksumBase) & 0xFF);
808806
return out.str();
809807
}
810808

@@ -824,15 +822,15 @@ namespace tivars
824822

825823
flash_block_t block{};
826824
block.address = static_cast<uint16_t>(std::stoul(record.substr(3, 4), nullptr, 16));
827-
block.blockType = static_cast<uint8_t>(hexdec(record.substr(7, 2)));
825+
block.blockType = hexdec(record.substr(7, 2));
828826
block.data = parseHex(record.substr(9, size * 2));
829827

830-
const uint8_t fileChecksum = static_cast<uint8_t>(hexdec(record.substr(record.size() - 2, 2)));
828+
const uint8_t fileChecksum = hexdec(record.substr(record.size() - 2, 2));
831829
uint32_t checksumBase = static_cast<uint32_t>(block.data.size())
832830
+ ((block.address >> 8) & 0xFF)
833831
+ (block.address & 0xFF)
834832
+ block.blockType;
835-
for (uint8_t byte : block.data)
833+
for (const uint8_t byte : block.data)
836834
{
837835
checksumBase += byte;
838836
}
@@ -879,7 +877,7 @@ namespace tivars
879877
data.reserve(hex.size() / 2);
880878
for (size_t i = 0; i < hex.size(); i += 2)
881879
{
882-
data.push_back(static_cast<uint8_t>(hexdec(hex.substr(i, 2))));
880+
data.push_back(hexdec(hex.substr(i, 2)));
883881
}
884882
return data;
885883
}
@@ -888,7 +886,7 @@ namespace tivars
888886
{
889887
std::ostringstream out;
890888
out << std::uppercase << std::hex << std::setfill('0');
891-
for (uint8_t byte : data)
889+
for (const uint8_t byte : data)
892890
{
893891
out << std::setw(2) << static_cast<int>(byte);
894892
}

src/TIModels.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ namespace tivars
3535
{
3636
const TIModel model(orderID, name, flags, sig, productId);
3737

38-
if (!models.count(name))
38+
if (!models.contains(name))
3939
models[name] = model;
4040

4141
const std::string pid_str = std::to_string(productId);
42-
if (!models.count(pid_str))
42+
if (!models.contains(pid_str))
4343
models[pid_str] = model;
4444

45-
if (!models.count(sig))
45+
if (!models.contains(sig))
4646
models[sig] = model;
4747
}
4848

@@ -85,17 +85,17 @@ namespace tivars
8585

8686
bool TIModels::isValidPID(uint8_t pid)
8787
{
88-
return (pid > 0 && models.count(std::to_string(pid)));
88+
return (pid > 0 && models.contains(std::to_string(pid)));
8989
}
9090

9191
bool TIModels::isValidName(const std::string& name)
9292
{
93-
return (!name.empty() && models.count(name));
93+
return (!name.empty() && models.contains(name));
9494
}
9595

9696
bool TIModels::isValidSignature(const std::string& sig)
9797
{
98-
return (!sig.empty() && models.count(sig));
98+
return (!sig.empty() && models.contains(sig));
9999
}
100100

101101
};

0 commit comments

Comments
 (0)