Skip to content

Commit 1cb1c4b

Browse files
authoredMar 24, 2023
fix using null string in vector (#7872)
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. #7846
1 parent 50cdf92 commit 1cb1c4b

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed
 

‎net/FlatBuffers/FlatBufferBuilder.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,8 @@ public void AddOffset(int off)
438438
if (off > Offset)
439439
throw new ArgumentException();
440440

441-
off = Offset - off + sizeof(int);
441+
if (off != 0)
442+
off = Offset - off + sizeof(int);
442443
PutInt(off);
443444
}
444445

‎net/FlatBuffers/Table.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ public static int __indirect(int offset, ByteBuffer bb)
6565
// Create a .NET String from UTF-8 data stored inside the flatbuffer.
6666
public string __string(int offset)
6767
{
68-
offset += bb.GetInt(offset);
68+
int stringOffset = bb.GetInt(offset);
69+
if (stringOffset == 0)
70+
return null;
71+
72+
offset += stringOffset;
6973
var len = bb.GetInt(offset);
7074
var startPos = offset + sizeof(int);
7175
return bb.GetStringUTF8(startPos, len);

0 commit comments

Comments
 (0)