OK, code geeks - way back when I was freelancing (20-noughties), I used a tool called 'purify' as a C compiler preprocessor, and it could spot buffer overruns (and much more, but those were the biggies for what we were doing at the time).
Later (couple of decades?) 'purify' was gone having been, I think bought out by IBM and then killed?
Anyway, later, there was a tool called 'valgrind', which I never got along with, and maybe could have done this, but the learning curve was silly-steep.
There *must* be something out right now in the FOSS community which can do this on a Linux box, no?