mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-01 19:58:15 +00:00
[Dart] Actually use resized FlexBuffers buffer (#8935)
When building a FlexBuffer using the Builder and adding data that exceeds the default buffer size (2048 bytes), in _newOffset() a larger buffer is created, but never used. This results in a RangeError. Resolve by actually replacing the too small with the new larger buffer. Add a test that verifies this by adding multiple large strings to a vector.
This commit is contained in:
@@ -5,7 +5,7 @@ import 'types.dart';
|
|||||||
|
|
||||||
/// The main builder class for creation of a FlexBuffer.
|
/// The main builder class for creation of a FlexBuffer.
|
||||||
class Builder {
|
class Builder {
|
||||||
final ByteData _buffer;
|
ByteData _buffer;
|
||||||
List<_StackValue> _stack = [];
|
List<_StackValue> _stack = [];
|
||||||
List<_StackPointer> _stackPointers = [];
|
List<_StackPointer> _stackPointers = [];
|
||||||
int _offset = 0;
|
int _offset = 0;
|
||||||
@@ -506,6 +506,7 @@ class Builder {
|
|||||||
if (prevSize < size) {
|
if (prevSize < size) {
|
||||||
final newBuf = ByteData(size);
|
final newBuf = ByteData(size);
|
||||||
newBuf.buffer.asUint8List().setAll(0, _buffer.buffer.asUint8List());
|
newBuf.buffer.asUint8List().setAll(0, _buffer.buffer.asUint8List());
|
||||||
|
_buffer = newBuf;
|
||||||
}
|
}
|
||||||
return newOffset;
|
return newOffset;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -318,6 +318,21 @@ void main() {
|
|||||||
1,
|
1,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
{
|
||||||
|
// Default buffer is 2048 bytes, add 2300 bytes of strings that force it
|
||||||
|
// to grow.
|
||||||
|
final s1 = 'A' * 1000;
|
||||||
|
final s2 = 'B' * 800;
|
||||||
|
final s3 = 'C' * 500;
|
||||||
|
|
||||||
|
var flx = Builder()
|
||||||
|
..startVector()
|
||||||
|
..addString(s1)
|
||||||
|
..addString(s2)
|
||||||
|
..addString(s3)
|
||||||
|
..end();
|
||||||
|
expect(flx.finish().length, 2323);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
test('build map', () {
|
test('build map', () {
|
||||||
|
|||||||
Reference in New Issue
Block a user