Search code examples
c++xcodexcode12bounds-checker

Xcode Sanitizer How to Enable


Using Xcode 12, Apple Clang Address Sanitizer, I enable... Enable C++ Container Overflow Checks (for both Debug and Release).

I wish Enable C++ Container Overflow Checks to give me run-time warnings, eg. by translating each [] into an .at().

I run a code similar to the below (which comes at the end of a bunch of previous functions calls where the number of elements of V can not be known at compile-time.)

// V is a container with 100 elements, 
// each element is a pointer to objects with a field f;

auto x = V[200]; // gives no error, no bounds checking (but V.size() is 100)
cout << x->f; // gives error, this object is not valid 

I do not get the expected result, no run-time bounds checking is done on V.

Question: Is Enable C++ Container Overflow Checks supposed to give me .at() like bounds checking for [] access? How may I achieve this?


Solution

  • This is the way to do it:

    https://developer.apple.com/documentation/xcode/diagnosing_memory_thread_and_crash_issues_early

    To enable this tool, select Address Sanitizer from the Diagnostics section of the appropriate scheme.

    enter image description here

    Also, other sanitizer checks may be enabled from that menu.

    I have verified that this works, it gives a very detailed account of any violation at runtime.

    Note: The difference from the (non working) approach given in the question is that here the configuration is done in the scheme menu, not in the build settings menu. I have no idea why the latter does not work.