Skip to content
Permalink
Browse files
Merge pull request #9340 from AdmiralCurtiss/truncated-shadercache
Common/LinearDiskCache: Handle truncated shadercache files.
  • Loading branch information
lioncash committed Dec 20, 2020
2 parents eb6fd56 + f5170dc commit c08fab6
Showing 1 changed file with 9 additions and 6 deletions.
@@ -6,6 +6,7 @@

#include <algorithm>
#include <cstring>
#include <memory>
#include <string>
#include <type_traits>

@@ -72,24 +73,26 @@ class LinearDiskCache
// good header, read some key/value pairs
K key;

V* value = nullptr;
std::unique_ptr<V[]> value = nullptr;
u32 value_size = 0;
u32 entry_number = 0;
u64 last_valid_value_start = m_file.Tell();

while (m_file.ReadArray(&value_size, 1))
{
const u64 next_extent = m_file.Tell() + sizeof(value_size) + value_size;
if (next_extent > file_size)
break;

delete[] value;
value = new V[value_size];
// TODO: use make_unique_for_overwrite in C++20
value = std::unique_ptr<V[]>(new V[value_size]);

// read key/value and pass to reader
if (m_file.ReadArray(&key, 1) && m_file.ReadArray(value, value_size) &&
if (m_file.ReadArray(&key, 1) && m_file.ReadArray(value.get(), value_size) &&
m_file.ReadArray(&entry_number, 1) && entry_number == m_num_entries + 1)
{
reader.Read(key, value, value_size);
last_valid_value_start = m_file.Tell();
reader.Read(key, value.get(), value_size);
}
else
{
@@ -99,8 +102,8 @@ class LinearDiskCache
m_num_entries++;
}
m_file.Clear();
m_file.Seek(last_valid_value_start, SEEK_SET);

delete[] value;
return m_num_entries;
}

0 comments on commit c08fab6

Please sign in to comment.