Repository navigation
Bounds-check NetDataReader Peek methods - #618
Merged
Merged
Conversation
PeekByte, PeekString and PeekUnmanaged did not check against the data size, so peeking near the end returned bytes past the packet (or past the array via the unsafe read). PeekUnmanaged also referenced an undefined 'size' in the UNITY_ANDROID branch, which broke that build.
This was referenced Oct 26, 2026
Open
This was referenced Nov 7, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
After the NetDataReader rework, the
Get*methods callEnsureAvailable, but thePeek*ones do not:PeekUnmanaged<T>(soPeekInt,PeekLong, etc.) readssizeof(T)bytes withUnsafe.ReadUnaligned/ a raw pointer. On a 2-byte buffer,PeekInt()reads past the end of the array instead of throwing.PeekByte,PeekStringandPeekString(int)only check against the array length, not_dataSize. A reader over aNetDataWriterwhose backing array is larger than the data returns garbage. For example, a truncated string returns a run of null characters.UNITY_ANDROIDbranch,PeekUnmanagedusessize, which is never declared. That branch fails with CS0103. I checked this by compiling withUNITY_ANDROIDdefined and a stubUnsafeUtility.The fix adds
EnsureAvailabletoPeekByte,PeekUnmanaged(which also declaressize) and bothPeekStringoverloads. Peeking past the end now throwsInvalidOperationException, the same as the matchingGet*call.There are two new tests in
ReaderWriterSimpleDataTest. I only have the .NET 7 SDK, so I could not run the net8/net9 test project. I built the library for netstandard2.1 and ran the same assertions from a net7 console. Before the fix, 7 assertions fail; after it, all pass.