Coverage Report

Created: 2026-01-25 06:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/getenv.c
Line
Count
Source
1
/***************************************************************************
2
 *                                  _   _ ____  _
3
 *  Project                     ___| | | |  _ \| |
4
 *                             / __| | | | |_) | |
5
 *                            | (__| |_| |  _ <| |___
6
 *                             \___|\___/|_| \_\_____|
7
 *
8
 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9
 *
10
 * This software is licensed as described in the file COPYING, which
11
 * you should have received as part of this distribution. The terms
12
 * are also available at https://curl.se/docs/copyright.html.
13
 *
14
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15
 * copies of the Software, and permit persons to whom the Software is
16
 * furnished to do so, under the terms of the COPYING file.
17
 *
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
 * KIND, either express or implied.
20
 *
21
 * SPDX-License-Identifier: curl
22
 *
23
 ***************************************************************************/
24
#include "curl_setup.h"
25
26
char *curl_getenv(const char *variable)
27
0
{
28
#if defined(CURL_WINDOWS_UWP) || \
29
  defined(__ORBIS__) || defined(__PROSPERO__) /* PlayStation 4 and 5 */
30
  (void)variable;
31
  return NULL;
32
#elif defined(_WIN32)
33
  /* This uses Windows API instead of C runtime getenv() to get the environment
34
     variable since some changes are not always visible to the latter. #4774 */
35
  char *buf = NULL;
36
  char *tmp;
37
  DWORD bufsize;
38
  DWORD rc = 1;
39
  const DWORD max = 32768; /* max env var size from MSCRT source */
40
41
  for(;;) {
42
    tmp = curlx_realloc(buf, rc);
43
    if(!tmp) {
44
      curlx_free(buf);
45
      return NULL;
46
    }
47
48
    buf = tmp;
49
    bufsize = rc;
50
51
    /* it is possible for rc to be 0 if the variable was found but empty.
52
       Since getenv does not make that distinction we ignore it as well. */
53
    rc = GetEnvironmentVariableA(variable, buf, bufsize);
54
    if(!rc || rc == bufsize || rc > max) {
55
      curlx_free(buf);
56
      return NULL;
57
    }
58
59
    /* if rc < bufsize then rc is bytes written not including null */
60
    if(rc < bufsize)
61
      return buf;
62
63
    /* else rc is bytes needed, try again */
64
  }
65
#else
66
0
  char *env = getenv(variable);
67
0
  return (env && env[0]) ? curlx_strdup(env) : NULL;
68
0
#endif
69
0
}