node icon indicating copy to clipboard operation
node copied to clipboard

src: fix crash when writing odd-length hex string via Writev

Open RajeshKumar11 opened this issue 6 months ago β€’ 3 comments

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:

  1. HTTP POST with a single odd-length hex write (was crashing)
  2. HTTP POST with cork/uncork and mixed hex writes (was crashing)
  3. net socket with cork/uncork and an odd-length hex write

Fixes: #45150

RajeshKumar11 avatar May 30 '26 07:05 RajeshKumar11

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% <ΓΈ> (ΓΈ)

... and 482 files with indirect coverage changes

: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.

codecov[bot] avatar May 30 '26 09:05 codecov[bot]

cc @nodejs/buffer

geeksilva97 avatar Jun 24 '26 13:06 geeksilva97

CI: https://ci.nodejs.org/job/node-test-pull-request/74400/

nodejs-github-bot avatar Jun 24 '26 13:06 nodejs-github-bot