From e7b0e884911e0a86cdb1e8f3227d89047bb3ea73 Mon Sep 17 00:00:00 2001 From: phenixxy Date: Mon, 20 Mar 2023 16:00:40 +0800 Subject: [PATCH] fix using null string in vector Use 0 offset as special value. 0 offset is not a valid relative offset, so it's safe to use 0 offset to indicate value is null. https://github.com/google/flatbuffers/issues/7846 --- net/FlatBuffers/FlatBufferBuilder.cs | 3 ++- net/FlatBuffers/Table.cs | 6 +++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/net/FlatBuffers/FlatBufferBuilder.cs b/net/FlatBuffers/FlatBufferBuilder.cs index e550f90f833..e08db847be8 100644 --- a/net/FlatBuffers/FlatBufferBuilder.cs +++ b/net/FlatBuffers/FlatBufferBuilder.cs @@ -438,7 +438,8 @@ public void AddOffset(int off) if (off > Offset) throw new ArgumentException(); - off = Offset - off + sizeof(int); + if (off != 0) + off = Offset - off + sizeof(int); PutInt(off); } diff --git a/net/FlatBuffers/Table.cs b/net/FlatBuffers/Table.cs index 21ef7dc8b82..2aaa86e99b3 100644 --- a/net/FlatBuffers/Table.cs +++ b/net/FlatBuffers/Table.cs @@ -65,7 +65,11 @@ public static int __indirect(int offset, ByteBuffer bb) // Create a .NET String from UTF-8 data stored inside the flatbuffer. public string __string(int offset) { - offset += bb.GetInt(offset); + int stringOffset = bb.GetInt(offset); + if (stringOffset == 0) + return null; + + offset += stringOffset; var len = bb.GetInt(offset); var startPos = offset + sizeof(int); return bb.GetStringUTF8(startPos, len);