Skip to content

Commit af2764c

Browse files
authored
[flutter_svg] loader buffer fix (flutter#9898)
Fixes buffer access when loading svg from assets. Making it consistent with Image.asset loading. Basically changes: ```String provideSvg(ByteData? message) => utf8.decode(message!.buffer.asUint8List(), allowMalformed: true);``` to: ```String provideSvg(ByteData? message) =>utf8.decode(Uint8List.sublistView(message!));``` since we do not want to decode whole buffer. This is useful when using custom `AssetBundles`, like when they provide as slice of buffer on `load()` instead of allocating a new array of bytes. Fixes flutter/flutter#174526 ## Pre-Review Checklist **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent 42bb347 commit af2764c

File tree

4 files changed

+30
-3
lines changed

4 files changed

+30
-3
lines changed

third_party/packages/flutter_svg/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
## NEXT
1+
## 2.2.1
22

3+
* Fixes message buffer access in SvgAssetLoader.
34
* Updates minimum supported SDK version to Flutter 3.29/Dart 3.7.
45

56
## 2.2.0

third_party/packages/flutter_svg/lib/src/loaders.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ class SvgAssetLoader extends SvgLoader<ByteData> {
379379

380380
@override
381381
String provideSvg(ByteData? message) =>
382-
utf8.decode(message!.buffer.asUint8List(), allowMalformed: true);
382+
utf8.decode(Uint8List.sublistView(message!), allowMalformed: true);
383383

384384
@override
385385
SvgCacheKey cacheKey(BuildContext? context) {

third_party/packages/flutter_svg/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: flutter_svg
22
description: An SVG rendering and widget library for Flutter, which allows painting and displaying Scalable Vector Graphics 1.1 files.
33
repository: https://github.com/flutter/packages/tree/main/third_party/packages/flutter_svg
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+flutter_svg%22
5-
version: 2.2.0
5+
version: 2.2.1
66

77
environment:
88
sdk: ^3.7.0

third_party/packages/flutter_svg/test/loaders_test.dart

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import 'dart:convert';
2+
import 'dart:typed_data';
3+
14
import 'package:flutter/services.dart';
25
import 'package:flutter/widgets.dart';
36
import 'package:flutter_svg/flutter_svg.dart';
@@ -64,6 +67,29 @@ void main() {
6467
expect((await packageLoader.prepareMessage(null))!.lengthInBytes, 1);
6568
});
6669

70+
test('AssetLoader correctly accesses buffer', () async {
71+
final ByteBuffer buffer = utf8.encode('foobar').buffer;
72+
final TestBundle bundle = TestBundle(<String, ByteData>{
73+
'foo': buffer.asByteData(0, 3),
74+
'bar': buffer.asByteData(3, 3),
75+
});
76+
final SvgAssetLoader loaderFoo = SvgAssetLoader('foo', assetBundle: bundle);
77+
final SvgAssetLoader loaderBar = SvgAssetLoader('bar', assetBundle: bundle);
78+
final ByteData? byteDataFoo = await loaderFoo.prepareMessage(null);
79+
final ByteData? byteDataBar = await loaderBar.prepareMessage(null);
80+
81+
expect(byteDataFoo!.buffer, equals(byteDataBar!.buffer));
82+
83+
expect(byteDataFoo.lengthInBytes, 3);
84+
expect(byteDataFoo.offsetInBytes, 0);
85+
86+
expect(byteDataBar.offsetInBytes, 3);
87+
expect(byteDataBar.lengthInBytes, 3);
88+
89+
expect(loaderFoo.provideSvg(byteDataFoo), equals('foo'));
90+
expect(loaderBar.provideSvg(byteDataBar), equals('bar'));
91+
});
92+
6793
test('SvgNetworkLoader closes internal client', () async {
6894
final List<VerifyCloseClient> createdClients = <VerifyCloseClient>[];
6995

0 commit comments

Comments
 (0)