Skip to content

Commit

Permalink
guard against oob possible read
Browse files Browse the repository at this point in the history
it is theoretically possible that a malformed input could cause this function to read out of bounds, so i rewrote it to be safer
  • Loading branch information
Ryan-rsm-McKenzie committed Apr 25, 2022
1 parent 5ff40da commit e686780
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/bsa/detail/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,13 @@ namespace bsa::detail
auto read_zstring(detail::istream_t& a_in)
-> std::string_view
{
const std::string_view result{
reinterpret_cast<const char*>(a_in->read_bytes(1u).data())
};
a_in->seek_relative(result.length()); // include null terminator
return result;
const auto first = reinterpret_cast<const char*>(a_in->read_bytes(1u).data());
auto last = first;
do {
++last;
} while (std::get<0>(a_in->read<char>()) != '\0');

return { first, last };
}

void write_bzstring(detail::ostream_t& a_out, std::string_view a_string) noexcept
Expand Down

0 comments on commit e686780

Please sign in to comment.