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

Fix -Wshorten-64-to-32 warning on Android NDK when targeting x86 #604

Merged
merged 2 commits into from
Dec 23, 2023

Conversation

zeux
Copy link
Owner

@zeux zeux commented Dec 20, 2023

stat.h defines struct stat to use long long on Android NDK when targeting x86; off_t however is defined as long, which is 32-bit (unlike other Unix-like platforms). This results in a narrowing conversion which produces a warning, and can also result in silently reading a prefix of a huge file instead of a clean "out of memory" error. To avoid this, we need to deduce the type of stat::st_size which requires moving the length conversion logic to a templated function since we can't use auto/decltype before C++11.

Fixes #603.

stat.h defines struct stat to use long long on Android NDK when
targeting x86; off_t however is defined as long, which is 32-bit (unlike
other Unix-like platforms). This results in a narrowing conversion which
produces a warning, and can also result in silently reading a prefix of
a huge file instead of a clean "out of memory" error.

There's no way for us to preserve the type exactly but always widening
to long long should be safe; get_file_size will proceed to check if
length actually fits into size_t which is what we ultimately need, and
that overflow check will fail on files that are >4 GB in size.
…nction

Since there's no reasonable way for us to figure out what the type of
stat::st_size is on Android NDK without resorting to C++11 auto /
non-standard decltype extensions, we're going to resort to a templated
function that can deduce the length type. The rest of the validation and
conversion logic is preserved as is.
@zeux zeux merged commit 96aebcb into master Dec 23, 2023
22 checks passed
@zeux zeux deleted the ndk-warnfix branch December 23, 2023 18:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Warning when building 1.14 in x86 on Clang
1 participant