Skip to content

Commit

Permalink
Add lower bounds checks in memory segment util
Browse files Browse the repository at this point in the history
  • Loading branch information
Col-E committed Apr 27, 2024
1 parent 3e0d996 commit 6046eb9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>software.coley</groupId>
<artifactId>lljzip</artifactId>
<version>2.4.0</version>
<version>2.4.1</version>

<name>LL Java ZIP</name>
<description>Lower level ZIP support for Java</description>
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/software/coley/lljzip/util/MemorySegmentUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class MemorySegmentUtil {
* @return First index of pattern in content, or {@code -1} for no match.
*/
public static long indexOfWord(MemorySegment data, long offset, int pattern) {
if (offset < 0) return -1;
long len = data.byteSize() - 2;
for (long i = offset; i < len; i++) {
if (pattern == (data.get(LITTLE_SHORT, i) & 0xFFFF))
Expand All @@ -50,6 +51,7 @@ public static long indexOfWord(MemorySegment data, long offset, int pattern) {
* @return First index of pattern in content, or {@code -1} for no match.
*/
public static long indexOfQuad(MemorySegment data, long offset, int pattern) {
if (offset < 0) return -1;
long len = data.byteSize() - 4;
long i = offset;
while (i < len) {
Expand Down Expand Up @@ -84,7 +86,7 @@ public static long indexOfQuad(MemorySegment data, long offset, int pattern) {
*/
public static long lastIndexOfWord(MemorySegment data, long offset, int pattern) {
long limit;
if (data == null || (limit = data.byteSize()) < 2 || offset >= limit)
if (offset < 0 || data == null || (limit = data.byteSize()) < 2 || offset >= limit)
return -1;
for (long i = offset; i >= 0; i--) {
if (pattern == data.get(LITTLE_SHORT, i))
Expand All @@ -106,7 +108,7 @@ public static long lastIndexOfWord(MemorySegment data, long offset, int pattern)
*/
public static long lastIndexOfQuad(MemorySegment data, long offset, int pattern) {
long limit;
if (data == null || (limit = data.byteSize()) < 4 || offset >= limit)
if (offset < 0 || data == null || (limit = data.byteSize()) < 4 || offset >= limit)
return -1;
long i = offset;
while (i >= 0) {
Expand Down Expand Up @@ -152,9 +154,9 @@ public static long indexOf(MemorySegment data, int[] pattern) {
* @return First index of pattern in content, or {@code -1} for no match.
*/
public static long indexOf(MemorySegment data, long offset, int[] pattern) {
// Remaining data must be as long as pattern
// Remaining data must be as long as pattern and in bounds
long limit;
if (data == null || (limit = data.byteSize()) < pattern.length || offset >= limit)
if (offset < 0 || data == null || (limit = data.byteSize()) < pattern.length || offset >= limit)
return -1;

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

// Search from offset going backwards
Expand Down

0 comments on commit 6046eb9

Please sign in to comment.