Skip to content

Commit

Permalink
zlib: fix brotli flush range
Browse files Browse the repository at this point in the history
Fixes: #38407

PR-URL: #38408
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Zijian Liu <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
  • Loading branch information
XadillaX authored and targos committed May 3, 2021
1 parent aabddfb commit 67bd0ec
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
21 changes: 18 additions & 3 deletions lib/zlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ const {
BROTLI_DECODE, BROTLI_ENCODE,
// Brotli operations (~flush levels)
BROTLI_OPERATION_PROCESS, BROTLI_OPERATION_FLUSH,
BROTLI_OPERATION_FINISH
BROTLI_OPERATION_FINISH, BROTLI_OPERATION_EMIT_METADATA,
} = constants;

// Translation table for return codes.
Expand Down Expand Up @@ -238,6 +238,13 @@ const checkRangesOrGetDefault = hideStackFrames(
}
);

const FLUSH_BOUND = [
[ Z_NO_FLUSH, Z_BLOCK ],
[ BROTLI_OPERATION_PROCESS, BROTLI_OPERATION_EMIT_METADATA ],
];
const FLUSH_BOUND_IDX_NORMAL = 0;
const FLUSH_BOUND_IDX_BROTLI = 1;

// The base class for all Zlib-style streams.
function ZlibBase(opts, mode, handle, { flush, finishFlush, fullFlush }) {
let chunkSize = Z_DEFAULT_CHUNK;
Expand All @@ -247,6 +254,13 @@ function ZlibBase(opts, mode, handle, { flush, finishFlush, fullFlush }) {
assert(typeof mode === 'number');
assert(mode >= DEFLATE && mode <= BROTLI_ENCODE);

let flushBoundIdx;
if (mode !== BROTLI_ENCODE && mode !== BROTLI_DECODE) {
flushBoundIdx = FLUSH_BOUND_IDX_NORMAL;
} else {
flushBoundIdx = FLUSH_BOUND_IDX_BROTLI;
}

if (opts) {
chunkSize = opts.chunkSize;
if (!checkFiniteNumber(chunkSize, 'options.chunkSize')) {
Expand All @@ -258,11 +272,12 @@ function ZlibBase(opts, mode, handle, { flush, finishFlush, fullFlush }) {

flush = checkRangesOrGetDefault(
opts.flush, 'options.flush',
Z_NO_FLUSH, Z_BLOCK, flush);
FLUSH_BOUND[flushBoundIdx][0], FLUSH_BOUND[flushBoundIdx][1], flush);

finishFlush = checkRangesOrGetDefault(
opts.finishFlush, 'options.finishFlush',
Z_NO_FLUSH, Z_BLOCK, finishFlush);
FLUSH_BOUND[flushBoundIdx][0], FLUSH_BOUND[flushBoundIdx][1],
finishFlush);

maxOutputLength = checkRangesOrGetDefault(
opts.maxOutputLength, 'options.maxOutputLength',
Expand Down
21 changes: 21 additions & 0 deletions test/parallel/test-zlib-brotli.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,24 @@ const sampleBuffer = fixtures.readSync('/pss-vectors.json');
message: 'Initialization failed'
});
}

{
// Test options.flush range
assert.throws(() => {
zlib.brotliCompressSync('', { flush: zlib.constants.Z_FINISH });
}, {
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError',
message: 'The value of "options.flush" is out of range. It must be >= 0 ' +
'and <= 3. Received 4',
});

assert.throws(() => {
zlib.brotliCompressSync('', { finishFlush: zlib.constants.Z_FINISH });
}, {
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError',
message: 'The value of "options.finishFlush" is out of range. It must be ' +
'>= 0 and <= 3. Received 4',
});
}

0 comments on commit 67bd0ec

Please sign in to comment.