refactor: use std::cmp_* instead of static_casts

This commit is contained in:
Marcus Holland-Moritz 2025-03-16 17:12:44 +01:00
parent d11a591837
commit a3182ae851
6 changed files with 25 additions and 24 deletions

View File

@ -22,6 +22,7 @@
#include <algorithm> #include <algorithm>
#include <array> #include <array>
#include <cstring> #include <cstring>
#include <utility>
#include <fmt/format.h> #include <fmt/format.h>
@ -115,7 +116,7 @@ filesystem_parser::filesystem_parser(std::shared_ptr<mmif> mm,
, image_offset_{find_image_offset(*mm_, image_offset)} , image_offset_{find_image_offset(*mm_, image_offset)}
, image_size_{ , image_size_{
std::min<file_off_t>(image_size, mm_->size() - image_offset_)} { std::min<file_off_t>(image_size, mm_->size() - image_offset_)} {
if (image_size_ < static_cast<file_off_t>(sizeof(file_header))) { if (std::cmp_less(image_size_, sizeof(file_header))) {
DWARFS_THROW(runtime_error, "file too small"); DWARFS_THROW(runtime_error, "file too small");
} }
@ -146,16 +147,16 @@ filesystem_parser::filesystem_parser(std::shared_ptr<mmif> mm,
std::optional<fs_section> filesystem_parser::next_section() { std::optional<fs_section> filesystem_parser::next_section() {
if (index_.empty()) { if (index_.empty()) {
if (offset_ < image_offset_ + image_size_) { if (std::cmp_less(offset_, image_offset_ + image_size_)) {
auto section = fs_section(*mm_, offset_, version_); auto section = fs_section(*mm_, offset_, version_);
offset_ = section.end(); offset_ = section.end();
return section; return section;
} }
} else { } else {
if (offset_ < static_cast<file_off_t>(index_.size())) { if (std::cmp_less(offset_, index_.size())) {
uint64_t id = index_[offset_++]; uint64_t id = index_[offset_++];
uint64_t offset = id & section_offset_mask; uint64_t offset = id & section_offset_mask;
uint64_t next_offset = offset_ < static_cast<file_off_t>(index_.size()) uint64_t next_offset = std::cmp_less(offset_, index_.size())
? index_[offset_] & section_offset_mask ? index_[offset_] & section_offset_mask
: image_size_; : image_size_;
return fs_section(mm_, static_cast<section_type>(id >> 48), return fs_section(mm_, static_cast<section_type>(id >> 48),
@ -212,7 +213,7 @@ void filesystem_parser::find_index() {
index_pos &= section_offset_mask; index_pos &= section_offset_mask;
index_pos += image_offset_; index_pos += image_offset_;
if (index_pos < static_cast<uint64_t>(image_offset_ + image_size_)) { if (std::cmp_less(index_pos, image_offset_ + image_size_)) {
auto section = fs_section(*mm_, index_pos, version_); auto section = fs_section(*mm_, index_pos, version_);
if (section.check_fast(*mm_)) { if (section.check_fast(*mm_)) {

View File

@ -284,7 +284,7 @@ inode_reader_<LoggerPolicy>::read_internal(uint32_t inode, size_t const size,
while (it < end) { while (it < end) {
size_t chunksize = it->size(); size_t chunksize = it->size();
if (static_cast<size_t>(offset) < chunksize) { if (std::cmp_less(offset, chunksize)) {
break; break;
} }

View File

@ -656,7 +656,7 @@ void check_chunks(global_metadata::Meta const& meta) {
std::array<size_t, 6> check_partitioning(global_metadata::Meta const& meta) { std::array<size_t, 6> check_partitioning(global_metadata::Meta const& meta) {
std::array<size_t, 6> offsets; std::array<size_t, 6> offsets;
for (int r = 0; r < static_cast<int>(offsets.size()); ++r) { for (int r = 0; std::cmp_less(r, offsets.size()); ++r) {
if (auto dep = meta.dir_entries()) { if (auto dep = meta.dir_entries()) {
auto pred = [r, modes = meta.modes()](auto ino) { auto pred = [r, modes = meta.modes()](auto ino) {
return mode_rank(modes[ino.mode_index()]) < r; return mode_rank(modes[ino.mode_index()]) < r;

View File

@ -99,7 +99,7 @@ void check_schema(std::span<uint8_t const> data) {
} }
for (auto const& kvl : *schema.layouts()) { for (auto const& kvl : *schema.layouts()) {
auto const& layout = kvl.second; auto const& layout = kvl.second;
if (kvl.first >= static_cast<int64_t>(schema.layouts()->size())) { if (std::cmp_greater_equal(kvl.first, schema.layouts()->size())) {
DWARFS_THROW(runtime_error, "invalid layout key in schema"); DWARFS_THROW(runtime_error, "invalid layout key in schema");
} }
if (*layout.size() < 0) { if (*layout.size() < 0) {
@ -434,8 +434,8 @@ class metadata_ final : public metadata_v2::impl {
PERFMON_CLS_TIMER_INIT(reg_file_size) PERFMON_CLS_TIMER_INIT(reg_file_size)
PERFMON_CLS_TIMER_INIT(unpack_metadata) // clang-format on PERFMON_CLS_TIMER_INIT(unpack_metadata) // clang-format on
{ {
if (static_cast<int>(meta_.directories().size() - 1) != if (std::cmp_not_equal(meta_.directories().size() - 1,
symlink_inode_offset_) { symlink_inode_offset_)) {
DWARFS_THROW( DWARFS_THROW(
runtime_error, runtime_error,
fmt::format("metadata inconsistency: number of directories ({}) does " fmt::format("metadata inconsistency: number of directories ({}) does "
@ -443,8 +443,8 @@ class metadata_ final : public metadata_v2::impl {
meta_.directories().size() - 1, symlink_inode_offset_)); meta_.directories().size() - 1, symlink_inode_offset_));
} }
if (static_cast<int>(meta_.symlink_table().size()) != if (std::cmp_not_equal(meta_.symlink_table().size(),
(file_inode_offset_ - symlink_inode_offset_)) { file_inode_offset_ - symlink_inode_offset_)) {
DWARFS_THROW( DWARFS_THROW(
runtime_error, runtime_error,
fmt::format( fmt::format(
@ -689,11 +689,11 @@ class metadata_ final : public metadata_v2::impl {
inode -= unique_files_; inode -= unique_files_;
if (!shared_files_.empty()) { if (!shared_files_.empty()) {
if (inode < static_cast<int>(shared_files_.size())) { if (std::cmp_less(inode, shared_files_.size())) {
inode = shared_files_[inode] + unique_files_; inode = shared_files_[inode] + unique_files_;
} }
} else if (auto sfp = meta_.shared_files_table()) { } else if (auto sfp = meta_.shared_files_table()) {
if (inode < static_cast<int>(sfp->size())) { if (std::cmp_less(inode, sfp->size())) {
inode = (*sfp)[inode] + unique_files_; inode = (*sfp)[inode] + unique_files_;
} }
} }
@ -896,19 +896,19 @@ class metadata_ final : public metadata_v2::impl {
std::vector<uint32_t> nlinks(dev_inode_offset_ - file_inode_offset_); std::vector<uint32_t> nlinks(dev_inode_offset_ - file_inode_offset_);
if (auto de = meta_.dir_entries()) { auto add_link = [&](int index) {
for (auto e : *de) { if (index >= 0 && std::cmp_less(index, nlinks.size())) {
int index = int(e.inode_num()) - file_inode_offset_;
if (index >= 0 && index < int(nlinks.size())) {
++nlinks[index]; ++nlinks[index];
} }
};
if (auto de = meta_.dir_entries()) {
for (auto e : *de) {
add_link(int(e.inode_num()) - file_inode_offset_);
} }
} else { } else {
for (auto e : meta_.inodes()) { for (auto e : meta_.inodes()) {
int index = int(e.inode_v2_2()) - file_inode_offset_; add_link(int(e.inode_v2_2()) - file_inode_offset_);
if (index >= 0 && index < int(nlinks.size())) {
++nlinks[index];
}
} }
} }

View File

@ -122,7 +122,7 @@ void categorizer_job_<LoggerPolicy>::categorize_sequential(
if (seq_jobs_.empty()) [[unlikely]] { if (seq_jobs_.empty()) [[unlikely]] {
for (auto&& [index, cat] : ranges::views::enumerate(mgr_.categorizers())) { for (auto&& [index, cat] : ranges::views::enumerate(mgr_.categorizers())) {
if (index_ >= 0 && static_cast<int>(index) >= index_) { if (index_ >= 0 && std::cmp_greater_equal(index, index_)) {
break; break;
} }

View File

@ -420,7 +420,7 @@ class inode_ : public inode {
for (auto [cat, size] : fragments_.get_category_sizes()) { for (auto [cat, size] : fragments_.get_category_sizes()) {
if (auto max = opts.max_similarity_scan_size; if (auto max = opts.max_similarity_scan_size;
max && static_cast<size_t>(size) > *max) { max && std::cmp_greater(size, *max)) {
continue; continue;
} }