Skip to content

Commit 3aa84a0

Browse files
authored
fix(system_monitor): fix bugprone-exception-escape (autowarefoundation#9781)
* fix: bugprone-error Signed-off-by: kobayu858 <yutaro.kobayashi@tier4.jp> * fix: clippy Signed-off-by: kobayu858 <yutaro.kobayashi@tier4.jp> --------- Signed-off-by: kobayu858 <yutaro.kobayashi@tier4.jp>
1 parent e2accc5 commit 3aa84a0

File tree

2 files changed

+123
-106
lines changed

2 files changed

+123
-106
lines changed

system/system_monitor/reader/hdd_reader/hdd_reader.cpp

+46-37
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include <cstdio>
4444
#include <cstdlib>
4545
#include <cstring>
46+
#include <iostream>
4647
#include <regex>
4748
#include <string>
4849
#include <utility>
@@ -655,48 +656,56 @@ void run(int port)
655656

656657
int main(int argc, char ** argv)
657658
{
658-
static struct option long_options[] = {
659-
{"help", no_argument, nullptr, 'h'},
660-
{"port", required_argument, nullptr, 'p'},
661-
{nullptr, 0, nullptr, 0}};
662-
663-
// Parse command-line options
664-
int c = 0;
665-
int option_index = 0;
666-
int port = PORT;
667-
while ((c = getopt_long(argc, argv, "hp:", long_options, &option_index)) != -1) {
668-
switch (c) {
669-
case 'h':
670-
usage();
671-
return EXIT_SUCCESS;
672-
673-
case 'p':
674-
try {
675-
port = boost::lexical_cast<int>(optarg);
676-
} catch (const boost::bad_lexical_cast & e) {
677-
printf("Error: %s\n", e.what());
678-
return EXIT_FAILURE;
679-
}
680-
break;
681-
682-
default:
683-
break;
659+
try {
660+
static struct option long_options[] = {
661+
{"help", no_argument, nullptr, 'h'},
662+
{"port", required_argument, nullptr, 'p'},
663+
{nullptr, 0, nullptr, 0}};
664+
665+
// Parse command-line options
666+
int c = 0;
667+
int option_index = 0;
668+
int port = PORT;
669+
while ((c = getopt_long(argc, argv, "hp:", long_options, &option_index)) != -1) {
670+
switch (c) {
671+
case 'h':
672+
usage();
673+
return EXIT_SUCCESS;
674+
675+
case 'p':
676+
try {
677+
port = boost::lexical_cast<int>(optarg);
678+
} catch (const boost::bad_lexical_cast & e) {
679+
printf("Error: %s\n", e.what());
680+
return EXIT_FAILURE;
681+
}
682+
break;
683+
684+
default:
685+
break;
686+
}
684687
}
685-
}
686688

687-
// Put the program in the background
688-
if (daemon(0, 0) < 0) {
689-
printf("Failed to put the program in the background. %s\n", strerror(errno));
690-
return errno;
691-
}
689+
// Put the program in the background
690+
if (daemon(0, 0) < 0) {
691+
printf("Failed to put the program in the background. %s\n", strerror(errno));
692+
return errno;
693+
}
692694

693-
// Open connection to system logger
694-
openlog(nullptr, LOG_PID, LOG_DAEMON);
695+
// Open connection to system logger
696+
openlog(nullptr, LOG_PID, LOG_DAEMON);
695697

696-
run(port);
698+
run(port);
697699

698-
// Close descriptor used to write to system logger
699-
closelog();
700+
// Close descriptor used to write to system logger
701+
closelog();
702+
} catch (const std::exception & e) {
703+
std::cerr << "Exception in main(): " << e.what() << std::endl;
704+
return EXIT_FAILURE;
705+
} catch (...) {
706+
std::cerr << "Unknown exception in main()" << std::endl;
707+
return EXIT_FAILURE;
708+
}
700709

701710
return EXIT_SUCCESS;
702711
}

system/system_monitor/reader/msr_reader/msr_reader.cpp

+77-69
Original file line numberDiff line numberDiff line change
@@ -212,90 +212,98 @@ void run(int port, const std::vector<std::string> & list)
212212

213213
int main(int argc, char ** argv)
214214
{
215-
static struct option long_options[] = {
216-
{"help", no_argument, 0, 'h'}, {"port", required_argument, 0, 'p'}, {0, 0, 0, 0}};
217-
218-
// Parse command-line options
219-
int c = 0;
220-
int option_index = 0;
221-
int port = PORT;
222-
while ((c = getopt_long(argc, argv, "hp:", long_options, &option_index)) != -1) {
223-
switch (c) {
224-
case 'h':
225-
usage();
226-
return EXIT_SUCCESS;
227-
228-
case 'p':
229-
try {
230-
port = boost::lexical_cast<int>(optarg);
231-
} catch (const boost::bad_lexical_cast & e) {
232-
printf("Error: %s\n", e.what());
233-
return EXIT_FAILURE;
234-
}
235-
break;
215+
try {
216+
static struct option long_options[] = {
217+
{"help", no_argument, 0, 'h'}, {"port", required_argument, 0, 'p'}, {0, 0, 0, 0}};
218+
219+
// Parse command-line options
220+
int c = 0;
221+
int option_index = 0;
222+
int port = PORT;
223+
while ((c = getopt_long(argc, argv, "hp:", long_options, &option_index)) != -1) {
224+
switch (c) {
225+
case 'h':
226+
usage();
227+
return EXIT_SUCCESS;
228+
229+
case 'p':
230+
try {
231+
port = boost::lexical_cast<int>(optarg);
232+
} catch (const boost::bad_lexical_cast & e) {
233+
printf("Error: %s\n", e.what());
234+
return EXIT_FAILURE;
235+
}
236+
break;
237+
238+
default:
239+
break;
240+
}
241+
}
236242

237-
default:
238-
break;
243+
if (!fs::exists("/dev/cpu")) {
244+
printf("Failed to access /dev/cpu.\n");
245+
return EXIT_FAILURE;
239246
}
240-
}
241247

242-
if (!fs::exists("/dev/cpu")) {
243-
printf("Failed to access /dev/cpu.\n");
244-
return EXIT_FAILURE;
245-
}
248+
std::vector<std::string> list;
249+
const fs::path root("/dev/cpu");
246250

247-
std::vector<std::string> list;
248-
const fs::path root("/dev/cpu");
251+
for (const fs::path & path : boost::make_iterator_range(
252+
fs::recursive_directory_iterator(root), fs::recursive_directory_iterator())) {
253+
if (fs::is_directory(path)) {
254+
continue;
255+
}
249256

250-
for (const fs::path & path : boost::make_iterator_range(
251-
fs::recursive_directory_iterator(root), fs::recursive_directory_iterator())) {
252-
if (fs::is_directory(path)) {
253-
continue;
254-
}
257+
std::cmatch match;
258+
const char * msr = path.generic_string().c_str();
255259

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

259-
// /dev/cpu/[0-9]/msr ?
260-
if (!std::regex_match(msr, match, std::regex(".*msr"))) {
261-
continue;
265+
list.push_back(path.generic_string());
262266
}
263267

264-
list.push_back(path.generic_string());
265-
}
268+
std::sort(list.begin(), list.end(), [](const std::string & c1, const std::string & c2) {
269+
std::cmatch match;
270+
const std::regex filter(".*/(\\d+)/msr");
271+
int n1 = 0;
272+
int n2 = 0;
273+
if (std::regex_match(c1.c_str(), match, filter)) {
274+
n1 = std::stoi(match[1].str());
275+
}
276+
if (std::regex_match(c2.c_str(), match, filter)) {
277+
n2 = std::stoi(match[1].str());
278+
}
279+
return n1 < n2;
280+
}); // NOLINT
266281

267-
std::sort(list.begin(), list.end(), [](const std::string & c1, const std::string & c2) {
268-
std::cmatch match;
269-
const std::regex filter(".*/(\\d+)/msr");
270-
int n1 = 0;
271-
int n2 = 0;
272-
if (std::regex_match(c1.c_str(), match, filter)) {
273-
n1 = std::stoi(match[1].str());
274-
}
275-
if (std::regex_match(c2.c_str(), match, filter)) {
276-
n2 = std::stoi(match[1].str());
282+
if (list.empty()) {
283+
printf("No msr found in /dev/cpu.\n");
284+
return EXIT_FAILURE;
277285
}
278-
return n1 < n2;
279-
}); // NOLINT
280-
281-
if (list.empty()) {
282-
printf("No msr found in /dev/cpu.\n");
283-
return EXIT_FAILURE;
284-
}
285286

286-
// Put the program in the background
287-
if (daemon(0, 0) < 0) {
288-
printf("Failed to put the program in the background. %s\n", strerror(errno));
289-
return errno;
290-
}
287+
// Put the program in the background
288+
if (daemon(0, 0) < 0) {
289+
printf("Failed to put the program in the background. %s\n", strerror(errno));
290+
return errno;
291+
}
291292

292-
// Open connection to system logger
293-
openlog(nullptr, LOG_PID, LOG_DAEMON);
293+
// Open connection to system logger
294+
openlog(nullptr, LOG_PID, LOG_DAEMON);
294295

295-
run(port, list);
296+
run(port, list);
296297

297-
// Close descriptor used to write to system logger
298-
closelog();
298+
// Close descriptor used to write to system logger
299+
closelog();
300+
} catch (const std::exception & e) {
301+
std::cerr << "Exception in main(): " << e.what() << std::endl;
302+
return EXIT_FAILURE;
303+
} catch (...) {
304+
std::cerr << "Unknown exception in main()" << std::endl;
305+
return EXIT_FAILURE;
306+
}
299307

300308
return EXIT_SUCCESS;
301309
}

0 commit comments

Comments
 (0)