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 get_file_size behavior inconsistency for folders #561

Merged
merged 2 commits into from
Apr 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 16 additions & 1 deletion src/pugixml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
// For placement new
#include <new>

// For load_file
#if defined(__linux__) || defined(__APPLE__)
#include <sys/stat.h>
#endif

#ifdef _MSC_VER
# pragma warning(push)
# pragma warning(disable: 4127) // conditional expression is constant
Expand Down Expand Up @@ -4759,7 +4764,17 @@ PUGI_IMPL_NS_BEGIN
// we need to get length of entire file to load it in memory; the only (relatively) sane way to do it is via seek/tell trick
PUGI_IMPL_FN xml_parse_status get_file_size(FILE* file, size_t& out_result)
{
#if defined(PUGI_IMPL_MSVC_CRT_VERSION) && PUGI_IMPL_MSVC_CRT_VERSION >= 1400
#if defined(__linux__) || defined(__APPLE__)
// this simultaneously retrieves the file size and file mode (to guard against loading non-files)
struct stat st;
if (fstat(fileno(file), &st) != 0) return status_io_error;

// 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;
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
typedef __int64 length_type;

Expand Down
2 changes: 1 addition & 1 deletion tests/test_document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ TEST(document_load_file_wide_out_of_memory)
CHECK(result.status == status_out_of_memory || result.status == status_file_not_found);
}

#if defined(__APPLE__)
#if defined(__linux__) || defined(__APPLE__)
TEST(document_load_file_special_folder)
{
xml_document doc;
Expand Down