Detect pointer arithmetics because of LARGEADDRESSAWARE

拜拜、爱过 提交于 2019-12-30 04:32:09

问题


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?


回答1:


PC-Lint can find this kind of problem.

Look at http://gimpel-online.com/MsgRef.html, error code 947:

Subtract operator applied to pointers -- An expression of the form p - q was found where both p and q are pointers. This is of special importance in cases where the maximum pointer can overflow the type that holds pointer differences. For example, suppose that the maximum pointer is 3 Gigabytes -1, and that pointer differences are represented by a long, where the maximum long is 2 Gigabytes -1. Note that both of these quantities fit within a 32 bit word. Then subtracting a small pointer from a very large pointer will produce an apparent negative value in the long representing the pointer difference. Conversely, subtracting a very large pointer from a small pointer can produce a positive quantity.




回答2:


Compile the code with a 64 bit compiler and Wp64 turned on.

Because pointers are 64bit wide, but int, long, DWORD etc. stay 32 bit wide, you get warnings for shorting a ptrdiff_t to a int32_t




回答3:


This is only a problem if you have 2 pointers that are more than 2000 million bytes (2GB) apart. This means that you:

  • either have very large arrays (> 2GB)
  • or you are subtracting pointers that point to totally different structures

So look for these special cases.

I think that in most cases this is not a problem.




回答4:


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.




回答5:


For whatever it's worth, I went through the Microsoft compiler warning docs for VS2017 and searched for all warnings referring to "signed", "trunc" (truncation) and "conv" (conversion), which were higher than warning level 1. Then I explicitly enabled those warnings for all projects in our solution via a propsheet. To enable specific warnings, go to "C/C++ / Command Line / Additional Options" and add them in the format /wL####, where L is the warning level you want to assign them to and #### is the warning number.

So what I came up with is this list:

/w14365 /w14018 /w14146 /w14245 /w14092 /w14287 /w14308 /w14388 /w14389 /w14757 /w14807 /w14302 /w14305 /w14306 /w14307 /w14308 /w14309 /w14310 /w14311 /w14312 /w14051 /w14055 /w14152 /w14239 /w14223 /w14242 /w14243 /w14244 /w14254 /w14267 /w14333 /w14334 /w14367 /w14686 /w14826

Note that I used /w1 because our global warning level is already down at 1 (don't judge me, it's legacy). So some of these warnings are already enabled when you have set the default warning level of 3 or higher.

This resulted in more than 88000 warnings, most of which were about using int instead of size_t in code using the STL and about conversions regarding Windows API types like handles, WPARAMs and UINT_PTRs and such. I only found a few warnings related to actual pointer arithmetics in a 3rd party library, but those looked alright in context.

Anyway, I thought this list of related warnings might help someone.

Also, use the tools described in this answer: https://stackoverflow.com/a/22745579/9635694

Another option is to run the builtin code analysis according to the CppCoreGuideLines. Go to "Main menu / Analyze / Configure Code Analysis / For Solution" and select "C++ Core Check Raw Pointer Rules" for all the projects you want to analyze. Then "Main menu / Analyze / Run Code Analysis / For Solution". Beware: Modifies your projects, takes a long time to build and might generate a lot of warnings. You might want to concentrate on C26481 "Don't use pointer arithmetics" and perhaps C26485 "No array-to-pointer decay".



来源:https://stackoverflow.com/questions/3052028/detect-pointer-arithmetics-because-of-largeaddressaware

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!