zlib: Brotli reset() drops quality and dictionary

Ouverte
#66,156 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Évaluation

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

Piste de recherche

Start in src/node_zlib.cc at BrotliEncoderContext::ResetStream, BrotliDecoderContext::ResetStream, and Init(), then run the JavaScript reproduction from the issue. Verify that reset preserves Brotli quality parameters and dictionaries for compression and decompression, and add regression coverage showing the configured behavior remains after reset.

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

Description

Version

v27.0.0-pre (built from main at a2a051cd42; src/node_zlib.cc is unchanged on current main)

Platform
Linux x86_64 (Ubuntu 22.04, built with clang 20)
Subsystem

zlib

What steps will reproduce the bug?

Create a Brotli compressor with a custom quality or a dictionary, call .reset() before writing anything, then compress as usual.

The quality/dictionary you passed in is gone. Nothing throws.

import * as zlib from 'node:zlib';

const input = Buffer.from('the quick brown fox jumps over the lazy dog '.repeat(100));
const dict = Buffer.from('the quick brown fox jumps over the lazy dog '.repeat(40));

function compress(options, reset) {
  return new Promise((resolve, reject) => {
    const s = zlib.createBrotliCompress(options);
    const chunks = [];
    s.on('data', (d) => chunks.push(d));
    s.on('error', reject);
    if (reset) s.reset();
    s.end(input);
    s.on('end', () => resolve(Buffer.concat(chunks).length));
  });
}

const q0 = { params: { [zlib.constants.BROTLI_PARAM_QUALITY]: 0 } };
console.log('quality 0, no reset:', await compress(q0, false));  // 109
console.log('quality 0, then reset:', await compress(q0, true)); // 55  (same as default quality 11)
console.log('with dictionary, no reset:', await compress({ dictionary: dict }, false));  // 18
console.log('with dictionary, then reset:', await compress({ dictionary: dict }, true)); // 55 (same as no dictionary)

const compressed = zlib.brotliCompressSync(input, { dictionary: dict });
const dec = zlib.createBrotliDecompress({ dictionary: dict });
dec.reset();
dec.end(compressed);
dec.on('error', (err) => console.log('decompress after reset:', err.code));
// ERR__ERROR_FORMAT_DICTIONARY
How often does it reproduce? Is there a required condition?

Always. You do not need to write any data first. Just construct the stream with options and call reset().

What is the expected behavior? Why is that the expected behavior?

reset() should start a new Brotli session on the same stream, keeping the quality/params and dictionary you already set.

That is what the same API already documents for Zstd (zlib.reset(): "start a new session while preserving the configured parameters and dictionary"), and it is what test/parallel/test-zlib-zstd-reset.js asserts. Brotli goes through the same JS reset() method.

If the dictionary is dropped, a decoder cannot read a dictionary-compressed frame at all.

What do you see instead?
  • Quality 0 after reset() compresses as if you had used the default quality 11 (109 bytes become 55).
  • A dictionary after reset() compresses as if you had used no dictionary (18 bytes become 55).
  • createBrotliDecompress({ dictionary }) then reset() fails with ERR__ERROR_FORMAT_DICTIONARY on a frame that was compressed with that dictionary.

The compressor never reports an error. The settings just disappear.

Additional information

C++: BrotliEncoderContext::ResetStream / BrotliDecoderContext::ResetStream both do return Init(); with no arguments. Init() immediately does dictionary_.clear() and creates a brand-new Brotli instance. It never replays SetParams.

This is not #66087 / #66088. Those are about flush() then reset() leaving a broken frame. Here there is no write at all — only the configuration is thrown away.

LLDB on src/node_zlib.cc at a2a051cd42 (same ResetStream/Init as current main):

  1. Stopped in BrotliEncoderContext::ResetStream at line 1514. Watch: this->dictionary_ still has size 1760. Next statement is return Init(); with no dictionary argument.
ResetStream at line 1514: dictionary still size 1760, about to call empty Init()
  1. That call lands in Init at line 1474, on dictionary_.clear();. Watch: the member this->dictionary_ is still 1760 (clear has not run yet), but the dictionary parameter is already size 0, because ResetStream did not pass it in.
Init at line 1474: member dictionary still 1760, parameter dictionary already 0
  1. Continue to line 1483, if (!dictionary.empty()). Watch: both this->dictionary_ and the dictionary parameter are now size 0, so the attach-dictionary branch is skipped.
Init at line 1483: both dictionaries size 0, attach branch skipped
Langage dominant
JavaScript
Étoiles
122k
Forks
37.4k
Merge moyen
4 j 3 h
PR mergées (30 j)
272

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.

Autres issues de nodejs/node

Toutes les issues de nodejs/node

Issues similaires

Plus d'issues JavaScript

Recevez les nouvelles issues par e-mail

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