C ++ code porting: handling uninitialized pointers

According to the title, I plan to move some old code developed ten years ago for AIX. The problem is that the code base is huge. Developers did not initialize their pointers in the source code. Now, transferring the code to the latest servers, I see some problems with it.

I know that the best solution is to skip all the code and initialize all the variables that are needed. However, I just want to know if there are other solutions available for this problem. I tried Google, but could not find a suitable answer.

+7
source share
2 answers

The most preventive long-term approach is to initialize all pointers in the declared place, change the code to use the corresponding smart pointers to control the lifetime. If you have any unit tests, this refactoring can be relatively painless.

In a shorter time, and if you port to Linux, you can use valgrind and get a good chance of tracking one or two real problems that bite you, giving you time to refactor at a more leisurely pace.

+2
source

Just initializing all the variables may not be a good idea.

Reliable behavior usually depends on variables that have values ​​that are known to be correct ("guaranteed by design" are correct). The problem with uninitialized variables is not just that they have unknown values. Obviously, obscurity is a problem, but again, the desired state has known and correct meanings. Initializing a variable to a known value, which is incorrect, does not provide reliable behavior.

It often happens that there is no default value that is correctly used as a backup if more complex initialization fails. A program may choose not to initialize a variable with a value if that value should be overwritten before using the variable.

Initializing a variable to its default value can have several problems in such cases. Often the default values ​​are harmless, because if they are used, the consequences are not immediately obvious. This is usually not desirable, because as a developer you want to notice when something goes wrong. You can avoid this problem by choosing default values ​​that will have obvious consequences, but this will not solve the second problem; Static analyzers can often detect and report when an uninitialized variable is used. If there is a problem with some complicated initialization logic, so that no value is set, you want it to be detectable. Setting the default value prevents static analysis when such cases are detected. Thus, there are times when you do not want to initialize variables.


With pointers, the default value is usually nullptr , which to some extent avoids the first problem, which was discussed above, since dereferencing a null pointer usually leads to an immediate failure (useful for debugging). However, the code may also detect a null pointer and report an error (good for debugging) or may return to another method (bad for debugging). You might be better off using static analysis to detect the use of uninitialized pointers, rather than initializing them. Although static analysis may detect dereferencing of null pointers, it does not detect when null pointers cause error messages or helper procedures to be used.


In response to your comment:

The main problems that I see

  • Pointers to local variables are returned from functions.
  • Almost all pointer variables are not initialized. I am sure that AIX really provides this comfort for the client on an earlier platform, but I really doubt that the code will work flawlessly on Linux when it is tested (Production).
  • I cannot implement partial decisions that may work. I prefer to give the best to my client, who pays me for his work. Therefore, do not prefer to use workarounds.
  • Quality cannot be compromised.
  • fix them (and pay special attention to proper cleaning)
  • As I say above, simply the absence of an initializer is not a defect in itself. There is only a defect if an uninitialized value is actually used illegally. I'm not sure what you mean by AIX providing comfort.
  • As I argue above, a “partial solution” and a “workaround” would have to blindly initialize everything.
  • Again, blind initialization of everything can lead not only to useless work, but also to a real decrease in quality, taking some tools for detecting errors.
+2
source

All Articles