[C++, JSON] Fix nullptr access when reading a key with a default value. (#6375)

This commit fixes handling of default and NULL `key` fields in `Parser::ParseVector` (#5928).

The JSON generator updated. It outputs `key` fields even if the `--force-defaults` option is inactive.

Additional test cases for `key` added.
This commit is contained in:
Vladimir Glavnyy
2021-01-08 02:24:59 +07:00
committed by GitHub
parent 4363c1d2cb
commit 83ce29cc22
28 changed files with 1243 additions and 579 deletions

View File

@@ -50,6 +50,31 @@ public struct Stat : IFlatbufferObject
int o = builder.EndTable();
return new Offset<MyGame.Example.Stat>(o);
}
public static VectorOffset CreateSortedVectorOfStat(FlatBufferBuilder builder, Offset<Stat>[] offsets) {
Array.Sort(offsets, (Offset<Stat> o1, Offset<Stat> o2) => builder.DataBuffer.GetUshort(Table.__offset(8, o1.Value, builder.DataBuffer)).CompareTo(builder.DataBuffer.GetUshort(Table.__offset(8, o2.Value, builder.DataBuffer))));
return builder.CreateVectorOfTables(offsets);
}
public static Stat? __lookup_by_key(int vectorLocation, ushort key, ByteBuffer bb) {
int span = bb.GetInt(vectorLocation - 4);
int start = 0;
while (span != 0) {
int middle = span / 2;
int tableOffset = Table.__indirect(vectorLocation + 4 * (start + middle), bb);
int comp = bb.GetUshort(Table.__offset(8, bb.Length - tableOffset, bb)).CompareTo(key);
if (comp > 0) {
span = middle;
} else if (comp < 0) {
middle++;
start += middle;
span -= middle;
} else {
return new Stat().__assign(tableOffset, bb);
}
}
return null;
}
public StatT UnPack() {
var _o = new StatT();
this.UnPackTo(_o);