Skip to content

Commit 67bd0ec

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 aabddfb commit 67bd0ec

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

‎lib/zlib.js‎

Lines 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+
constFLUSH_BOUND=[
242+
[Z_NO_FLUSH,Z_BLOCK],
243+
[BROTLI_OPERATION_PROCESS,BROTLI_OPERATION_EMIT_METADATA],
244+
];
245+
constFLUSH_BOUND_IDX_NORMAL=0;
246+
constFLUSH_BOUND_IDX_BROTLI=1;
247+
241248
// The base class for all Zlib-style streams.
242249
functionZlibBase(opts,mode,handle,{ flush, finishFlush, fullFlush }){
243250
letchunkSize=Z_DEFAULT_CHUNK;
@@ -247,6 +254,13 @@ function ZlibBase(opts, mode, handle,{flush, finishFlush, fullFlush }){
247254
assert(typeofmode==='number');
248255
assert(mode>=DEFLATE&&mode<=BROTLI_ENCODE);
249256

257+
letflushBoundIdx;
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',

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

Lines 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)