Skip to content

feat(host_name): add free function returning the local hostname#248

Merged
sgerbino merged 1 commit into
cppalliance:developfrom
sgerbino:feature/host-name
May 13, 2026
Merged

feat(host_name): add free function returning the local hostname#248
sgerbino merged 1 commit into
cppalliance:developfrom
sgerbino:feature/host-name

Conversation

@sgerbino
Copy link
Copy Markdown
Collaborator

Adds boost::corosio::host_name(), a synchronous free function returning the local machine's hostname as a UTF-8 std::string. Common uses: logging, default Host: headers, cluster registration, identifying a node.

A single overload that throws std::runtime_error on failure. No std::error_code variant: failures of the underlying syscalls are pathological on a configured machine, and the throwing form is sufficient.

POSIX path uses gethostname(2). The buffer is sized via sysconf(_SC_HOST_NAME_MAX), falling back to HOST_NAME_MAX or 256. errno is captured immediately on failure. POSIX does not guarantee NUL termination on truncation, so the implementation verifies the buffer's final byte is NUL and throws otherwise.

Windows path uses GetComputerNameExW(ComputerNameDnsHostname, ...) followed by WideCharToMultiByte(CP_UTF8, ...) to produce UTF-8. Chosen over winsock gethostname() so the function works without WSAStartup(); in corosio winsock is initialized lazily inside io_context, so a winsock-based implementation would tie host_name() to io_context lifetime, contradicting the "no io_context needed" design goal.

Adds five unit tests covering: non-empty result, stability across calls, reasonable length (<= 255 octets), no-io_context-needed (load-bearing regression guard for the Windows implementation choice), and charset sanity (catches UTF-16 -> UTF-8 regressions).

Resolves #247.

Adds boost::corosio::host_name(), a synchronous free function returning
the local machine's hostname as a UTF-8 std::string. Common uses:
logging, default Host: headers, cluster registration, identifying a
node.

A single overload that throws std::runtime_error on failure. No
std::error_code variant: failures of the underlying syscalls are
pathological on a configured machine, and the throwing form is
sufficient.

POSIX path uses gethostname(2) with a 256-byte stack buffer, which
comfortably exceeds the _POSIX_HOST_NAME_MAX floor of 255 and every
mainstream OS's actual cap. errno is captured immediately on failure.
POSIX does not guarantee NUL termination on truncation, so the
implementation checks for a NUL anywhere in the buffer and throws
otherwise.

Windows path uses GetComputerNameExW(ComputerNameDnsHostname, ...)
followed by WideCharToMultiByte(CP_UTF8, ...) to produce UTF-8.
Chosen over winsock gethostname() so the function works without
WSAStartup(); in corosio winsock is initialized lazily inside
io_context, so a winsock-based implementation would tie host_name()
to io_context lifetime, contradicting the "no io_context needed"
design goal.

Adds five unit tests covering: non-empty result, stability across
calls, reasonable length (<= 255 octets), no-io_context-needed
(load-bearing regression guard for the Windows implementation
choice), and charset sanity (catches UTF-16 -> UTF-8 regressions).
@cppalliance-bot
Copy link
Copy Markdown

An automated preview of the documentation is available at https://248.corosio.prtest3.cppalliance.org/index.html

If more commits are pushed to the pull request, the docs will rebuild at the same URL.

2026-05-13 18:38:28 UTC

@cppalliance-bot
Copy link
Copy Markdown

GCOVR code coverage report https://248.corosio.prtest3.cppalliance.org/gcovr/index.html
LCOV code coverage report https://248.corosio.prtest3.cppalliance.org/genhtml/index.html
Coverage Diff Report https://248.corosio.prtest3.cppalliance.org/diff-report/index.html

Build time: 2026-05-13 18:46:52 UTC

@codecov
Copy link
Copy Markdown

codecov Bot commented May 13, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 77.71%. Comparing base (cd19108) to head (32dd71a).
⚠️ Report is 1 commits behind head on develop.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff            @@
##           develop     #248   +/-   ##
========================================
  Coverage    77.71%   77.71%           
========================================
  Files           96       96           
  Lines         7298     7298           
  Branches      1787     1787           
========================================
  Hits          5672     5672           
  Misses        1108     1108           
  Partials       518      518           

Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update cd19108...32dd71a. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@sgerbino sgerbino merged commit 0e568b9 into cppalliance:develop May 13, 2026
41 checks passed
@sgerbino sgerbino deleted the feature/host-name branch May 13, 2026 18:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add host_name() free function returning the local hostname

2 participants