First-fit behavior
Consider the sample code:
The state of unsorted bin progresses as:
- ‘a’ freed.
- ‘malloc’ request.
head -> a2 -> tail [ ‘a1’ is returned ]
‘a’ chunk is split into two chunks ‘a1’ and ‘a2’ as the requested size (250 bytes) is smaller than the size of the chunk ‘a’ (300 bytes). This corresponds to [6. iii.] in .
Consider the sample code:
The state of the particular fastbin progresses as:
- ‘b’ freed.
- ‘c’ freed.
head -> c -> b -> a -> tail
- ‘d’ freed.
- ‘malloc’ request.
head -> b -> a -> tail [ ‘c’ is returned ]
- ‘malloc’ request.
- ‘malloc’ request.
head -> tail [ ‘a’ is returned ]
The smaller size here (20 bytes) ensured that on freeing, chunks went into fastbins
instead of the bin.
See sample piece of vulnerable code: