diff --git a/CHANGELOG.md b/CHANGELOG.md index c366dc6..44ba904 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All package updates & migration steps will be listed in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [4.3.1] - 2025-01-06 +### Fixed +- `ReadOnlyListContainer` now throws `IndexOutOfRangeException` when accessed out of range. + ## [4.3.0] - 2024-11-22 ### Added - `Warning` and `Error` severity levels for `ValidatorError` diff --git a/Runtime/Types/ReadOnlyListContainer.cs b/Runtime/Types/ReadOnlyListContainer.cs index 4fc8adf..895b9b3 100644 --- a/Runtime/Types/ReadOnlyListContainer.cs +++ b/Runtime/Types/ReadOnlyListContainer.cs @@ -16,7 +16,15 @@ public ReadOnlyListContainer(Func countFunc, Func getterFunc) _getterFunc = getterFunc; } - public T this[int index] => _getterFunc(index); + public T this[int index] + { + get + { + if (index < 0 || index >= Count) + throw new IndexOutOfRangeException(); + return _getterFunc(index); + } + } public int Count { diff --git a/Tests/Runtime/Types/ReadOnlyListContainerTest.cs b/Tests/Runtime/Types/ReadOnlyListContainerTest.cs index afefc41..716c4a3 100644 --- a/Tests/Runtime/Types/ReadOnlyListContainerTest.cs +++ b/Tests/Runtime/Types/ReadOnlyListContainerTest.cs @@ -41,6 +41,23 @@ public void ReadingValues() _countFunc.Received(1); } + [Test] + public void ReadingOutOfBounds() + { + int length = 2; + var container = new ReadOnlyListContainer(() => length, x => 0); + + // trying to access outside of the length throws an exception + Assert.Throws(() => { + _ = container[length]; + }); + + // trying to access outside of the length throws an exception + Assert.Throws(() => { + _ = container[-1]; + }); + } + [Test] public void Enumerator() { diff --git a/package.json b/package.json index 5672466..03f0080 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "com.pocketgems.scriptableobject.flatbuffer", - "version": "4.3.0", + "version": "4.3.1", "displayName": "Scriptable Object - FlatBuffer", "description": "Seamless syncing between Scriptable Objects and CSVs. Scriptable Object data built to Google FlatBuffers for optimal runtime loading & access.", "unity": "2021.3",