Skip to content

Commit 6046eb9

Browse files
committed
Add lower bounds checks in memory segment util
1 parent 3e0d996 commit 6046eb9

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>software.coley</groupId>
88
<artifactId>lljzip</artifactId>
9-
<version>2.4.0</version>
9+
<version>2.4.1</version>
1010

1111
<name>LL Java ZIP</name>
1212
<description>Lower level ZIP support for Java</description>

src/main/java/software/coley/lljzip/util/MemorySegmentUtil.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public class MemorySegmentUtil {
3131
* @return First index of pattern in content, or {@code -1} for no match.
3232
*/
3333
public static long indexOfWord(MemorySegment data, long offset, int pattern) {
34+
if (offset < 0) return -1;
3435
long len = data.byteSize() - 2;
3536
for (long i = offset; i < len; i++) {
3637
if (pattern == (data.get(LITTLE_SHORT, i) & 0xFFFF))
@@ -50,6 +51,7 @@ public static long indexOfWord(MemorySegment data, long offset, int pattern) {
5051
* @return First index of pattern in content, or {@code -1} for no match.
5152
*/
5253
public static long indexOfQuad(MemorySegment data, long offset, int pattern) {
54+
if (offset < 0) return -1;
5355
long len = data.byteSize() - 4;
5456
long i = offset;
5557
while (i < len) {
@@ -84,7 +86,7 @@ public static long indexOfQuad(MemorySegment data, long offset, int pattern) {
8486
*/
8587
public static long lastIndexOfWord(MemorySegment data, long offset, int pattern) {
8688
long limit;
87-
if (data == null || (limit = data.byteSize()) < 2 || offset >= limit)
89+
if (offset < 0 || data == null || (limit = data.byteSize()) < 2 || offset >= limit)
8890
return -1;
8991
for (long i = offset; i >= 0; i--) {
9092
if (pattern == data.get(LITTLE_SHORT, i))
@@ -106,7 +108,7 @@ public static long lastIndexOfWord(MemorySegment data, long offset, int pattern)
106108
*/
107109
public static long lastIndexOfQuad(MemorySegment data, long offset, int pattern) {
108110
long limit;
109-
if (data == null || (limit = data.byteSize()) < 4 || offset >= limit)
111+
if (offset < 0 || data == null || (limit = data.byteSize()) < 4 || offset >= limit)
110112
return -1;
111113
long i = offset;
112114
while (i >= 0) {
@@ -152,9 +154,9 @@ public static long indexOf(MemorySegment data, int[] pattern) {
152154
* @return First index of pattern in content, or {@code -1} for no match.
153155
*/
154156
public static long indexOf(MemorySegment data, long offset, int[] pattern) {
155-
// Remaining data must be as long as pattern
157+
// Remaining data must be as long as pattern and in bounds
156158
long limit;
157-
if (data == null || (limit = data.byteSize()) < pattern.length || offset >= limit)
159+
if (offset < 0 || data == null || (limit = data.byteSize()) < pattern.length || offset >= limit)
158160
return -1;
159161

160162
// Search from offset going forwards
@@ -189,8 +191,8 @@ public static long lastIndexOf(MemorySegment data, int[] pattern) {
189191
* @return Last index of pattern in content, or {@code -1} for no match.
190192
*/
191193
public static long lastIndexOf(MemorySegment data, long offset, int[] pattern) {
192-
// Remaining data must be as long as pattern
193-
if (data == null || data.byteSize() < pattern.length)
194+
// Remaining data must be as long as pattern and in bounds
195+
if (offset < 0 || data == null || data.byteSize() < pattern.length)
194196
return -1;
195197

196198
// Search from offset going backwards

0 commit comments

Comments
 (0)