Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[flutter_image] Fix : network image memory and nullsafety #8440

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
9 changes: 9 additions & 0 deletions packages/flutter_image/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

* Updates minimum supported SDK version to Flutter 3.22/Dart 3.4.


## 5.0.0

* **Breaking change**: Adds memory usage limit (50MB) to prevent excessive memory allocation in `_Uint8ListBuilder`. Images larger than 50MB will throw an exception.
* Adds `dispose()` method to properly clean up HTTP client resources.
* Improves null safety handling in HTTP request headers.
* Improves error messages for empty responses and failed requests.
* Updates documentation to reference latest HTTP RFC.

## 4.1.11

* Replaces deprecated loadBuffer API usage.
Expand Down
12 changes: 11 additions & 1 deletion packages/flutter_image/lib/network.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ class NetworkImageWithRetry extends ImageProvider<NetworkImageWithRetry> {
/// The HTTP client used to download images.
static final io.HttpClient _client = io.HttpClient();

/// Closes the static HTTP client
static void dispose() {
_client.close();
}

/// The URL from which the image will be fetched.
final String url;

Expand Down Expand Up @@ -342,6 +347,7 @@ class FetchFailure implements Exception {
/// An indefinitely growing builder of a [Uint8List].
class _Uint8ListBuilder {
static const int _kInitialSize = 100000; // 100KB-ish
static const int _kMaxSize = 50 * 1024 * 1024; // 50MB limit

int _usedLength = 0;
Uint8List _buffer = Uint8List(_kInitialSize);
Expand All @@ -357,9 +363,13 @@ class _Uint8ListBuilder {
void _ensureCanAdd(int byteCount) {
final int totalSpaceNeeded = _usedLength + byteCount;

if (totalSpaceNeeded > _kMaxSize) {
throw Exception('Response too large: $totalSpaceNeeded bytes');
}

int newLength = _buffer.length;
while (totalSpaceNeeded > newLength) {
newLength *= 2;
newLength = math.min(_kMaxSize, newLength * 2);
}

if (newLength != _buffer.length) {
Expand Down
2 changes: 1 addition & 1 deletion packages/flutter_image/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: >
Image utilities for Flutter: improved network providers, effects, etc.
repository: https://github.com/flutter/packages/tree/main/packages/flutter_image
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+flutter_image%22
version: 4.1.11
version: 5.0.0

environment:
sdk: ^3.4.0
Expand Down