From 5e057e152e214d7c92a03192e6c3a235df5961ea Mon Sep 17 00:00:00 2001 From: ljfa Date: Thu, 13 Aug 2015 14:50:33 +0200 Subject: Set failbit on ostream in case of error --- src/io/stream_writer.cpp | 1 + src/tag_array.cpp | 6 ++++++ src/tag_list.cpp | 6 ++++++ 3 files changed, 13 insertions(+) (limited to 'src') diff --git a/src/io/stream_writer.cpp b/src/io/stream_writer.cpp index ee8b0ee2b6..036c5d40ac 100644 --- a/src/io/stream_writer.cpp +++ b/src/io/stream_writer.cpp @@ -41,6 +41,7 @@ void stream_writer::write_string(const std::string& str) { if(str.size() > max_string_len) { + os.setstate(std::ios::failbit); std::ostringstream sstr; sstr << "String is too long for NBT (" << str.size() << " > " << max_string_len << ")"; throw std::length_error(sstr.str()); diff --git a/src/tag_array.cpp b/src/tag_array.cpp index 124c470bd1..41f00eedc3 100644 --- a/src/tag_array.cpp +++ b/src/tag_array.cpp @@ -147,7 +147,10 @@ template<> void tag_array::write_payload(io::stream_writer& writer) const { if(size() > INT32_MAX) + { + writer.get_ostr().setstate(std::ios::failbit); throw std::length_error("Byte array is too large for NBT"); + } writer.write_num(static_cast(size())); writer.get_ostr().write(reinterpret_cast(data.data()), data.size()); } @@ -156,7 +159,10 @@ template<> void tag_array::write_payload(io::stream_writer& writer) const { if(size() > INT32_MAX) + { + writer.get_ostr().setstate(std::ios::failbit); throw std::length_error("Int array is too large for NBT"); + } writer.write_num(static_cast(size())); for(int32_t i: data) writer.write_num(i); diff --git a/src/tag_list.cpp b/src/tag_list.cpp index dbdda2db77..d2aa01d314 100644 --- a/src/tag_list.cpp +++ b/src/tag_list.cpp @@ -162,7 +162,10 @@ void tag_list::read_payload(io::stream_reader& reader) void tag_list::write_payload(io::stream_writer& writer) const { if(size() > INT32_MAX) + { + writer.get_ostr().setstate(std::ios::failbit); throw std::length_error("List is too large for NBT"); + } writer.write_type(el_type_ != tag_type::Null ? el_type_ : tag_type::End); @@ -171,7 +174,10 @@ void tag_list::write_payload(io::stream_writer& writer) const { //check if the value is of the correct type if(val.get_type() != el_type_) + { + writer.get_ostr().setstate(std::ios::failbit); throw std::bad_cast(); + } writer.write_payload(val); } } -- cgit 0.0.5-2-1-g0f52