Skip to content

fix: validate byte array length in RegisterFunctions converters (#38)#216

Open
JoshuaVlantis wants to merge 1 commit into
NModbus:masterfrom
JoshuaVlantis:fix/issue-38-readintholdingregisters-wordsize
Open

fix: validate byte array length in RegisterFunctions converters (#38)#216
JoshuaVlantis wants to merge 1 commit into
NModbus:masterfrom
JoshuaVlantis:fix/issue-38-readintholdingregisters-wordsize

Conversation

@JoshuaVlantis

Copy link
Copy Markdown

Summary

Closes #38.

ModbusMasterEnhanced.ReadIntHoldingRegisters (and its Uint / Float siblings) crashed with ArgumentOutOfRangeException: startIndex when the configured wordSize was smaller than the requested numeric type. The exception came from BitConverter.ToInt32(e, e.Length - 4) with e.Length == 2, which gave the caller no idea why their request failed.

This change adds an explicit RequireMinimumByteLength guard in RegisterFunctions so ByteValueArraysToInts, ByteValueArraysToUInts and ByteValueArraysToFloats reject narrow input up front with an ArgumentException that names the offending element and the minimum wordSize required for the read.

It also multi-targets NModbus.UnitTests at net6.0;net8.0 (in addition to the existing net4.6) so the suite can run on Linux .NET runtimes.

Test plan

  • dotnet test NModbus.UnitTests/NModbus.UnitTests.csproj -f net8.0 -c Release passes (265/265 after the fix, including 6 new tests for the validation behavior)
  • Failing-first run with the new tests confirmed the original crash before the fix
  • dotnet build NModbus.IntegrationTests/NModbus.IntegrationTests.csproj -f net6.0 -c Release still succeeds

…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Extensions ReadIntHoldingRegisters crashes when using it on 16 bit devices

1 participant