Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@KanouAo
Copy link

@KanouAo KanouAo commented Nov 6, 2025

Fix memory alignment issue: calculate correct byte length (element count × element size)
Just add a line : var byteLen = len * System.Runtime.InteropServices.Marshal.SizeOf();
#8759

@google-cla
Copy link

google-cla bot commented Nov 6, 2025

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@github-actions github-actions bot added the c# label Nov 6, 2025
@bjornharrtell
Copy link
Collaborator

@aardappel this looks sensible to me but I'm not entirely sure, can you take a look?

@aardappel
Copy link
Collaborator

Hmm, this seems a bug indeed, wonder why no-one has ever run into it?

Why not use https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/sizeof ?
The call you're making generally amounts to the same thing, but does type introspection for more complex types of T, would not trust it to be as cheap as the operator.

@aardappel
Copy link
Collaborator

Also on that link:

The value returned by sizeof can differ from the result of [Marshal.SizeOf(Object)](https://learn.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.marshal.sizeof#system-runtime-interopservices-marshal-sizeof(system-object)), which returns the size of the type in unmanaged memory.

Apparently unmanaged bool can become 4 bytes etc, not corresponding to FlatBuffers types anymore.

Fix memory alignment issue: calculate correct byte length (element count × element size)
@bjornharrtell
Copy link
Collaborator

@KanouAo can you make a regression test for this? And investigate if sizeof can/should be used instead?

@jtdavis777 jtdavis777 linked an issue Dec 3, 2025 that may be closed by this pull request
@jtdavis777 jtdavis777 added the waiting-for-update This PR is waiting for a change from the author or contributors before it is ready for merge label Dec 20, 2025
@stefan301
Copy link
Contributor

@aardappel

Hmm, this seems a bug indeed, wonder why no-one has ever run into it?

I think not many people have encountered this problem so far because the bug was only introduced on October 25, 2025, by commit 95053e6. This commit changed the meaning of the parameter len in ByteBuffer.ToArray<T> from 'number of Ts' to 'length in bytes', but did not update Table.__vector_as_array<T>, which still calls ToArray<T> with the number of Ts.

Since there were no tests for GetVectorOfLongsArray/GetVectorOfDoublesArray, all tests passed.

I created a PR 2036b0b that adds these tests to FlatBuffersExampleTests.cs and includes a slightly different solution for Table.__vector_as_array<T>. These tests pass before commit 95053e6 and fail with it and succeed again with commit 2036b0b.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c# waiting-for-update This PR is waiting for a change from the author or contributors before it is ready for merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

In C#. A bug where the array count must be a multiple of 4

5 participants