Reject bad listen_port values in server.conf

C++ · C++ · beginner · modification

`listen_port` in `server.conf` went through `std::atoi`, so a typo like `80800` or `8o80` quietly gave the server a different port (or port 0, which lets the OS pick any free port) instead of stopping it. This PR adds `server::parse_port` to `config.hpp`/`config.cpp`: it reads the value with `std::from_chars` into an `unsigned long`, requires the whole text to be consumed and the number to be from 1 to 65535, and returns an `std::optional<std::uint16_t>`. `apply_setting` now returns a message naming the setting, the allowed range and the rejected value, and keeps the previous port on failure, so `read_config` stops the server at startup with the file and line. `tests/config_test.cpp` is new: it covers accepted ports at both ends of the range, numbers out of range, text that is not plain digits, and the message `apply_setting` returns.

C++20, built with CMake by Apple clang 17 locally and GCC 13 in CI, with `-Wall -Wextra -Wconversion -Werror`; standard library only. The CMake files are unchanged: they build every `src/**/*.cpp` into the library and every `tests/*_test.cpp` together with `tests/check_main.cpp` into the test binary that CI runs. The server is a Linux and macOS daemon whose `server.conf` is written by the ops team. Existing and unchanged: `read_config(path)` reads `server.conf` on the main thread at startup, before any other thread exists, one `key = value` per line (lines are at most 4,096 bytes, read as raw bytes); it trims ASCII spaces and tabs around the key and the value, calls `apply_setting` for each line, and on a returned error prints `server.conf:<line>: <message>` to stderr and exits with status 1 before it binds the listener. Ports are `std::uint16_t` and 0 is never a valid configured port. Tests use the repo's tiny helper `tests/check.hpp` (`TEST(name)`, `CHECK(expr)`, `CHECK_EQ(a, b)`), and `tests/check_main.cpp` runs every registered test.

Requirements

Files touched

Review this PR

C++ practice