Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: end parsing if EOF is hit #3223

Merged
merged 1 commit into from
Jul 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import brut.androlib.res.data.axml.NamespaceStack;
import brut.androlib.res.xml.ResXmlEncoders;
import brut.util.ExtDataInput;
import org.apache.commons.io.input.CountingInputStream;
import com.google.common.io.LittleEndianDataInputStream;
import org.xmlpull.v1.XmlPullParserException;
import java.io.DataInput;
Expand Down Expand Up @@ -65,6 +66,7 @@ public void setAttrDecoder(ResAttrDecoder attrDecoder) {
public void open(InputStream stream) {
close();
if (stream != null) {
stream = mCountIn = new CountingInputStream(stream);
// We need to explicitly cast to DataInput as otherwise the constructor is ambiguous.
// We choose DataInput instead of InputStream as ExtDataInput wraps an InputStream in
// a DataInputStream which is big-endian and ignores the little-endian behavior.
Expand All @@ -79,6 +81,7 @@ public void close() {
}
isOperational = false;
mIn = null;
mCountIn = null;
mStringBlock = null;
mResourceIds = null;
mNamespaces.reset();
Expand Down Expand Up @@ -677,6 +680,13 @@ private void doNext() throws IOException {
break;
}

// #2070 - Some applications have 2 start namespaces, but only 1 end namespace.
if (mCountIn.available() == 0) {
LOGGER.warning(String.format("AXML hit unexpected end of file at byte: 0x%X", mCountIn.getCount()));
mEvent = END_DOCUMENT;
break;
}

int chunkType;
int headerSize = 0;
if (event == START_DOCUMENT) {
Expand Down Expand Up @@ -788,6 +798,7 @@ private void setFirstError(AndrolibException error) {
}

private ExtDataInput mIn;
private CountingInputStream mCountIn;
private ResAttrDecoder mAttrDecoder;
private AndrolibException mFirstError;

Expand Down