Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions runtime/src/shader_library.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ class BitReader {
return true;
}

// After align(), take up to length bytes, including the available prefix at EOF.
std::span<const std::uint8_t> take_aligned(std::size_t length) {
const auto bytes = in_.subspan(pos_, std::min(length, in_.size() - pos_));
pos_ += bytes.size();
return bytes;
}

std::size_t position() const { return pos_ - std::size_t(count_ / 8); }
bool failed() const { return failed_; }

Expand Down Expand Up @@ -285,11 +292,9 @@ inline bool zlib_inflate(std::span<const std::uint8_t> in, std::vector<std::uint
const unsigned length = b[0] | (b[1] << 8), complement = b[2] | (b[3] << 8);
if (length != (~complement & 0xffff)) return fail(ZlibError::data);
if (out.size() > limit || length > limit - out.size()) return fail(ZlibError::limit);
for (unsigned i = 0; i < length; ++i) {
std::uint8_t x;
if (!reader.byte(x)) return fail(ZlibError::data);
out.push_back(x);
}
const auto payload = reader.take_aligned(length);
out.insert(out.end(), payload.begin(), payload.end());
if (payload.size() != length) return fail(ZlibError::data);
} else if (type == 1) { // fixed Huffman
static const auto tables = [] {
std::pair<detail::Huffman, detail::Huffman> t;
Expand Down
12 changes: 11 additions & 1 deletion runtime/test/shader_library_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ int huffman_lut_checks() {
w.write_byte(std::uint8_t(n >> 8));
w.write_byte(std::uint8_t(nlen));
w.write_byte(std::uint8_t(nlen >> 8));
const std::size_t payload_at = w.bytes.size();
for (char c : mid) {
w.write_byte(std::uint8_t(c));
expected.push_back(std::uint8_t(c));
Expand All @@ -326,7 +327,16 @@ int huffman_lut_checks() {
CHECK(zlib_inflate(w.bytes, out, expected.size()) && out == expected);
for (std::size_t cut = 0; cut < w.bytes.size(); ++cut) {
std::vector<std::uint8_t> trunc(w.bytes.begin(), w.bytes.begin() + cut);
CHECK(!zlib_inflate(trunc, out, expected.size()));
ZlibError why{};
CHECK(!zlib_inflate(trunc, out, expected.size(), &why));
if (cut >= payload_at && cut < payload_at + n) {
CHECK(why == ZlibError::data);
const auto prefix_end = expected.begin() + k + (cut - payload_at);
CHECK(std::equal(out.begin(), out.end(), expected.begin(), prefix_end));
// The output limit applies to the whole declared block before consuming its payload.
CHECK(!zlib_inflate(trunc, out, k + n - 1, &why) && why == ZlibError::limit);
CHECK(std::equal(out.begin(), out.end(), expected.begin(), expected.begin() + k));
}
}
}
}
Expand Down
Loading