Ensure consistent behavior between Android and other OSes

Android never checked if m_file was null and thus would invoke UB
upon calling certain functions whereas all other OSes would return
a defined value.
This commit is contained in:
Chris Thrasher 2023-11-27 13:21:52 -07:00
parent 9b751899eb
commit fe0785769e

View File

@ -80,13 +80,12 @@ bool FileInputStream::open(const std::filesystem::path& filename)
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
std::int64_t FileInputStream::read(void* data, std::int64_t size) std::int64_t FileInputStream::read(void* data, std::int64_t size)
{ {
if (!m_file)
return -1;
#ifdef SFML_SYSTEM_ANDROID #ifdef SFML_SYSTEM_ANDROID
return m_file->read(data, size); return m_file->read(data, size);
#else #else
if (m_file) return static_cast<std::int64_t>(std::fread(data, 1, static_cast<std::size_t>(size), m_file.get()));
return static_cast<std::int64_t>(std::fread(data, 1, static_cast<std::size_t>(size), m_file.get()));
else
return -1;
#endif #endif
} }
@ -94,20 +93,15 @@ std::int64_t FileInputStream::read(void* data, std::int64_t size)
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
std::int64_t FileInputStream::seek(std::int64_t position) std::int64_t FileInputStream::seek(std::int64_t position)
{ {
if (!m_file)
return -1;
#ifdef SFML_SYSTEM_ANDROID #ifdef SFML_SYSTEM_ANDROID
return m_file->seek(position); return m_file->seek(position);
#else #else
if (m_file) if (std::fseek(m_file.get(), static_cast<long>(position), SEEK_SET))
{
if (std::fseek(m_file.get(), static_cast<long>(position), SEEK_SET))
return -1;
return tell();
}
else
{
return -1; return -1;
}
return tell();
#endif #endif
} }
@ -115,13 +109,12 @@ std::int64_t FileInputStream::seek(std::int64_t position)
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
std::int64_t FileInputStream::tell() std::int64_t FileInputStream::tell()
{ {
if (!m_file)
return -1;
#ifdef SFML_SYSTEM_ANDROID #ifdef SFML_SYSTEM_ANDROID
return m_file->tell(); return m_file->tell();
#else #else
if (m_file) return std::ftell(m_file.get());
return std::ftell(m_file.get());
else
return -1;
#endif #endif
} }
@ -129,24 +122,19 @@ std::int64_t FileInputStream::tell()
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
std::int64_t FileInputStream::getSize() std::int64_t FileInputStream::getSize()
{ {
if (!m_file)
return -1;
#ifdef SFML_SYSTEM_ANDROID #ifdef SFML_SYSTEM_ANDROID
return m_file->getSize(); return m_file->getSize();
#else #else
if (m_file) const std::int64_t position = tell();
{ std::fseek(m_file.get(), 0, SEEK_END);
const std::int64_t position = tell(); const std::int64_t size = tell();
std::fseek(m_file.get(), 0, SEEK_END);
const std::int64_t size = tell();
if (seek(position) == -1) if (seek(position) == -1)
return -1;
return size;
}
else
{
return -1; return -1;
}
return size;
#endif #endif
} }