Undefining freed stack space

I have been guilty in the past of accessing out-of-scope data. It fortunately hasn’t happened recently, but I know I sometimes have not immediately noticed, when I did. Delayed noticing in particular happened, when the data was accessed directly after the function returned.

There have been cases where I only noticed that there was a problem, when the code contained an intermediate function call, that in turn overwrote a small part of the stack, before the program accessed the out-of-scope data. A call that caused some of the data to contain garbage.

Is there some way to have a debug build, that includes code that overwrites any just freed stack space, with some recognisable value ( 0xAA bytes or maybe something else (0xBB) to distinguish it from “normal” undefined memory ). Since the compiler knows how much stack space to reserve, it could use the same amount for overwriting, so it doesn’t seem difficult to do that.

If that stack space was overwritten on function exit, then even using the data directly after it went out-of-scope (i.e. without intermediate calls to other functions), would give corrupted/unexpected data and cause me to take another look at the the faulty code.

There are some stack related compiler options -fstack-check and -fstack-protector, but those don’t do the above.

Would that be a useful option to have? Or is there another way to detect out-of-scope access without resorting to a debugger?

Just to be clear: this is not about Initializing Stack Buffers: std.mem.zeroes vs. undefined. Which would typically happen at the start of a function call, and not after it returns (which is what I am intersted in).

There was a proposal to do exactly this, fill the stack frame with 0xAA in debug mode (same value used by undefined in safe modes).

Unfortunately I cant seem to find the proposal.