From ec0937981328f8dfd7f734b629a07bd2aad11e82 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Beat=20K=C3=BCng?= <beat-kueng@gmx.net>
Date: Mon, 6 Aug 2018 15:38:10 +0200
Subject: [PATCH] px4_daemon: fixes for clang-tidy

Also ignore the warning:
error: calling 'system' uses a command processor [cert-env33-c,-warnings-as-errors]
---
 .clang-tidy                                  |  1 +
 platforms/posix/src/main.cpp                 | 30 +++++++++++---------
 platforms/posix/src/px4_daemon/client.cpp    |  4 +--
 platforms/posix/src/px4_daemon/history.cpp   |  2 +-
 platforms/posix/src/px4_daemon/pxh.cpp       | 10 +++----
 platforms/posix/src/px4_daemon/server.cpp    | 13 ++++-----
 platforms/posix/src/px4_daemon/server_io.cpp |  8 +++---
 7 files changed, 36 insertions(+), 32 deletions(-)

diff --git a/.clang-tidy b/.clang-tidy
index f3a24ba332..3ceae6ad0f 100644
--- a/.clang-tidy
+++ b/.clang-tidy
@@ -1,5 +1,6 @@
 Checks:          '*
 		,-cert-dcl50-cpp
+		,-cert-env33-c
 		,-cert-err34-c
 		,-cert-err58-cpp
 		,-cert-msc30-c
diff --git a/platforms/posix/src/main.cpp b/platforms/posix/src/main.cpp
index f305009c90..bf24137154 100644
--- a/platforms/posix/src/main.cpp
+++ b/platforms/posix/src/main.cpp
@@ -88,7 +88,7 @@ static volatile bool _exit_requested = false;
 
 namespace px4
 {
-void init_once(void);
+void init_once();
 }
 
 static void sig_int_handler(int sig_num);
@@ -174,9 +174,9 @@ int main(int argc, char **argv)
 	} else {
 		/* Server/daemon apps need to parse the command line arguments. */
 
-		std::string data_path = "";
+		std::string data_path;
 		std::string commands_file = "etc/init.d/rcS";
-		std::string test_data_path = "";
+		std::string test_data_path;
 		int instance = 0;
 
 		int myoptind = 1;
@@ -234,8 +234,12 @@ int main(int argc, char **argv)
 		if (test_data_path != "") {
 			const std::string required_test_data_path = "./test_data";
 
-			if (!dir_exists(required_test_data_path.c_str())) {
-				symlink(test_data_path.c_str(), required_test_data_path.c_str());
+			if (!dir_exists(required_test_data_path)) {
+				ret = symlink(test_data_path.c_str(), required_test_data_path.c_str());
+
+				if (ret != PX4_OK) {
+					return ret;
+				}
 			}
 		}
 
@@ -293,14 +297,14 @@ int create_symlinks_if_needed(std::string &data_path)
 {
 	std::string current_path = pwd();
 
-	if (data_path.size() == 0) {
+	if (data_path.empty()) {
 		// No data path given, we'll just try to use the current working dir.
 		data_path = current_path;
 		PX4_INFO("assuming working directory is rootfs, no symlinks needed.");
 		return PX4_OK;
 	}
 
-	if (data_path.compare(current_path) == 0) {
+	if (data_path == current_path) {
 		// We are already running in the data path, so no need to symlink
 		PX4_INFO("working directory seems to be rootfs, no symlinks needed");
 		return PX4_OK;
@@ -313,7 +317,7 @@ int create_symlinks_if_needed(std::string &data_path)
 	std::string src_path = data_path;
 	std::string dest_path = current_path + "/" + path_sym_link;
 
-	if (dir_exists(dest_path.c_str())) {
+	if (dir_exists(dest_path)) {
 		return PX4_OK;
 	}
 
@@ -385,10 +389,10 @@ void register_sig_handler()
 	sig_segv.sa_handler = sig_segv_handler;
 	sig_segv.sa_flags = SA_RESTART | SA_SIGINFO;
 
-	sigaction(SIGINT, &sig_int, NULL);
-	//sigaction(SIGTERM, &sig_int, NULL);
-	sigaction(SIGFPE, &sig_fpe, NULL);
-	sigaction(SIGPIPE, &sig_pipe, NULL);
+	sigaction(SIGINT, &sig_int, nullptr);
+	//sigaction(SIGTERM, &sig_int, nullptr);
+	sigaction(SIGFPE, &sig_fpe, nullptr);
+	sigaction(SIGPIPE, &sig_pipe, nullptr);
 	sigaction(SIGSEGV, &sig_segv, nullptr);
 }
 
@@ -438,7 +442,7 @@ std::string get_absolute_binary_path(const std::string &argv0)
 {
 	// On Linux we could also use readlink("/proc/self/exe", buf, bufsize) to get the absolute path
 
-	std::size_t last_slash = argv0.find_last_of("/");
+	std::size_t last_slash = argv0.find_last_of('/');
 
 	if (last_slash == std::string::npos) {
 		// either relative path or in PATH (PATH is ignored here)
diff --git a/platforms/posix/src/px4_daemon/client.cpp b/platforms/posix/src/px4_daemon/client.cpp
index c1db985887..8f227302d0 100644
--- a/platforms/posix/src/px4_daemon/client.cpp
+++ b/platforms/posix/src/px4_daemon/client.cpp
@@ -309,8 +309,8 @@ Client::register_sig_handler()
 	// been pressed, and we can't wait for the return value from the
 	// cancelled command.
 	sig_int.sa_flags = SA_RESTART;
-	sigaction(SIGINT, &sig_int, NULL);
-	sigaction(SIGTERM, &sig_int, NULL);
+	sigaction(SIGINT, &sig_int, nullptr);
+	sigaction(SIGTERM, &sig_int, nullptr);
 }
 
 void
diff --git a/platforms/posix/src/px4_daemon/history.cpp b/platforms/posix/src/px4_daemon/history.cpp
index 0d601bae7b..eef05ae6c2 100644
--- a/platforms/posix/src/px4_daemon/history.cpp
+++ b/platforms/posix/src/px4_daemon/history.cpp
@@ -52,7 +52,7 @@ void History::try_to_add(const std::string &line)
 	}
 
 	// Don't add duplicate entries.
-	if (!_history.empty() && line.compare(_history.back()) == 0) {
+	if (!_history.empty() && line == _history.back()) {
 		return;
 	}
 
diff --git a/platforms/posix/src/px4_daemon/pxh.cpp b/platforms/posix/src/px4_daemon/pxh.cpp
index 93e86daa44..4544878a33 100644
--- a/platforms/posix/src/px4_daemon/pxh.cpp
+++ b/platforms/posix/src/px4_daemon/pxh.cpp
@@ -71,7 +71,7 @@ int Pxh::process_line(const std::string &line, bool silently_fail)
 		return 0;
 	}
 
-	if (_apps.size() == 0) {
+	if (_apps.empty()) {
 		init_app_map(_apps);
 	}
 
@@ -109,7 +109,7 @@ int Pxh::process_line(const std::string &line, bool silently_fail)
 
 		return retval;
 
-	} else if (command.compare("help") == 0) {
+	} else if (command == "help") {
 		list_builtins(_apps);
 		return 0;
 
@@ -134,7 +134,7 @@ void Pxh::run_pxh()
 
 	_setup_term();
 
-	std::string mystr = "";
+	std::string mystr;
 	int cursor_position = 0; // position of the cursor from right to left
 	// (0: all the way to the right, mystr.length: all the way to the left)
 
@@ -262,10 +262,10 @@ void Pxh::_setup_term()
 	term.c_lflag &= ~ICANON;
 	term.c_lflag &= ~ECHO;
 	tcsetattr(0, TCSANOW, &term);
-	setbuf(stdin, NULL);
+	setbuf(stdin, nullptr);
 }
 
-void Pxh::_restore_term(void)
+void Pxh::_restore_term()
 {
 	if (_instance) {
 		tcsetattr(0, TCSANOW, &_instance->_orig_term);
diff --git a/platforms/posix/src/px4_daemon/server.cpp b/platforms/posix/src/px4_daemon/server.cpp
index ab1ed53758..c61bc72eaa 100644
--- a/platforms/posix/src/px4_daemon/server.cpp
+++ b/platforms/posix/src/px4_daemon/server.cpp
@@ -73,9 +73,9 @@ int
 Server::start()
 {
 	if (0 != pthread_create(&_server_main_pthread,
-				NULL,
+				nullptr,
 				_server_main_trampoline,
-				NULL)) {
+				nullptr)) {
 
 		PX4_ERR("error creating client handler thread");
 
@@ -92,7 +92,7 @@ Server::_server_main_trampoline(void *arg)
 		_instance->_server_main(arg);
 	}
 
-	return NULL;
+	return nullptr;
 }
 
 void Server::_pthread_key_destructor(void *arg)
@@ -156,7 +156,6 @@ Server::_server_main(void *arg)
 	}
 
 	close(client_send_pipe_fd);
-	return;
 }
 
 void
@@ -214,7 +213,7 @@ Server::_execute_cmd_packet(const client_send_packet_s &packet)
 	args->is_atty = packet.payload.execute_msg.is_atty;
 
 	_lock(); // need to lock, otherwise the thread could already exit before we insert into the map
-	ret = pthread_create(&new_pthread, NULL, Server::_run_cmd, (void *)args);
+	ret = pthread_create(&new_pthread, nullptr, Server::_run_cmd, (void *)args);
 
 	if (ret != 0) {
 		PX4_ERR("could not start pthread (%i)", ret);
@@ -284,7 +283,7 @@ void
 	// We register thread specific data. This is used for PX4_INFO (etc.) log calls.
 	CmdThreadSpecificData *thread_data_ptr;
 
-	if ((thread_data_ptr = (CmdThreadSpecificData *)pthread_getspecific(_instance->_key)) == NULL) {
+	if ((thread_data_ptr = (CmdThreadSpecificData *)pthread_getspecific(_instance->_key)) == nullptr) {
 		thread_data_ptr = new CmdThreadSpecificData;
 		thread_data_ptr->pipe_fd = pipe_fd;
 		thread_data_ptr->is_atty = is_atty;
@@ -302,7 +301,7 @@ void
 	// Clean up before returning.
 	_instance->_cleanup_thread(client_uuid);
 
-	return NULL;
+	return nullptr;
 }
 
 
diff --git a/platforms/posix/src/px4_daemon/server_io.cpp b/platforms/posix/src/px4_daemon/server_io.cpp
index 67e9e138be..72564b5c88 100644
--- a/platforms/posix/src/px4_daemon/server_io.cpp
+++ b/platforms/posix/src/px4_daemon/server_io.cpp
@@ -69,7 +69,7 @@ int get_stdout_pipe_buffer(char **buffer, unsigned *max_length, bool *is_atty)
 	// have any thread specific data set and we won't have a pipe to write
 	// stdout to.
 	if ((thread_data_ptr = (Server::CmdThreadSpecificData *)pthread_getspecific(
-				       Server::get_pthread_key())) == NULL) {
+				       Server::get_pthread_key())) == nullptr) {
 		return -1;
 	}
 
@@ -104,7 +104,7 @@ int send_stdout_pipe_buffer(unsigned buffer_length)
 	}
 
 	if ((thread_data_ptr = (Server::CmdThreadSpecificData *)pthread_getspecific(
-				       Server::get_pthread_key())) == NULL) {
+				       Server::get_pthread_key())) == nullptr) {
 		return -1;
 	}
 
@@ -117,11 +117,11 @@ int send_stdout_pipe_buffer(unsigned buffer_length)
 	// Check if we can write first by writing 0 bytes.
 	// If we don't do this, we'll get SIGPIPE and be very unhappy
 	// because the whole process will go down.
-	int ret = write(pipe_fd, NULL, 0);
+	int ret = write(pipe_fd, nullptr, 0);
 
 	if (ret == 0 && errno == EPIPE) {
 		printf("Error: can't write to closed pipe, giving up.\n");
-		pthread_exit(NULL);
+		pthread_exit(nullptr);
 	}
 
 	int bytes_sent = write(pipe_fd, packet, bytes_to_send);
-- 
GitLab