GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage-daily/nodes/benchmark/out/../src/node_env_var.cc Lines: 59 62 95.2 %
Date: 2019-02-01 22:03:38 Branches: 23 42 54.8 %

Line Branch Exec Source
1
#include "node_errors.h"
2
#include "node_process.h"
3
#include "util.h"
4
5
#ifdef __APPLE__
6
#include <crt_externs.h>
7
#define environ (*_NSGetEnviron())
8
#elif !defined(_MSC_VER)
9
extern char** environ;
10
#endif
11
12
namespace node {
13
using v8::Array;
14
using v8::Boolean;
15
using v8::Context;
16
using v8::EscapableHandleScope;
17
using v8::Integer;
18
using v8::Isolate;
19
using v8::Local;
20
using v8::MaybeLocal;
21
using v8::Name;
22
using v8::NamedPropertyHandlerConfiguration;
23
using v8::NewStringType;
24
using v8::Object;
25
using v8::ObjectTemplate;
26
using v8::PropertyCallbackInfo;
27
using v8::String;
28
using v8::Value;
29
30
namespace per_process {
31
164
Mutex env_var_mutex;
32
}  // namespace per_process
33
34
38671
static void EnvGetter(Local<Name> property,
35
                      const PropertyCallbackInfo<Value>& info) {
36
38671
  Isolate* isolate = info.GetIsolate();
37
38671
  if (property->IsSymbol()) {
38
36175
    return info.GetReturnValue().SetUndefined();
39
  }
40
38671
  Mutex::ScopedLock lock(per_process::env_var_mutex);
41
#ifdef __POSIX__
42
41167
  node::Utf8Value key(isolate, property);
43
38671
  const char* val = getenv(*key);
44
38671
  if (val) {
45
    return info.GetReturnValue().Set(
46
        String::NewFromUtf8(isolate, val, NewStringType::kNormal)
47
108525
            .ToLocalChecked());
48
2496
  }
49
#else  // _WIN32
50
  node::TwoByteValue key(isolate, property);
51
  WCHAR buffer[32767];  // The maximum size allowed for environment variables.
52
  SetLastError(ERROR_SUCCESS);
53
  DWORD result = GetEnvironmentVariableW(
54
      reinterpret_cast<WCHAR*>(*key), buffer, arraysize(buffer));
55
  // If result >= sizeof buffer the buffer was too small. That should never
56
  // happen. If result == 0 and result != ERROR_SUCCESS the variable was not
57
  // found.
58
  if ((result > 0 || GetLastError() == ERROR_SUCCESS) &&
59
      result < arraysize(buffer)) {
60
    const uint16_t* two_byte_buffer = reinterpret_cast<const uint16_t*>(buffer);
61
    v8::MaybeLocal<String> rc = String::NewFromTwoByte(
62
        isolate, two_byte_buffer, NewStringType::kNormal);
63
    if (rc.IsEmpty()) {
64
      isolate->ThrowException(ERR_STRING_TOO_LONG(isolate));
65
      return;
66
    }
67
    return info.GetReturnValue().Set(rc.ToLocalChecked());
68
  }
69
#endif
70
}
71
72
498
static void EnvSetter(Local<Name> property,
73
                      Local<Value> value,
74
                      const PropertyCallbackInfo<Value>& info) {
75
498
  Environment* env = Environment::GetCurrent(info);
76
  // calling env->EmitProcessEnvWarning() sets a variable indicating that
77
  // warnings have been emitted. It should be called last after other
78
  // conditions leading to a warning have been met.
79


1494
  if (env->options()->pending_deprecation && !value->IsString() &&
80

1494
      !value->IsNumber() && !value->IsBoolean() &&
81
      env->EmitProcessEnvWarning()) {
82
    if (ProcessEmitDeprecationWarning(
83
            env,
84
            "Assigning any value other than a string, number, or boolean to a "
85
            "process.env property is deprecated. Please make sure to convert "
86
            "the "
87
            "value to a string before setting process.env with it.",
88
            "DEP0104")
89
            .IsNothing())
90
498
      return;
91
  }
92
93
498
  Mutex::ScopedLock lock(per_process::env_var_mutex);
94
#ifdef __POSIX__
95
996
  node::Utf8Value key(info.GetIsolate(), property);
96
996
  node::Utf8Value val(info.GetIsolate(), value);
97
498
  setenv(*key, *val, 1);
98
#else  // _WIN32
99
  node::TwoByteValue key(info.GetIsolate(), property);
100
  node::TwoByteValue val(info.GetIsolate(), value);
101
  WCHAR* key_ptr = reinterpret_cast<WCHAR*>(*key);
102
  // Environment variables that start with '=' are read-only.
103
  if (key_ptr[0] != L'=') {
104
    SetEnvironmentVariableW(key_ptr, reinterpret_cast<WCHAR*>(*val));
105
  }
106
#endif
107
  // Whether it worked or not, always return value.
108
1494
  info.GetReturnValue().Set(value);
109
}
110
111
38185
static void EnvQuery(Local<Name> property,
112
                     const PropertyCallbackInfo<Integer>& info) {
113
38185
  Mutex::ScopedLock lock(per_process::env_var_mutex);
114
38185
  int32_t rc = -1;  // Not found unless proven otherwise.
115
76370
  if (property->IsString()) {
116
#ifdef __POSIX__
117
38185
    node::Utf8Value key(info.GetIsolate(), property);
118
38185
    if (getenv(*key)) rc = 0;
119
#else  // _WIN32
120
    node::TwoByteValue key(info.GetIsolate(), property);
121
    WCHAR* key_ptr = reinterpret_cast<WCHAR*>(*key);
122
    SetLastError(ERROR_SUCCESS);
123
    if (GetEnvironmentVariableW(key_ptr, nullptr, 0) > 0 ||
124
        GetLastError() == ERROR_SUCCESS) {
125
      rc = 0;
126
      if (key_ptr[0] == L'=') {
127
        // Environment variables that start with '=' are hidden and read-only.
128
        rc = static_cast<int32_t>(v8::ReadOnly) |
129
             static_cast<int32_t>(v8::DontDelete) |
130
             static_cast<int32_t>(v8::DontEnum);
131
      }
132
    }
133
#endif
134
  }
135
114443
  if (rc != -1) info.GetReturnValue().Set(rc);
136
38185
}
137
138
10
static void EnvDeleter(Local<Name> property,
139
                       const PropertyCallbackInfo<Boolean>& info) {
140
10
  Mutex::ScopedLock lock(per_process::env_var_mutex);
141
20
  if (property->IsString()) {
142
#ifdef __POSIX__
143
10
    node::Utf8Value key(info.GetIsolate(), property);
144
10
    unsetenv(*key);
145
#else
146
    node::TwoByteValue key(info.GetIsolate(), property);
147
    WCHAR* key_ptr = reinterpret_cast<WCHAR*>(*key);
148
    SetEnvironmentVariableW(key_ptr, nullptr);
149
#endif
150
  }
151
152
  // process.env never has non-configurable properties, so always
153
  // return true like the tc39 delete operator.
154
20
  info.GetReturnValue().Set(true);
155
10
}
156
157
423
static void EnvEnumerator(const PropertyCallbackInfo<Array>& info) {
158
423
  Environment* env = Environment::GetCurrent(info);
159
423
  Isolate* isolate = env->isolate();
160
161
423
  Mutex::ScopedLock lock(per_process::env_var_mutex);
162
  Local<Array> envarr;
163
423
  int env_size = 0;
164
#ifdef __POSIX__
165
27617
  while (environ[env_size]) {
166
26771
    env_size++;
167
  }
168
846
  std::vector<Local<Value>> env_v(env_size);
169
170
27194
  for (int i = 0; i < env_size; ++i) {
171
26771
    const char* var = environ[i];
172
26771
    const char* s = strchr(var, '=');
173
26771
    const int length = s ? s - var : strlen(var);
174
26771
    env_v[i] = String::NewFromUtf8(isolate, var, NewStringType::kNormal, length)
175
53542
                   .ToLocalChecked();
176
  }
177
#else  // _WIN32
178
  std::vector<Local<Value>> env_v;
179
  WCHAR* environment = GetEnvironmentStringsW();
180
  if (environment == nullptr) return;  // This should not happen.
181
  WCHAR* p = environment;
182
  while (*p) {
183
    WCHAR* s;
184
    if (*p == L'=') {
185
      // If the key starts with '=' it is a hidden environment variable.
186
      p += wcslen(p) + 1;
187
      continue;
188
    } else {
189
      s = wcschr(p, L'=');
190
    }
191
    if (!s) {
192
      s = p + wcslen(p);
193
    }
194
    const uint16_t* two_byte_buffer = reinterpret_cast<const uint16_t*>(p);
195
    const size_t two_byte_buffer_len = s - p;
196
    v8::MaybeLocal<String> rc = String::NewFromTwoByte(
197
        isolate, two_byte_buffer, NewStringType::kNormal, two_byte_buffer_len);
198
    if (rc.IsEmpty()) {
199
      isolate->ThrowException(ERR_STRING_TOO_LONG(isolate));
200
      FreeEnvironmentStringsW(environment);
201
      return;
202
    }
203
    env_v.push_back(rc.ToLocalChecked());
204
    p = s + wcslen(s) + 1;
205
  }
206
  FreeEnvironmentStringsW(environment);
207
#endif
208
209
423
  envarr = Array::New(isolate, env_v.data(), env_v.size());
210
1269
  info.GetReturnValue().Set(envarr);
211
423
}
212
213
164
MaybeLocal<Object> CreateEnvVarProxy(Local<Context> context,
214
                                     Isolate* isolate,
215
                                     Local<Value> data) {
216
164
  EscapableHandleScope scope(isolate);
217
164
  Local<ObjectTemplate> env_proxy_template = ObjectTemplate::New(isolate);
218
  env_proxy_template->SetHandler(NamedPropertyHandlerConfiguration(
219
328
      EnvGetter, EnvSetter, EnvQuery, EnvDeleter, EnvEnumerator, data));
220
328
  return scope.EscapeMaybe(env_proxy_template->NewInstance(context));
221
}
222

492
}  // namespace node