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 -1;
return tell(); return tell();
}
else
{
return -1;
}
#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,11 +122,11 @@ 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(); const std::int64_t position = tell();
std::fseek(m_file.get(), 0, SEEK_END); std::fseek(m_file.get(), 0, SEEK_END);
const std::int64_t size = tell(); const std::int64_t size = tell();
@ -142,11 +135,6 @@ std::int64_t FileInputStream::getSize()
return -1; return -1;
return size; return size;
}
else
{
return -1;
}
#endif #endif
} }