0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-25 16:22:35 +01:00

ircd:Ⓜ️🆔 Improve size constants and range enforcements.

This commit is contained in:
Jason Volk 2018-04-24 19:18:50 -07:00
parent bf445492db
commit 12592cd22c
2 changed files with 45 additions and 12 deletions

View file

@ -49,7 +49,7 @@ struct ircd::m::id
struct device;
enum sigil :char;
template<class T, size_t SIZE = 256> struct buf;
template<class T> struct buf;
template<class it> struct input;
template<class it> struct output;
struct parser;
@ -60,6 +60,11 @@ struct ircd::m::id
struct printer static const printer;
struct validator static const validator;
static constexpr const size_t MAX_SIZE
{
255
};
public:
// Extract elements
string_view local() const; // The full localpart including sigil
@ -213,21 +218,20 @@ namespace ircd::m
/// ID object backed by an internal buffer of default worst-case size.
///
template<class T,
size_t MAX>
template<class T>
struct ircd::m::id::buf
:T
{
static constexpr const size_t SIZE
{
MAX
m::id::MAX_SIZE
};
private:
fixed_buffer<mutable_buffer, SIZE> b;
fixed_buffer<mutable_buffer, SIZE + 1> b;
public:
operator const fixed_buffer<mutable_buffer, SIZE> &() const
operator const fixed_buffer<mutable_buffer, SIZE + 1> &() const
{
return b;
}

View file

@ -259,7 +259,12 @@ const try
string_view out;
const char *start{id.begin()};
const bool res(qi::parse(start, id.end(), view_mxid, out));
const char *const stop
{
std::min(id.end(), start + MAX_SIZE)
};
const bool res{qi::parse(start, stop, view_mxid, out)};
assert(res == true);
return out;
}
@ -279,7 +284,12 @@ const try
string_view out;
const char *start{id.begin()};
const bool res(qi::parse(start, id.end(), view_mxid, out));
const char *const stop
{
std::min(id.end(), start + MAX_SIZE)
};
const bool res(qi::parse(start, stop, view_mxid, out));
assert(res == true);
return out;
}
@ -301,7 +311,12 @@ ircd::m::id::validator::operator()(const string_view &id)
const try
{
const char *start{id.begin()};
const bool ret(qi::parse(start, id.end(), eps > mxid));
const char *const stop
{
std::min(id.end(), start + MAX_SIZE)
};
const bool ret(qi::parse(start, stop, eps > mxid));
assert(ret == true);
}
catch(const qi::expectation_failure<const char *> &e)
@ -326,7 +341,12 @@ const try
};
const char *start{id.begin()};
const bool ret(qi::parse(start, id.end(), valid_mxid));
const char *const stop
{
std::min(id.end(), start + MAX_SIZE)
};
const bool ret(qi::parse(start, stop, valid_mxid));
assert(ret == true);
}
catch(const qi::expectation_failure<const char *> &e)
@ -649,7 +669,11 @@ noexcept try
};
const char *start{begin(id)};
const char *const &stop{end(id)};
const char *const &stop
{
std::min(end(id), start + id::MAX_SIZE)
};
return id.at(0) == sigil && qi::parse(start, stop, test) && start == stop;
}
catch(...)
@ -668,7 +692,12 @@ noexcept try
};
const char *start{begin(id)};
return id.at(0) == sigil && qi::parse(start, end(id), test);
const char *const &stop
{
std::min(end(id), start + id::MAX_SIZE)
};
return id.at(0) == sigil && qi::parse(start, stop, test);
}
catch(...)
{