buffer: UTF-16LE indexOf can hang on unaligned Buffer views
まだ誰も着手していません。
- 主要言語
- JavaScript
- スター
- 122k
- フォーク
- 37.4k
- 平均マージ
- 4日 3時間
- マージ済み PR(30日)
- 272
説明
Version
v27.0.0-pre (b805fb5158a)
Platform
Darwin 24.6.0 arm64
Subsystem
buffer
What steps will reproduce the bug?
Buffer#indexOf() can hang when its receiver is an unaligned view. The child process timeout prevents the reproduction from hanging indefinitely.
'use strict';
const { spawnSync } = require('node:child_process');
for (const prefixLength of [0, 1]) {
const source = `
const data = Buffer.from('abc', 'utf16le');
const backing = Buffer.alloc(data.length + ${prefixLength});
data.copy(backing, ${prefixLength});
const view = backing.subarray(${prefixLength});
console.log(view.indexOf('a', 0, 'utf16le'));
`;
const result = spawnSync(process.execPath, ['-e', source], {
encoding: 'utf8',
timeout: 2000,
});
console.log({
prefixLength,
status: result.status,
error: result.error?.code,
stdout: result.stdout.trim(),
});
}
How often does it reproduce? Is there a required condition?
It's consistent when the receiver view starts at an odd byte offset. The aligned control returns normally.
What is the expected behavior? Why is that the expected behavior?
Both searches should terminate and return 0.
The views contain the same bytes, and the UTF-16LE value begins at index 0 of each view. The result should not depend on the underlying memory address.
What do you see instead?
The aligned view returns 0. The view starting at byte offset 1 does not complete before the timeout:
{ prefixLength: 0, status: 0, error: undefined, stdout: '0' }
{ prefixLength: 1, status: null, error: 'ETIMEDOUT', stdout: '' }
Additional information
Buffer#indexOf() passes the receiver data to the 16-bit search, while nbytes aligns matched addresses down. For an odd-offset view, this can move before the start of the view and appears to cause the hang.
This is distinct from #26448, which concerns search-offset rounding, and remains reproducible after #65905, which affects UTF-16LE decoding rather than Buffer#indexOf().
Copying the view or using a Buffer needle avoids the hang. Both workarounds return 0 locally:
Buffer.from(view).indexOf('a', 0, 'utf16le');
view.indexOf(Buffer.from('a', 'utf16le'));
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず提供された再現手順を実行し、その後、src/node_buffer.cc の 1119-1124 行付近にある Buffer#indexOf と、deps/nbytes/include/nbytes.h の 459-465 行付近にある 16 ビット検索のアライメントを調べます。奇数オフセットのビューがどのように検索に渡されるかを追跡し、アラインされたビューとアラインされていないビューの回帰テストを追加します。完了条件は、両方の UTF-16LE 検索が終了して 0 を返すことです。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- cpp, javascript, node.js
- 領域
- backend
- issue の種類
- バグ
- 難易度
- 3/5
- 見積もり時間
- 1〜2日
- 活発さ
- 活発
- 明瞭さ
- 明確に書かれている
- 初心者へのやさしさ
- 68/100