Skip to content

New crossfile#10969

Closed
bparrishMines wants to merge 143 commits intoflutter:mainfrom
bparrishMines:new_crossfile
Closed

New crossfile#10969
bparrishMines wants to merge 143 commits intoflutter:mainfrom
bparrishMines:new_crossfile

Conversation

@bparrishMines
Copy link
Contributor

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

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-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.

Footnotes

  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. 2 3

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines 69 to 77
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 {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The current implementation of openRead has two potential issues:

  1. It yields the entire response.bytes buffer, which may contain uninitialized data if the native read call doesn't fill the whole buffer. You should only yield the portion of the buffer that contains valid data, which is indicated by response.returnValue.
  2. The do-while loop can result in an infinite loop if the underlying InputStream.read method 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;
      }

Comment on lines +115 to +129
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);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

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);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant