Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(Potentially) incorrect use of getenv function #2152

Open
maxirmx opened this issue Nov 17, 2023 · 0 comments
Open

(Potentially) incorrect use of getenv function #2152

maxirmx opened this issue Nov 17, 2023 · 0 comments

Comments

@maxirmx
Copy link
Member

maxirmx commented Nov 17, 2023

getenv function is not required to be thread-safe.
Another call to getenv, as well as a call to the POSIX functions setenv(), unsetenv(), and putenv()
may invalidate the pointer returned by a previous call or modify the string obtained from a previous call.

Sample incorrect use, cause memory corruption on Windows
in TEST_F(rnp_tests, test_cli_logname)

    char *      logname = getenv("LOGNAME");

...
    setenv("LOGNAME", testname.c_str(), 1);   <===== this call may invalidate the pinter save in logname
    assert_string_equal(getenv_logname(), testname.c_str());
    if (user) {
        unsetenv("LOGNAME");
        assert_string_equal(getenv_logname(), user);
    }

    if (logname) {
        setenv("LOGNAME", logname, 1);
    } else {
        unsetenv("LOGNAME");
    }

Sample correct use:
TEST_F(rnp_tests, test_ffi_set_log_fd)

    char *saved_env = NULL;
    if (getenv(RNP_LOG_CONSOLE)) {
        saved_env = strdup(getenv(RNP_LOG_CONSOLE));
    }

getenv is used in 8 cpp files. Needs review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant