Closed
Description
openedon Dec 24, 2022
flatbuffers version: 22.11.23
OS: MacOS and Linux
If a schema defines two consecutive required string fields, the generated createX()
function DOES NOT THROW when the second field is missing.
Further info, builder.requiredFiled
gets a non zero value in this line var ok = this.bb.readInt16(vtable_start + field) != 0;
and hence it does not throw.
NOTE: If the schema defines two NON consecutive required string fields, the generated createX
function THROWS when the first or second string field is missing.
Above the STR demonstrating the bug.
tests.fbs:
namespace FBS.Test;
// Table with two consecutive required string fields.
// - createX() THROWS is null|undefined value is provided as `str_a`.
// - createX() DOES NOT THROW is null|undefined value is provided as `str_b`.
table Foo {
str_a:string (required);
str_b:string (required);
}
// Table with two NON consecutive required string fields.
// - createX() THROWS is null|undefined value is provided as `str_a`.
// - createX() THROWS is null|undefined value is provided as `str_b`.
table Bar {
str_a:string (required);
int_a:uint8;
str_b:string (required);
}
test.ts:
import * as flatbuffers from 'flatbuffers';
import * as FbsTest from '../fbs/test_generated';
// flatbuffers builder.
const builder:flatbuffers.Builder = new flatbuffers.Builder(1024);
afterEach(() => builder.clear());
test('createFoo() throws if str_a is not set', () =>
{
const strA = builder.createString(undefined);
const strB = builder.createString('foo');
expect(() => FbsTest.Foo.createFoo(
builder, strA, strB
)).toThrow();
});
test('createFoo() throws if str_b is not set', () =>
{
const strA = builder.createString('foo');
const strB = builder.createString(undefined);
expect(() => FbsTest.Foo.createFoo(
builder, strA, strB
)).toThrow();
});
test('createBar() throws if str_a is not set', () =>
{
const strA = builder.createString(undefined);
const strB = builder.createString('foo');
expect(() => FbsTest.Bar.createBar(
builder, strA, 0, strB
)).toThrow();
});
test('createBar() throws if str_b is not set', () =>
{
const strA = builder.createString('foo');
const strB = builder.createString(undefined);
expect(() => FbsTest.Bar.createBar(
builder, strA, 0, strB
)).toThrow();
});
Result of running the test:
Corresponding PR #7752
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Metadata
Assignees
Labels
No labels