io_posix: test the file size can actually be cast to size_t
This commit is contained in:
parent
72bd68e60b
commit
85e9dba38c
30
io_posix.cpp
30
io_posix.cpp
@ -10,6 +10,7 @@
|
|||||||
|
|
||||||
#include "cast.hpp"
|
#include "cast.hpp"
|
||||||
#include "debug.hpp"
|
#include "debug.hpp"
|
||||||
|
#include "except.hpp"
|
||||||
#include "posix/fd.hpp"
|
#include "posix/fd.hpp"
|
||||||
#include "posix/except.hpp"
|
#include "posix/except.hpp"
|
||||||
|
|
||||||
@ -18,6 +19,16 @@
|
|||||||
using cruft::detail::posix::mapped_file;
|
using cruft::detail::posix::mapped_file;
|
||||||
|
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
namespace {
|
||||||
|
struct overflow_error : public cruft::error {
|
||||||
|
std::ostream& describe (std::ostream &os) const override
|
||||||
|
{
|
||||||
|
return os << "mapping is too large for the system";
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////////
|
||||||
mapped_file::mapped_file (const std::filesystem::path &path,
|
mapped_file::mapped_file (const std::filesystem::path &path,
|
||||||
int fflags,
|
int fflags,
|
||||||
@ -27,18 +38,29 @@ mapped_file::mapped_file (const std::filesystem::path &path,
|
|||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
mapped_file::mapped_file (const ::cruft::posix::fd &src, int mflags)
|
mapped_file::mapped_file (::cruft::posix::fd const &src, int const mflags)
|
||||||
{
|
{
|
||||||
struct stat meta;
|
struct stat meta;
|
||||||
::cruft::posix::error::try_value (fstat (src, &meta));
|
::cruft::posix::error::try_value (fstat (src, &meta));
|
||||||
|
|
||||||
m_size = cruft::cast::sign<size_t> (meta.st_size);
|
// Zero length mappings aren't allowed by POSIX (even though they're
|
||||||
if (!m_size) {
|
// accepted by some systems; like Linux)
|
||||||
|
if (meta.st_size == 0) {
|
||||||
m_data = nullptr;
|
m_data = nullptr;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
m_data = static_cast<uint8_t*> (mmap (nullptr, m_size, mflags, MAP_SHARED, src, 0));
|
// Ensure we're not trying to map more than our address space allows.
|
||||||
|
//
|
||||||
|
// It's a little nasty, but we protect a
|
||||||
|
if constexpr (sizeof (meta.st_size) > sizeof (size_t)) {
|
||||||
|
auto const hi = cruft::cast::lossless<off_t> (std::numeric_limits<size_t>::max ());
|
||||||
|
if (meta.st_size > hi)
|
||||||
|
throw overflow_error ();
|
||||||
|
}
|
||||||
|
|
||||||
|
m_size = cruft::cast::lossless<size_t> (meta.st_size);
|
||||||
|
m_data = static_cast<u08*> (mmap (nullptr, m_size, mflags, MAP_SHARED, src, 0));
|
||||||
if (m_data == MAP_FAILED)
|
if (m_data == MAP_FAILED)
|
||||||
::cruft::posix::error::throw_code ();
|
::cruft::posix::error::throw_code ();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user