Skip to content

Commit

Permalink
Fix -Wshorten-64-to-32 warning on Android NDK when targeting x86
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
zeux committed Dec 20, 2023
1 parent 499750a commit 8fef459
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/pugixml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4801,7 +4801,8 @@ PUGI_IMPL_NS_BEGIN
// anything that's not a regular file doesn't have a coherent length
if (!S_ISREG(st.st_mode)) return status_io_error;

typedef off_t length_type;
// normally st_size is off_t, but Android NDK defines off_t as long (which is 32-bit when targeting x86 on Android) and st_size as long long
typedef long long length_type;
length_type length = st.st_size;
#elif defined(PUGI_IMPL_MSVC_CRT_VERSION) && PUGI_IMPL_MSVC_CRT_VERSION >= 1400
// there are 64-bit versions of fseek/ftell, let's use them
Expand Down

0 comments on commit 8fef459

Please sign in to comment.