Environment pointer invalidated by previous operation
Call to setenv
or putenv
family function
modifies environment pointed to by pointer
Description
This defect occurs when you use the third argument of main() in a hosted environment to access the environment after an operation modifies the environment. In a hosted environment, many C implementations support the nonstandard syntax:
main (int argc, char *argv[], char *envp[])
setenv
or putenv
family function
modifies the environment pointed to by *envp
.Risk
When you modify the environment through a call to a setenv
or
putenv
family function, the environment memory can
potentially be reallocated. The hosted environment pointer is not updated and might
point to an incorrect location. A call to this pointer can return unexpected results
or cause an abnormal program termination.
Fix
Do not use the hosted environment pointer. Instead, use global external variable
environ
in Linux®, _environ
or _wenviron
in
Windows®, or their equivalent. When you modify the environment, these variables
are updated.
Examples
Result Information
Group: Programming |
Language: C | C++ |
Default: On for handwritten code, off for generated code |
Command-Line Syntax:
INVALID_ENV_POINTER |
Impact: Medium |
Version History
Introduced in R2018a
See Also
Find defects (-checkers)
| Misuse of return value from nonreentrant standard
function
| Modification of internal buffer returned from
nonreentrant standard function
Topics
- Interpret Bug Finder Results in Polyspace Desktop User Interface
- Interpret Bug Finder Results in Polyspace Access Web Interface (Polyspace Access)
- Address Results in Polyspace User Interface Through Bug Fixes or Justifications
- Address Results in Polyspace Access Through Bug Fixes or Justifications (Polyspace Access)