From d6c0195f92585e1934305a692d0fe90c701d5770 Mon Sep 17 00:00:00 2001 From: Uwe Date: Wed, 11 Feb 2026 08:36:25 +0100 Subject: [PATCH] [Dart] Actually use resized FlexBuffers buffer 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. --- dart/lib/src/builder.dart | 3 ++- dart/test/flex_builder_test.dart | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/dart/lib/src/builder.dart b/dart/lib/src/builder.dart index cdd0aec5323..9e90e4c38e5 100644 --- a/dart/lib/src/builder.dart +++ b/dart/lib/src/builder.dart @@ -5,7 +5,7 @@ import 'types.dart'; /// The main builder class for creation of a FlexBuffer. class Builder { - final ByteData _buffer; + ByteData _buffer; List<_StackValue> _stack = []; List<_StackPointer> _stackPointers = []; int _offset = 0; @@ -506,6 +506,7 @@ class Builder { if (prevSize < size) { final newBuf = ByteData(size); newBuf.buffer.asUint8List().setAll(0, _buffer.buffer.asUint8List()); + _buffer = newBuf; } return newOffset; } diff --git a/dart/test/flex_builder_test.dart b/dart/test/flex_builder_test.dart index 0f766de7385..834afe87686 100644 --- a/dart/test/flex_builder_test.dart +++ b/dart/test/flex_builder_test.dart @@ -318,6 +318,21 @@ void main() { 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', () {