Avoid setting a pointer to the address of a local variable defined in a lower scope | CAST Appmarq

Avoid setting a pointer to the address of a local variable defined in a lower scope

CRITICAL

Rule Definition
When a variable goes out of scope, all pointers or references to this variable become invalid. Any access to this data results in undefined behavior. The best way to make sure this situation does not occur is to have the pointer itself go out of scope at the same time as the pointed-to variable.

Remediation
Try to declare the local variable and the pointer in the same scope to avoid protection failures.

Violation Code Sample
int *p;

void f()
{
 int i = 0;
 p = &i; // Violation
}

Related Technologies
C++

Health Factor

  Security


Technical Criterion
Programming Practices - Unexpected Behavior

About CAST Appmarq

CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.

Benchmark Statistics

Global Compliance

99.99%

Total Violations
263
Total Opportunities
1,757,367
Average Violations / App.
2.50
The compliance score represents 1 minus the ratio between the number of times a rule has been violated compared to the number of opportunities in a set of applications that the rule could have been violated.

Industry Insights

Insurance

99.74%

Software ISV

99.97%

Financial Services

99.95%