The libmemcached documentation has this note:
You may wish to avoid using memcached_create(3) or memcached_clone(3) with a stack based allocation. The most common issues related to ABI safety involve heap allocated structures.
How do I have to understand it? Stands in this case ABI for Application Binary Interface
? I would like to provide the pointer to the struct for memcached_clone()
from a variable in the local function scope. Is it safe?
Example - is this code OK?
foo()
{
memcached_st clone_memc;
memcached_clone(&clone_memc, master_memc);
// some memcache function calls (get/set)
...
memcached_free(&clone_memc);
}
Yes, ABI stands for application binary interface
.
I can think of three reasons to avoid stack allocations. In ascending order of relevance:
Stack smashing on overflow may be more straightforward to exploit than heap overflow.
Lifetime of allocation ends when the scope of the creating function is left, not keeping this in mind this is a fairly common error for C beginners.
Stack size limits are generally rather small (~8kb), allocating significant amounts of storage on the stack may overflow it.