Skip to content

Undetected zlib error "invalid distance too far back" #14

@jan-kebernik

Description

@jan-kebernik

JZLib 1.1.3 currently fails to detect the zlib error "invalid distance too far back", which can lead to corrupt results when inflating, without throwing an exception. This specifically seems to happen with Inflaters created with a "nowrap == true" paramterer that also require a custom dictionary.

The bug seems to result from an ommited distance check in the method "inflate_fast" in the class "InfCodes.java". The pertinent code currently reads:

...
// copy all or what's left
if(q-r>0 && c>(q-r)){
do{
s.window[q++] = s.window[r++];
}
while(--c!=0);
}
else {
System.arraycopy(s.window, r, s.window, q, c);
q += c;
r +=c;
c=0;
}
break;
...

Changing this code to something like the following seems to fix the issue:

...
// copy all or what's left
if(q-r>0 && c>(q-r)){
do{
s.window[q++] = s.window[r++];
}
while(--c!=0);
}
else {
if (q - r < 0) {
mode = BADCODE;
z.msg = "invalid distance too far back";
return Z_DATA_ERROR;
}
System.arraycopy(s.window, r, s.window, q, c);
q += c;
r +=c;
c=0;
}
break;
...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions