Search code examples
c++visual-studiopointersstatic-analysislarge-address-aware

Detect pointer arithmetics because of LARGEADDRESSAWARE


I would like to switch my application to LARGEADDRESSAWARE. One of issues to watch for is pointer arithmetic, as pointer difference can no longer be represented as signed 32b.

Is there some way how to find automatically all instances of pointer subtraction in a large C++ project?

If not, is there some "least effort" manual or semi-automatic method how to achieve this?


Solution

  • As our code already compiles with GCC, I think perhaps the fastest way might be:

    • build a GCC
    • create a custom modification of GCC so that it prints warning (or error) whenever pointer subtraction is detected
    • build the project and gather all warnings about pointer subtraction

    Here is the outline of changes which need to be done to GCC for this:

    Add your warnings into:

    • c-typeck.c (pointer_diff function)
    • cp/typeck.c (pointer_diff function).

    Besides of directly detecting pointer subtraction, another thing to do can be to detect cases where you first convert pointers to integral types and then subtract them. This may be more difficult depending on how is your code structured, in out case regexp search for (.intptr_t).-.*-(.*intptr_t) has worked quite well.