Skip to content

Commit ebbd6b4

Browse files
Add bounds checking and diagnostics to NIF block parser
Add index validation in GetBlock and count validation in ReadBlockRefList to diagnose Linux-specific NIF parsing failures. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent fc36d2f commit ebbd6b4

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

Maple2.File.IO/Nif/NifDocument.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ private int ReadHeaderString() {
3838
}
3939

4040
public NifBlock GetBlock(int index) {
41+
if (index < 0 || index >= Blocks.Length) {
42+
throw new IndexOutOfRangeException(
43+
$"Block index {index} out of range [0..{Blocks.Length}). " +
44+
$"Stream position: {Reader.BaseStream.Position}, " +
45+
$"Reading block: [{ReadingBlock?.BlockIndex}] {ReadingBlock?.BlockType} \"{ReadingBlock?.Name}\"");
46+
}
47+
4148
if (Blocks[index] is not null) {
4249
return Blocks[index];
4350
}
@@ -81,8 +88,16 @@ public T GetBlock<T>(int index) {
8188
public List<T> ReadBlockRefList<T>() {
8289
List<T> blocks = new List<T>();
8390

91+
long countPos = Reader.BaseStream.Position;
8492
int count = Reader.ReadAdjustedInt32();
8593

94+
if (count < 0 || count > Blocks.Length) {
95+
throw new InvalidDataException(
96+
$"Unreasonable block ref list count {count} at stream position {countPos}. " +
97+
$"Total blocks: {Blocks.Length}. " +
98+
$"Reading block: [{ReadingBlock?.BlockIndex}] {ReadingBlock?.BlockType} \"{ReadingBlock?.Name}\"");
99+
}
100+
86101
#if NETSTANDARD2_1
87102
blocks.EnsureCapacityCompat(count);
88103
#else

0 commit comments

Comments
 (0)