Skip to content

fix(system_monitor): fix bugprone-exception-escape #9781

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

Merged
merged 3 commits into from
Jan 7, 2025
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
83 changes: 46 additions & 37 deletions system/system_monitor/reader/hdd_reader/hdd_reader.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 Autoware Foundation

Check notice on line 1 in system/system_monitor/reader/hdd_reader/hdd_reader.cpp

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

ℹ Getting worse: Overall Code Complexity

The mean cyclomatic complexity increases from 6.00 to 6.20, threshold = 4. This file has many conditional statements (e.g. if, for, while) across its implementation, leading to lower code health. Avoid adding more conditionals.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -43,6 +43,7 @@
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <regex>
#include <string>
#include <utility>
Expand Down Expand Up @@ -655,48 +656,56 @@

int main(int argc, char ** argv)
{
static struct option long_options[] = {
{"help", no_argument, nullptr, 'h'},
{"port", required_argument, nullptr, 'p'},
{nullptr, 0, nullptr, 0}};

// Parse command-line options
int c = 0;
int option_index = 0;
int port = PORT;
while ((c = getopt_long(argc, argv, "hp:", long_options, &option_index)) != -1) {
switch (c) {
case 'h':
usage();
return EXIT_SUCCESS;

case 'p':
try {
port = boost::lexical_cast<int>(optarg);
} catch (const boost::bad_lexical_cast & e) {
printf("Error: %s\n", e.what());
return EXIT_FAILURE;
}
break;

default:
break;
try {
static struct option long_options[] = {
{"help", no_argument, nullptr, 'h'},
{"port", required_argument, nullptr, 'p'},
{nullptr, 0, nullptr, 0}};

// Parse command-line options
int c = 0;
int option_index = 0;

Check warning on line 667 in system/system_monitor/reader/hdd_reader/hdd_reader.cpp

View check run for this annotation

Codecov / codecov/patch

system/system_monitor/reader/hdd_reader/hdd_reader.cpp#L667

Added line #L667 was not covered by tests
int port = PORT;
while ((c = getopt_long(argc, argv, "hp:", long_options, &option_index)) != -1) {
switch (c) {
case 'h':
usage();
return EXIT_SUCCESS;

Check warning on line 673 in system/system_monitor/reader/hdd_reader/hdd_reader.cpp

View check run for this annotation

Codecov / codecov/patch

system/system_monitor/reader/hdd_reader/hdd_reader.cpp#L670-L673

Added lines #L670 - L673 were not covered by tests

case 'p':

Check warning on line 675 in system/system_monitor/reader/hdd_reader/hdd_reader.cpp

View check run for this annotation

Codecov / codecov/patch

system/system_monitor/reader/hdd_reader/hdd_reader.cpp#L675

Added line #L675 was not covered by tests
try {
port = boost::lexical_cast<int>(optarg);
} catch (const boost::bad_lexical_cast & e) {
printf("Error: %s\n", e.what());

Check warning on line 679 in system/system_monitor/reader/hdd_reader/hdd_reader.cpp

View check run for this annotation

Codecov / codecov/patch

system/system_monitor/reader/hdd_reader/hdd_reader.cpp#L677-L679

Added lines #L677 - L679 were not covered by tests
return EXIT_FAILURE;
}

Check warning on line 681 in system/system_monitor/reader/hdd_reader/hdd_reader.cpp

View check run for this annotation

Codecov / codecov/patch

system/system_monitor/reader/hdd_reader/hdd_reader.cpp#L681

Added line #L681 was not covered by tests
break;

default:
break;
}
}
}

// Put the program in the background
if (daemon(0, 0) < 0) {
printf("Failed to put the program in the background. %s\n", strerror(errno));
return errno;
}
// Put the program in the background
if (daemon(0, 0) < 0) {
printf("Failed to put the program in the background. %s\n", strerror(errno));
return errno;

Check warning on line 692 in system/system_monitor/reader/hdd_reader/hdd_reader.cpp

View check run for this annotation

Codecov / codecov/patch

system/system_monitor/reader/hdd_reader/hdd_reader.cpp#L691-L692

Added lines #L691 - L692 were not covered by tests
}

// Open connection to system logger
openlog(nullptr, LOG_PID, LOG_DAEMON);
// Open connection to system logger
openlog(nullptr, LOG_PID, LOG_DAEMON);

Check warning on line 696 in system/system_monitor/reader/hdd_reader/hdd_reader.cpp

View check run for this annotation

Codecov / codecov/patch

system/system_monitor/reader/hdd_reader/hdd_reader.cpp#L696

Added line #L696 was not covered by tests

run(port);
run(port);

Check warning on line 698 in system/system_monitor/reader/hdd_reader/hdd_reader.cpp

View check run for this annotation

Codecov / codecov/patch

system/system_monitor/reader/hdd_reader/hdd_reader.cpp#L698

Added line #L698 was not covered by tests

// Close descriptor used to write to system logger
closelog();
// Close descriptor used to write to system logger
closelog();
} catch (const std::exception & e) {
std::cerr << "Exception in main(): " << e.what() << std::endl;

Check warning on line 703 in system/system_monitor/reader/hdd_reader/hdd_reader.cpp

View check run for this annotation

Codecov / codecov/patch

system/system_monitor/reader/hdd_reader/hdd_reader.cpp#L701-L703

Added lines #L701 - L703 were not covered by tests
return EXIT_FAILURE;
} catch (...) {

Check warning on line 705 in system/system_monitor/reader/hdd_reader/hdd_reader.cpp

View check run for this annotation

Codecov / codecov/patch

system/system_monitor/reader/hdd_reader/hdd_reader.cpp#L705

Added line #L705 was not covered by tests
std::cerr << "Unknown exception in main()" << std::endl;
return EXIT_FAILURE;
}

Check warning on line 708 in system/system_monitor/reader/hdd_reader/hdd_reader.cpp

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

❌ New issue: Complex Method

main has a cyclomatic complexity of 9, threshold = 9. This function has many conditional statements (e.g. if, for, while), leading to lower code health. Avoid adding more conditionals and code to it without refactoring.

Check warning on line 708 in system/system_monitor/reader/hdd_reader/hdd_reader.cpp

View check run for this annotation

Codecov / codecov/patch

system/system_monitor/reader/hdd_reader/hdd_reader.cpp#L708

Added line #L708 was not covered by tests

return EXIT_SUCCESS;
}
146 changes: 77 additions & 69 deletions system/system_monitor/reader/msr_reader/msr_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,90 +212,98 @@

int main(int argc, char ** argv)
{
static struct option long_options[] = {
{"help", no_argument, 0, 'h'}, {"port", required_argument, 0, 'p'}, {0, 0, 0, 0}};

// Parse command-line options
int c = 0;
int option_index = 0;
int port = PORT;
while ((c = getopt_long(argc, argv, "hp:", long_options, &option_index)) != -1) {
switch (c) {
case 'h':
usage();
return EXIT_SUCCESS;

case 'p':
try {
port = boost::lexical_cast<int>(optarg);
} catch (const boost::bad_lexical_cast & e) {
printf("Error: %s\n", e.what());
return EXIT_FAILURE;
}
break;
try {
static struct option long_options[] = {
{"help", no_argument, 0, 'h'}, {"port", required_argument, 0, 'p'}, {0, 0, 0, 0}};

// Parse command-line options
int c = 0;
int option_index = 0;

Check warning on line 221 in system/system_monitor/reader/msr_reader/msr_reader.cpp

View check run for this annotation

Codecov / codecov/patch

system/system_monitor/reader/msr_reader/msr_reader.cpp#L221

Added line #L221 was not covered by tests
int port = PORT;
while ((c = getopt_long(argc, argv, "hp:", long_options, &option_index)) != -1) {
switch (c) {
case 'h':
usage();
return EXIT_SUCCESS;

Check warning on line 227 in system/system_monitor/reader/msr_reader/msr_reader.cpp

View check run for this annotation

Codecov / codecov/patch

system/system_monitor/reader/msr_reader/msr_reader.cpp#L224-L227

Added lines #L224 - L227 were not covered by tests

case 'p':

Check warning on line 229 in system/system_monitor/reader/msr_reader/msr_reader.cpp

View check run for this annotation

Codecov / codecov/patch

system/system_monitor/reader/msr_reader/msr_reader.cpp#L229

Added line #L229 was not covered by tests
try {
port = boost::lexical_cast<int>(optarg);
} catch (const boost::bad_lexical_cast & e) {
printf("Error: %s\n", e.what());

Check warning on line 233 in system/system_monitor/reader/msr_reader/msr_reader.cpp

View check run for this annotation

Codecov / codecov/patch

system/system_monitor/reader/msr_reader/msr_reader.cpp#L231-L233

Added lines #L231 - L233 were not covered by tests
return EXIT_FAILURE;
}

Check warning on line 235 in system/system_monitor/reader/msr_reader/msr_reader.cpp

View check run for this annotation

Codecov / codecov/patch

system/system_monitor/reader/msr_reader/msr_reader.cpp#L235

Added line #L235 was not covered by tests
break;

default:
break;
}
}

default:
break;
if (!fs::exists("/dev/cpu")) {
printf("Failed to access /dev/cpu.\n");
return EXIT_FAILURE;
}
}

if (!fs::exists("/dev/cpu")) {
printf("Failed to access /dev/cpu.\n");
return EXIT_FAILURE;
}
std::vector<std::string> list;

Check warning on line 248 in system/system_monitor/reader/msr_reader/msr_reader.cpp

View check run for this annotation

Codecov / codecov/patch

system/system_monitor/reader/msr_reader/msr_reader.cpp#L248

Added line #L248 was not covered by tests
const fs::path root("/dev/cpu");

std::vector<std::string> list;
const fs::path root("/dev/cpu");
for (const fs::path & path : boost::make_iterator_range(
fs::recursive_directory_iterator(root), fs::recursive_directory_iterator())) {

Check warning on line 252 in system/system_monitor/reader/msr_reader/msr_reader.cpp

View check run for this annotation

Codecov / codecov/patch

system/system_monitor/reader/msr_reader/msr_reader.cpp#L252

Added line #L252 was not covered by tests
if (fs::is_directory(path)) {
continue;

Check warning on line 254 in system/system_monitor/reader/msr_reader/msr_reader.cpp

View check run for this annotation

Codecov / codecov/patch

system/system_monitor/reader/msr_reader/msr_reader.cpp#L254

Added line #L254 was not covered by tests
}

for (const fs::path & path : boost::make_iterator_range(
fs::recursive_directory_iterator(root), fs::recursive_directory_iterator())) {
if (fs::is_directory(path)) {
continue;
}
std::cmatch match;
const char * msr = path.generic_string().c_str();

std::cmatch match;
const char * msr = path.generic_string().c_str();
// /dev/cpu/[0-9]/msr ?
if (!std::regex_match(msr, match, std::regex(".*msr"))) {
continue;
}

// /dev/cpu/[0-9]/msr ?
if (!std::regex_match(msr, match, std::regex(".*msr"))) {
continue;
list.push_back(path.generic_string());

Check warning on line 265 in system/system_monitor/reader/msr_reader/msr_reader.cpp

View check run for this annotation

Codecov / codecov/patch

system/system_monitor/reader/msr_reader/msr_reader.cpp#L265

Added line #L265 was not covered by tests
}

list.push_back(path.generic_string());
}
std::sort(list.begin(), list.end(), [](const std::string & c1, const std::string & c2) {

Check warning on line 268 in system/system_monitor/reader/msr_reader/msr_reader.cpp

View check run for this annotation

Codecov / codecov/patch

system/system_monitor/reader/msr_reader/msr_reader.cpp#L268

Added line #L268 was not covered by tests
std::cmatch match;
const std::regex filter(".*/(\\d+)/msr");

Check warning on line 270 in system/system_monitor/reader/msr_reader/msr_reader.cpp

View check run for this annotation

Codecov / codecov/patch

system/system_monitor/reader/msr_reader/msr_reader.cpp#L270

Added line #L270 was not covered by tests
int n1 = 0;
int n2 = 0;
if (std::regex_match(c1.c_str(), match, filter)) {
n1 = std::stoi(match[1].str());

Check warning on line 274 in system/system_monitor/reader/msr_reader/msr_reader.cpp

View check run for this annotation

Codecov / codecov/patch

system/system_monitor/reader/msr_reader/msr_reader.cpp#L274

Added line #L274 was not covered by tests
}
if (std::regex_match(c2.c_str(), match, filter)) {
n2 = std::stoi(match[1].str());

Check warning on line 277 in system/system_monitor/reader/msr_reader/msr_reader.cpp

View check run for this annotation

Codecov / codecov/patch

system/system_monitor/reader/msr_reader/msr_reader.cpp#L277

Added line #L277 was not covered by tests
}
return n1 < n2;
}); // NOLINT

Check warning on line 280 in system/system_monitor/reader/msr_reader/msr_reader.cpp

View check run for this annotation

Codecov / codecov/patch

system/system_monitor/reader/msr_reader/msr_reader.cpp#L279-L280

Added lines #L279 - L280 were not covered by tests

std::sort(list.begin(), list.end(), [](const std::string & c1, const std::string & c2) {
std::cmatch match;
const std::regex filter(".*/(\\d+)/msr");
int n1 = 0;
int n2 = 0;
if (std::regex_match(c1.c_str(), match, filter)) {
n1 = std::stoi(match[1].str());
}
if (std::regex_match(c2.c_str(), match, filter)) {
n2 = std::stoi(match[1].str());
if (list.empty()) {
printf("No msr found in /dev/cpu.\n");
return EXIT_FAILURE;
}
return n1 < n2;
}); // NOLINT

if (list.empty()) {
printf("No msr found in /dev/cpu.\n");
return EXIT_FAILURE;
}

// Put the program in the background
if (daemon(0, 0) < 0) {
printf("Failed to put the program in the background. %s\n", strerror(errno));
return errno;
}
// Put the program in the background
if (daemon(0, 0) < 0) {
printf("Failed to put the program in the background. %s\n", strerror(errno));
return errno;

Check warning on line 290 in system/system_monitor/reader/msr_reader/msr_reader.cpp

View check run for this annotation

Codecov / codecov/patch

system/system_monitor/reader/msr_reader/msr_reader.cpp#L289-L290

Added lines #L289 - L290 were not covered by tests
}

// Open connection to system logger
openlog(nullptr, LOG_PID, LOG_DAEMON);
// Open connection to system logger
openlog(nullptr, LOG_PID, LOG_DAEMON);

Check warning on line 294 in system/system_monitor/reader/msr_reader/msr_reader.cpp

View check run for this annotation

Codecov / codecov/patch

system/system_monitor/reader/msr_reader/msr_reader.cpp#L294

Added line #L294 was not covered by tests

run(port, list);
run(port, list);

Check warning on line 296 in system/system_monitor/reader/msr_reader/msr_reader.cpp

View check run for this annotation

Codecov / codecov/patch

system/system_monitor/reader/msr_reader/msr_reader.cpp#L296

Added line #L296 was not covered by tests

// Close descriptor used to write to system logger
closelog();
// Close descriptor used to write to system logger
closelog();
} catch (const std::exception & e) {
std::cerr << "Exception in main(): " << e.what() << std::endl;

Check warning on line 301 in system/system_monitor/reader/msr_reader/msr_reader.cpp

View check run for this annotation

Codecov / codecov/patch

system/system_monitor/reader/msr_reader/msr_reader.cpp#L299-L301

Added lines #L299 - L301 were not covered by tests
return EXIT_FAILURE;
} catch (...) {

Check warning on line 303 in system/system_monitor/reader/msr_reader/msr_reader.cpp

View check run for this annotation

Codecov / codecov/patch

system/system_monitor/reader/msr_reader/msr_reader.cpp#L303

Added line #L303 was not covered by tests
std::cerr << "Unknown exception in main()" << std::endl;
return EXIT_FAILURE;
}

Check warning on line 306 in system/system_monitor/reader/msr_reader/msr_reader.cpp

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

❌ Getting worse: Complex Method

main increases in cyclomatic complexity from 16 to 18, threshold = 9. This function has many conditional statements (e.g. if, for, while), leading to lower code health. Avoid adding more conditionals and code to it without refactoring.

Check warning on line 306 in system/system_monitor/reader/msr_reader/msr_reader.cpp

View check run for this annotation

Codecov / codecov/patch

system/system_monitor/reader/msr_reader/msr_reader.cpp#L306

Added line #L306 was not covered by tests

return EXIT_SUCCESS;
}
Loading