txeh-v1.6.0
This release contains critical bug fixes addressing thread safety, parsing edge cases, and panic conditions. It also includes a breaking API change to improve data safety, enhanced Windows compatibility, and significantly expanded test coverage.
Breaking Changes
GetHostFileLines() Return Type Changed
GetHostFileLines() now returns HostFileLines (value) instead of *HostFileLines (pointer).
Reason: The previous implementation returned a pointer to internal state, which allowed external code to modify the Hosts struct's data without going through the mutex-protected API. This created potential data races and violated encapsulation.
Migration:
// Before (v1.5.x)
lines := hosts.GetHostFileLines()
for i := range *lines {
line := (*lines)[i]
// ...
}
// After (v1.6.0)
lines := hosts.GetHostFileLines()
for i := range lines {
line := lines[i]
// ...
}Bug Fixes
1. Last Line Dropped When Hosts File Has No Trailing Newline
When parsing a hosts file that did not end with a newline character, the last line was silently dropped. This could result in missing host entries. The parser now correctly handles files regardless of whether they end with a newline.
2. GetHostFileLines Data Race Fixed
As noted in the breaking changes section, GetHostFileLines() now returns a defensive copy of the internal data. This prevents data races when the returned slice is accessed concurrently with modifications to the Hosts struct.
3. TOCTOU Race Condition in AddHost
The AddHost method had a time-of-check-to-time-of-use (TOCTOU) race condition where it would release the mutex between checking existing entries and adding new ones. The lock is now held for the entire operation, ensuring atomic read-modify-write behavior.
4. Nil Pointer Panic in ListHostsByCIDR
Calling ListHostsByCIDR with an invalid CIDR string (e.g., malformed or unparseable) would cause a nil pointer panic. The method now validates input and returns an empty result for invalid CIDR strings instead of panicking.
New Features and Improvements
Windows Hosts File Location
The Windows hosts file location is now determined using the SystemRoot environment variable instead of assuming C:\Windows. This improves compatibility with non-standard Windows installations where the system root is on a different drive or path.
Contributors: @errpunk (PR #27)
Code Quality Improvements
- Updated to Go 1.23
- Applied
gofumptandgofmtformatting - Fixed all
staticcheckwarnings - Improved error handling for deferred file operations in tests
Contributors: @shantanugadgil (PR #29)
Test Coverage
This release includes a significant expansion of the test suite:
- Over 1,500 lines of new test code
- Comprehensive CLI integration tests covering all commands
- Library unit tests for edge cases and error conditions
- Regression tests for all bug fixes listed above
- Race condition tests to verify thread safety
Upgrade Recommendations
All users should upgrade. The parsing bug that drops the last line when files lack a trailing newline could cause silent data loss. The thread safety fixes are important for any concurrent usage of the library.
If you have code that depends on GetHostFileLines(), see the migration guide in the Breaking Changes section above.
Full Changelog
Bug Fixes
- Fix last line being dropped when hosts file has no trailing newline
- Fix data race in GetHostFileLines by returning a copy
- Fix TOCTOU race condition in AddHost
- Fix nil pointer panic in ListHostsByCIDR with invalid CIDR
Enhancements
- Use SystemRoot environment variable for Windows hosts file location
- Update to Go 1.23
- Apply gofumpt/gofmt formatting
- Fix staticcheck warnings
Testing
- Add comprehensive CLI integration tests
- Add library unit tests for edge cases
- Add regression tests for all bug fixes
Documentation
- Update README with clarified Go install requirements
- Add Go Reference badge
- Add feature list and fix typos
Contributors
Thank you to the contributors who helped with this release:
- @errpunk - Windows SystemRoot environment variable support
- @shantanugadgil - Go version update and staticcheck fixes
Installation
CLI
# Using Go install (requires Go 1.23+)
go install github.com/txn2/txeh/txeh@v1.6.0
# Using Homebrew
brew install txn2/tap/txehLibrary
go get github.com/txn2/txeh@v1.6.0Checksums
See the assets below for SHA256 checksums of all release binaries.