src: fix crash when writing odd-length hex string via Writev
Summary
Writing an odd-length hex string to a stream that batches writes via
Writev (e.g. http.request which auto-corks its socket) would
fatal-assert:
node: ../src/string_bytes.cc: Assertion `str->Length() % 2 == 0 &&
"invalid hex string length"' failed.
Reproduction:
const http = require('http');
const req = http.request('http://example.org', { method: 'POST' });
req.write('1', 'hex'); // crashes
Root cause
StringBytes::StorageSize had a CHECK that asserted the hex string
length was even. When writes are batched through Writev, this check
runs before any data is written. A single Write (non-corked path)
did not crash because StringBytes::Write calls HexDecode, which
silently drops the trailing incomplete nibble.
Fix
Remove the CHECK and use integer division for the HEX case, which
naturally rounds down for odd lengths. This is already the behaviour
of StringBytes::Size and HexDecode.
Test
Added test/parallel/test-http-odd-hex-write.js with three cases:
- HTTP POST with a single odd-length hex write (was crashing)
- HTTP POST with cork/uncork and mixed hex writes (was crashing)
- net socket with cork/uncork and an odd-length hex write
Fixes: #45150
Codecov Report
:white_check_mark: All modified and coverable lines are covered by tests.
:white_check_mark: Project coverage is 90.34%. Comparing base (30a7e28) to head (0e3192c).
:warning: Report is 246 commits behind head on main.
Additional details and impacted files
@@ Coverage Diff @@
## main #63658 +/- ##
==========================================
- Coverage 91.95% 90.34% -1.61%
==========================================
Files 379 732 +353
Lines 166454 236434 +69980
Branches 25427 44532 +19105
==========================================
+ Hits 153058 213614 +60556
- Misses 13104 14522 +1418
- Partials 292 8298 +8006
| Files with missing lines | Coverage Ξ | |
|---|---|---|
| src/string_bytes.cc | 71.54% <ΓΈ> (ΓΈ) |
:rocket: New features to boost your workflow:
- :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
- :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
cc @nodejs/buffer
CI: https://ci.nodejs.org/job/node-test-pull-request/74400/