Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces a major refactoring of the cross_file package, migrating it to a federated plugin architecture. This is a significant improvement that modernizes the plugin structure. My review focuses on the correctness of the new implementation.
I've found a couple of potential issues in the file stream reading logic for both Android and Darwin platforms that could lead to incorrect behavior or infinite loops. Please see the detailed comments.
Also, as a process note, the pull request description is currently empty and the pre-review checklist is not filled out. Please update the description to summarize the changes and their motivation, and complete the checklist as per the repository's contributing guidelines. The PR title should also be updated to follow the [package_name] convention.
| late android.InputStreamReadBytesResponse response; | ||
| do { | ||
| response = await inputStream.readBytes( | ||
| min(bytesToRead, maxByteArrayLen), | ||
| ); | ||
| yield response.bytes; | ||
| bytesToRead -= response.returnValue; | ||
| } while (response.returnValue > -1 && bytesToRead > 0); | ||
| } else { |
There was a problem hiding this comment.
The current implementation of openRead has two potential issues:
- It yields the entire
response.bytesbuffer, which may contain uninitialized data if the nativereadcall doesn't fill the whole buffer. You should only yield the portion of the buffer that contains valid data, which is indicated byresponse.returnValue. - The
do-whileloop can result in an infinite loop if the underlyingInputStream.readmethod returns 0, which is a valid return value indicating no bytes were read.
I suggest refactoring this loop to be more robust against these edge cases.
while (bytesToRead > 0) {
final int readSize = min(bytesToRead, maxByteArrayLen);
final android.InputStreamReadBytesResponse response =
await inputStream.readBytes(readSize);
if (response.returnValue <= 0) {
// End of stream or error.
break;
}
yield response.bytes.sublist(0, response.returnValue);
bytesToRead -= response.returnValue;
}| do { | ||
| final Uint8List? bytes = await handle.readUpToCount( | ||
| min(bytesToRead, maxByteArrayLen), | ||
| ); | ||
|
|
||
| if (bytes == null) { | ||
| throw UnsupportedError( | ||
| 'Failed to read bytes from file: ${params.uri}', | ||
| ); | ||
| } else { | ||
| yield bytes; | ||
| } | ||
|
|
||
| bytesToRead -= bytes.length; | ||
| } while (bytesToRead > 0); |
There was a problem hiding this comment.
The do-while loop here could become an infinite loop if handle.readUpToCount returns an empty Uint8List. This can happen when reaching the end of the file. When an empty list is returned, bytes.length is 0, bytesToRead is not decremented, and the loop condition bytesToRead > 0 remains true.
To prevent this, you should check if the returned bytes is empty and break the loop if it is.
do {
final Uint8List? bytes = await handle.readUpToCount(
min(bytesToRead, maxByteArrayLen),
);
if (bytes == null) {
throw UnsupportedError(
'Failed to read bytes from file: ${params.uri}',
);
}
if (bytes.isEmpty) {
break;
}
yield bytes;
bytesToRead -= bytes.length;
} while (bytesToRead > 0);
Replace this paragraph with a description of what this PR is changing or adding, and why. Consider including before/after screenshots.
List which issues are fixed by this PR. You must list at least one issue.
Pre-Review Checklist
[shared_preferences]pubspec.yamlwith an appropriate new version according to the pub versioning philosophy, or I have commented below to indicate which version change exemption this PR falls under1.CHANGELOG.mdto add a description of the change, following repository CHANGELOG style, or I have commented below to indicate which CHANGELOG exemption this PR falls under1.///).If you need help, consider asking for advice on the #hackers-new channel on Discord.
Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the
gemini-code-assistbot 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.Footnotes
Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling. ↩ ↩2 ↩3