Type confusion occurs when a program creates a resource—like a pointer, object, or variable—with one data type, but later incorrectly accesses it as a different, incompatible type.
This mismatch can cause the program to misinterpret the data in memory, leading to logical errors, crashes, or unexpected behavior because the resource lacks the properties the code expects. In memory-unsafe languages like C and C++, this often results in dangerous out-of-bounds memory access, corrupting data or creating security vulnerabilities. While commonly seen with C/C++ unions when parsing complex data structures, type confusion can appear in many languages. For instance, PHP might fail if an array is passed where a single value is required, and languages like Perl that perform automatic type conversion can also introduce subtle bugs when variables are accessed as unintended types.
Impact: Read MemoryModify MemoryExecute Unauthorized Code or CommandsDoS: Crash, Exit, or Restart
When a memory buffer is accessed using the wrong type, it could read or write memory out of the bounds of the buffer, if the allocated buffer is smaller than the type that the code is attempting to access, leading to a crash and possibly code execution.
c
/* This particular value for nameID is used to make the code architecture-independent. If coming from untrusted input, it could be any value. /
c
phpperl