3 ms·
Option 4: * Code up a unit test that replicates end of buffer scenarios and verifies that X bytes is enough and X-1 bytes fails.
by ben509 6y ago
Option 4:
* Code up a unit test that replicates end of buffer scenarios and verifies that X bytes is enough and X-1 bytes fails.
- zabzonk 6y agoReading or writing past the end of a buffer in C is undefined behaviour - you cannot therefore write a test in C to check this, as you have no idea how such a test would behave.
- lmm 6y agoFind a compiler vendor who offers stronger guarantees than the bare minimum that's in the standard, and run the test suite with their compiler. Undefined behaviour was supposed to offer compiler vendors space to improve, not to be the limit of what was implemented.
- scatters 6y agoActually it's more likely than not that your vendor already offers those guarantees as a compiler mode. In clang and gcc it's the sanitizers (ubsan, asan, tsan and msan) while in MSVC it's RTC and SDL. These build modes catch the vast majority of undefined behavior, and with relatively low performance overhead. If you're not running your tests with sanitizers enabled, you're missing out on a nearly free way to locate and eliminate an entire class of bugs.
- woofie11 6y agoNope. There are plenty of C/C++ tools which do this sort of test. This was a popular one for a while, back when C++ was still used for major new applications: https://en.wikipedia.org/wiki/BoundsChecker https://en.wikipedia.org/wiki/BoundsChecker There are lots of techniques for this. For example, you can instrument malloc() to allocate 10 extra bytes, put a specific known value there, and confirm it didn't change. There are much fancier ones too, which make clever use of hardware features like MXP. It's a whole research field.