nodejs / nodejs/node

`TextEncoder.encodeInto()` underfills the destination for some non-ASCII text

Aperta Adatta ai principianti
#65,994 2 commenti 2 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Lingua principale
JavaScript
Stelle
122k
Fork
37.3k
Merge medio
4g 2h
PR unite (30g)
283

Descrizione

There are two problems I found with TextEncoder.encodeInto(), both of which can cause encoding to stall even when the next char can fit the destination.

  1. A 2-byte char requires a 3-byte destination

    const encoder = new TextEncoder();
    const text = '\u0400'.repeat(33);
    
    console.log(encoder.encodeInto(text, new Uint8Array(2)));  // { read: 0, written: 0 }
    console.log(encoder.encodeInto(text, new Uint8Array(3)));  // { read: 1, written: 2 }
    

    The second call proves that '\u0400' should fit into a 2-byte array.

  2. Appending an unread character changes encoding progress

    const encoder = new TextEncoder();
    const text = 'é'.repeat(33);
    
    console.log(encoder.encodeInto(text, new Uint8Array(2)));  // { read: 0, written: 0 }
    console.log(encoder.encodeInto(text + '☺', new Uint8Array(2)));  // { read: 1, written: 2 }
    

    Appending should not change whether preceding chars can be read into the buffer, but there it is.

The bugs were introduced by the encodeInto() performance change in Node.js v25.4.0. The examples above use length 33 strings to exercise that optimized path(kSmallStringThreshold = 32). Unfortunately the current encodeInto.any.js WPT tests fail to expose the problems because:

  1. all input cases use 7 or fewer code units
  2. even then, the cases don't use chars between U+0400 and U+07FF, and
  3. their cases don't contain a narrow dst capacity to reveal the signed-byte problem.

Proposed fixes

src/encoding_binding.cc

  1. Incorrect cutoff in simpleUtfEncodingLength()

    -  if (c < 0x400) return 2;
    +  if (c < 0x800) return 2;
    

    (very likely a typo, given the comment immediately below it says "Code points < 0x800: 2 bytes")

  2. Signed-byte handling in findBestFit()

    -    size_t extra = simpleUtfEncodingLength(data[pos]);
    +    size_t extra = simpleUtfEncodingLength(UTF16 ? data[pos] : static_cast<uint8_t>(data[pos]));
    

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia in src/encoding_binding.cc, soprattutto con simpleUtfEncodingLength() e findBestFit(), poi esamina la copertura WPT in encodeInto.any.js. Riproduci gli esempi di 33 caratteri e aggiungi la copertura per U+0400–U+07FF e per le destinazioni strette; il lavoro è completato quando encodeInto riporta i valori attesi di lettura/scrittura e i test WPT pertinenti passano.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
cpp, javascript, nodejs
Ambito
api, backend
Tipo di issue
Bug
Difficoltà
2/5
Tempo stimato
1-3 ore
Stato di attività
Attiva
Chiarezza
Specificata chiaramente
Idoneità per principianti
84/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.