Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 67bd0ec

Browse filesBrowse files
XadillaXtargos
authored andcommitted
zlib: fix brotli flush range
Fixes: #38407 PR-URL: #38408 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Zijian Liu <lxxyxzj@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
1 parent aabddfb commit 67bd0ec
Copy full SHA for 67bd0ec

File tree

Expand file treeCollapse file tree

2 files changed

+39
-3
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+39
-3
lines changed
Open diff view settings
Collapse file

‎lib/zlib.js‎

Copy file name to clipboardExpand all lines: lib/zlib.js
+18-3Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ const {
8989
BROTLI_DECODE, BROTLI_ENCODE,
9090
// Brotli operations (~flush levels)
9191
BROTLI_OPERATION_PROCESS, BROTLI_OPERATION_FLUSH,
92-
BROTLI_OPERATION_FINISH
92+
BROTLI_OPERATION_FINISH, BROTLI_OPERATION_EMIT_METADATA,
9393
} = constants;
9494

9595
// Translation table for return codes.
@@ -238,6 +238,13 @@ const checkRangesOrGetDefault = hideStackFrames(
238238
}
239239
);
240240

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

257+
let flushBoundIdx;
258+
if (mode !== BROTLI_ENCODE && mode !== BROTLI_DECODE) {
259+
flushBoundIdx = FLUSH_BOUND_IDX_NORMAL;
260+
} else {
261+
flushBoundIdx = FLUSH_BOUND_IDX_BROTLI;
262+
}
263+
250264
if (opts) {
251265
chunkSize = opts.chunkSize;
252266
if (!checkFiniteNumber(chunkSize, 'options.chunkSize')) {
@@ -258,11 +272,12 @@ function ZlibBase(opts, mode, handle, { flush, finishFlush, fullFlush }) {
258272

259273
flush = checkRangesOrGetDefault(
260274
opts.flush, 'options.flush',
261-
Z_NO_FLUSH, Z_BLOCK, flush);
275+
FLUSH_BOUND[flushBoundIdx][0], FLUSH_BOUND[flushBoundIdx][1], flush);
262276

263277
finishFlush = checkRangesOrGetDefault(
264278
opts.finishFlush, 'options.finishFlush',
265-
Z_NO_FLUSH, Z_BLOCK, finishFlush);
279+
FLUSH_BOUND[flushBoundIdx][0], FLUSH_BOUND[flushBoundIdx][1],
280+
finishFlush);
266281

267282
maxOutputLength = checkRangesOrGetDefault(
268283
opts.maxOutputLength, 'options.maxOutputLength',
Collapse file

‎test/parallel/test-zlib-brotli.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-zlib-brotli.js
+21Lines changed: 21 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.