Skip to content

Commit

Permalink
Did not notice checkWindowBits modified incoming wbits value
Browse files Browse the repository at this point in the history
  • Loading branch information
enebo committed Nov 21, 2024
1 parent ad16294 commit 73aeb8e
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 12 deletions.
9 changes: 3 additions & 6 deletions core/src/main/java/org/jruby/ext/zlib/JZlibDeflate.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,10 @@ public IRubyObject _initialize(IRubyObject[] args) {
@JRubyMethod(name = "initialize", optional = 4, checkArity = false, visibility = PRIVATE)
public IRubyObject _initialize(ThreadContext context, IRubyObject[] args) {
args = Arity.scanArgs(context, args, 0, 4);
level = -1;
windowBits = JZlib.MAX_WBITS;
strategy = 0;
if (!args[0].isNil()) level = checkLevel(context, RubyNumeric.fix2int(args[0]));
if (!args[1].isNil()) windowBits = checkWindowBits(context, RubyNumeric.fix2int(args[1]), false);
level = !args[0].isNil() ? checkLevel(context, RubyNumeric.fix2int(args[0])) : -1;
windowBits = !args[1].isNil() ? checkWindowBits(context, RubyNumeric.fix2int(args[1]), false) : JZlib.MAX_WBITS;
int memlevel = !args[2].isNil() ? RubyNumeric.fix2int(args[2]) : 8; // ignored. Memory setting means nothing on Java.
if (!args[3].isNil()) strategy = RubyNumeric.fix2int(args[3]);
strategy = !args[3].isNil() ? RubyNumeric.fix2int(args[3]) : 0;

init(context, level, windowBits, memlevel, strategy);
return this;
Expand Down
5 changes: 2 additions & 3 deletions core/src/main/java/org/jruby/ext/zlib/JZlibInflate.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,8 @@ public IRubyObject _initialize(IRubyObject[] args) {
public IRubyObject _initialize(ThreadContext context, IRubyObject[] args) {
int argc = Arity.checkArgumentCount(context, args, 0, 1);

windowBits = JZlib.DEF_WBITS;

if (argc > 0 && !args[0].isNil()) windowBits = checkWindowBits(context, RubyNumeric.fix2int(args[0]), true);
windowBits = argc > 0 && !args[0].isNil() ?
checkWindowBits(context, RubyNumeric.fix2int(args[0]), true) : JZlib.DEF_WBITS;

init(windowBits);
return this;
Expand Down
6 changes: 3 additions & 3 deletions core/src/main/java/org/jruby/ext/zlib/ZStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,8 @@ static int checkLevel(ThreadContext context, int level) {
* decompression) and MAX_WBITS + 32(automatic detection of gzip and LZ77).
*/
// TODO: remove when JZlib checks the given windowBits
static int checkWindowBits(ThreadContext context, int wbits, boolean forInflate) {
wbits = Math.abs(wbits);
static int checkWindowBits(ThreadContext context, int value, boolean forInflate) {
int wbits = Math.abs(value);
if ((wbits & 0xf) < 8) {
throw RubyZlib.newStreamError(context.runtime, "stream error: invalid window bits");
}
Expand All @@ -221,7 +221,7 @@ static int checkWindowBits(ThreadContext context, int wbits, boolean forInflate)
throw RubyZlib.newStreamError(context.runtime, "stream error: invalid window bits");
}

return wbits;
return value;
}

// TODO: remove when JZlib checks the given strategy
Expand Down

0 comments on commit 73aeb8e

Please sign in to comment.