Quantcast
Channel: User Schwern - Stack Overflow
Viewing all articles
Browse latest Browse all 581

Answer by Schwern for Measuring actual indefiniteness of undefined behavior of memory accession after free() call in C

$
0
0

Can anyone provide me with evidence that this simple code from cases 4-5 can actually show an undefined behavior at any circumstances? On any machine controlled by any OS inside any program.

Exhibit A. At the time of this writing, instead of printing lines 0-99 it doesn't even print 99 newlines. Because it is undefined behavior its behavior may change at any time.

enter image description here

The behavior depends on the malloc/free implementation, your compiler, compiler flags, the particulars of the memory layout when the program starts, how the program perturbs its heap memory by allocating and freeing blocks of memory, your operating system, and hardware. If you're curious to know more, look at how your C code is translated into assembly code.

I don't believe the CPU load matters at all, nor does the time between your allocation and freeing matters. In a modern operating system with memory protection, what other processes are doing shouldn't perturb another process's memory. But, again, this depends on the OS and malloc/free implementation.

Some operating systems will deliberately randomize key memory locations. This makes memory-based undefined behavior even more unpredictable, which is the goal; it makes attacks which rely on certain types of undefined behavior more difficult.


Viewing all articles
Browse latest Browse all 581

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>