Coverage Report

Created: 2025-12-10 06:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/getenv.c
Line
Count
Source
1
/*
2
 * Copyright 2018-2022 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
#ifndef _GNU_SOURCE
11
#define _GNU_SOURCE
12
#endif
13
14
#include <stdlib.h>
15
#include "internal/cryptlib.h"
16
#include "internal/e_os.h"
17
18
char *ossl_safe_getenv(const char *name)
19
1
{
20
#if defined(_WIN32) && defined(CP_UTF8) && !defined(_WIN32_WCE)
21
    if (GetEnvironmentVariableW(L"OPENSSL_WIN32_UTF8", NULL, 0) != 0) {
22
        char *val = NULL;
23
        int vallen = 0;
24
        WCHAR *namew = NULL;
25
        WCHAR *valw = NULL;
26
        DWORD envlen = 0;
27
        DWORD dwFlags = MB_ERR_INVALID_CHARS;
28
        int rsize, fsize;
29
        UINT curacp;
30
31
        curacp = GetACP();
32
33
        /*
34
         * For the code pages listed below, dwFlags must be set to 0.
35
         * Otherwise, the function fails with ERROR_INVALID_FLAGS.
36
         */
37
        if (curacp == 50220 || curacp == 50221 || curacp == 50222 || curacp == 50225 || curacp == 50227 || curacp == 50229 || (57002 <= curacp && curacp <= 57011) || curacp == 65000 || curacp == 42)
38
            dwFlags = 0;
39
40
        /* query for buffer len */
41
        rsize = MultiByteToWideChar(curacp, dwFlags, name, -1, NULL, 0);
42
        /* if name is valid string and can be converted to wide string */
43
        if (rsize > 0)
44
            namew = _malloca(rsize * sizeof(WCHAR));
45
46
        if (NULL != namew) {
47
            /* convert name to wide string */
48
            fsize = MultiByteToWideChar(curacp, dwFlags, name, -1, namew, rsize);
49
            /* if conversion is ok, then determine value string size in wchars */
50
            if (fsize > 0)
51
                envlen = GetEnvironmentVariableW(namew, NULL, 0);
52
        }
53
54
        if (envlen > 0)
55
            valw = _malloca(envlen * sizeof(WCHAR));
56
57
        if (NULL != valw) {
58
            /* if can get env value as wide string */
59
            if (GetEnvironmentVariableW(namew, valw, envlen) < envlen) {
60
                /* determine value string size in utf-8 */
61
                vallen = WideCharToMultiByte(CP_UTF8, 0, valw, -1, NULL, 0,
62
                    NULL, NULL);
63
            }
64
        }
65
66
        if (vallen > 0)
67
            val = OPENSSL_malloc(vallen);
68
69
        if (NULL != val) {
70
            /* convert value string from wide to utf-8 */
71
            if (WideCharToMultiByte(CP_UTF8, 0, valw, -1, val, vallen,
72
                    NULL, NULL)
73
                == 0) {
74
                OPENSSL_free(val);
75
                val = NULL;
76
            }
77
        }
78
79
        if (NULL != namew)
80
            _freea(namew);
81
82
        if (NULL != valw)
83
            _freea(valw);
84
85
        return val;
86
    }
87
#endif
88
89
1
#if defined(__GLIBC__) && defined(__GLIBC_PREREQ)
90
1
#if __GLIBC_PREREQ(2, 17)
91
1
#define SECURE_GETENV
92
1
    return secure_getenv(name);
93
1
#endif
94
1
#endif
95
96
#ifndef SECURE_GETENV
97
    if (OPENSSL_issetugid())
98
        return NULL;
99
    return getenv(name);
100
#endif
101
1
}