Skip to content

Commit

Permalink
Merge pull request #6 from sjinks/fix-sonarcloud
Browse files Browse the repository at this point in the history
fix: issues found by SonarCloud
  • Loading branch information
sjinks authored Jan 14, 2024
2 parents 327adef + 71e14b8 commit 0855fe1
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 40 deletions.
4 changes: 4 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
"settings": {
"sonarlint.ls.javaHome": "/usr/lib/jvm/java-21-openjdk-amd64",
"sonarlint.pathToCompileCommands": "${workspaceFolder}/build/compile_commands.json",
"sonarlint.connectedMode.project": {
"connectionId": "wildwolf",
"projectKey": "sjinks_tfhttp"
},
"C_Cpp.codeAnalysis.clangTidy.enabled": true,
"C_Cpp.codeAnalysis.clangTidy.useBuildPath": true,
"C_Cpp.default.cppStandard": "c++20",
Expand Down
18 changes: 9 additions & 9 deletions src/clienthandler_p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void ClientHandlerPrivate::on_read(ev::io& watcher, int) // NOSONAR(cpp:S995)
constexpr std::size_t BUFFER_SIZE = 8192;
std::array<char, BUFFER_SIZE> buffer{};
const ssize_t received = recv(watcher.fd, buffer.data(), sizeof(buffer), 0);
if (received <= 0) {
if (received <= 0) [[unlikely]] {
std::cerr << std::format(
"Error: failed to read from socket: {}\n", std::error_code(errno, std::system_category()).message()
);
Expand All @@ -51,7 +51,7 @@ void ClientHandlerPrivate::on_read(ev::io& watcher, int) // NOSONAR(cpp:S995)
}

if (auto status = llhttp_execute(&this->m_parser, buffer.data(), static_cast<std::size_t>(received));
status != HPE_OK) {
status != HPE_OK) [[unlikely]] {
std::cerr << std::format("Error: failed to parse HTTP request: {}\n", llhttp_errno_name(status));
this->stop_watchers();
this->terminate();
Expand All @@ -68,7 +68,7 @@ void ClientHandlerPrivate::on_write(ev::io& watcher, int)
auto len = this->m_response.size() - this->m_offset;
const ssize_t written = send(watcher.fd, buf, len, 0);

if (written <= 0) {
if (written <= 0) [[unlikely]] {
std::cerr << std::format(
"Error: failed to write to socket: {}\n", std::error_code(errno, std::system_category()).message()
);
Expand Down Expand Up @@ -101,7 +101,7 @@ void ClientHandlerPrivate::stop_watchers()

void ClientHandlerPrivate::terminate()
{
if (auto server = this->m_server.lock(); server) {
if (auto server = this->m_server.lock(); server) [[likely]] {
server->remove_handler(this->q_ptr);
}
}
Expand Down Expand Up @@ -162,7 +162,7 @@ void ClientHandlerPrivate::not_found()
void ClientHandlerPrivate::method_not_allowed(const char* allowed)
{
const auto status = static_cast<int>(HTTP_STATUS_METHOD_NOT_ALLOWED);
const char* text = ClientHandlerPrivate::get_status_text(status);
const auto text = ClientHandlerPrivate::get_status_text(status);

this->m_response = std::format(
"HTTP/1.1 {0} {1}\r\n"
Expand All @@ -173,7 +173,7 @@ void ClientHandlerPrivate::method_not_allowed(const char* allowed)
"Connection: close\r\n"
"\r\n"
"{1}\n",
status, text, ClientHandlerPrivate::generate_http_date(), std::strlen(text) + 1, allowed
status, text, ClientHandlerPrivate::generate_http_date(), text.size() + 1, allowed
);
}

Expand Down Expand Up @@ -274,14 +274,14 @@ std::string ClientHandlerPrivate::generate_http_date()
return std::format("{0:%a} {0:%d} {0:%b} {0:%Y} {0:%X} GMT", std::chrono::system_clock::now());
}

const char* ClientHandlerPrivate::get_status_text(int status)
std::string_view ClientHandlerPrivate::get_status_text(int status)
{
return llhttp_status_name(static_cast<llhttp_status_t>(status));
}

void ClientHandlerPrivate::generate_text_response(int status)
{
const char* text = ClientHandlerPrivate::get_status_text(status);
auto text = ClientHandlerPrivate::get_status_text(status);

this->m_response = std::format(
"HTTP/1.1 {0} {1}\r\n"
Expand All @@ -291,6 +291,6 @@ void ClientHandlerPrivate::generate_text_response(int status)
"Connection: close\r\n"
"\r\n"
"{1}\n",
status, text, ClientHandlerPrivate::generate_http_date(), std::strlen(text) + 1
status, text, ClientHandlerPrivate::generate_http_date(), text.size() + 1
);
}
2 changes: 1 addition & 1 deletion src/clienthandler_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class ClientHandlerPrivate {
static int on_body(llhttp_t* parser, const char* at, std::size_t length);

static std::string generate_http_date();
static const char* get_status_text(int status);
static std::string_view get_status_text(int status);
void generate_text_response(int status);
};

Expand Down
23 changes: 5 additions & 18 deletions src/database_p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ DatabasePrivate::set_state(const std::string& slug, const std::string& state, co
return this->transaction([this, &slug, &state, &lock_id]() {
using enum Database::status_t;
if (this->has_lock(slug)) {
if (lock_id.empty()) {
if (lock_id.empty()) [[unlikely]] {
return BAD_REQUEST;
}

Expand All @@ -50,7 +50,7 @@ Database::status_t DatabasePrivate::delete_state(const std::string& slug, const
return this->transaction([this, &slug, &lock_id]() {
using enum Database::status_t;
if (this->has_lock(slug)) {
if (lock_id.empty()) {
if (lock_id.empty()) [[unlikely]] {
return BAD_REQUEST;
}

Expand Down Expand Up @@ -79,7 +79,7 @@ Database::status_t DatabasePrivate::put_lock(const std::string& slug, const std:
Database::status_t DatabasePrivate::delete_lock(const std::string& slug, const std::string& lock_id)
{
return this->transaction([this, &slug, &lock_id]() {
if (!this->has_lock(slug, lock_id)) {
if (!this->has_lock(slug, lock_id)) [[unlikely]] {
return Database::status_t::BAD_REQUEST;
}

Expand Down Expand Up @@ -136,27 +136,14 @@ void DatabasePrivate::run_query(const char* query, std::initializer_list<std::st
cmd.bind(i + 1, *(args.begin() + i), sqlite3pp::nocopy);
}

const int rc = cmd.execute();
if (rc != SQLITE_OK) {
if (cmd.execute() != SQLITE_OK) [[unlikely]] {
throw sqlite3pp::database_error(this->m_db);
}
}

void DatabasePrivate::execute(const char* query)
{
const int rc = this->m_db.execute(query);
if (rc != SQLITE_OK) {
if (this->m_db.execute(query) != SQLITE_OK) [[unlikely]] {
throw sqlite3pp::database_error(this->m_db);
}
}

Database::status_t DatabasePrivate::transaction(const transaction_t& f)
{
sqlite3pp::transaction xact(this->m_db);
auto result = f();
if (xact.commit() != SQLITE_OK) {
throw sqlite3pp::database_error(this->m_db);
}

return result;
}
14 changes: 11 additions & 3 deletions src/database_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,17 @@ class DatabasePrivate {
void run_query(const char* query, std::initializer_list<std::string> args);
void execute(const char* query);

using transaction_t = std::function<Database::status_t()>;

Database::status_t transaction(const transaction_t& f);
template<typename F, typename... Args>
Database::status_t transaction(const F& f, Args&&... args)
{
sqlite3pp::transaction xact(this->m_db);
auto result = f(std::forward<Args>(args)...);
if (xact.commit() != SQLITE_OK) [[unlikely]] {
throw sqlite3pp::database_error(this->m_db);
}

return result;
}
};

#endif /* F640FA93_5723_4C84_8793_6509B318D18B */
16 changes: 7 additions & 9 deletions src/serversocket_p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
ServerSocketPrivate::ServerSocketPrivate(const std::string& ip, std::uint16_t port)
: m_socket(socket(PF_INET, SOCK_STREAM | SOCK_NONBLOCK, IPPROTO_TCP))
{
if (this->m_socket < 0) {
if (this->m_socket < 0) [[unlikely]] {
throw std::system_error(errno, std::system_category(), "socket() failed");
}

Expand All @@ -23,27 +23,25 @@ ServerSocketPrivate::ServerSocketPrivate(const std::string& ip, std::uint16_t po
s.as_sockaddr_in6()->sin6_family = AF_INET6;
s.as_sockaddr_in6()->sin6_port = htons(port);
}
else {
else [[unlikely]] {
throw std::invalid_argument("Invalid IP address");
}

if (const int res = bind(this->m_socket, s.as_sockaddr(), s.size()); res < 0) {
if (const int res = bind(this->m_socket, s.as_sockaddr(), s.size()); res < 0) [[unlikely]] {
throw std::system_error(errno, std::system_category(), "bind() failed");
}
}

ServerSocketPrivate::~ServerSocketPrivate()
{
if (this->m_socket >= 0) {
if (this->m_socket >= 0) [[likely]] {
close(this->m_socket);
}
}

std::uint16_t ServerSocketPrivate::listen() const
{
constexpr int BACKLOG_SIZE = 512;

if (::listen(this->m_socket, BACKLOG_SIZE) < 0) {
if (::listen(this->m_socket, ServerSocketPrivate::BACKLOG_SIZE) < 0) [[unlikely]] {
throw std::system_error(errno, std::system_category(), "listen() failed");
}

Expand All @@ -53,7 +51,7 @@ std::uint16_t ServerSocketPrivate::listen() const
int ServerSocketPrivate::accept() const
{
const int res = accept4(this->m_socket, nullptr, nullptr, SOCK_NONBLOCK);
if (res < 0) {
if (res < 0) [[unlikely]] {
throw std::system_error(errno, std::system_category(), "accept4() failed");
}

Expand All @@ -69,7 +67,7 @@ std::uint16_t ServerSocketPrivate::get_port() const
{
SockAddr addr{};

if (socklen_t addr_len = addr.size(); getsockname(this->m_socket, addr.as_sockaddr(), &addr_len) < 0) {
if (socklen_t addr_len = addr.size(); getsockname(this->m_socket, addr.as_sockaddr(), &addr_len) < 0) [[unlikely]] {
throw std::system_error(errno, std::system_category(), "getsockname() failed");
}

Expand Down
2 changes: 2 additions & 0 deletions src/serversocket_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class ServerSocketPrivate {
private:
int m_socket = -1;

static constexpr const int BACKLOG_SIZE = 512;

std::uint16_t get_port() const; // NOLINT(*-use-nodiscard)
};

Expand Down

0 comments on commit 0855fe1

Please sign in to comment.