From 97ee2108260ef816d66486f68800550276a56ffc Mon Sep 17 00:00:00 2001 From: Maxim Zaks Date: Tue, 13 Dec 2022 06:20:26 +0100 Subject: [PATCH] Fix a bug where a floating point number was cast to int and the value was stored incorrectly because of low byte width. (#7703) Reported in https://github.com/google/flatbuffers/issues/7690 --- dart/lib/src/types.dart | 2 +- dart/test/flex_builder_test.dart | 4 ++++ dart/test/flex_reader_test.dart | 1 + 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/dart/lib/src/types.dart b/dart/lib/src/types.dart index f9eefd8bc..b4d006072 100644 --- a/dart/lib/src/types.dart +++ b/dart/lib/src/types.dart @@ -9,7 +9,7 @@ class BitWidthUtil { } static BitWidth width(num value) { - if (value.toInt() == value) { + if (value is int) { var v = value.toInt().abs(); if (v >> 7 == 0) return BitWidth.width8; if (v >> 15 == 0) return BitWidth.width16; diff --git a/dart/test/flex_builder_test.dart b/dart/test/flex_builder_test.dart index 66e66384b..0c4a18e61 100644 --- a/dart/test/flex_builder_test.dart +++ b/dart/test/flex_builder_test.dart @@ -40,6 +40,10 @@ void main() { flx.addInt(-1025); expect(flx.finish(), [255, 251, 5, 2]); } + { + var builder = Builder()..addDouble(1.0); + expect(builder.finish(), [0, 0, 128, 63, 14, 4]); + } { var flx = Builder(); flx.addDouble(0.1); diff --git a/dart/test/flex_reader_test.dart b/dart/test/flex_reader_test.dart index 875b1c161..6e0855fc2 100644 --- a/dart/test/flex_reader_test.dart +++ b/dart/test/flex_reader_test.dart @@ -37,6 +37,7 @@ void main() { // expect(FlxValue.fromBuffer(b([255, 255, 255, 255, 255, 255, 255, 255, 11, 8])).intValue, 18446744073709551615); }); test('double value', () { + expect(Reference.fromBuffer(b([0, 0, 128, 63, 14, 4])).doubleValue, 1.0); expect(Reference.fromBuffer(b([0, 0, 144, 64, 14, 4])).doubleValue, 4.5); expect(Reference.fromBuffer(b([205, 204, 204, 61, 14, 4])).doubleValue, closeTo(.1, .001));