Skip to content

Commit

Permalink
Prevent crash on unexpected input length
Browse files Browse the repository at this point in the history
  • Loading branch information
bpellin committed May 24, 2020
1 parent 2cdcb31 commit 9fdd3e1
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion app/src/main/java/com/keepassdroid/stream/LEDataInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ public byte[] readBytes(int length) throws IOException {

public static int readUShort(InputStream is) throws IOException {
byte[] buf = readBytes(is, 2);

buf = padOut(buf, 2);

return readUShort(buf, 0);
}
Expand Down Expand Up @@ -165,10 +167,30 @@ public static long readUInt( byte buf[], int offset ) {

public static int readInt(InputStream is) throws IOException {
byte[] buf = readBytes(is, 4);


buf = padOut(buf, 4);

return readInt(buf, 0);
}

public static byte[] padOut(byte[] input, int length) {
if (input == null || input.length < length) {
byte[] output = new byte[4];

if (input == null) {
return output;
}

for (int i = 0; i < input.length; i++) {
output[i] = input[i];
}

return output;
}

return input;
}

public static long readUInt(InputStream is) throws IOException {
return (readInt(is) & INT_TO_LONG_MASK);
}
Expand Down

0 comments on commit 9fdd3e1

Please sign in to comment.