Skip to content

Commit

Permalink
[C#] Fix truncated ArraySegment<byte> if elementSize != 1 (#6462)
Browse files Browse the repository at this point in the history
* WIP: Fix returned truncated ArraySegment<byte> if elementSize is not byte

* Fix

* Regenerated test code
  • Loading branch information
bjornharrtell authored Feb 23, 2021
1 parent ffc2ef7 commit cbbbaa6
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 19 deletions.
4 changes: 2 additions & 2 deletions net/FlatBuffers/Table.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public Span<T> __vector_as_span<T>(int offset, int elementSize) where T : struct
// Get the data of a vector whoses offset is stored at "offset" in this object as an
// ArraySegment&lt;byte&gt;. If the vector is not present in the ByteBuffer,
// then a null value will be returned.
public ArraySegment<byte>? __vector_as_arraysegment(int offset)
public ArraySegment<byte>? __vector_as_arraysegment(int offset, int elementSize)
{
var o = this.__offset(offset);
if (0 == o)
Expand All @@ -122,7 +122,7 @@ public Span<T> __vector_as_span<T>(int offset, int elementSize) where T : struct

var pos = this.__vector(o);
var len = this.__vector_len(o);
return bb.ToArraySegment(pos, len);
return bb.ToArraySegment(pos, len * elementSize);
}
#endif

Expand Down
2 changes: 2 additions & 0 deletions src/idl_gen_csharp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,8 @@ class CSharpGenerator : public BaseGenerator {
code += "Bytes() { return ";
code += "__p.__vector_as_arraysegment(";
code += NumToString(field.value.offset);
code +=
", " + NumToString(SizeOf(field.value.type.VectorType().base_type));
code += "); }\n";
code += "#endif\n";

Expand Down
3 changes: 3 additions & 0 deletions tests/FlatBuffers.Test/FlatBuffersExampleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,9 @@ private void TestBuffer(ByteBuffer bb)
{
Assert.IsTrue(monster.GetTestarrayofboolsBytes().HasValue);
}

Assert.AreEqual(monster.VectorOfDoublesLength * 8, monster.GetVectorOfDoublesBytes().Value.Count);

#endif
}

Expand Down
24 changes: 12 additions & 12 deletions tests/MyGame/Example/Monster.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ public struct Monster : IFlatbufferObject
#if ENABLE_SPAN_T
public Span<byte> GetNameBytes() { return __p.__vector_as_span<byte>(10, 1); }
#else
public ArraySegment<byte>? GetNameBytes() { return __p.__vector_as_arraysegment(10); }
public ArraySegment<byte>? GetNameBytes() { return __p.__vector_as_arraysegment(10, 1); }
#endif
public byte[] GetNameArray() { return __p.__vector_as_array<byte>(10); }
public byte Inventory(int j) { int o = __p.__offset(14); return o != 0 ? __p.bb.Get(__p.__vector(o) + j * 1) : (byte)0; }
public int InventoryLength { get { int o = __p.__offset(14); return o != 0 ? __p.__vector_len(o) : 0; } }
#if ENABLE_SPAN_T
public Span<byte> GetInventoryBytes() { return __p.__vector_as_span<byte>(14, 1); }
#else
public ArraySegment<byte>? GetInventoryBytes() { return __p.__vector_as_arraysegment(14); }
public ArraySegment<byte>? GetInventoryBytes() { return __p.__vector_as_arraysegment(14, 1); }
#endif
public byte[] GetInventoryArray() { return __p.__vector_as_array<byte>(14); }
public bool MutateInventory(int j, byte inventory) { int o = __p.__offset(14); if (o != 0) { __p.bb.Put(__p.__vector(o) + j * 1, inventory); return true; } else { return false; } }
Expand All @@ -64,7 +64,7 @@ public struct Monster : IFlatbufferObject
#if ENABLE_SPAN_T
public Span<byte> GetTestnestedflatbufferBytes() { return __p.__vector_as_span<byte>(30, 1); }
#else
public ArraySegment<byte>? GetTestnestedflatbufferBytes() { return __p.__vector_as_arraysegment(30); }
public ArraySegment<byte>? GetTestnestedflatbufferBytes() { return __p.__vector_as_arraysegment(30, 1); }
#endif
public byte[] GetTestnestedflatbufferArray() { return __p.__vector_as_array<byte>(30); }
public MyGame.Example.Monster? GetTestnestedflatbufferAsMonster() { int o = __p.__offset(30); return o != 0 ? (MyGame.Example.Monster?)(new MyGame.Example.Monster()).__assign(__p.__indirect(__p.__vector(o)), __p.bb) : null; }
Expand Down Expand Up @@ -93,7 +93,7 @@ public struct Monster : IFlatbufferObject
#if ENABLE_SPAN_T
public Span<bool> GetTestarrayofboolsBytes() { return __p.__vector_as_span<bool>(52, 1); }
#else
public ArraySegment<byte>? GetTestarrayofboolsBytes() { return __p.__vector_as_arraysegment(52); }
public ArraySegment<byte>? GetTestarrayofboolsBytes() { return __p.__vector_as_arraysegment(52, 1); }
#endif
public bool[] GetTestarrayofboolsArray() { return __p.__vector_as_array<bool>(52); }
public bool MutateTestarrayofbools(int j, bool testarrayofbools) { int o = __p.__offset(52); if (o != 0) { __p.bb.Put(__p.__vector(o) + j * 1, (byte)(testarrayofbools ? 1 : 0)); return true; } else { return false; } }
Expand All @@ -112,7 +112,7 @@ public struct Monster : IFlatbufferObject
#if ENABLE_SPAN_T
public Span<byte> GetFlexBytes() { return __p.__vector_as_span<byte>(64, 1); }
#else
public ArraySegment<byte>? GetFlexBytes() { return __p.__vector_as_arraysegment(64); }
public ArraySegment<byte>? GetFlexBytes() { return __p.__vector_as_arraysegment(64, 1); }
#endif
public byte[] GetFlexArray() { return __p.__vector_as_array<byte>(64); }
public bool MutateFlex(int j, byte flex) { int o = __p.__offset(64); if (o != 0) { __p.bb.Put(__p.__vector(o) + j * 1, flex); return true; } else { return false; } }
Expand All @@ -123,7 +123,7 @@ public struct Monster : IFlatbufferObject
#if ENABLE_SPAN_T
public Span<long> GetVectorOfLongsBytes() { return __p.__vector_as_span<long>(68, 8); }
#else
public ArraySegment<byte>? GetVectorOfLongsBytes() { return __p.__vector_as_arraysegment(68); }
public ArraySegment<byte>? GetVectorOfLongsBytes() { return __p.__vector_as_arraysegment(68, 8); }
#endif
public long[] GetVectorOfLongsArray() { return __p.__vector_as_array<long>(68); }
public bool MutateVectorOfLongs(int j, long vector_of_longs) { int o = __p.__offset(68); if (o != 0) { __p.bb.PutLong(__p.__vector(o) + j * 8, vector_of_longs); return true; } else { return false; } }
Expand All @@ -132,7 +132,7 @@ public struct Monster : IFlatbufferObject
#if ENABLE_SPAN_T
public Span<double> GetVectorOfDoublesBytes() { return __p.__vector_as_span<double>(70, 8); }
#else
public ArraySegment<byte>? GetVectorOfDoublesBytes() { return __p.__vector_as_arraysegment(70); }
public ArraySegment<byte>? GetVectorOfDoublesBytes() { return __p.__vector_as_arraysegment(70, 8); }
#endif
public double[] GetVectorOfDoublesArray() { return __p.__vector_as_array<double>(70); }
public bool MutateVectorOfDoubles(int j, double vector_of_doubles) { int o = __p.__offset(70); if (o != 0) { __p.bb.PutDouble(__p.__vector(o) + j * 8, vector_of_doubles); return true; } else { return false; } }
Expand All @@ -147,7 +147,7 @@ public struct Monster : IFlatbufferObject
#if ENABLE_SPAN_T
public Span<ulong> GetVectorOfWeakReferencesBytes() { return __p.__vector_as_span<ulong>(78, 8); }
#else
public ArraySegment<byte>? GetVectorOfWeakReferencesBytes() { return __p.__vector_as_arraysegment(78); }
public ArraySegment<byte>? GetVectorOfWeakReferencesBytes() { return __p.__vector_as_arraysegment(78, 8); }
#endif
public ulong[] GetVectorOfWeakReferencesArray() { return __p.__vector_as_array<ulong>(78); }
public bool MutateVectorOfWeakReferences(int j, ulong vector_of_weak_references) { int o = __p.__offset(78); if (o != 0) { __p.bb.PutUlong(__p.__vector(o) + j * 8, vector_of_weak_references); return true; } else { return false; } }
Expand All @@ -161,7 +161,7 @@ public struct Monster : IFlatbufferObject
#if ENABLE_SPAN_T
public Span<ulong> GetVectorOfCoOwningReferencesBytes() { return __p.__vector_as_span<ulong>(84, 8); }
#else
public ArraySegment<byte>? GetVectorOfCoOwningReferencesBytes() { return __p.__vector_as_arraysegment(84); }
public ArraySegment<byte>? GetVectorOfCoOwningReferencesBytes() { return __p.__vector_as_arraysegment(84, 8); }
#endif
public ulong[] GetVectorOfCoOwningReferencesArray() { return __p.__vector_as_array<ulong>(84); }
public bool MutateVectorOfCoOwningReferences(int j, ulong vector_of_co_owning_references) { int o = __p.__offset(84); if (o != 0) { __p.bb.PutUlong(__p.__vector(o) + j * 8, vector_of_co_owning_references); return true; } else { return false; } }
Expand All @@ -172,7 +172,7 @@ public struct Monster : IFlatbufferObject
#if ENABLE_SPAN_T
public Span<ulong> GetVectorOfNonOwningReferencesBytes() { return __p.__vector_as_span<ulong>(88, 8); }
#else
public ArraySegment<byte>? GetVectorOfNonOwningReferencesBytes() { return __p.__vector_as_arraysegment(88); }
public ArraySegment<byte>? GetVectorOfNonOwningReferencesBytes() { return __p.__vector_as_arraysegment(88, 8); }
#endif
public ulong[] GetVectorOfNonOwningReferencesArray() { return __p.__vector_as_array<ulong>(88); }
public bool MutateVectorOfNonOwningReferences(int j, ulong vector_of_non_owning_references) { int o = __p.__offset(88); if (o != 0) { __p.bb.PutUlong(__p.__vector(o) + j * 8, vector_of_non_owning_references); return true; } else { return false; } }
Expand All @@ -191,7 +191,7 @@ public struct Monster : IFlatbufferObject
#if ENABLE_SPAN_T
public Span<MyGame.Example.Color> GetVectorOfEnumsBytes() { return __p.__vector_as_span<MyGame.Example.Color>(98, 1); }
#else
public ArraySegment<byte>? GetVectorOfEnumsBytes() { return __p.__vector_as_arraysegment(98); }
public ArraySegment<byte>? GetVectorOfEnumsBytes() { return __p.__vector_as_arraysegment(98, 1); }
#endif
public MyGame.Example.Color[] GetVectorOfEnumsArray() { int o = __p.__offset(98); if (o == 0) return null; int p = __p.__vector(o); int l = __p.__vector_len(o); MyGame.Example.Color[] a = new MyGame.Example.Color[l]; for (int i = 0; i < l; i++) { a[i] = (MyGame.Example.Color)__p.bb.Get(p + i * 1); } return a; }
public bool MutateVectorOfEnums(int j, MyGame.Example.Color vector_of_enums) { int o = __p.__offset(98); if (o != 0) { __p.bb.Put(__p.__vector(o) + j * 1, (byte)vector_of_enums); return true; } else { return false; } }
Expand All @@ -202,7 +202,7 @@ public struct Monster : IFlatbufferObject
#if ENABLE_SPAN_T
public Span<byte> GetTestrequirednestedflatbufferBytes() { return __p.__vector_as_span<byte>(102, 1); }
#else
public ArraySegment<byte>? GetTestrequirednestedflatbufferBytes() { return __p.__vector_as_arraysegment(102); }
public ArraySegment<byte>? GetTestrequirednestedflatbufferBytes() { return __p.__vector_as_arraysegment(102, 1); }
#endif
public byte[] GetTestrequirednestedflatbufferArray() { return __p.__vector_as_array<byte>(102); }
public MyGame.Example.Monster? GetTestrequirednestedflatbufferAsMonster() { int o = __p.__offset(102); return o != 0 ? (MyGame.Example.Monster?)(new MyGame.Example.Monster()).__assign(__p.__indirect(__p.__vector(o)), __p.bb) : null; }
Expand Down
2 changes: 1 addition & 1 deletion tests/MyGame/Example/Stat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public struct Stat : IFlatbufferObject
#if ENABLE_SPAN_T
public Span<byte> GetIdBytes() { return __p.__vector_as_span<byte>(4, 1); }
#else
public ArraySegment<byte>? GetIdBytes() { return __p.__vector_as_arraysegment(4); }
public ArraySegment<byte>? GetIdBytes() { return __p.__vector_as_arraysegment(4, 1); }
#endif
public byte[] GetIdArray() { return __p.__vector_as_array<byte>(4); }
public long Val { get { int o = __p.__offset(6); return o != 0 ? __p.bb.GetLong(o + __p.bb_pos) : (long)0; } }
Expand Down
4 changes: 2 additions & 2 deletions tests/MyGame/Example/TypeAliases.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public struct TypeAliases : IFlatbufferObject
#if ENABLE_SPAN_T
public Span<sbyte> GetV8Bytes() { return __p.__vector_as_span<sbyte>(24, 1); }
#else
public ArraySegment<byte>? GetV8Bytes() { return __p.__vector_as_arraysegment(24); }
public ArraySegment<byte>? GetV8Bytes() { return __p.__vector_as_arraysegment(24, 1); }
#endif
public sbyte[] GetV8Array() { return __p.__vector_as_array<sbyte>(24); }
public bool MutateV8(int j, sbyte v8) { int o = __p.__offset(24); if (o != 0) { __p.bb.PutSbyte(__p.__vector(o) + j * 1, v8); return true; } else { return false; } }
Expand All @@ -53,7 +53,7 @@ public struct TypeAliases : IFlatbufferObject
#if ENABLE_SPAN_T
public Span<double> GetVf64Bytes() { return __p.__vector_as_span<double>(26, 8); }
#else
public ArraySegment<byte>? GetVf64Bytes() { return __p.__vector_as_arraysegment(26); }
public ArraySegment<byte>? GetVf64Bytes() { return __p.__vector_as_arraysegment(26, 8); }
#endif
public double[] GetVf64Array() { return __p.__vector_as_array<double>(26); }
public bool MutateVf64(int j, double vf64) { int o = __p.__offset(26); if (o != 0) { __p.bb.PutDouble(__p.__vector(o) + j * 8, vf64); return true; } else { return false; } }
Expand Down
4 changes: 2 additions & 2 deletions tests/MyGame/MonsterExtra.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public struct MonsterExtra : IFlatbufferObject
#if ENABLE_SPAN_T
public Span<double> GetDvecBytes() { return __p.__vector_as_span<double>(20, 8); }
#else
public ArraySegment<byte>? GetDvecBytes() { return __p.__vector_as_arraysegment(20); }
public ArraySegment<byte>? GetDvecBytes() { return __p.__vector_as_arraysegment(20, 8); }
#endif
public double[] GetDvecArray() { return __p.__vector_as_array<double>(20); }
public bool MutateDvec(int j, double dvec) { int o = __p.__offset(20); if (o != 0) { __p.bb.PutDouble(__p.__vector(o) + j * 8, dvec); return true; } else { return false; } }
Expand All @@ -50,7 +50,7 @@ public struct MonsterExtra : IFlatbufferObject
#if ENABLE_SPAN_T
public Span<float> GetFvecBytes() { return __p.__vector_as_span<float>(22, 4); }
#else
public ArraySegment<byte>? GetFvecBytes() { return __p.__vector_as_arraysegment(22); }
public ArraySegment<byte>? GetFvecBytes() { return __p.__vector_as_arraysegment(22, 4); }
#endif
public float[] GetFvecArray() { return __p.__vector_as_array<float>(22); }
public bool MutateFvec(int j, float fvec) { int o = __p.__offset(22); if (o != 0) { __p.bb.PutFloat(__p.__vector(o) + j * 4, fvec); return true; } else { return false; } }
Expand Down

0 comments on commit cbbbaa6

Please sign in to comment.