JavaScript is not currently enabled, but is required for full CodeSonar manual search and browse functionality.

If you are viewing this file in your hub's Web GUI, enable JavaScript in your browser: you will also need it for GUI functionality.

If you opened this file directly from disk, your browser may be directly suppressing JavaScript functionality: certain browsers perform this suppression on local files (but not files delivered by web servers) for security reasons.

CodeSonar® 9.2p0 CONFIDENTIAL CodeSecure Inc
C and C++
Binaries


BADFUNC.PUTENV : Use of putenv

Summary

A use of putenv(), which can cause problems when called with a pointer to a buffer with automatic storage duration. It is usually preferable to use setenv(), which does not have these issues.

Properties

Class Name Use of putenv
Significance style
Mnemonic BADFUNC.PUTENV
Categories
MisraC2025 MisraC2025:18.6 The address of an object with automatic or thread-local storage shall not be copied to another object that persists after the first object has ceased to exist
MisraC2023 MisraC2023:18.6 The address of an object with automatic or thread-local storage shall not be copied to another object that persists after the first object has ceased to exist
Misra2012 Misra2012:18.6 The address of an object with automatic or thread-local storage shall not be copied to another object that persists after the first object has ceased to exist
Misra2004 Misra2004:17.6 The address of an object with automatic storage shall not be assigned to another object that may persist after the first object has ceased to exist
AUTOSARC++14 AUTOSARC++14:M7-5-2 The address of an object with automatic storage shall not be assigned to another object that may persist after the first object has ceased to exist.
MisraC++2008 MisraC++2008:7-5-2 The address of an object with automatic storage shall not be assigned to another object that may persist after the first object has ceased to exist.
MisraC++2023 MisraC++2023:6.8.3 An assignment operator shall not assign the address of an object with automatic storage duration to an object with a greater lifetime
CWE CWE:676 Use of Potentially Dangerous Function
  CWE:758 Reliance on Undefined, Unspecified, or Implementation-Defined Behavior
TS17961 TS17961:5.15-addrescape Escaping of the address of an automatic object
CERT-C CERT-C:POS34-C Do not call putenv() with a pointer to an automatic variable as the argument
OWASP-2021 OWASP-2021:A4 Insecure design
OWASP-2025 OWASP-2025:A06 Insecure Design
Availability Available for C and C++.
Enabling Checks for this warning class are disabled by default. To enable them, add the following WARNING_FILTER rule to the project configuration file.
WARNING_FILTER += allow class="Use of putenv"

Example

#include <stdlib.h>
#include <string.h>

int set_with_putenv(char *val){
    char myarg[35];
    if (!val || strlen(val)>20){ return -1;}
    return putenv(strcat(strcpy(myarg, "MY_ENV_VAR="), val)); /* 'Use of putenv' warning issued here */
                                                              /* The string pointed to by strcat(strcpy(myarg, "MY_ENV_VAR="), val)
                                                               * has been made part of the environment, but that memory may be overwritten after 
                                                               * control returns from set_with_putenv()
                                                               */ 
}

int set_with_setenv(char *val){
    if (!val || strlen(val)>20){ return -1;}
    return setenv("MY_ENV_VAR", val, 1);                                 /* You can set environment variables with setenv()
                                                                          * to avoid problems with putenv(). */
}

Relevant Configuration File Parameters

This class is implemented using a BAD_FUNCTION_* rule set in the general template configuration file.

The following configuration file parameters affect checks for this warning class.

 

To report problems with this documentation, please visit https://support.codesecure.com/.