# Memory corruption due to FSkeletalMeshMerge::GenerateLODModel writing out of bounds in certain cases

**URL:** <https://forums.unrealengine.com/t/memory-corruption-due-to-fskeletalmeshmerge-generatelodmodel-writing-out-of-bounds-in-certain-cases/451630>\
**Category:** Mobile\
**Tags:** unreal-engine, crash, bug-report, editor, CPP, question\
**Created:** [July 3, 2019, 4:43am UTC](https://forums.unrealengine.com/t/memory-corruption-due-to-fskeletalmeshmerge-generatelodmodel-writing-out-of-bounds-in-certain-cases/451630 "2019-07-03T04:43:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rajkosto](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@rajkosto](https://forums.unrealengine.com/u/rajkosto)\
**Post date:** [July 3, 2019, 4:43am UTC](https://forums.unrealengine.com/t/memory-corruption-due-to-fskeletalmeshmerge-generatelodmodel-writing-out-of-bounds-in-certain-cases/451630/1 "2019-07-03T04:43:45Z")

</div>

I was having memory corruption after running FSkeletalMeshMerge::DoMerge on certain mesh combinations (caused TBBMalloc to crash on random new allocations) so i ran the engine with the stomp debug allocator, now it reliably crashes here:

> ```
> else
> 
> ```

```
                {
                    Section.DuplicatedVerticesBuffer.DupVertData = MergeSectionInfo.Section->DuplicatedVerticesBuffer.DupVertData;
                    Section.DuplicatedVerticesBuffer.DupVertIndexData = MergeSectionInfo.Section->DuplicatedVerticesBuffer.DupVertIndexData;
                    uint8* VertData = Section.DuplicatedVerticesBuffer.DupVertData.GetDataPointer();
                    for (uint32 i = 0; i < MergeSectionInfo.Section->NumVertices; ++i)
                    {
                        *((uint32*)(VertData + i * sizeof(uint32))) += CurrentBaseVertexIndex - MergeSectionInfo.Section->BaseVertexIndex;
                    }
                }
                Section.DuplicatedVerticesBuffer.bHasOverlappingVertices = true;

```

the problem is the VertData write out of bounds, i is 356 because it goes from 0 to NumVertices (391) however the array size is only 354 (since thats the size of DupVertData).  
There is some faulty logic here that determines that number of vertices, since DupVertIndexData is indeed sized 391, but DupVertData is not.

---

<div class="post-metadata">

**Author:** ![rajkosto](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@rajkosto](https://forums.unrealengine.com/u/rajkosto)\
**Post date:** [July 3, 2019, 4:55am UTC](https://forums.unrealengine.com/t/memory-corruption-due-to-fskeletalmeshmerge-generatelodmodel-writing-out-of-bounds-in-certain-cases/451630/2 "2019-07-03T04:55:10Z")

</div>

Sigh…

This was fixed in [https://github.com/EpicGames/UnrealEngine/commit/3c281c4ca5289802b741050f81117b88a52868ee#diff-4307661913a7b8476f50603ebd8eb3e1](https://github.com/EpicGames/UnrealEngine/commit/3c281c4ca5289802b741050f81117b88a52868ee#diff-4307661913a7b8476f50603ebd8eb3e1)  
It just never made it into release.

Always good when master is MORE STABLE than release.
