nodejs / nodejs/node

buffer: UTF-16LE indexOf can hang on unaligned Buffer views

Đang mở
#65,959 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Ngôn ngữ chính
JavaScript
Star
122k
Fork
37.3k
Merge trung bình
4 ngày 2 giờ
Pull request đã merge (30 ngày)
283

Mô tả

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'));

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Trước tiên, hãy chạy bản tái hiện được cung cấp, sau đó kiểm tra Buffer#indexOf trong src/node_buffer.cc quanh các dòng 1119-1124 và việc căn chỉnh tìm kiếm 16-bit trong deps/nbytes/include/nbytes.h quanh các dòng 459-465. Theo dõi cách một view có offset lẻ được truyền vào quá trình tìm kiếm và bổ sung coverage hồi quy cho các view được căn chỉnh và không được căn chỉnh. Hoàn tất khi cả hai lần tìm kiếm UTF-16LE đều kết thúc và trả về 0.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
cpp, javascript, node.js
Lĩnh vực
backend
Loại issue
Lỗi
Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức độ hoạt động
Sôi nổi
Độ rõ ràng
Đặc tả rõ ràng
Mức phù hợp với người mới
68/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.