Skip to content

Commit 2e8c1dc

Browse files
clang-tidy fixes (#470)
Signed-off-by: Alexey-N-Chernyshov <[email protected]>
1 parent 262a217 commit 2e8c1dc

File tree

12 files changed

+180
-185
lines changed

12 files changed

+180
-185
lines changed

core/common/ffi.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ namespace fc::common::ffi {
1616
}
1717

1818
template <size_t size>
19+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
1920
auto array(const uint8_t (&rhs)[size]) {
2021
Blob<size> lhs;
2122
std::copy(std::begin(rhs), std::end(rhs), std::begin(lhs));
2223
return lhs;
2324
}
2425

2526
template <size_t size>
27+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
2628
void array(uint8_t (&lhs)[size], const std::array<uint8_t, size> rhs) {
2729
std::copy(std::begin(rhs), std::end(rhs), std::begin(lhs));
2830
}

core/common/libp2p/cbor_buffering.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ OUTCOME_CPP_DEFINE_CATEGORY(fc::common::libp2p, CborBuffering::Error, e) {
1212
namespace fc::common::libp2p {
1313
using Head = CborBuffering::Head;
1414

15-
outcome::result<Head> Head::first(size_t &more, uint8_t first) {
15+
outcome::result<Head> Head::first(ssize_t &more, uint8_t first) {
1616
// https://tools.ietf.org/html/rfc7049#section-2
1717
// decode major type
1818
auto type = Type{(first & 0xe0) >> 5};
@@ -57,7 +57,7 @@ namespace fc::common::libp2p {
5757
return head;
5858
}
5959

60-
void Head::next(size_t &more, uint8_t next, Head &head) {
60+
void Head::next(ssize_t &more, uint8_t next, Head &head) {
6161
assert(more > 0);
6262
--more;
6363
head.value = (head.value << 8) | next;
@@ -76,11 +76,11 @@ namespace fc::common::libp2p {
7676
return more_bytes != 0 ? more_bytes : more_nested.empty() ? 0 : 1;
7777
}
7878

79-
outcome::result<size_t> CborBuffering::consume(
79+
outcome::result<ssize_t> CborBuffering::consume(
8080
gsl::span<const uint8_t> input) {
8181
assert(!done());
82-
auto size = static_cast<size_t>(input.size());
83-
size_t consumed = 0;
82+
ssize_t size = input.size();
83+
ssize_t consumed = 0;
8484
if (!partial_head && more_bytes != 0) {
8585
auto partial = std::min(more_bytes, size - consumed);
8686
more_bytes -= partial;
@@ -111,8 +111,7 @@ namespace fc::common::libp2p {
111111
break;
112112
case Type::Bytes:
113113
case Type::Text: {
114-
auto partial =
115-
std::min(static_cast<size_t>(head.value), size - consumed);
114+
auto partial = std::min(head.value, size - consumed);
116115
consumed += partial;
117116
more_bytes = head.value - partial;
118117
break;

core/common/libp2p/cbor_buffering.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ namespace fc::common::libp2p {
3333

3434
struct Head {
3535
/// Parse first header byte
36-
static outcome::result<Head> first(size_t &more, uint8_t first);
36+
static outcome::result<Head> first(ssize_t &more, uint8_t first);
3737

3838
/// Parse next header byte
39-
static void next(size_t &more, uint8_t next, Head &head);
39+
static void next(ssize_t &more, uint8_t next, Head &head);
4040

4141
Type type;
42-
uint64_t value;
42+
ssize_t value;
4343
};
4444

4545
/// Was object fully read
@@ -52,10 +52,10 @@ namespace fc::common::libp2p {
5252
size_t moreBytes() const;
5353

5454
/// Continue reading, return bytes consumed
55-
outcome::result<size_t> consume(gsl::span<const uint8_t> input);
55+
outcome::result<ssize_t> consume(gsl::span<const uint8_t> input);
5656

5757
std::vector<size_t> more_nested;
58-
size_t more_bytes{};
58+
ssize_t more_bytes{};
5959
boost::optional<Head> partial_head;
6060
};
6161
} // namespace fc::common::libp2p

core/common/libp2p/cbor_stream.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ namespace fc::common::libp2p {
4040
return cb(count.error());
4141
}
4242
self->buffer_.resize(self->size_ + count.value());
43-
self->consume(gsl::make_span(self->buffer_).subspan(self->size_),
44-
std::move(cb));
43+
self->consume(gsl::make_span(self->buffer_).subspan(self->size_), cb);
4544
});
4645
}
4746

core/common/libp2p/cbor_stream.hpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,12 @@ namespace fc::common::libp2p {
5151

5252
/// Write cbor object
5353
template <typename T>
54-
void write(const T &value, WriteCallbackFunc cb) {
54+
void write(const T &value, const WriteCallbackFunc &cb) {
5555
auto maybe_encoded = codec::cbor::encode(value);
5656
if (!maybe_encoded) {
5757
return cb(maybe_encoded.error());
5858
}
59-
writeRaw(std::make_shared<Buffer>(std::move(maybe_encoded.value())),
60-
std::move(cb));
59+
writeRaw(std::make_shared<Buffer>(std::move(maybe_encoded.value())), cb);
6160
}
6261

6362
void close() {
@@ -71,6 +70,6 @@ namespace fc::common::libp2p {
7170
std::shared_ptr<Stream> stream_;
7271
CborBuffering buffering_;
7372
std::vector<uint8_t> buffer_;
74-
size_t size_{};
73+
ssize_t size_{};
7574
};
7675
} // namespace fc::common::libp2p

core/common/logger.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ namespace {
2929
} // namespace
3030

3131
namespace fc::common {
32+
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
3233
decltype(file_sink) file_sink;
3334

3435
Logger createLogger(const std::string &tag) {

core/common/tarutil.cpp

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,30 @@ namespace fs = boost::filesystem;
1616

1717
namespace fc::common {
1818

19-
static Logger logger = createLogger("tar util");
19+
const static Logger logger = createLogger("tar util");
2020

2121
int copy_data(struct archive *ar, struct archive *aw) {
22-
int return_code;
22+
la_ssize_t return_code = 0;
2323
const void *buff = nullptr;
2424
size_t size{};
25-
la_int64_t offset;
25+
la_int64_t offset{};
2626

2727
for (;;) {
2828
return_code = archive_read_data_block(ar, &buff, &size, &offset);
2929
if (return_code == ARCHIVE_EOF) {
3030
return (ARCHIVE_OK);
3131
}
3232
if (return_code < ARCHIVE_OK) {
33-
return (return_code);
33+
return static_cast<int>(return_code);
3434
}
3535
return_code = archive_write_data_block(aw, buff, size, offset);
3636
if (return_code < ARCHIVE_OK) {
37-
return (return_code);
37+
return static_cast<int>(return_code);
3838
}
3939
}
4040
}
4141

42+
// NOLINTNEXTLINE(readability-function-cognitive-complexity)
4243
outcome::result<void> zipTar(const boost::filesystem::path &input_path,
4344
const boost::filesystem::path &output_path) {
4445
if (!fs::exists(input_path)) {
@@ -62,35 +63,33 @@ namespace fc::common {
6263
if (return_code < ARCHIVE_WARN) {
6364
logger->error("Zip tar: {}", archive_error_string(archive.get()));
6465
return TarErrors::kCannotZipTarArchive;
65-
} else {
66-
logger->warn("Zip tar: {}", archive_error_string(archive.get()));
6766
}
67+
logger->warn("Zip tar: {}", archive_error_string(archive.get()));
6868
}
6969
std::function<outcome::result<void>(const fs::path &, const fs::path &)>
70+
// NOLINTNEXTLINE(readability-function-cognitive-complexity)
7071
zipDir = [&](const fs::path &absolute_path,
7172
const fs::path &relative_path) -> outcome::result<void> {
7273
struct stat entry_stat {};
73-
char buff[8192];
74-
fs::directory_iterator dir_iter(absolute_path), end;
74+
std::array<char, 8192> buff{};
7575

76-
while (dir_iter != end) {
76+
for (const auto &dir_item : fs::directory_iterator(absolute_path)) {
7777
auto type = AE_IFREG;
78-
if (fs::is_directory(dir_iter->path())) {
79-
if (fs::directory_iterator(dir_iter->path())
78+
if (fs::is_directory(dir_item.path())) {
79+
if (fs::directory_iterator(dir_item.path())
8080
!= fs::directory_iterator()) {
81-
OUTCOME_TRY(zipDir(dir_iter->path(),
82-
relative_path / dir_iter->path().filename()));
83-
++dir_iter;
81+
OUTCOME_TRY(zipDir(dir_item.path(),
82+
relative_path / dir_item.path().filename()));
8483
continue;
8584
}
8685
type = AE_IFDIR;
8786
}
88-
if (stat(dir_iter->path().c_str(), &entry_stat) < 0) {
87+
if (stat(dir_item.path().c_str(), &entry_stat) < 0) {
8988
return TarErrors::kCannotZipTarArchive;
9089
}
9190
auto entry = ffi::wrap(archive_entry_new(), archive_entry_free);
9291
archive_entry_set_pathname(
93-
entry.get(), (relative_path / dir_iter->path().filename()).c_str());
92+
entry.get(), (relative_path / dir_item.path().filename()).c_str());
9493
archive_entry_set_size(entry.get(), entry_stat.st_size);
9594
archive_entry_set_filetype(entry.get(), type);
9695
archive_entry_set_perm(entry.get(), 0644);
@@ -99,31 +98,29 @@ namespace fc::common {
9998
if (return_code < ARCHIVE_WARN) {
10099
logger->error("Zip tar: {}", archive_error_string(archive.get()));
101100
return TarErrors::kCannotZipTarArchive;
102-
} else {
103-
logger->warn("Zip tar: {}", archive_error_string(archive.get()));
104101
}
102+
logger->warn("Zip tar: {}", archive_error_string(archive.get()));
105103
}
106104
if (type == AE_IFREG) {
107-
std::ifstream file(dir_iter->path().c_str());
105+
std::ifstream file(dir_item.path().c_str());
108106
if (not file.is_open()) {
109107
return TarErrors::kCannotOpenFile;
110108
}
111109

112110
while (not file.eof()) {
113-
file.read(buff, sizeof(buff));
111+
file.read(buff.data(), sizeof(buff));
114112

115113
if (not file.good() && not file.eof()) {
116114
return TarErrors::kCannotReadFile;
117115
}
118116

119-
if (archive_write_data(archive.get(), buff, file.gcount()) == -1) {
117+
if (archive_write_data(archive.get(), buff.data(), file.gcount())
118+
== -1) {
120119
logger->error("Zip tar: {}", archive_error_string(archive.get()));
121120
return TarErrors::kCannotZipTarArchive;
122121
}
123122
}
124123
}
125-
126-
++dir_iter;
127124
}
128125
return outcome::success();
129126
};
@@ -135,6 +132,7 @@ namespace fc::common {
135132
return outcome::success();
136133
}
137134

135+
// NOLINTNEXTLINE(readability-function-cognitive-complexity)
138136
outcome::result<void> extractTar(const boost::filesystem::path &tar_path,
139137
const boost::filesystem::path &output_path) {
140138
if (!fs::exists(output_path)) {
@@ -148,8 +146,8 @@ namespace fc::common {
148146
}
149147

150148
struct archive_entry *entry = nullptr;
151-
int flags;
152-
int return_code;
149+
int flags = 0;
150+
int return_code = 0;
153151

154152
/* Select which attributes we want to restore. */
155153
flags = ARCHIVE_EXTRACT_TIME;

core/common/uri_parser/CMakeLists.txt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
# SPDX-License-Identifier: Apache-2.0
33

44
add_library(uri_parser
5-
uri_parser.cpp
6-
percent_encoding.cpp
7-
)
5+
uri_parser.cpp
6+
percent_encoding.cpp
7+
)
8+
# any library is linked to include GSL
9+
target_link_libraries(uri_parser
10+
outcome
11+
)

0 commit comments

Comments
 (0)