@@ -241,6 +241,74 @@ void test_read_string(EndianState endian, bool read_offset)
241241 }
242242}
243243
244+ void test_read_overflow ()
245+ {
246+ {
247+ std::string value (" " , 0 );
248+ size_t index = 0 ;
249+ Amulet::BinaryReader reader (value, index);
250+ std::uint32_t int32 = 0 ;
251+ ASSERT_RAISES (std::out_of_range, reader.read_numeric_into <std::uint32_t >(int32))
252+ ASSERT_RAISES (std::out_of_range, reader.read_numeric <std::uint32_t >())
253+ ASSERT_RAISES (std::out_of_range, reader.read_bytes (4 ))
254+ ASSERT_RAISES (std::out_of_range, reader.read_string (4 ))
255+ ASSERT_RAISES (std::out_of_range, reader.read_size_and_bytes ())
256+ ASSERT_RAISES (std::out_of_range, reader.read_size_and_string ())
257+ ASSERT_EQUAL (size_t , 0 , reader.get_position ())
258+ ASSERT_EQUAL (bool , false , reader.has_more_data ())
259+ }
260+ {
261+ std::string value (" \x00\x00 " , 2 );
262+ size_t index = 0 ;
263+ Amulet::BinaryReader reader (value, index);
264+ std::uint32_t int32 = 0 ;
265+ ASSERT_RAISES (std::out_of_range, reader.read_numeric_into <std::uint32_t >(int32))
266+ ASSERT_RAISES (std::out_of_range, reader.read_numeric <std::uint32_t >())
267+ ASSERT_RAISES (std::out_of_range, reader.read_bytes (4 ))
268+ ASSERT_RAISES (std::out_of_range, reader.read_string (4 ))
269+ ASSERT_RAISES (std::out_of_range, reader.read_size_and_bytes ())
270+ ASSERT_RAISES (std::out_of_range, reader.read_size_and_string ())
271+ ASSERT_EQUAL (size_t , 0 , reader.get_position ())
272+ ASSERT_EQUAL (bool , true , reader.has_more_data ())
273+ }
274+ {
275+ std::string value (" \x01\x00\x00\x00 " , 4 );
276+ size_t index = 0 ;
277+ Amulet::BinaryReader reader (value, index);
278+ std::uint32_t int32 = 0 ;
279+ ASSERT_RAISES (std::out_of_range, reader.read_size_and_bytes <std::uint32_t >())
280+ ASSERT_EQUAL (size_t , 4 , reader.get_position ())
281+ ASSERT_EQUAL (bool , false , reader.has_more_data ())
282+ }
283+ {
284+ std::string value (" \x01\x00\x00\x00 " , 4 );
285+ size_t index = 0 ;
286+ Amulet::BinaryReader reader (value, index);
287+ std::uint32_t int32 = 0 ;
288+ ASSERT_RAISES (std::out_of_range, reader.read_size_and_string <std::uint32_t >())
289+ ASSERT_EQUAL (size_t , 4 , reader.get_position ())
290+ ASSERT_EQUAL (bool , false , reader.has_more_data ())
291+ }
292+ {
293+ std::string value (" \x02\x00\x00\x00\x00 " , 5 );
294+ size_t index = 0 ;
295+ Amulet::BinaryReader reader (value, index);
296+ std::uint32_t int32 = 0 ;
297+ ASSERT_RAISES (std::out_of_range, reader.read_size_and_bytes <std::uint32_t >())
298+ ASSERT_EQUAL (size_t , 4 , reader.get_position ())
299+ ASSERT_EQUAL (bool , true , reader.has_more_data ())
300+ }
301+ {
302+ std::string value (" \x02\x00\x00\x00\x00 " , 5 );
303+ size_t index = 0 ;
304+ Amulet::BinaryReader reader (value, index);
305+ std::uint32_t int32 = 0 ;
306+ ASSERT_RAISES (std::out_of_range, reader.read_size_and_string <std::uint32_t >())
307+ ASSERT_EQUAL (size_t , 4 , reader.get_position ())
308+ ASSERT_EQUAL (bool , true , reader.has_more_data ())
309+ }
310+ }
311+
244312void test_write_numeric (EndianState endian)
245313{
246314 Amulet::BinaryWriter writer = endian == EndianState::Big ? Amulet::BinaryWriter (std::endian::big)
@@ -288,6 +356,7 @@ PYBIND11_MODULE(_test_amulet_io, m)
288356
289357 m.def (" test_read_numeric" , &test_read_numeric);
290358 m.def (" test_read_string" , &test_read_string);
359+ m.def (" test_read_overflow" , &test_read_overflow);
291360 m.def (" test_write_numeric" , &test_write_numeric);
292361 m.def (" test_write_string" , &test_write_string);
293362}
0 commit comments