nodejs / nodejs/node

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

Ouverte
#65,959 1 commentaire 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Langage dominant
JavaScript
Étoiles
122k
Forks
37.3k
Merge moyen
4 j 2 h
PR mergées (30 j)
283

Description

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

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Exécutez d’abord la reproduction fournie, puis inspectez Buffer#indexOf dans src/node_buffer.cc autour des lignes 1119-1124 ainsi que l’alignement de la recherche sur 16 bits dans deps/nbytes/include/nbytes.h autour des lignes 459-465. Suivez la manière dont une vue avec un offset impair est transmise à la recherche et ajoutez une couverture de régression pour les vues alignées et non alignées. C’est terminé lorsque les deux recherches UTF-16LE se terminent et renvoient 0.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
cpp, javascript, node.js
Domaine
backend
Type d'issue
Bug
Difficulté
3/5
Temps estimé
1-2 jours
Activité
Active
Clarté
Clairement spécifiée
Accessibilité débutants
68/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.