From 4dd79d590f88834d1911291c6c930bd732935408 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Thu, 11 May 2017 02:01:11 +0300 Subject: utils: exceptions instead of asserts --- utils/helpers/file.hpp | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'utils') diff --git a/utils/helpers/file.hpp b/utils/helpers/file.hpp index e767bbc..48e4c51 100644 --- a/utils/helpers/file.hpp +++ b/utils/helpers/file.hpp @@ -3,12 +3,12 @@ // For details, see https://github.com/egor-tensin/aes-tools. // Distributed under the MIT License. -#include #include #include #include #include +#include #include #include #include @@ -17,8 +17,11 @@ namespace file { inline std::size_t cast_to_size_t(std::streamoff size) { - assert(size >= 0); - assert(static_cast::type>(size) <= std::numeric_limits::max()); + if (size < 0) + throw std::range_error{"file::cast_to_size_t: something went really wrong"}; + typedef std::make_unsigned::type unsigned_streamoff; + if (static_cast(size) > std::numeric_limits::max()) + throw std::range_error{"file::cast_to_size_t: this file is too large"}; return static_cast(size); } @@ -41,8 +44,8 @@ namespace file std::vector src_buf; src_buf.reserve(size); src_buf.assign( - std::istreambuf_iterator(ifs), - std::istreambuf_iterator()); + std::istreambuf_iterator{ifs}, + std::istreambuf_iterator{}); return src_buf; } -- cgit v1.2.3