-
Notifications
You must be signed in to change notification settings - Fork 870
[Build] fix file_data_loader.cpp build issues for windows #4899
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,59 @@ | ||||||||||||||||||||||||
| /* | ||||||||||||||||||||||||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||||||||||||||||||||||||
| * All rights reserved. | ||||||||||||||||||||||||
| * | ||||||||||||||||||||||||
| * This source code is licensed under the BSD-style license found in the | ||||||||||||||||||||||||
| * LICENSE file in the root directory of this source tree. | ||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // This file ensures that a pread-compatible function is defined in the global namespace for windows and posix environments. | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| #pragma once | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| #ifndef _WIN32 | ||||||||||||||||||||||||
| #include <unistd.h> | ||||||||||||||||||||||||
| #else | ||||||||||||||||||||||||
| #include <executorch/runtime/platform/compiler.h> // For ssize_t. | ||||||||||||||||||||||||
| #include <io.h> | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| #include <windows.h> | ||||||||||||||||||||||||
| // To avoid conflicts with std::numeric_limits<int32_t>::max() in | ||||||||||||||||||||||||
| // file_data_loader.cpp. | ||||||||||||||||||||||||
| #undef max | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| inline ssize_t pread(int fd, void* buf, size_t nbytes, size_t offset) { | ||||||||||||||||||||||||
| OVERLAPPED overlapped; /* The offset for ReadFile. */ | ||||||||||||||||||||||||
| memset(&overlapped, 0, sizeof(overlapped)); | ||||||||||||||||||||||||
|
Comment on lines
+16
to
+26
|
||||||||||||||||||||||||
| overlapped.Offset = offset; | ||||||||||||||||||||||||
| overlapped.OffsetHigh = offset >> 32; | ||||||||||||||||||||||||
|
Comment on lines
+27
to
+28
|
||||||||||||||||||||||||
| overlapped.Offset = offset; | |
| overlapped.OffsetHigh = offset >> 32; | |
| ULONGLONG offset64 = (ULONGLONG)offset; | |
| overlapped.Offset = (DWORD)offset64; | |
| overlapped.OffsetHigh = (DWORD)(offset64 >> 32); |
Copilot
AI
Feb 25, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ReadFile takes a DWORD byte count, but this wrapper passes size_t nbytes directly. On 64-bit this can silently truncate (and may warn/error under -Wconversion/-Werror). Add an explicit bounds check (e.g., nbytes <= MAXDWORD) and cast to DWORD before calling ReadFile; return -1 with an appropriate errno if the request is too large.
Copilot
AI
Feb 25, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The ERROR_HANDLE_EOF case currently sets errno = 0 and returns -1. POSIX pread signals EOF by returning 0 (not an error). Returning -1 here can lead to misleading logs like strerror(0) ("Success") and incorrect error handling. Return 0 for this case to match pread semantics.
| break; | |
| default: | |
| errno = EIO; | |
| break; | |
| } | |
| return -1; | |
| return 0; | |
| default: | |
| errno = EIO; | |
| return -1; | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On Windows, including
<windows.h>withoutNOMINMAXtypically defines bothminandmaxmacros. This header only#undef max, butfile_data_loader.cppusesstd::min, which can be broken by theminmacro. Consider definingNOMINMAXbefore including<windows.h>and/or#undef minalongside#undef maxhere.