Skip to content

Commit

Permalink
Fix LumpFile error with invalid offet/size
Browse files Browse the repository at this point in the history
Fixes checks of valid offset/size and also try to gracefully handle these cases by defaulting to sensible values.
Motivated by #150 (comment)
  • Loading branch information
rihi committed Mar 31, 2024
1 parent 985655b commit c8afc5b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
4 changes: 2 additions & 2 deletions bspsrc-lib/src/main/java/info/ata4/bspsrc/lib/BspFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ private void loadLumps(ByteBuffer bb) {
}

Lump l = new Lump(i, ltype);
l.setBuffer(ByteBufferUtils.getSlice(bb, ofs, len));
l.setBuffer(bb.slice(ofs, len).order(bb.order()));
l.setOffset(ofs);
l.setParentFile(file);
l.setFourCC(fourCC);
Expand Down Expand Up @@ -600,7 +600,7 @@ && checkInvalidHeaders(in, false)
}

GameLump gl = new GameLump();
gl.setBuffer(ByteBufferUtils.getSlice(lump.getBuffer(), ofs, len));
gl.setBuffer(lump.getBuffer().slice(ofs, len).order(lump.getBuffer().order()));
gl.setOffset(ofs);
gl.setFourCC(fourCC);
gl.setFlags(flags);
Expand Down
17 changes: 9 additions & 8 deletions bspsrc-lib/src/main/java/info/ata4/bspsrc/lib/lump/LumpFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,21 +78,22 @@ public void load(Path file, ByteOrder bo) throws IOException {
L.trace("Lump size: {}", lumpSize);
L.trace("Map revision: {}", mapRev);

if (lumpOffset != HEADER_SIZE) {
throw new LumpException("Unexpected lump offset: " + lumpOffset);
}

if (lumpIndex < 0 || lumpIndex > BspFile.HEADER_LUMPS) {
throw new LumpException("Invalid lump ID: " + lumpIndex);
}

if (lumpSize < 0 || lumpOffset > bb.limit()) {
throw new LumpException("Invalid lump size: " + lumpOffset);
if (lumpOffset < 0 || lumpOffset > bb.limit()) {
L.warn("Invalid offset %d for lump %d, assuming %d".formatted(lumpOffset, lumpIndex, HEADER_SIZE));
lumpOffset = HEADER_SIZE;
}
if (lumpSize < 0 || lumpOffset + lumpSize > bb.limit()) {
int newLumpSize = bb.limit() - lumpOffset;
L.warn("Invalid size %d for lump %d, assuming %d".formatted(lumpSize, lumpIndex, newLumpSize));
lumpSize = newLumpSize;
}

// lump data
lump = new Lump(lumpIndex, LumpType.get(lumpIndex, bspVersion));
lump.setBuffer(ByteBufferUtils.getSlice(bb, lumpOffset, lumpSize));
lump.setBuffer(bb.slice(lumpOffset, lumpSize).order(bb.order()));
lump.setOffset(lumpOffset);
lump.setParentFile(file);
}
Expand Down

0 comments on commit c8afc5b

Please sign in to comment.