Runtime array bounds checking in C++ built with g++

Viewed 15808

Is there any way to do array bounds checking in C++ compiled using g++?

Ideally, the source code shouldn't be modified in any way. Using std::vector, std::tr1::array or boost::array is not an option because the codebase is large and such shift would be infeasible.

3 Answers

There is a Valgrind tool called SGCheck (formerly known as Ptrcheck) that does check stack array bounds overrun.

valgrind --tool=exp-sgcheck <program> <arguments>

The tool is still labeled experimental and it comes with several limitations. One of them is:

Platforms: the stack/global checks won't work properly on PowerPC, ARM or S390X platforms, only on X86 and AMD64 targets. That's because the stack and global checking requires tracking function calls and exits reliably, and there's no obvious way to do it on ABIs that use a link register for function returns.

Related