Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gta-core-five): crash in out bound alternate variation cache #3195

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
20 changes: 12 additions & 8 deletions code/components/gta-core-five/src/PedAlternateVariationCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,24 @@ static void AddAlternateVariationsCacheEntry(TAlternateVariationsCache* cacheMap

static void LoadAlternateVariationSwitches(TAlternateVariationsSwitchSet* switchSet, atArray<AlternateVariationsSwitchAsset>* outArray)
{
uint16_t index = 0;
outArray->m_count = 0;

for (auto cacheEntry : *switchSet)
{
// outArray is stored on stack so we want
// to prevent atArray->Set from expanding it
// and possiblity causing issues
if (outArray->m_count >= outArray->GetSize())
{
trace("Overflowing alternates array max=%i dlcNameHash=%08X\n", outArray->GetSize(), cacheEntry->data.dlcNameHash);
continue;
}

// rough but will work
auto switchAsset = *(AlternateVariationsSwitchAsset*)&cacheEntry->data;
outArray->Set(index++, std::move(switchAsset));
assert(outArray->m_count < outArray->GetSize());
outArray->Set(outArray->m_count++, std::move(switchAsset));
}

outArray->m_count = index;
}

static void ClearAlternateVariationsCache()
Expand Down Expand Up @@ -139,8 +147,6 @@ static bool GetAlternateVariationSwitchesByIndex(AlternateVariationsPed* pedEntr
return g_origGetAlternateVariationSwitchesByIndex(pedEntry, component, index, dlcNameHash, outArray);
}

outArray->m_count = 0;

if (auto cachedSwitches = GetAlternateVariationsCacheEntry(&g_cachedAlternatesByIndex, pedEntry->name, dlcNameHash, component, index))
{
LoadAlternateVariationSwitches(cachedSwitches, outArray);
Expand All @@ -158,8 +164,6 @@ static bool GetAlternateVariationSwitchesByAnchor(AlternateVariationsPed* pedEnt
return g_origGetAlternateVariationSwitchesByAnchor(pedEntry, component, anchor, dlcNameHash, outArray);
}

outArray->m_count = 0;

if (auto cachedSwitches = GetAlternateVariationsCacheEntry(&g_cachedAlternatesByAnchor, pedEntry->name, dlcNameHash, component, anchor))
{
LoadAlternateVariationSwitches(cachedSwitches, outArray);
Expand Down
Loading