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

FileDataLoader fails to read the file when size > INT32_MAX #4435

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion extension/data_loader/file_data_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@

#include <executorch/extension/data_loader/file_data_loader.h>

#include <algorithm>
#include <cerrno>
#include <cstddef>
#include <cstring>
#include <limits>

#include <fcntl.h>
#include <sys/stat.h>
Expand Down Expand Up @@ -189,7 +191,12 @@ Result<FreeableBuffer> FileDataLoader::load(
size_t needed = size;
uint8_t* buf = reinterpret_cast<uint8_t*>(aligned_buffer);
while (needed > 0) {
ssize_t nread = ::read(fd_, buf, needed);
// read on macos would fail with EINVAL if size > INT32_MAX
Copy link
Contributor

Choose a reason for hiding this comment

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

Please begin comments with uppercase letters and end with a period, to be consistent with the rest of the file. Thanks for adding this comment, though, to explain why this extra logic is necessary, and where to focus testing if it changes.

Suggested change
// read on macos would fail with EINVAL if size > INT32_MAX
// Reads on macOS will fail with EINVAL if size > INT32_MAX.

Copy link
Contributor

Choose a reason for hiding this comment

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

When you get a chance please update this comment, then we can land the PR

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done.

ssize_t nread = ::read(
fd_,
buf,
std::min<size_t>(
needed, static_cast<size_t>(std::numeric_limits<int32_t>::max())));
if (nread < 0 && errno == EINTR) {
// Interrupted by a signal; zero bytes read.
continue;
Expand Down
Loading