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
- New: `server::parse_port(std::string_view text)` returns the port when `text` is a whole decimal number from 1 to 65535, and nothing otherwise. The whole text must be ASCII digits `0` to `9`, at least one; leading zeros are allowed, however many (`08080` is 8080). Everything else is rejected: empty text, a sign (`+` or `-`), a space, tab or any other whitespace anywhere, any other character (letters, `_`, `.`, `0x`, digits from other scripts such as fullwidth `8`, a NUL byte inside the view), 0, and any number above 65535 however many digits it has.
- `apply_setting(config, "listen_port", value)` uses `parse_port`. For a valid value it sets `config.listen_port` and returns no error. For any other value it returns exactly `listen_port: expected a whole number from 1 to 65535, got "<value>"`, with the value as given, and leaves `config.listen_port` as it was. Today `std::atoi` turns `abc` into port 0 and `70000` into port 4464 without a word.
- Unchanged: the `bind_address` setting and the error for an unknown key. Every current deployment's `server.conf` has a plain decimal `listen_port` from 1 to 65535 (checked by ops), so no running server changes its port.
- Ship unit tests for both bounds, leading zeros, each kind of rejected text and the error message.
Files touched
- src/server/config.hpp
- src/server/config.cpp
- tests/config_test.cpp