From 9fdd3e1611e9915c6da14c099aabc91dd79d3acd Mon Sep 17 00:00:00 2001 From: Brian Pellin Date: Sat, 23 May 2020 20:39:49 -0500 Subject: [PATCH] Prevent crash on unexpected input length --- .../stream/LEDataInputStream.java | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/keepassdroid/stream/LEDataInputStream.java b/app/src/main/java/com/keepassdroid/stream/LEDataInputStream.java index 22c66c04..3b723364 100644 --- a/app/src/main/java/com/keepassdroid/stream/LEDataInputStream.java +++ b/app/src/main/java/com/keepassdroid/stream/LEDataInputStream.java @@ -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); } @@ -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); }