Skip to content

Commit

Permalink
only throw system_error
Browse files Browse the repository at this point in the history
  • Loading branch information
grisumbras committed Jun 2, 2023
1 parent 51d86cd commit be759c5
Show file tree
Hide file tree
Showing 31 changed files with 559 additions and 569 deletions.
6 changes: 3 additions & 3 deletions include/boost/json/array.hpp
Expand Up @@ -593,14 +593,14 @@ class array
Returns a reference to the element specified at
location `pos`, with bounds checking. If `pos` is
not within the range of the container, an exception
of type `std::out_of_range` is thrown.
of type @ref system_error is thrown.
@par Complexity
Constant.
@param pos A zero-based index.
@throw std::out_of_range `pos >= size()`
@throw system_error `pos >= size()`
*/
/* @{ */
inline
Expand Down Expand Up @@ -1070,7 +1070,7 @@ class array
@param new_capacity The new capacity of the array.
@throw std::length_error `new_capacity > max_size()`
@throw system_error `new_capacity > max_size()`
*/
inline
void
Expand Down
18 changes: 13 additions & 5 deletions include/boost/json/detail/except.hpp
Expand Up @@ -20,11 +20,19 @@ namespace detail {
BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION; \
(ec).assign(e, &loc);

BOOST_JSON_DECL void BOOST_NORETURN throw_bad_alloc(source_location const& loc = BOOST_CURRENT_LOCATION);
BOOST_JSON_DECL void BOOST_NORETURN throw_invalid_argument(char const* what, source_location const& loc = BOOST_CURRENT_LOCATION);
BOOST_JSON_DECL void BOOST_NORETURN throw_length_error(char const* what, source_location const& loc = BOOST_CURRENT_LOCATION);
BOOST_JSON_DECL void BOOST_NORETURN throw_out_of_range(source_location const& loc = BOOST_CURRENT_LOCATION);
BOOST_JSON_DECL void BOOST_NORETURN throw_system_error(error_code const& ec, source_location const& loc = BOOST_CURRENT_LOCATION);
BOOST_JSON_DECL
void
BOOST_NORETURN
throw_system_error(
error_code const& ec,
source_location const& loc = BOOST_CURRENT_LOCATION);

BOOST_JSON_DECL
void
BOOST_NORETURN
throw_system_error(
error e,
source_location const* loc);

} // detail
} // namespace json
Expand Down
44 changes: 9 additions & 35 deletions include/boost/json/detail/impl/except.ipp
Expand Up @@ -21,52 +21,26 @@ namespace json {
namespace detail {

void
throw_bad_alloc(
source_location const& loc)
{
throw_exception(
std::bad_alloc(),
loc);
}

void
throw_length_error(
char const* what,
source_location const& loc)
{
throw_exception(
std::length_error(what),
loc);
}

void
throw_invalid_argument(
char const* what,
source_location const& loc)
{
throw_exception(
std::invalid_argument(what),
loc);
}

void
throw_out_of_range(
throw_system_error(
error_code const& ec,
source_location const& loc)
{
throw_exception(
std::out_of_range(
"out of range"),
system_error(ec),
loc);
}

void
throw_system_error(
error_code const& ec,
source_location const& loc)
error e,
source_location const* loc)
{
error_code ec;
ec.assign(e, loc);

throw_exception(
system_error(ec),
loc);
*loc);
}

} // detail
Expand Down
50 changes: 40 additions & 10 deletions include/boost/json/detail/impl/string_impl.ipp
Expand Up @@ -121,7 +121,10 @@ growth(
std::size_t capacity)
{
if(new_size > max_size())
detail::throw_length_error( "string too large" );
{
BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION;
detail::throw_system_error( error::string_too_large, &loc );
}
// growth factor 2
if( capacity >
max_size() - capacity)
Expand Down Expand Up @@ -156,7 +159,10 @@ append(
storage_ptr const& sp)
{
if(n > max_size() - size())
detail::throw_length_error( "string too large" );
{
BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION;
detail::throw_system_error( error::string_too_large, &loc );
}
if(n <= capacity() - size())
{
term(size() + n);
Expand All @@ -182,7 +188,10 @@ insert(
{
const auto curr_size = size();
if(pos > curr_size)
detail::throw_out_of_range();
{
BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION;
detail::throw_system_error( error::out_of_range, &loc );
}
const auto curr_data = data();
if(n <= capacity() - curr_size)
{
Expand Down Expand Up @@ -212,7 +221,10 @@ insert(
else
{
if(n > max_size() - curr_size)
detail::throw_length_error( "string too large" );
{
BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION;
detail::throw_system_error( error::string_too_large, &loc );
}
string_impl tmp(growth(
curr_size + n, capacity()), sp);
tmp.size(curr_size + n);
Expand Down Expand Up @@ -242,7 +254,10 @@ insert_unchecked(
{
const auto curr_size = size();
if(pos > curr_size)
detail::throw_out_of_range();
{
BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION;
detail::throw_system_error( error::out_of_range, &loc );
}
const auto curr_data = data();
if(n <= capacity() - size())
{
Expand All @@ -256,7 +271,10 @@ insert_unchecked(
return dest;
}
if(n > max_size() - curr_size)
detail::throw_length_error( "string too large" );
{
BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION;
detail::throw_system_error( error::string_too_large, &loc );
}
string_impl tmp(growth(
curr_size + n, capacity()), sp);
tmp.size(curr_size + n);
Expand Down Expand Up @@ -284,7 +302,10 @@ replace(
{
const auto curr_size = size();
if (pos > curr_size)
detail::throw_out_of_range();
{
BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION;
detail::throw_system_error( error::out_of_range, &loc );
}
const auto curr_data = data();
n1 = (std::min)(n1, curr_size - pos);
const auto delta = (std::max)(n1, n2) -
Expand Down Expand Up @@ -332,7 +353,10 @@ replace(
else
{
if (delta > max_size() - curr_size)
detail::throw_length_error( "string too large" );
{
BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION;
detail::throw_system_error( error::string_too_large, &loc );
}
// would exceed capacity, reallocate
string_impl tmp(growth(
curr_size + delta, capacity()), sp);
Expand Down Expand Up @@ -366,7 +390,10 @@ replace_unchecked(
{
const auto curr_size = size();
if(pos > curr_size)
detail::throw_out_of_range();
{
BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION;
detail::throw_system_error( error::out_of_range, &loc );
}
const auto curr_data = data();
const auto delta = (std::max)(n1, n2) -
(std::min)(n1, n2);
Expand All @@ -387,7 +414,10 @@ replace_unchecked(
return replace_pos;
}
if(delta > max_size() - curr_size)
detail::throw_length_error( "string too large" );
{
BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION;
detail::throw_system_error( error::string_too_large, &loc );
}
// would exceed capacity, reallocate
string_impl tmp(growth(
curr_size + delta, capacity()), sp);
Expand Down
5 changes: 4 additions & 1 deletion include/boost/json/detail/sbo_buffer.hpp
Expand Up @@ -144,7 +144,10 @@ class sbo_buffer
return;

if( max_size() - size_ < size )
detail::throw_length_error( "buffer too large" );
{
BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION;
detail::throw_system_error( error::number_too_large, &loc );
}

std::size_t const old_capacity = this->capacity();
std::size_t new_capacity = size_ + size;
Expand Down

0 comments on commit be759c5

Please sign in to comment.