fix: validate byte array length in RegisterFunctions converters (#38)#216
Open
JoshuaVlantis wants to merge 1 commit into
Open
Conversation
…bus#38) ReadIntHoldingRegisters, ReadUintHoldingRegisters and ReadFloatHoldingRegisters on ModbusMasterEnhanced crashed with a confusing "ArgumentOutOfRangeException: startIndex" thrown from BitConverter when the configured wordSize was smaller than the target numeric type (e.g. wordSize=16 for a 32-bit int read). Add an explicit length check to ByteValueArraysToInts, ByteValueArraysToUInts and ByteValueArraysToFloats so callers see an actionable ArgumentException naming the parameter, the offending element index, and the minimum wordSize required for that read. Also multi-target NModbus.UnitTests at net6.0 and net8.0 so the test suite can run on Linux .NET runtimes alongside the existing net4.6 target. Tests: * ReadIntHoldingRegisters_WordSize16_ThrowsClearArgumentException * ReadUintHoldingRegisters_WordSize16_ThrowsClearArgumentException * ReadFloatHoldingRegisters_WordSize16_ThrowsClearArgumentException * ReadShortHoldingRegisters_WordSize16_StillWorks * ReadIntHoldingRegisters_WordSize32_StillWorks * ByteValueArraysToInts_FrontPaddingWithSmallArray_ThrowsClearArgumentException
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.
Summary
Closes #38.
ModbusMasterEnhanced.ReadIntHoldingRegisters(and itsUint/Floatsiblings) crashed withArgumentOutOfRangeException: startIndexwhen the configuredwordSizewas smaller than the requested numeric type. The exception came fromBitConverter.ToInt32(e, e.Length - 4)withe.Length == 2, which gave the caller no idea why their request failed.This change adds an explicit
RequireMinimumByteLengthguard inRegisterFunctionssoByteValueArraysToInts,ByteValueArraysToUIntsandByteValueArraysToFloatsreject narrow input up front with anArgumentExceptionthat names the offending element and the minimumwordSizerequired for the read.It also multi-targets
NModbus.UnitTestsatnet6.0;net8.0(in addition to the existingnet4.6) so the suite can run on Linux .NET runtimes.Test plan
dotnet test NModbus.UnitTests/NModbus.UnitTests.csproj -f net8.0 -c Releasepasses (265/265after the fix, including 6 new tests for the validation behavior)dotnet build NModbus.IntegrationTests/NModbus.IntegrationTests.csproj -f net6.0 -c Releasestill succeeds