Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial expansion of if-brackets in all collector/lib files #1849

Merged
merged 6 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion collector/lib/AbortHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ extern "C" void AbortHandler(int signum) {
}
}

if (n_frames == max_frames)
if (n_frames == max_frames) {
write(STDERR_FILENO, "[truncated]\n", 13);
}

// Write a message to stderr using only reentrant functions.
num_bytes = snprintf(message_buffer, message_buffer_size,
Expand Down
6 changes: 4 additions & 2 deletions collector/lib/CollectorConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,9 @@ void CollectorConfig::InitCollectorConfig(CollectorArgs* args) {
}

for (const std::string& str : ignored_networks.value()) {
if (str.empty())
if (str.empty()) {
continue;
}

std::optional<IPNet> net = IPNet::parse(str);

Expand All @@ -251,8 +252,9 @@ void CollectorConfig::InitCollectorConfig(CollectorArgs* args) {
}

for (const std::string& str : non_aggregated_networks.value()) {
if (str.empty())
if (str.empty()) {
continue;
}

std::optional<IPNet> net = IPNet::parse(str);

Expand Down
4 changes: 3 additions & 1 deletion collector/lib/CollectorService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ void CollectorService::RunForever() {

CLOG(INFO) << "Shutting down collector.";

if (net_status_notifier) net_status_notifier->Stop();
if (net_status_notifier) {
net_status_notifier->Stop();
}
// Shut down these first since they access the system inspector object.
exporter.stop();
server.close();
Expand Down
17 changes: 13 additions & 4 deletions collector/lib/CollectorStatsExporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,12 +241,21 @@ void CollectorStatsExporter::run() {

nUserspace += userspace;

if (counters.userspace) counters.userspace->Set(userspace);
if (counters.userspace) {
counters.userspace->Set(userspace);
}

if (counters.parse_micros_total) counters.parse_micros_total->Set(parse_micros_total);
if (counters.process_micros_total) counters.process_micros_total->Set(process_micros_total);
if (counters.parse_micros_total) {
counters.parse_micros_total->Set(parse_micros_total);
}
Molter73 marked this conversation as resolved.
Show resolved Hide resolved

if (counters.parse_micros_avg) counters.parse_micros_avg->Set(userspace ? parse_micros_total / userspace : 0);
if (counters.process_micros_total) {
counters.process_micros_total->Set(process_micros_total);
}

if (counters.parse_micros_avg) {
counters.parse_micros_avg->Set(userspace ? parse_micros_total / userspace : 0);
}
}

userspaceEvents->Set(nUserspace);
Expand Down
8 changes: 6 additions & 2 deletions collector/lib/Containers.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ using MappedType = WithConst<typename M::mapped_type, M>;
template <typename C>
ValueType<C>* Find(C& container, const typename C::key_type& key) {
auto it = container.find(key);
if (it == container.end()) return nullptr;
if (it == container.end()) {
return nullptr;
}
return &*it;
}

Expand All @@ -37,7 +39,9 @@ bool Contains(const C& container, const typename C::key_type& key) {
template <typename M>
MappedType<M>* Lookup(M& map, const typename M::key_type& key) {
auto* val = Find(map, key);
if (!val) return nullptr;
if (!val) {
return nullptr;
}
return &val->second;
}

Expand Down
16 changes: 12 additions & 4 deletions collector/lib/DuplexGRPC.h
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,9 @@ class DuplexClient : public virtual IDuplexClient {
while (result && op_res.op != op_desc.op) {
result = ProcessSingle(nullptr, deadline, &op_res);
}
if (!result) return result;
if (!result) {
return result;
}
return Result(op_res.ok);
}

Expand Down Expand Up @@ -561,14 +563,18 @@ class DuplexClientReaderWriter : public DuplexClientWriter<W> {
auto deadline = ToDeadline(time_spec);

auto result = PollAll(DuplexClient::CAN_READ, deadline);
if (!result) return result;
if (!result) {
return result;
}

// We can read, but the read buffer is not valid -> there has been an error. This is the last read.
if (!read_buf_valid_) {
return Result(Status::ERROR);
}

if (obj) *obj = std::move(read_buf_);
if (obj) {
*obj = std::move(read_buf_);
}
ReadNext();

return Result(Status::OK);
Expand Down Expand Up @@ -666,7 +672,9 @@ class DuplexClientReaderWriter : public DuplexClientWriter<W> {
this->SetFlags(Done(Op::SHUTDOWN));
}

if (flags_out) *flags_out = this->flags_;
if (flags_out) {
*flags_out = this->flags_;
}
return Result(next_status);
}

Expand Down
8 changes: 6 additions & 2 deletions collector/lib/FileSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,18 @@ class DirHandle : public ResourceWrapper<DIR*, DirHandle> {
DirHandle(FDHandle&& fd) : ResourceWrapper(fdopendir(fd.release())) {}

FDHandle openat(const char* path, int mode) const {
if (!valid()) return -1;
if (!valid()) {
return -1;
}
return ::openat(dirfd(get()), path, mode);
}

int fd() const { return ::dirfd(get()); }

struct dirent* read() {
if (!valid()) return nullptr;
if (!valid()) {
return nullptr;
}
return readdir(get());
}

Expand Down
36 changes: 27 additions & 9 deletions collector/lib/NRadix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,18 @@ bool NRadixTree::Insert(const IPNet& network) const {
next = node->left_;
}

if (!next) break;
if (!next) {
break;
}

bit >>= 1;
node = next;

if (bit == 0) {
// We have walked 128 bits, stop.
if (++i >= Address::kU64MaxLen) break;
if (++i >= Address::kU64MaxLen) {
break;
}

// Reset and move to lower part.
bit = 0x8000000000000000ULL;
Expand Down Expand Up @@ -105,7 +109,9 @@ bool NRadixTree::Insert(const IPNet& network) const {

if (bit == 0) {
// We have walked all 128 bits, stop.
if (++i >= Address::kU64MaxLen) break;
if (++i >= Address::kU64MaxLen) {
break;
}

bit = 0x8000000000000000ULL;
if (network.bits() >= 64) {
Expand All @@ -120,7 +126,9 @@ bool NRadixTree::Insert(const IPNet& network) const {
}

IPNet NRadixTree::Find(const IPNet& network) const {
if (network.IsNull()) return {};
if (network.IsNull()) {
return {};
}

if (network.bits() == 0) {
CLOG(ERROR) << "Cannot handle CIDR " << network << " with /0, in network tree";
Expand Down Expand Up @@ -148,7 +156,9 @@ IPNet NRadixTree::Find(const IPNet& network) const {

// All network bits are traversed. If a supernet was found along the way, `ret` holds it,
// else there does not exist any supernet containing the search network/address.
if (!(*net_mask_p & bit)) break;
if (!(*net_mask_p & bit)) {
break;
}

bit >>= 1;

Expand Down Expand Up @@ -177,7 +187,9 @@ IPNet NRadixTree::Find(const Address& addr) const {
}

void getAll(nRadixNode* node, std::vector<IPNet>& ret) {
if (!node) return;
if (!node) {
return;
}

if (node->value_) {
ret.push_back(*node->value_);
Expand All @@ -199,14 +211,20 @@ bool isAnyIPNetSubsetUtil(Address::Family family, const nRadixNode* n1, const nR
// If we have found networks from both trees belonging to same family, we have the answer.
if (containing_net && contained_net) {
if (family == Address::Family::UNKNOWN) {
if (containing_net->family() == contained_net->family()) return true;
if (containing_net->family() == contained_net->family()) {
return true;
}
} else {
if (containing_net->family() == family && contained_net->family() == family) return true;
if (containing_net->family() == family && contained_net->family() == family) {
return true;
}
}
}

// There are no more networks down the path in second tree, so stop.
if (!n2) return false;
if (!n2) {
return false;
}

if (n1 && n1->value_) {
containing_net = n1->value_;
Expand Down
8 changes: 6 additions & 2 deletions collector/lib/NRadix.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ struct nRadixNode {
}

nRadixNode& operator=(const nRadixNode& other) {
if (this == &other) return *this;
if (this == &other) {
return *this;
}
auto* new_node = new nRadixNode(other);
std::swap(*new_node, *this);
delete new_node;
Expand Down Expand Up @@ -92,7 +94,9 @@ class NRadixTree {
}

NRadixTree& operator=(const NRadixTree& other) {
if (this == &other) return *this;
if (this == &other) {
return *this;
}
delete root_;
// This calls the node copy constructor which in turn copies all the nodes.
root_ = new nRadixNode(*other.root_);
Expand Down
22 changes: 16 additions & 6 deletions collector/lib/NetworkConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,9 @@ class IPNet {
}

size_t Hash() const {
if (is_addr_) return HashAll(address_.array(), mask_, bits_);
if (is_addr_) {
return HashAll(address_.array(), mask_, bits_);
}
return HashAll(mask_, bits_);
}

Expand Down Expand Up @@ -411,11 +413,19 @@ inline bool IsRelevantEndpoint(const Endpoint& ep) {
// operating systems adhere to the IANA-recommended range. Therefore, the return value is not a bool, but instead an
// int which indicates the confidence that the port is in fact ephemeral.
inline int IsEphemeralPort(uint16_t port) {
if (port >= 49152) return 4; // IANA range
if (port >= 32768) return 3; // Modern Linux kernel range
if (port >= 1025 && port <= 5000) return 2; // FreeBSD (partial) + Windows <=XP range
if (port == 1024) return 1; // FreeBSD
return 0; // not ephemeral according to any range
if (port >= 49152) {
return 4; // IANA range
}
if (port >= 32768) {
return 3; // Modern Linux kernel range
}
if (port >= 1025 && port <= 5000) {
return 2; // FreeBSD (partial) + Windows <=XP range
}
if (port == 1024) {
return 1; // FreeBSD
}
return 0; // not ephemeral according to any range
}

// PrivateIPv4Networks return private IPv4 networks.
Expand Down
7 changes: 5 additions & 2 deletions collector/lib/NetworkConnectionInfoServiceComm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,16 @@ bool NetworkConnectionInfoServiceComm::WaitForConnectionReady(const std::functio

void NetworkConnectionInfoServiceComm::TryCancel() {
WITH_LOCK(context_mutex_) {
if (context_) context_->TryCancel();
if (context_) {
context_->TryCancel();
}
}
}

std::unique_ptr<IDuplexClientWriter<sensor::NetworkConnectionInfoMessage>> NetworkConnectionInfoServiceComm::PushNetworkConnectionInfoOpenStream(std::function<void(const sensor::NetworkFlowsControlMessage*)> receive_func) {
if (!context_)
if (!context_) {
ResetClientContext();
}

if (channel_) {
return DuplexClient::CreateWithReadCallback(
Expand Down
12 changes: 9 additions & 3 deletions collector/lib/NetworkSignalHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ NetworkSignalHandler::NetworkSignalHandler(sinsp* inspector, std::shared_ptr<Con
std::optional<Connection> NetworkSignalHandler::GetConnection(sinsp_evt* evt) {
auto* fd_info = evt->get_fd_info();

if (!fd_info) return std::nullopt;
if (!fd_info) {
return std::nullopt;
}

// With collect_connection_status_ set, we can prevent reporting of asynchronous
// connections which fail.
Expand Down Expand Up @@ -117,13 +119,17 @@ std::optional<Connection> NetworkSignalHandler::GetConnection(sinsp_evt* evt) {
const Endpoint* remote = is_server ? &client : &server;

const std::string* container_id = event_extractor_->get_container_id(evt);
if (!container_id) return std::nullopt;
if (!container_id) {
return std::nullopt;
}
return {Connection(*container_id, *local, *remote, l4proto, is_server)};
}

SignalHandler::Result NetworkSignalHandler::HandleSignal(sinsp_evt* evt) {
auto modifier = modifiers[evt->get_type()];
if (modifier == Modifier::INVALID) return SignalHandler::IGNORED;
if (modifier == Modifier::INVALID) {
return SignalHandler::IGNORED;
}

auto result = GetConnection(evt);
if (!result.has_value() || !IsRelevantConnection(*result)) {
Expand Down
4 changes: 3 additions & 1 deletion collector/lib/NetworkStatusNotifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,9 @@ void NetworkStatusNotifier::RunSingleAfterglow(IDuplexClientWriter<sensor::Netwo
}

sensor::NetworkConnectionInfoMessage* NetworkStatusNotifier::CreateInfoMessage(const ConnMap& conn_delta, const AdvertisedEndpointMap& endpoint_delta) {
if (conn_delta.empty() && endpoint_delta.empty()) return nullptr;
if (conn_delta.empty() && endpoint_delta.empty()) {
return nullptr;
}

Reset();
auto* msg = AllocateRoot();
Expand Down
4 changes: 3 additions & 1 deletion collector/lib/Process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ std::string Process::args() const {
std::ostringstream args;
for (auto it = system_inspector_threadinfo_->m_args.begin(); it != system_inspector_threadinfo_->m_args.end();) {
args << *it++;
if (it != system_inspector_threadinfo_->m_args.end()) args << " ";
if (it != system_inspector_threadinfo_->m_args.end()) {
args << " ";
}
}
return args.str();
}
Expand Down
Loading
Loading