Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Updated references from C11->C23

Some implementations provide a nonportable environment pointer that is valid when main() is called but may be invalidated by operations that modify the environment.

The C Standard, J.5.1 2 [ISO/IEC 9899:20112024], states

In a hosted environment, the main function receives a third argument, char *envp[], that points to a null-terminated array of pointers to char, each of which points to a string that provides information about the environment for this execution of the program (5.1.2.3.2).

Consequently, under a hosted environment supporting this common extension, it is possible to access the environment through a modified form of main():

...

Because envp may no longer point to the current environment, this program has undefined unanticipated behavior.

Compliant Solution (POSIX)

...

Because envp no longer points to the current environment, this program has undefined unanticipated behavior.

Compliant Solution (Windows)

...

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

ENV31-C

Low

Probable

Medium

P4

L3

Automated Detection

Tool

Version

Checker

Description

Astrée
Include Page
Astrée_V
Astrée_V
 Supported
Compass/ROSE




Cppcheck Premium
24.9.0
premium-cert-env31-c

Fully implemented

Helix QAC

Include Page
Helix QAC_V
Helix QAC_V

DF4991, DF4992, DF4993


LDRA tool suite
Include Page
LDRA_V
LDRA_V
118 SFully Implemented
Parasoft C/C++test

Include Page
Parasoft_V
Parasoft_V

BD

CERT_C-

PB-INVENV

ENV31-a

Do not rely on an environment pointer following an operation that may invalidate it

Polyspace Bug Finder

Include Page
Polyspace Bug Finder_V

Implemented

Polyspace Bug Finder

R2018b

_V

CERT C: Rule ENV31-CChecks for environment
Environment
pointer invalidated by previous operation
Call to setenv or putenv family function modifies environment pointed to by pointer
(rule fully covered)

Related Vulnerabilities

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

...

[IEEE Std 1003.1:2013]XSH, System Interfaces, setenv
[ISO/IEC 9899:20112024]J.5.12, "Environment Arguments"
[MSDN]_environ, _wenviron,
getenv, _wgetenv,
_putenv_s, _wputenv_s

...