Adding checks to readHeader for invalid firstOffset and lastOffset values
* Adding checks to readHeader for invalid values in the header for firstOffset and lastOffset.
This will resolve an `ArrayIndexOutOfBoundsException` crash in Android 9 devices as IOExceptions are handled by `SegmentIntegration.createQueueFile`.
* Fixing formatting violations
MODIFY
analytics/src/main/java/com/segment/analytics/QueueFile.java
analytics/src/main/java/com/segment/analytics/QueueFile.java
@@ -154,16 +154,23 @@ public class QueueFile implements Closeable {
raf.seek(0);
raf.readFully(buffer);
fileLength = readInt(buffer, 0);
+ elementCount = readInt(buffer, 4);
+ int firstOffset = readInt(buffer, 8);
+ int lastOffset = readInt(buffer, 12);
+
if (fileLength > raf.length()) {
throw new IOException(
"File is truncated. Expected length: " + fileLength + ", Actual length: " + raf.length());
} else if (fileLength <= 0) {
throw new IOException(
"File is corrupt; length stored in header (" + fileLength + ") is invalid.");
+ } else if (firstOffset < 0 || fileLength <= wrapPosition(firstOffset)) {
+ throw new IOException(
+ "File is corrupt; first position stored in header (" + firstOffset + ") is invalid.");
+ } else if (lastOffset < 0 || fileLength <= wrapPosition(lastOffset)) {
+ throw new IOException(
+ "File is corrupt; last position stored in header (" + lastOffset + ") is invalid.");
}
- elementCount = readInt(buffer, 4);
- int firstOffset = readInt(buffer, 8);
- int lastOffset = readInt(buffer, 12);
first = readElement(firstOffset);
last = readElement(lastOffset);
}

MODIFY
analytics/src/test/java/com/segment/analytics/QueueFileTest.java
analytics/src/test/java/com/segment/analytics/QueueFileTest.java
@@ -186,6 +186,86 @@ public class QueueFileTest {
}
}
+ @Test
+ public void testInvalidFirstPositionThrows() throws IOException {
+ RandomAccessFile emptyFile = new RandomAccessFile(file, "rwd");
+ emptyFile.seek(0);
+ emptyFile.writeInt(4096);
+ emptyFile.setLength(4096);
+ emptyFile.seek(8);
+ emptyFile.writeInt(10000);
+ emptyFile.getChannel().force(true);
+ emptyFile.close();
+
+ try {
+ new QueueFile(file);
+ fail("Should have thrown about bad first position value");
+ } catch (IOException ex) {
+ assertThat(ex) //
+ .hasMessage("File is corrupt; first position stored in header (10000) is invalid.");
+ }
+ }
+
+ @Test
+ public void testNegativeFirstPositionThrows() throws IOException {
+ RandomAccessFile emptyFile = new RandomAccessFile(file, "rwd");
+ emptyFile.seek(0);
+ emptyFile.writeInt(4096);
+ emptyFile.setLength(4096);
+ emptyFile.seek(8);
+ emptyFile.writeInt(-2147483648);
+ emptyFile.getChannel().force(true);
+ emptyFile.close();
+
+ try {
+ new QueueFile(file);
+ fail("Should have thrown about bad first position value");
+ } catch (IOException ex) {
+ assertThat(ex) //
+ .hasMessage("File is corrupt; first position stored in header (-2147483648) is invalid.");
+ }
+ }
+
+ @Test
+ public void testInvalidLastPositionThrows() throws IOException {
+ RandomAccessFile emptyFile = new RandomAccessFile(file, "rwd");
+ emptyFile.seek(0);
+ emptyFile.writeInt(4096);
+ emptyFile.setLength(4096);
+ emptyFile.seek(12);
+ emptyFile.writeInt(10000);
+ emptyFile.getChannel().force(true);
+ emptyFile.close();
+
+ try {
+ new QueueFile(file);
+ fail("Should have thrown about bad last position value");
+ } catch (IOException ex) {
+ assertThat(ex) //
+ .hasMessage("File is corrupt; last position stored in header (10000) is invalid.");
+ }
+ }
+
+ @Test
+ public void testNegativeLastPositionThrows() throws IOException {
+ RandomAccessFile emptyFile = new RandomAccessFile(file, "rwd");
+ emptyFile.seek(0);
+ emptyFile.writeInt(4096);
+ emptyFile.setLength(4096);
+ emptyFile.seek(12);
+ emptyFile.writeInt(-2147483648);
+ emptyFile.getChannel().force(true);
+ emptyFile.close();
+
+ try {
+ new QueueFile(file);
+ fail("Should have thrown about bad last position value");
+ } catch (IOException ex) {
+ assertThat(ex) //
+ .hasMessage("File is corrupt; last position stored in header (-2147483648) is invalid.");
+ }
+ }
+
@Test
public void removeMultipleDoesNotCorrupt() throws IOException {
QueueFile queue = new QueueFile(file);

Adding checks to readHeader for invalid firstOffset and lastOffset values (#646)

* Adding checks to readHeader for invalid values in the header for firstOffset and lastOffset.

This will resolve an `ArrayIndexOutOfBoundsException` (#586) crash in Android 9 devices as IOExceptions are handled by `SegmentIntegration.createQueueFile`.

* Fixing formatting violations

Co-authored-by: Coby Plain <cplain@.atlassian.com>