Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Heap corruption: What could the cause be?

I am investigating a crash due to heap corruption. As this issue is non-trivial and involves analyzing the stack and dump results, I have decided to do a code review of files related to the crash.

To be frank, I don't have in-depth knowledge of when the heap could be corrupted.

I would appreciate if you could suggest scenarios which could lead to heap corruption.

Platform: Windows XP

Language: C++

Compiler: VC6

like image 519
Satbir Avatar asked Oct 01 '09 14:10

Satbir


People also ask

How do you find the source of heap corruption?

Check for heap corruptionTry using the Global Flags Utility (gflags.exe) or pageheap.exe. See /windows-hardware/drivers/debugger/gflags-and-pageheap.

How do I know if heap is corrupted?

Then you can sprinkle calls to CheckForHeapCorruption() throughout your code, so that when heap corruption occurs it will be detected at the next call to CheckForHeapCorruption() rather than some time later on.

What does it mean when the memory is corrupted?

Definition: Memory corruption can be described as the vulnerability that may occur in a computer system when its memory is altered without an explicit assignment. The contents of a memory location are modified due to programming errors which enable attackers to execute an arbitrary code.


1 Answers

Common scenarios include:

  • Writing outside the allocated space of an array (char *stuff = new char[10]; stuff[10] = 3;)
  • Casting to the wrong type
  • Uninitialized pointers
  • Typo error for -> and .
  • Typo error when using * and & (or multiple of either)

[EDIT] From the comments, a few more:

  • Mixing new [] and new with delete [] and delete
  • Missing or incorrect copy-constructors
  • Pointer pointing to garbage
  • Calling delete multiple times on the same data
  • Polymorphic baseclasses without virtual destructors
like image 79
cwap Avatar answered Oct 18 '22 15:10

cwap