src: update IndexedStorageFile

This commit is contained in:
luk
2026-01-26 15:45:44 +00:00
parent be2834d0ea
commit fc28afcdb9

View File

@@ -277,16 +277,27 @@ public class IndexedStorageFile implements Closeable {
if (firstSegmentIndex == 0) { if (firstSegmentIndex == 0) {
compressedLength = 0; compressedLength = 0;
} else { } else {
ByteBuffer blobHeaderBuffer = this.readBlobHeader(firstSegmentIndex); try {
compressedLength = blobHeaderBuffer.getInt(COMPRESSED_LENGTH_OFFSET); ByteBuffer blobHeaderBuffer = this.readBlobHeader(firstSegmentIndex);
compressedLength = blobHeaderBuffer.getInt(COMPRESSED_LENGTH_OFFSET);
} catch (Exception e) {
// Corrupted blob header - skip this blob
compressedLength = 0;
}
} }
} finally { } finally {
this.indexLocks[blobIndex].unlockRead(segmentStamp); this.indexLocks[blobIndex].unlockRead(segmentStamp);
} }
if (compressedLength > 0) { // Validate to prevent OOM from corrupted compressedLength values
// Max reasonable compressed length ~256MB, prevents BitSet from allocating huge arrays
if (compressedLength > 0 && compressedLength < 256 * 1024 * 1024 && firstSegmentIndex > 0) {
int segmentsCount = this.requiredSegments(BLOB_HEADER_LENGTH + compressedLength); int segmentsCount = this.requiredSegments(BLOB_HEADER_LENGTH + compressedLength);
this.usedSegments.set(firstSegmentIndex, firstSegmentIndex + segmentsCount); int toIndex = firstSegmentIndex + segmentsCount;
// Ensure no overflow and reasonable range
if (segmentsCount > 0 && toIndex > firstSegmentIndex) {
this.usedSegments.set(firstSegmentIndex, toIndex);
}
} }
} }
} finally { } finally {