diff --git a/src/connection.cpp b/src/connection.cpp index c4e4be0..31e4ecc 100644 --- a/src/connection.cpp +++ b/src/connection.cpp @@ -1,10 +1,9 @@ #include "connection.hpp" +#include #include #include #include -#include -#include #include "server.hpp" // Need this for releaseBackToServer implementation diff --git a/src/connection_registry.cpp b/src/connection_registry.cpp index 19037da..545fda0 100644 --- a/src/connection_registry.cpp +++ b/src/connection_registry.cpp @@ -47,7 +47,7 @@ ConnectionRegistry::~ConnectionRegistry() { void ConnectionRegistry::store(int fd, std::unique_ptr connection) { if (fd < 0 || static_cast(fd) >= max_fds_) { - abort(); + std::abort(); } // Release ownership from unique_ptr and store raw pointer connections_[fd].store(connection.release(), std::memory_order_release); @@ -55,7 +55,7 @@ void ConnectionRegistry::store(int fd, std::unique_ptr connection) { std::unique_ptr ConnectionRegistry::remove(int fd) { if (fd < 0 || static_cast(fd) >= max_fds_) { - abort(); + std::abort(); } return std::unique_ptr( connections_[fd].exchange(nullptr, std::memory_order_acquire)); diff --git a/src/json_tokens.gperf b/src/json_tokens.gperf index 451f8b0..90545ea 100644 --- a/src/json_tokens.gperf +++ b/src/json_tokens.gperf @@ -1,5 +1,5 @@ %{ -#include +#include %} %define hash-function-name hash_json_token %define lookup-function-name lookup_json_token diff --git a/src/main.cpp b/src/main.cpp index c6357dc..1c3b78a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,6 +6,7 @@ #include "server.hpp" #include #include +#include #include #include #include @@ -44,7 +45,7 @@ std::vector create_listen_sockets(const weaseldb::Config &config) { unlink(config.server.unix_socket_path.c_str()); struct sockaddr_un addr; - memset(&addr, 0, sizeof(addr)); + std::memset(&addr, 0, sizeof(addr)); addr.sun_family = AF_UNIX; if (config.server.unix_socket_path.length() >= sizeof(addr.sun_path)) { @@ -52,8 +53,8 @@ std::vector create_listen_sockets(const weaseldb::Config &config) { std::abort(); } - strncpy(addr.sun_path, config.server.unix_socket_path.c_str(), - sizeof(addr.sun_path) - 1); + std::strncpy(addr.sun_path, config.server.unix_socket_path.c_str(), + sizeof(addr.sun_path) - 1); if (bind(sfd, (struct sockaddr *)&addr, sizeof(addr)) == -1) { perror("bind"); @@ -74,7 +75,7 @@ std::vector create_listen_sockets(const weaseldb::Config &config) { struct addrinfo *result, *rp; int s; - memset(&hints, 0, sizeof(hints)); + std::memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */ hints.ai_socktype = SOCK_STREAM; /* stream socket */ hints.ai_flags = AI_PASSIVE; /* For wildcard IP address */ @@ -86,7 +87,7 @@ std::vector create_listen_sockets(const weaseldb::Config &config) { s = getaddrinfo(config.server.bind_address.c_str(), std::to_string(config.server.port).c_str(), &hints, &result); if (s != 0) { - fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s)); + std::fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s)); std::abort(); } @@ -254,9 +255,9 @@ int main(int argc, char *argv[]) { g_server = server.get(); // Setup signal handling - signal(SIGPIPE, SIG_IGN); - signal(SIGTERM, signal_handler); - signal(SIGINT, signal_handler); + std::signal(SIGPIPE, SIG_IGN); + std::signal(SIGTERM, signal_handler); + std::signal(SIGINT, signal_handler); std::cout << "Starting WeaselDB HTTP server..." << std::endl; server->run(); diff --git a/src/server.cpp b/src/server.cpp index e3c3ea7..1f53f42 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -131,7 +131,7 @@ void Server::shutdown() { // Write single byte to avoid partial write complexity while (write(shutdown_pipe_[1], &val, 1) == -1) { if (errno != EINTR) { - abort(); // graceful shutdown didn't work. Let's go ungraceful. + std::abort(); // graceful shutdown didn't work. Let's go ungraceful. } } } @@ -328,7 +328,7 @@ void Server::start_io_threads(std::vector &threads) { continue; } perror("epoll_wait"); - abort(); + std::abort(); } ready_listen_fds.clear(); // Clear from previous iteration @@ -384,7 +384,7 @@ void Server::start_io_threads(std::vector &threads) { if (errno == EINTR) continue; perror("accept4"); - abort(); + std::abort(); } // Check connection limit diff --git a/src/thread_pipeline.hpp b/src/thread_pipeline.hpp index 3adec57..c492f62 100644 --- a/src/thread_pipeline.hpp +++ b/src/thread_pipeline.hpp @@ -344,10 +344,10 @@ public: // Violating preconditions results in program termination via abort(). [[nodiscard]] ProducerGuard push(uint32_t const size, bool block) { if (size == 0) { - abort(); + std::abort(); } if (size > slot_count) { - abort(); + std::abort(); } // Reserve a slot to construct an item, but don't publish to consumer yet uint32_t slot; diff --git a/style.md b/style.md index 700583f..4484231 100644 --- a/style.md +++ b/style.md @@ -22,6 +22,39 @@ This document describes the C++ coding style used in the WeaselDB project. These - Use modern C++ features: RAII, move semantics, constexpr, concepts where appropriate - Prefer standard library containers and algorithms over custom implementations +### C Library Functions and Headers +- **Always use std:: prefixed versions** of C library functions for consistency and clarity +- **Use C++ style headers** (``, ``, etc.) instead of C style headers (``, ``, etc.) +- This applies to all standard libc functions: `std::abort()`, `std::fprintf()`, `std::free()`, `std::memcpy()`, `std::strlen()`, `std::strncpy()`, `std::memset()`, `std::signal()`, etc. +- Exception: Functions with no std:: equivalent (e.g., `perror()`, `gai_strerror()`) and system-specific headers (e.g., ``, ``) +```cpp +// Preferred - C++ style +#include +#include +#include +std::abort(); +std::fprintf(stderr, "Error message\n"); +std::free(ptr); +std::memcpy(dest, src, size); +std::strlen(str); +std::strncpy(dest, src, n); +std::memset(ptr, value, size); +std::signal(SIGTERM, handler); + +// Avoid - C style +#include +#include +#include +abort(); +fprintf(stderr, "Error message\n"); +free(ptr); +memcpy(dest, src, size); +strlen(str); +strncpy(dest, src, n); +memset(ptr, value, size); +signal(SIGTERM, handler); +``` + ### Data Types - **Almost always signed** - prefer `int`, `int64_t`, `size_t` over unsigned types except for: - Bit manipulation operations diff --git a/tests/test_server_connection_return.cpp b/tests/test_server_connection_return.cpp index 415333f..fe2d829 100644 --- a/tests/test_server_connection_return.cpp +++ b/tests/test_server_connection_return.cpp @@ -3,6 +3,7 @@ #include "connection.hpp" #include "perfetto_categories.hpp" #include "server.hpp" +#include #include #include @@ -71,9 +72,9 @@ TEST_CASE( const char *test_message = "Hello, World!"; ssize_t bytes_written; do { - bytes_written = write(client_fd, test_message, strlen(test_message)); + bytes_written = write(client_fd, test_message, std::strlen(test_message)); } while (bytes_written == -1 && errno == EINTR); - REQUIRE(bytes_written == strlen(test_message)); + REQUIRE(bytes_written == std::strlen(test_message)); // Read the echoed response char buffer[1024] = {0}; @@ -84,7 +85,7 @@ TEST_CASE( if (bytes_read == -1) { perror("read failed"); } - REQUIRE(bytes_read == strlen(test_message)); + REQUIRE(bytes_read == std::strlen(test_message)); // Verify we got back exactly what we sent CHECK(std::string(buffer, bytes_read) == std::string(test_message)); @@ -93,7 +94,7 @@ TEST_CASE( int e = close(client_fd); if (e == -1 && errno != EINTR) { perror("close client_fd"); - abort(); + std::abort(); } server->shutdown(); server_thread.join();