summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorljfa <ljfa-ag@web.de>2015-08-13 14:50:33 +0200
committerljfa <ljfa-ag@web.de>2015-08-13 14:54:34 +0200
commit5e057e152e214d7c92a03192e6c3a235df5961ea (patch)
tree197ecb30d7d93a43e3c1850620052697d500f7f6
parentaac94b91ad83ad8f7832976e6da55d15de6e54e1 (diff)
downloadProject-Tick-5e057e152e214d7c92a03192e6c3a235df5961ea.tar.gz
Project-Tick-5e057e152e214d7c92a03192e6c3a235df5961ea.zip
Set failbit on ostream in case of error
-rw-r--r--src/io/stream_writer.cpp1
-rw-r--r--src/tag_array.cpp6
-rw-r--r--src/tag_list.cpp6
-rw-r--r--test/write_test.cpp20
4 files changed, 31 insertions, 2 deletions
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<int8_t>::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<int32_t>(size()));
writer.get_ostr().write(reinterpret_cast<const char*>(data.data()), data.size());
}
@@ -156,7 +159,10 @@ template<>
void tag_array<int32_t>::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<int32_t>(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);
}
}
diff --git a/test/write_test.cpp b/test/write_test.cpp
index d6eac53a53..9445e4ab70 100644
--- a/test/write_test.cpp
+++ b/test/write_test.cpp
@@ -54,8 +54,11 @@ void test_stream_writer_big()
0x00, 0x06, //string length in Big Endian
'f', 'o', 'o', 'b', 'a', 'r'
};
- std::string s = os.str();
ASSERT(os.str() == expected);
+
+ //too long for NBT
+ EXPECT_EXCEPTION(writer.write_string(std::string(65536, '.')), std::length_error);
+ ASSERT(!os);
std::clog << "test_stream_writer_big passed" << std::endl;
}
@@ -77,8 +80,10 @@ void test_stream_writer_little()
0x06, 0x00, //string length in Little Endian
'f', 'o', 'o', 'b', 'a', 'r'
};
- std::string s = os.str();
ASSERT(os.str() == expected);
+
+ EXPECT_EXCEPTION(writer.write_string(std::string(65536, '.')), std::length_error);
+ ASSERT(!os);
std::clog << "test_stream_writer_little passed" << std::endl;
}
@@ -114,6 +119,9 @@ void test_write_payload_big()
0x00, 0x06, //string length in Big Endian
'b', 'a', 'r', 'b', 'a', 'z'
}));
+ EXPECT_EXCEPTION(writer.write_payload(tag_string(std::string(65536, '.'))), std::length_error);
+ ASSERT(!os);
+ os.clear();
//tag_byte_array
os.str("");
@@ -193,7 +201,15 @@ void test_write_payload_big()
ASSERT(os.str() == endtag + subtag1 + subtag2 + endtag
|| os.str() == endtag + subtag2 + subtag1 + endtag);
+ //Now for write_tag:
+ os.str("");
+ writer.write_tag("foo", tag_string("quux"));
+ ASSERT(os.str() == subtag1);
ASSERT(os);
+
+ //too long key for NBT
+ EXPECT_EXCEPTION(writer.write_tag(std::string(65536, '.'), tag_long(-1)), std::length_error);
+ ASSERT(!os);
std::clog << "test_write_payload_big passed" << std::endl;
}