Skip to content

fix(Class): align nested structs and arrays of structs to even bytes - #569

Open
haitTPC wants to merge 2 commits into
S7NetPlus:mainfrom
haitTPC:tpc
Open

haitTPC wants to merge 2 commits into
S7NetPlus:mainfrom
haitTPC:tpc

Conversation

@haitTPC

@haitTPC haitTPC commented Oct 24, 2026

Copy link
Copy Markdown

Problem

Nested UDT/STRUCT members and arrays of STRUCTs are serialized at the wrong offsets.

GetClassSize/FromBytes only align the outermost class; an inner UDT starts at the
current offset and its length is never padded, so a struct that starts or ends on an odd
byte shifts every following field. Arrays of structs have the same problem, and ToBytes
was never aligned at all, so writing a class used different offsets than reading it.

Related: #190, #413. The earlier fix (#191) only made the inner type be measured from the
current offset; it did not align it.

Reproduced on a real S7-1500 (TIA Portal, non-optimized DB). The DB has 10 elements of
UDT { Bool, Bool, Bool, UInt, Array[0..4] of Bool }, each occupying 6 bytes in the DB
(5 bytes of data + 1 byte of padding). With the current code the first element is written
correctly and every following element is shifted; e.g. element 2's Switch ends up in the
padding byte of element 1 and its Frequency is read from the address of element 2's alarm
area.

Change

  • GetIncreasedNumberOfBytes: IncrementToEven before an inner UDT, and measure it with
    isInnerProperty: false so its own length is padded to an even byte as well
  • GetClassSize / FromBytes: pad arrays to an even byte count after the loop
  • GetPropertyValue: align before and after a nested FromBytes
  • SetBytesFromProperty / ToBytes: apply the same alignment on the write path

Behavior change

Class.GetClassSize(new TestClassWithNestedClass()) is now 10 instead of 6, and its nested
members move to even offsets (DBB2 / DBB4 / DBX6 / DBW8). The DB4 image on the Snap7 test
server and the two expectations in S7NetTestsSync / S7NetTestsAsync are updated in the
second commit to match.

Testing

  • dotnet test S7.Net.UnitTest (net6.0, Windows, Snap7 test server): 224 passed, 0 failed
  • verified against a real S7-1500: 10 UDT elements in DB31, write then read back all 10
    elements, values and bit positions match TIA Portal's offsets

Nested types were only measured from the current offset (PR S7NetPlus#191); the
offset itself was never aligned and the nested type's length was never
padded, so a UDT starting or ending on an odd byte shifted every
following field (see S7NetPlus#190 / S7NetPlus#413). Only the read/size path was aligned,
which left ToBytes writing at the old offsets.

- GetIncreasedNumberOfBytes: IncrementToEven before a nested type and
  measure it with isInnerProperty: false so its length is padded
- GetClassSize / FromBytes array branches: IncrementToEven after the loop
- GetPropertyValue: IncrementToEven before and after nested FromBytes
- SetBytesFromProperty / ToBytes: same alignment on the write path

Behavior change: GetClassSize(TestClassWithNestedClass) 6 -> 10; the
DB4 fixtures/assertions in the integration tests reflect the old layout.
The nested members of TestClassWithNestedClass now sit on even offsets
(DBB2 / DBB4 / DBX6 / DBW8) and the class is 10 bytes instead of 6, so the
DB4 image on the Snap7 test server has to grow and the expected size has
to follow. Verified with the full unit test suite (224 passed).
@S7NetPlus S7NetPlus deleted a comment from ynioba Oct 24, 2026
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.

2 participants