Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CSharp C#] Verifier fails for valid messages for resized buffers(even with disabled alignment check) #8533

Open
wonbinkanglg opened this issue Feb 19, 2025 · 2 comments

Comments

@wonbinkanglg
Copy link

Here is full minimally reproducible code:

Test.cs

        public static void Main(string[] args)
        {
            var b0 = TestFbs(1); // This fails both VerifyTableStart, TestTableVerify.Verify
            var b1 = TestFbs(512);  // both Succeeds
        }

        static private ByteBuffer TestFbs(int startingSize)
        {
            // Start parsing fbs.
            var fbsBuilder = new FlatBufferBuilder(startingSize);
            var childrenOffset = new List<Offset<TestTableChild>>();

            for (int i = 0; i < 4; i++)
            {
                TestTableChild.StartChildrenVector(fbsBuilder, 2);
                var off1 = TestStruct.CreateTestStruct(fbsBuilder, [1, 2]);
                var off2 = TestStruct.CreateTestStruct(fbsBuilder, [1, 2]);
                var structChildrenOffset = fbsBuilder.EndVector();
                var  testTableChildOffset = TestTableChild.CreateTestTableChild(fbsBuilder, 1, structChildrenOffset);
                childrenOffset.Add(testTableChildOffset);
            }
            var childrenVectorOffset = TestTable.CreateChildrenVector(fbsBuilder, childrenOffset.ToArray());
            var offset = TestTable.CreateTestTable(fbsBuilder, 1, childrenVectorOffset);
            fbsBuilder.Finish(offset.Value);

            Verifier verifier = new Verifier(fbsBuilder.DataBuffer, new Options {alignmentCheck = false  });
            var ok = verifier.VerifyTableStart((uint)offset.Value);
            var totalOk = TestTableVerify.Verify(verifier, (uint)offset.Value);
            return fbsBuilder.DataBuffer;
        }

Message.fbs

struct TestStruct{
  index:[uint32:2];
}
table TestTableChild{
  index:uint32;
  children:[TestStruct];
}

table TestTable{
  index:uint32;
  children:[TestTableChild];
}

I don't understand why resizing buffer causes different results. Funny thing is both buffers result in same Table data.

@legokangpalla
Copy link

Could somebody comment on this at least if I'm using verifier incorrectly?

@mustiikhalil
Copy link
Collaborator

mustiikhalil commented Feb 21, 2025

Hello,

It seems that the usage of the verifier is incorrect, there should be a function within the generated code that should verify the code for you. so in your case it should be something like Verify.TestTable and try using the SizedByteArray instead

public static bool VerifyMonster(ByteBuffer _bb) {
   Google.FlatBuffers.Verifier verifier = new Google.FlatBuffers.Verifier(_bb); 
   return verifier.VerifyBuffer("MONS", false, MonsterVerify.Verify);
 }

https://github.com/google/flatbuffers/blob/master/tests/MyGame/Example/Monster.cs#L51

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

No branches or pull requests

3 participants