Skip to content

Commit ba5ece7

Browse files
XadillaXtargos
authored andcommitted
zlib: fix brotli flush range
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]>
1 parent 707c36a commit ba5ece7

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

lib/zlib.js

+18-3
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ const {
8686
BROTLI_DECODE, BROTLI_ENCODE,
8787
// Brotli operations (~flush levels)
8888
BROTLI_OPERATION_PROCESS, BROTLI_OPERATION_FLUSH,
89-
BROTLI_OPERATION_FINISH
89+
BROTLI_OPERATION_FINISH, BROTLI_OPERATION_EMIT_METADATA,
9090
} = constants;
9191

9292
// Translation table for return codes.
@@ -236,6 +236,13 @@ const checkRangesOrGetDefault = hideStackFrames(
236236
}
237237
);
238238

239+
const FLUSH_BOUND = [
240+
[ Z_NO_FLUSH, Z_BLOCK ],
241+
[ BROTLI_OPERATION_PROCESS, BROTLI_OPERATION_EMIT_METADATA ],
242+
];
243+
const FLUSH_BOUND_IDX_NORMAL = 0;
244+
const FLUSH_BOUND_IDX_BROTLI = 1;
245+
239246
// The base class for all Zlib-style streams.
240247
function ZlibBase(opts, mode, handle, { flush, finishFlush, fullFlush }) {
241248
let chunkSize = Z_DEFAULT_CHUNK;
@@ -245,6 +252,13 @@ function ZlibBase(opts, mode, handle, { flush, finishFlush, fullFlush }) {
245252
assert(typeof mode === 'number');
246253
assert(mode >= DEFLATE && mode <= BROTLI_ENCODE);
247254

255+
let flushBoundIdx;
256+
if (mode !== BROTLI_ENCODE && mode !== BROTLI_DECODE) {
257+
flushBoundIdx = FLUSH_BOUND_IDX_NORMAL;
258+
} else {
259+
flushBoundIdx = FLUSH_BOUND_IDX_BROTLI;
260+
}
261+
248262
if (opts) {
249263
chunkSize = opts.chunkSize;
250264
if (!checkFiniteNumber(chunkSize, 'options.chunkSize')) {
@@ -256,11 +270,12 @@ function ZlibBase(opts, mode, handle, { flush, finishFlush, fullFlush }) {
256270

257271
flush = checkRangesOrGetDefault(
258272
opts.flush, 'options.flush',
259-
Z_NO_FLUSH, Z_BLOCK, flush);
273+
FLUSH_BOUND[flushBoundIdx][0], FLUSH_BOUND[flushBoundIdx][1], flush);
260274

261275
finishFlush = checkRangesOrGetDefault(
262276
opts.finishFlush, 'options.finishFlush',
263-
Z_NO_FLUSH, Z_BLOCK, finishFlush);
277+
FLUSH_BOUND[flushBoundIdx][0], FLUSH_BOUND[flushBoundIdx][1],
278+
finishFlush);
264279

265280
maxOutputLength = checkRangesOrGetDefault(
266281
opts.maxOutputLength, 'options.maxOutputLength',

test/parallel/test-zlib-brotli.js

+21
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,24 @@ const sampleBuffer = fixtures.readSync('/pss-vectors.json');
7171
message: 'Initialization failed'
7272
});
7373
}
74+
75+
{
76+
// Test options.flush range
77+
assert.throws(() => {
78+
zlib.brotliCompressSync('', { flush: zlib.constants.Z_FINISH });
79+
}, {
80+
code: 'ERR_OUT_OF_RANGE',
81+
name: 'RangeError',
82+
message: 'The value of "options.flush" is out of range. It must be >= 0 ' +
83+
'and <= 3. Received 4',
84+
});
85+
86+
assert.throws(() => {
87+
zlib.brotliCompressSync('', { finishFlush: zlib.constants.Z_FINISH });
88+
}, {
89+
code: 'ERR_OUT_OF_RANGE',
90+
name: 'RangeError',
91+
message: 'The value of "options.finishFlush" is out of range. It must be ' +
92+
'>= 0 and <= 3. Received 4',
93+
});
94+
}

0 commit comments

Comments
 (0)