From de3a4e0a62b06fdc97465b85043f35f6d31168f1 Mon Sep 17 00:00:00 2001 From: Oleg Zhurakousky Date: Wed, 4 Jan 2017 12:09:34 -0500 Subject: [PATCH 1/2] NIFI-3278 Fixed ArrayIndexOutOfBoundsException in TextLineDemarcator --- .../nifi/stream/io/util/TextLineDemarcator.java | 7 +++++-- .../nifi/stream/io/util/TextLineDemarcatorTest.java | 12 ++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/nifi-commons/nifi-utils/src/main/java/org/apache/nifi/stream/io/util/TextLineDemarcator.java b/nifi-commons/nifi-utils/src/main/java/org/apache/nifi/stream/io/util/TextLineDemarcator.java index 124e6735bdb3..ab339da32f6f 100644 --- a/nifi-commons/nifi-utils/src/main/java/org/apache/nifi/stream/io/util/TextLineDemarcator.java +++ b/nifi-commons/nifi-utils/src/main/java/org/apache/nifi/stream/io/util/TextLineDemarcator.java @@ -159,8 +159,11 @@ private int isEol(byte currentByte, int currentIndex) { this.index = currentIndex + 1; this.fill(); } - currentByte = this.buffer[currentIndex + 1]; - crlfLength = currentByte == '\n' ? 2 : 1; + crlfLength = 1; + if (this.bufferLength != -1) { + currentByte = this.buffer[currentIndex + 1]; + crlfLength = currentByte == '\n' ? 2 : 1; + } } return crlfLength; } diff --git a/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/stream/io/util/TextLineDemarcatorTest.java b/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/stream/io/util/TextLineDemarcatorTest.java index ce66cad06aa3..c1d1e27ae3a2 100644 --- a/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/stream/io/util/TextLineDemarcatorTest.java +++ b/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/stream/io/util/TextLineDemarcatorTest.java @@ -51,6 +51,7 @@ public void emptyStreamNoStartWithFilter() { assertNull(demarcator.nextOffsetInfo()); } + @Test public void emptyStreamAndStartWithFilter() { String data = ""; @@ -59,6 +60,17 @@ public void emptyStreamAndStartWithFilter() { assertNull(demarcator.nextOffsetInfo("hello".getBytes())); } + // this test has no assertions. It's success criteria is validated by lack + // of failure (see NIFI-3278) + @Test + public void endsWithCRWithBufferLengthEqualStringLength() { + String str = "abcde\r"; + InputStream is = stringToIs(str); + TextLineDemarcator demarcator = new TextLineDemarcator(is, str.length()); + while (demarcator.nextOffsetInfo() != null) { + } + } + @Test public void singleCR() { InputStream is = stringToIs("\r"); From 57b6d352c6eba4259a4e3d578a99bcdb313b209e Mon Sep 17 00:00:00 2001 From: Oleg Zhurakousky Date: Wed, 4 Jan 2017 15:47:04 -0500 Subject: [PATCH 2/2] NIFI-3278 addressed PR comments --- .../nifi/stream/io/util/TextLineDemarcator.java | 2 +- .../nifi/stream/io/util/TextLineDemarcatorTest.java | 13 +++++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/nifi-commons/nifi-utils/src/main/java/org/apache/nifi/stream/io/util/TextLineDemarcator.java b/nifi-commons/nifi-utils/src/main/java/org/apache/nifi/stream/io/util/TextLineDemarcator.java index ab339da32f6f..7c918b4b8f3e 100644 --- a/nifi-commons/nifi-utils/src/main/java/org/apache/nifi/stream/io/util/TextLineDemarcator.java +++ b/nifi-commons/nifi-utils/src/main/java/org/apache/nifi/stream/io/util/TextLineDemarcator.java @@ -160,7 +160,7 @@ private int isEol(byte currentByte, int currentIndex) { this.fill(); } crlfLength = 1; - if (this.bufferLength != -1) { + if (currentIndex < this.buffer.length - 1) { currentByte = this.buffer[currentIndex + 1]; crlfLength = currentByte == '\n' ? 2 : 1; } diff --git a/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/stream/io/util/TextLineDemarcatorTest.java b/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/stream/io/util/TextLineDemarcatorTest.java index c1d1e27ae3a2..cd8b7c5a7f4e 100644 --- a/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/stream/io/util/TextLineDemarcatorTest.java +++ b/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/stream/io/util/TextLineDemarcatorTest.java @@ -63,8 +63,17 @@ public void emptyStreamAndStartWithFilter() { // this test has no assertions. It's success criteria is validated by lack // of failure (see NIFI-3278) @Test - public void endsWithCRWithBufferLengthEqualStringLength() { - String str = "abcde\r"; + public void endsWithCRWithBufferLengthEqualStringLengthA() { + String str = "\r"; + InputStream is = stringToIs(str); + TextLineDemarcator demarcator = new TextLineDemarcator(is, str.length()); + while (demarcator.nextOffsetInfo() != null) { + } + } + + @Test + public void endsWithCRWithBufferLengthEqualStringLengthB() { + String str = "abc\r"; InputStream is = stringToIs(str); TextLineDemarcator demarcator = new TextLineDemarcator(is, str.length()); while (demarcator.nextOffsetInfo() != null) {