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: 157 166 94.6 %
Date: 2019-09-12 22:30:12 Branches: 74 102 72.5 %

Line Branch Exec Source
1
#include "env-inl.h"
2
#include "node_errors.h"
3
#include "node_process.h"
4
5
#include <time.h>  // tzset(), _tzset()
6
7
#ifdef __APPLE__
8
#include <crt_externs.h>
9
#define environ (*_NSGetEnviron())
10
#elif !defined(_MSC_VER)
11
extern char** environ;
12
#endif
13
14
namespace node {
15
using v8::Array;
16
using v8::Boolean;
17
using v8::Context;
18
using v8::EscapableHandleScope;
19
using v8::HandleScope;
20
using v8::Integer;
21
using v8::Isolate;
22
using v8::Just;
23
using v8::Local;
24
using v8::Maybe;
25
using v8::MaybeLocal;
26
using v8::Name;
27
using v8::NamedPropertyHandlerConfiguration;
28
using v8::NewStringType;
29
using v8::Nothing;
30
using v8::Object;
31
using v8::ObjectTemplate;
32
using v8::PropertyCallbackInfo;
33
using v8::PropertyHandlerFlags;
34
using v8::String;
35
using v8::Value;
36
37
9506
class RealEnvStore final : public KVStore {
38
 public:
39
  MaybeLocal<String> Get(Isolate* isolate, Local<String> key) const override;
40
  void Set(Isolate* isolate, Local<String> key, Local<String> value) override;
41
  int32_t Query(Isolate* isolate, Local<String> key) const override;
42
  void Delete(Isolate* isolate, Local<String> key) override;
43
  Local<Array> Enumerate(Isolate* isolate) const override;
44
};
45
46
213
class MapKVStore final : public KVStore {
47
 public:
48
  MaybeLocal<String> Get(Isolate* isolate, Local<String> key) const override;
49
  void Set(Isolate* isolate, Local<String> key, Local<String> value) override;
50
  int32_t Query(Isolate* isolate, Local<String> key) const override;
51
  void Delete(Isolate* isolate, Local<String> key) override;
52
  Local<Array> Enumerate(Isolate* isolate) const override;
53
54
  std::shared_ptr<KVStore> Clone(Isolate* isolate) const override;
55
56
212
  MapKVStore() = default;
57
3
  MapKVStore(const MapKVStore& other) : map_(other.map_) {}
58
59
 private:
60
  mutable Mutex mutex_;
61
  std::unordered_map<std::string, std::string> map_;
62
};
63
64
namespace per_process {
65
4956
Mutex env_var_mutex;
66
4956
std::shared_ptr<KVStore> system_environment = std::make_shared<RealEnvStore>();
67
}  // namespace per_process
68
69
template <typename T>
70
14571
void DateTimeConfigurationChangeNotification(Isolate* isolate, const T& key) {
71


14571
  if (key.length() == 2 && key[0] == 'T' && key[1] == 'Z') {
72
#ifdef __POSIX__
73
4
    tzset();
74
#else
75
    _tzset();
76
#endif
77
4
    auto constexpr time_zone_detection = Isolate::TimeZoneDetection::kRedetect;
78
4
    isolate->DateTimeConfigurationChangeNotification(time_zone_detection);
79
  }
80
14571
}
81
82
942123
MaybeLocal<String> RealEnvStore::Get(Isolate* isolate,
83
                                     Local<String> property) const {
84
942123
  Mutex::ScopedLock lock(per_process::env_var_mutex);
85
86
1884246
  node::Utf8Value key(isolate, property);
87
942123
  size_t init_sz = 256;
88
1884246
  MaybeStackBuffer<char, 256> val;
89
942123
  int ret = uv_os_getenv(*key, *val, &init_sz);
90
91
942123
  if (ret == UV_ENOBUFS) {
92
    // Buffer is not large enough, reallocate to the updated init_sz
93
    // and fetch env value again.
94
    val.AllocateSufficientStorage(init_sz);
95
    ret = uv_os_getenv(*key, *val, &init_sz);
96
  }
97
98
942123
  if (ret >= 0) {  // Env key value fetch success.
99
    MaybeLocal<String> value_string =
100
880288
        String::NewFromUtf8(isolate, *val, NewStringType::kNormal, init_sz);
101
880288
    return value_string;
102
  }
103
104
1003958
  return MaybeLocal<String>();
105
}
106
107
13562
void RealEnvStore::Set(Isolate* isolate,
108
                       Local<String> property,
109
                       Local<String> value) {
110
13562
  Mutex::ScopedLock lock(per_process::env_var_mutex);
111
112
27124
  node::Utf8Value key(isolate, property);
113
27124
  node::Utf8Value val(isolate, value);
114
115
#ifdef _WIN32
116
  if (key[0] == L'=') return;
117
#endif
118
13562
  uv_os_setenv(*key, *val);
119
27124
  DateTimeConfigurationChangeNotification(isolate, key);
120
13562
}
121
122
1397528
int32_t RealEnvStore::Query(Isolate* isolate, Local<String> property) const {
123
1397528
  Mutex::ScopedLock lock(per_process::env_var_mutex);
124
125
2795056
  node::Utf8Value key(isolate, property);
126
#ifdef _WIN32
127
  if (key[0] == L'=')
128
    return static_cast<int32_t>(v8::ReadOnly) |
129
           static_cast<int32_t>(v8::DontDelete) |
130
           static_cast<int32_t>(v8::DontEnum);
131
#endif
132
133
  char val[2];
134
1397528
  size_t init_sz = sizeof(val);
135
1397528
  int ret = uv_os_getenv(*key, val, &init_sz);
136
137
1397528
  if (ret == UV_ENOENT) {
138
5444
    return -1;
139
  }
140
141
2789612
  return 0;
142
}
143
144
1009
void RealEnvStore::Delete(Isolate* isolate, Local<String> property) {
145
1009
  Mutex::ScopedLock lock(per_process::env_var_mutex);
146
147
2018
  node::Utf8Value key(isolate, property);
148
1009
  uv_os_unsetenv(*key);
149
2018
  DateTimeConfigurationChangeNotification(isolate, key);
150
1009
}
151
152
13340
Local<Array> RealEnvStore::Enumerate(Isolate* isolate) const {
153
13340
  Mutex::ScopedLock lock(per_process::env_var_mutex);
154
#ifdef __POSIX__
155
13340
  int env_size = 0;
156
889907
  while (environ[env_size]) {
157
863227
    env_size++;
158
  }
159
26680
  std::vector<Local<Value>> env_v(env_size);
160
161
876567
  for (int i = 0; i < env_size; ++i) {
162
863227
    const char* var = environ[i];
163
863227
    const char* s = strchr(var, '=');
164
863227
    const int length = s ? s - var : strlen(var);
165
863227
    env_v[i] = String::NewFromUtf8(isolate, var, NewStringType::kNormal, length)
166
1726454
                   .ToLocalChecked();
167
  }
168
#else  // _WIN32
169
  std::vector<Local<Value>> env_v;
170
  WCHAR* environment = GetEnvironmentStringsW();
171
  if (environment == nullptr)
172
    return Array::New(isolate);  // This should not happen.
173
  WCHAR* p = environment;
174
  while (*p) {
175
    WCHAR* s;
176
    if (*p == L'=') {
177
      // If the key starts with '=' it is a hidden environment variable.
178
      p += wcslen(p) + 1;
179
      continue;
180
    } else {
181
      s = wcschr(p, L'=');
182
    }
183
    if (!s) {
184
      s = p + wcslen(p);
185
    }
186
    const uint16_t* two_byte_buffer = reinterpret_cast<const uint16_t*>(p);
187
    const size_t two_byte_buffer_len = s - p;
188
    v8::MaybeLocal<String> rc = String::NewFromTwoByte(
189
        isolate, two_byte_buffer, NewStringType::kNormal, two_byte_buffer_len);
190
    if (rc.IsEmpty()) {
191
      isolate->ThrowException(ERR_STRING_TOO_LONG(isolate));
192
      FreeEnvironmentStringsW(environment);
193
      return Local<Array>();
194
    }
195
    env_v.push_back(rc.ToLocalChecked());
196
    p = s + wcslen(s) + 1;
197
  }
198
  FreeEnvironmentStringsW(environment);
199
#endif
200
201
26680
  return Array::New(isolate, env_v.data(), env_v.size());
202
}
203
204
211
std::shared_ptr<KVStore> KVStore::Clone(v8::Isolate* isolate) const {
205
211
  HandleScope handle_scope(isolate);
206
211
  Local<Context> context = isolate->GetCurrentContext();
207
208
211
  std::shared_ptr<KVStore> copy = KVStore::CreateMapKVStore();
209
211
  Local<Array> keys = Enumerate(isolate);
210
211
  uint32_t keys_length = keys->Length();
211
13754
  for (uint32_t i = 0; i < keys_length; i++) {
212
27086
    Local<Value> key = keys->Get(context, i).ToLocalChecked();
213
27086
    CHECK(key->IsString());
214
13543
    copy->Set(isolate,
215
              key.As<String>(),
216
40629
              Get(isolate, key.As<String>()).ToLocalChecked());
217
  }
218
211
  return copy;
219
}
220
221
25723
MaybeLocal<String> MapKVStore::Get(Isolate* isolate, Local<String> key) const {
222
25723
  Mutex::ScopedLock lock(mutex_);
223
51448
  Utf8Value str(isolate, key);
224
25724
  auto it = map_.find(std::string(*str, str.length()));
225
26815
  if (it == map_.end()) return Local<String>();
226
24633
  return String::NewFromUtf8(isolate, it->second.data(),
227
74990
                             NewStringType::kNormal, it->second.size());
228
}
229
230
13548
void MapKVStore::Set(Isolate* isolate, Local<String> key, Local<String> value) {
231
13548
  Mutex::ScopedLock lock(mutex_);
232
27096
  Utf8Value key_str(isolate, key);
233
27096
  Utf8Value value_str(isolate, value);
234

13548
  if (*key_str != nullptr && *value_str != nullptr) {
235
40644
    map_[std::string(*key_str, key_str.length())] =
236
27096
        std::string(*value_str, value_str.length());
237
13548
  }
238
13548
}
239
240
47551
int32_t MapKVStore::Query(Isolate* isolate, Local<String> key) const {
241
47551
  Mutex::ScopedLock lock(mutex_);
242
95102
  Utf8Value str(isolate, key);
243
47551
  auto it = map_.find(std::string(*str, str.length()));
244
95102
  return it == map_.end() ? -1 : 0;
245
}
246
247
void MapKVStore::Delete(Isolate* isolate, Local<String> key) {
248
  Mutex::ScopedLock lock(mutex_);
249
  Utf8Value str(isolate, key);
250
  map_.erase(std::string(*str, str.length()));
251
}
252
253
718
Local<Array> MapKVStore::Enumerate(Isolate* isolate) const {
254
718
  Mutex::ScopedLock lock(mutex_);
255
1436
  std::vector<Local<Value>> values;
256
718
  values.reserve(map_.size());
257
47840
  for (const auto& pair : map_) {
258
    values.emplace_back(
259
        String::NewFromUtf8(isolate, pair.first.data(),
260
47122
                            NewStringType::kNormal, pair.first.size())
261
94244
            .ToLocalChecked());
262
  }
263
1436
  return Array::New(isolate, values.data(), values.size());
264
}
265
266
3
std::shared_ptr<KVStore> MapKVStore::Clone(Isolate* isolate) const {
267
3
  return std::make_shared<MapKVStore>(*this);
268
}
269
270
212
std::shared_ptr<KVStore> KVStore::CreateMapKVStore() {
271
212
  return std::make_shared<MapKVStore>();
272
}
273
274
1
Maybe<bool> KVStore::AssignFromObject(Local<Context> context,
275
                                      Local<Object> entries) {
276
1
  Isolate* isolate = context->GetIsolate();
277
1
  HandleScope handle_scope(isolate);
278
  Local<Array> keys;
279
2
  if (!entries->GetOwnPropertyNames(context).ToLocal(&keys))
280
    return Nothing<bool>();
281
1
  uint32_t keys_length = keys->Length();
282
2
  for (uint32_t i = 0; i < keys_length; i++) {
283
    Local<Value> key;
284
2
    if (!keys->Get(context, i).ToLocal(&key))
285
      return Nothing<bool>();
286
2
    if (!key->IsString()) continue;
287
288
    Local<Value> value;
289
    Local<String> value_string;
290


7
    if (!entries->Get(context, key.As<String>()).ToLocal(&value) ||
291
3
        !value->ToString(context).ToLocal(&value_string)) {
292
      return Nothing<bool>();
293
    }
294
295
2
    Set(isolate, key.As<String>(), value_string);
296
  }
297
1
  return Just(true);
298
}
299
300
940056
static void EnvGetter(Local<Name> property,
301
                      const PropertyCallbackInfo<Value>& info) {
302
940056
  Environment* env = Environment::GetCurrent(info);
303
940057
  if (property->IsSymbol()) {
304
953016
    return info.GetReturnValue().SetUndefined();
305
  }
306
1867154
  CHECK(property->IsString());
307
  MaybeLocal<String> value_string =
308
1867154
      env->env_vars()->Get(env->isolate(), property.As<String>());
309
933576
  if (!value_string.IsEmpty()) {
310
1762172
    info.GetReturnValue().Set(value_string.ToLocalChecked());
311
  }
312
}
313
314
13568
static void EnvSetter(Local<Name> property,
315
                      Local<Value> value,
316
                      const PropertyCallbackInfo<Value>& info) {
317
13568
  Environment* env = Environment::GetCurrent(info);
318
  // calling env->EmitProcessEnvWarning() sets a variable indicating that
319
  // warnings have been emitted. It should be called last after other
320
  // conditions leading to a warning have been met.
321


40735
  if (env->options()->pending_deprecation && !value->IsString() &&
322

40707
      !value->IsNumber() && !value->IsBoolean() &&
323
1
      env->EmitProcessEnvWarning()) {
324
2
    if (ProcessEmitDeprecationWarning(
325
            env,
326
            "Assigning any value other than a string, number, or boolean to a "
327
            "process.env property is deprecated. Please make sure to convert "
328
            "the "
329
            "value to a string before setting process.env with it.",
330
            "DEP0104")
331
2
            .IsNothing())
332
2
      return;
333
  }
334
335
  Local<String> key;
336
  Local<String> value_string;
337


81406
  if (!property->ToString(env->context()).ToLocal(&key) ||
338
54269
      !value->ToString(env->context()).ToLocal(&value_string)) {
339
2
    return;
340
  }
341
342
13566
  env->env_vars()->Set(env->isolate(), key, value_string);
343
344
  // Whether it worked or not, always return value.
345
27132
  info.GetReturnValue().Set(value);
346
}
347
348
1445080
static void EnvQuery(Local<Name> property,
349
                     const PropertyCallbackInfo<Integer>& info) {
350
1445080
  Environment* env = Environment::GetCurrent(info);
351
2890160
  if (property->IsString()) {
352
2890158
    int32_t rc = env->env_vars()->Query(env->isolate(), property.As<String>());
353
4323625
    if (rc != -1) info.GetReturnValue().Set(rc);
354
  }
355
1445080
}
356
357
1010
static void EnvDeleter(Local<Name> property,
358
                       const PropertyCallbackInfo<Boolean>& info) {
359
1010
  Environment* env = Environment::GetCurrent(info);
360
2020
  if (property->IsString()) {
361
2018
    env->env_vars()->Delete(env->isolate(), property.As<String>());
362
  }
363
364
  // process.env never has non-configurable properties, so always
365
  // return true like the tc39 delete operator.
366
2020
  info.GetReturnValue().Set(true);
367
1010
}
368
369
13847
static void EnvEnumerator(const PropertyCallbackInfo<Array>& info) {
370
13847
  Environment* env = Environment::GetCurrent(info);
371
372
  info.GetReturnValue().Set(
373
41541
      env->env_vars()->Enumerate(env->isolate()));
374
13847
}
375
376
5103
MaybeLocal<Object> CreateEnvVarProxy(Local<Context> context,
377
                                     Isolate* isolate,
378
                                     Local<Object> data) {
379
5103
  EscapableHandleScope scope(isolate);
380
5103
  Local<ObjectTemplate> env_proxy_template = ObjectTemplate::New(isolate);
381
  env_proxy_template->SetHandler(NamedPropertyHandlerConfiguration(
382
      EnvGetter, EnvSetter, EnvQuery, EnvDeleter, EnvEnumerator, data,
383
10206
      PropertyHandlerFlags::kHasNoSideEffect));
384
10206
  return scope.EscapeMaybe(env_proxy_template->NewInstance(context));
385
}
386

14868
}  // namespace node