Handling Null Pointers on AIX Using GCC C

We have code written in C that sometimes does not handle null pointers very well.

The code was originally written in Solaris, and such pointers cause a segmentation error. Not perfect, but better than plowing.

Our experience is that if you read the null pointer to AIX, you get 0. If you use the xlc compiler, you can add the -qcheck=all option to catch these pointers. But we use gcc (and want to continue using this compiler). Does gcc provide such a parameter?

+7
source share
2 answers

Does gcc provide such a parameter?

I shyly volunteered the answer is no, it is not . Although I can not refer to the lack of information about gcc and NULL runtime checks.

The problem you are solving is that you are trying to make undefined behavior a little more defined in a poorly written program.

I recommend that you bite the bullet and either switch to xlc or manually add NULL to the code until the wrong behavior is found and removed.

Consider:

  • Make a macro for null pointer check
  • Adding this macro after pointer assignments
  • Adding this macro to an entry point for functions that take pointers

As errors are removed, you can remove these checks.

+6
source
  • Please do us a favor and add the correct NULL checks to your code. Not only will you have a small performance gain by checking NULL only when necessary, rather than having the compiler check everywhere, but your code will be more portable for other platforms.

    And we will not mention that you are more likely to print the correct error message, instead of having the compiler delete some obscure code / dump of the source code / source code, which will not help your users at all.

  • AIX uses the concept of a NULL page. Essentially, NULL (i.e., Virtual Address 0x0 ) maps to a location containing a whole bunch of zeros. This allows you to use the string manipulation code etc to continue, even though a NULL pointer is encountered.

    This is contrary to most other Unix-like systems, but it does not violate the C standard, which considers dereferencing NULL a undefined. In my opinion, this is terribly violated: this requires an application that breaks sharply and turns it into one that silently ignores programming errors, which can lead to completely incorrect results.

  • As far as I know, GCC is not able to work with fundamentally broken code. Even historically supported patterns, such as writable string literals, have been phased out in newer versions of GCC.

    There may be some support when using memory debugging options such as -fmudflap , but I really don't know - in any case, you should not use debugging code on production systems, especially to make corrupted code work.

Bottom line: I don't think you can avoid adding explicit NULL checks.

Unfortunately, now we move on to the main question: where should NULL checks be added ?. I believe that the compiler will add such checks indiscriminately, it will help if you add an explicit check when you find a problem.

Unfortunately, there is no Valgrind support for AIX. If you have money, you can take a look at IBM Rational Purify Plus for AIX - this can cause such errors.

You can also use xlc in the test system and gcc for everything else, but, unfortunately, they are not fully compatible.

+2
source

All Articles