Fix for previous Java commit.

optimization would cause vtable fields from previous tables to be written.

Bug: 19046968
Change-Id: I781f7bcbceeaec0b499d4f1e4e5e8a1e750e0707
Tested: on Linux.
parent 3e1b789d
...@@ -33,6 +33,7 @@ public class FlatBufferBuilder { ...@@ -33,6 +33,7 @@ public class FlatBufferBuilder {
static final Charset utf8charset = Charset.forName("UTF-8"); static final Charset utf8charset = Charset.forName("UTF-8");
int minalign = 1; // Minimum alignment encountered so far. int minalign = 1; // Minimum alignment encountered so far.
int[] vtable = null; // The vtable for the current table. int[] vtable = null; // The vtable for the current table.
int vtable_in_use = 0; // The amount of fields we're actually using.
boolean nested = false; // Whether we are currently serializing a table. boolean nested = false; // Whether we are currently serializing a table.
int object_start; // Starting offset of the current struct/table. int object_start; // Starting offset of the current struct/table.
int[] vtables = new int[16]; // List of offsets of all vtables. int[] vtables = new int[16]; // List of offsets of all vtables.
...@@ -306,6 +307,8 @@ public class FlatBufferBuilder { ...@@ -306,6 +307,8 @@ public class FlatBufferBuilder {
public void startObject(int numfields) { public void startObject(int numfields) {
notNested(); notNested();
if (vtable == null || vtable.length < numfields) vtable = new int[numfields]; if (vtable == null || vtable.length < numfields) vtable = new int[numfields];
vtable_in_use = numfields;
Arrays.fill(vtable, 0, vtable_in_use, 0);
nested = true; nested = true;
object_start = offset(); object_start = offset();
} }
...@@ -345,7 +348,7 @@ public class FlatBufferBuilder { ...@@ -345,7 +348,7 @@ public class FlatBufferBuilder {
addInt(0); addInt(0);
int vtableloc = offset(); int vtableloc = offset();
// Write out the current vtable. // Write out the current vtable.
for (int i = vtable.length - 1; i >= 0 ; i--) { for (int i = vtable_in_use - 1; i >= 0 ; i--) {
// Offset relative to the start of the table. // Offset relative to the start of the table.
short off = (short)(vtable[i] != 0 ? vtableloc - vtable[i] : 0); short off = (short)(vtable[i] != 0 ? vtableloc - vtable[i] : 0);
addShort(off); addShort(off);
...@@ -353,7 +356,7 @@ public class FlatBufferBuilder { ...@@ -353,7 +356,7 @@ public class FlatBufferBuilder {
final int standard_fields = 2; // The fields below: final int standard_fields = 2; // The fields below:
addShort((short)(vtableloc - object_start)); addShort((short)(vtableloc - object_start));
addShort((short)((vtable.length + standard_fields) * SIZEOF_SHORT)); addShort((short)((vtable_in_use + standard_fields) * SIZEOF_SHORT));
// Search for an existing vtable that matches the current one. // Search for an existing vtable that matches the current one.
int existing_vtable = 0; int existing_vtable = 0;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment