Skip to content

Commit 0edab75

Browse files
Fix DataReader.findNewLine with lone EOL character (helidon-io#9325)
Add a missing check to keep the internal toIndex within bounds. Fixes helidon-io#9287
1 parent 3750d38 commit 0edab75

2 files changed

Lines changed: 47 additions & 0 deletions

File tree

common/buffers/src/main/java/io/helidon/common/buffers/DataReader.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,9 @@ public int findNewLine(int max) throws IncorrectNewLineException {
445445
}
446446
indexWithinNode = crIndex + 1;
447447
idx += indexWithinNode;
448+
if (idx >= max) {
449+
return max;
450+
}
448451
continue;
449452
}
450453
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright (c) 2024 Oracle and/or its affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.helidon.common.buffers;
18+
19+
import java.util.concurrent.atomic.AtomicReference;
20+
21+
import org.junit.jupiter.api.Test;
22+
23+
import static org.hamcrest.MatcherAssert.assertThat;
24+
import static org.hamcrest.Matchers.is;
25+
26+
class DataReaderTest {
27+
28+
@Test
29+
void testFindNewLineWithLoneCR() {
30+
// reading N bytes at a time until a new line is found
31+
// with data containing a lone CR
32+
33+
byte[] data = new byte[] {0, 0, (byte) '\r', 0, (byte) '\r', (byte) '\n'};
34+
AtomicReference<byte[]> ref = new AtomicReference<>(data);
35+
DataReader dataReader = new DataReader(() -> ref.getAndSet(null), true);
36+
37+
int n = 2;
38+
assertThat(dataReader.findNewLine(n), is(n));
39+
dataReader.skip(n);
40+
assertThat(dataReader.findNewLine(n), is(n));
41+
dataReader.skip(n);
42+
assertThat(dataReader.findNewLine(n), is(0));
43+
}
44+
}

0 commit comments

Comments
 (0)