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: 161 172 93.6 %
Date: 2020-02-19 22:14:06 Branches: 70 98 71.4 %

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
namespace node {
8
using v8::Array;
9
using v8::Boolean;
10
using v8::Context;
11
using v8::EscapableHandleScope;
12
using v8::HandleScope;
13
using v8::Integer;
14
using v8::Isolate;
15
using v8::Just;
16
using v8::Local;
17
using v8::Maybe;
18
using v8::MaybeLocal;
19
using v8::Name;
20
using v8::NamedPropertyHandlerConfiguration;
21
using v8::NewStringType;
22
using v8::Nothing;
23
using v8::Object;
24
using v8::ObjectTemplate;
25
using v8::PropertyCallbackInfo;
26
using v8::PropertyHandlerFlags;
27
using v8::String;
28
using v8::Value;
29
30
7967
class RealEnvStore final : public KVStore {
31
 public:
32
  MaybeLocal<String> Get(Isolate* isolate, Local<String> key) const override;
33
  void Set(Isolate* isolate, Local<String> key, Local<String> value) override;
34
  int32_t Query(Isolate* isolate, Local<String> key) const override;
35
  void Delete(Isolate* isolate, Local<String> key) override;
36
  Local<Array> Enumerate(Isolate* isolate) const override;
37
};
38
39
232
class MapKVStore final : public KVStore {
40
 public:
41
  MaybeLocal<String> Get(Isolate* isolate, Local<String> key) const override;
42
  void Set(Isolate* isolate, Local<String> key, Local<String> value) override;
43
  int32_t Query(Isolate* isolate, Local<String> key) const override;
44
  void Delete(Isolate* isolate, Local<String> key) override;
45
  Local<Array> Enumerate(Isolate* isolate) const override;
46
47
  std::shared_ptr<KVStore> Clone(Isolate* isolate) const override;
48
49
228
  MapKVStore() = default;
50
4
  MapKVStore(const MapKVStore& other) : KVStore(), map_(other.map_) {}
51
52
 private:
53
  mutable Mutex mutex_;
54
  std::unordered_map<std::string, std::string> map_;
55
};
56
57
namespace per_process {
58
4186
Mutex env_var_mutex;
59
4186
std::shared_ptr<KVStore> system_environment = std::make_shared<RealEnvStore>();
60
}  // namespace per_process
61
62
template <typename T>
63
6759
void DateTimeConfigurationChangeNotification(Isolate* isolate, const T& key) {
64


6759
  if (key.length() == 2 && key[0] == 'T' && key[1] == 'Z') {
65
#ifdef __POSIX__
66
4
    tzset();
67
#else
68
    _tzset();
69
#endif
70
4
    auto constexpr time_zone_detection = Isolate::TimeZoneDetection::kRedetect;
71
4
    isolate->DateTimeConfigurationChangeNotification(time_zone_detection);
72
  }
73
6759
}
74
75
455947
MaybeLocal<String> RealEnvStore::Get(Isolate* isolate,
76
                                     Local<String> property) const {
77
911894
  Mutex::ScopedLock lock(per_process::env_var_mutex);
78
79
911894
  node::Utf8Value key(isolate, property);
80
455947
  size_t init_sz = 256;
81
911894
  MaybeStackBuffer<char, 256> val;
82
455947
  int ret = uv_os_getenv(*key, *val, &init_sz);
83
84
455947
  if (ret == UV_ENOBUFS) {
85
    // Buffer is not large enough, reallocate to the updated init_sz
86
    // and fetch env value again.
87
    val.AllocateSufficientStorage(init_sz);
88
    ret = uv_os_getenv(*key, *val, &init_sz);
89
  }
90
91
455947
  if (ret >= 0) {  // Env key value fetch success.
92
    MaybeLocal<String> value_string =
93
405708
        String::NewFromUtf8(isolate, *val, NewStringType::kNormal, init_sz);
94
405708
    return value_string;
95
  }
96
97
50239
  return MaybeLocal<String>();
98
}
99
100
6017
void RealEnvStore::Set(Isolate* isolate,
101
                       Local<String> property,
102
                       Local<String> value) {
103
12034
  Mutex::ScopedLock lock(per_process::env_var_mutex);
104
105
12034
  node::Utf8Value key(isolate, property);
106
12034
  node::Utf8Value val(isolate, value);
107
108
#ifdef _WIN32
109
  if (key[0] == L'=') return;
110
#endif
111
6017
  uv_os_setenv(*key, *val);
112
6017
  DateTimeConfigurationChangeNotification(isolate, key);
113
6017
}
114
115
526502
int32_t RealEnvStore::Query(Isolate* isolate, Local<String> property) const {
116
1053004
  Mutex::ScopedLock lock(per_process::env_var_mutex);
117
118
1053004
  node::Utf8Value key(isolate, property);
119
120
  char val[2];
121
526502
  size_t init_sz = sizeof(val);
122
526502
  int ret = uv_os_getenv(*key, val, &init_sz);
123
124
526502
  if (ret == UV_ENOENT) {
125
5162
    return -1;
126
  }
127
128
#ifdef _WIN32
129
  if (key[0] == L'=') {
130
    return static_cast<int32_t>(v8::ReadOnly) |
131
           static_cast<int32_t>(v8::DontDelete) |
132
           static_cast<int32_t>(v8::DontEnum);
133
  }
134
#endif
135
136
521340
  return 0;
137
}
138
139
742
void RealEnvStore::Delete(Isolate* isolate, Local<String> property) {
140
1484
  Mutex::ScopedLock lock(per_process::env_var_mutex);
141
142
1484
  node::Utf8Value key(isolate, property);
143
742
  uv_os_unsetenv(*key);
144
742
  DateTimeConfigurationChangeNotification(isolate, key);
145
742
}
146
147
6165
Local<Array> RealEnvStore::Enumerate(Isolate* isolate) const {
148
12330
  Mutex::ScopedLock lock(per_process::env_var_mutex);
149
  uv_env_item_t* items;
150
  int count;
151
152
18495
  auto cleanup = OnScopeLeave([&]() { uv_os_free_environ(items, count); });
153
6165
  CHECK_EQ(uv_os_environ(&items, &count), 0);
154
155
12330
  MaybeStackBuffer<Local<Value>, 256> env_v(count);
156
6165
  int env_v_index = 0;
157
431958
  for (int i = 0; i < count; i++) {
158
#ifdef _WIN32
159
    // If the key starts with '=' it is a hidden environment variable.
160
    // The '\0' check is a workaround for the bug behind
161
    // https://github.com/libuv/libuv/pull/2473 and can be removed later.
162
    if (items[i].name[0] == '=' || items[i].name[0] == '\0') continue;
163
#endif
164
    MaybeLocal<String> str = String::NewFromUtf8(
165
425793
        isolate, items[i].name, NewStringType::kNormal);
166
425793
    if (str.IsEmpty()) {
167
      isolate->ThrowException(ERR_STRING_TOO_LONG(isolate));
168
      return Local<Array>();
169
    }
170
851586
    env_v[env_v_index++] = str.ToLocalChecked();
171
  }
172
173
6165
  return Array::New(isolate, env_v.out(), env_v_index);
174
}
175
176
206
std::shared_ptr<KVStore> KVStore::Clone(v8::Isolate* isolate) const {
177
412
  HandleScope handle_scope(isolate);
178
206
  Local<Context> context = isolate->GetCurrentContext();
179
180
206
  std::shared_ptr<KVStore> copy = KVStore::CreateMapKVStore();
181
206
  Local<Array> keys = Enumerate(isolate);
182
206
  uint32_t keys_length = keys->Length();
183
14249
  for (uint32_t i = 0; i < keys_length; i++) {
184
28086
    Local<Value> key = keys->Get(context, i).ToLocalChecked();
185
28086
    CHECK(key->IsString());
186
14043
    copy->Set(isolate,
187
              key.As<String>(),
188
42129
              Get(isolate, key.As<String>()).ToLocalChecked());
189
  }
190
412
  return copy;
191
}
192
193
28001
MaybeLocal<String> MapKVStore::Get(Isolate* isolate, Local<String> key) const {
194
56002
  Mutex::ScopedLock lock(mutex_);
195
56003
  Utf8Value str(isolate, key);
196
28002
  auto it = map_.find(std::string(*str, str.length()));
197
29405
  if (it == map_.end()) return Local<String>();
198
26599
  return String::NewFromUtf8(isolate, it->second.data(),
199
53197
                             NewStringType::kNormal, it->second.size());
200
}
201
202
15478
void MapKVStore::Set(Isolate* isolate, Local<String> key, Local<String> value) {
203
30956
  Mutex::ScopedLock lock(mutex_);
204
30956
  Utf8Value key_str(isolate, key);
205
30956
  Utf8Value value_str(isolate, value);
206

15478
  if (*key_str != nullptr && *value_str != nullptr) {
207
30956
    map_[std::string(*key_str, key_str.length())] =
208
46434
        std::string(*value_str, value_str.length());
209
  }
210
15478
}
211
212
50423
int32_t MapKVStore::Query(Isolate* isolate, Local<String> key) const {
213
100846
  Mutex::ScopedLock lock(mutex_);
214
100846
  Utf8Value str(isolate, key);
215
50423
  auto it = map_.find(std::string(*str, str.length()));
216
100846
  return it == map_.end() ? -1 : 0;
217
}
218
219
void MapKVStore::Delete(Isolate* isolate, Local<String> key) {
220
  Mutex::ScopedLock lock(mutex_);
221
  Utf8Value str(isolate, key);
222
  map_.erase(std::string(*str, str.length()));
223
}
224
225
718
Local<Array> MapKVStore::Enumerate(Isolate* isolate) const {
226
1436
  Mutex::ScopedLock lock(mutex_);
227
1436
  std::vector<Local<Value>> values;
228
718
  values.reserve(map_.size());
229
50708
  for (const auto& pair : map_) {
230
    values.emplace_back(
231
99980
        String::NewFromUtf8(isolate, pair.first.data(),
232
49990
                            NewStringType::kNormal, pair.first.size())
233
49990
            .ToLocalChecked());
234
  }
235
1436
  return Array::New(isolate, values.data(), values.size());
236
}
237
238
4
std::shared_ptr<KVStore> MapKVStore::Clone(Isolate* isolate) const {
239
4
  return std::make_shared<MapKVStore>(*this);
240
}
241
242
228
std::shared_ptr<KVStore> KVStore::CreateMapKVStore() {
243
228
  return std::make_shared<MapKVStore>();
244
}
245
246
22
Maybe<bool> KVStore::AssignFromObject(Local<Context> context,
247
                                      Local<Object> entries) {
248
22
  Isolate* isolate = context->GetIsolate();
249
44
  HandleScope handle_scope(isolate);
250
  Local<Array> keys;
251
44
  if (!entries->GetOwnPropertyNames(context).ToLocal(&keys))
252
    return Nothing<bool>();
253
22
  uint32_t keys_length = keys->Length();
254
1453
  for (uint32_t i = 0; i < keys_length; i++) {
255
    Local<Value> key;
256
2862
    if (!keys->Get(context, i).ToLocal(&key))
257
      return Nothing<bool>();
258
2862
    if (!key->IsString()) continue;
259
260
    Local<Value> value;
261
    Local<String> value_string;
262

5724
    if (!entries->Get(context, key.As<String>()).ToLocal(&value) ||
263
4293
        !value->ToString(context).ToLocal(&value_string)) {
264
      return Nothing<bool>();
265
    }
266
267
2862
    Set(isolate, key.As<String>(), value_string);
268
  }
269
22
  return Just(true);
270
}
271
272
458421
static void EnvGetter(Local<Name> property,
273
                      const PropertyCallbackInfo<Value>& info) {
274
458421
  Environment* env = Environment::GetCurrent(info);
275
458421
  CHECK(env->has_run_bootstrapping_code());
276
458421
  if (property->IsSymbol()) {
277
12960
    return info.GetReturnValue().SetUndefined();
278
  }
279
903882
  CHECK(property->IsString());
280
  MaybeLocal<String> value_string =
281
903882
      env->env_vars()->Get(env->isolate(), property.As<String>());
282
451941
  if (!value_string.IsEmpty()) {
283
818782
    info.GetReturnValue().Set(value_string.ToLocalChecked());
284
  }
285
}
286
287
6023
static void EnvSetter(Local<Name> property,
288
                      Local<Value> value,
289
                      const PropertyCallbackInfo<Value>& info) {
290
6023
  Environment* env = Environment::GetCurrent(info);
291
6023
  CHECK(env->has_run_bootstrapping_code());
292
  // calling env->EmitProcessEnvWarning() sets a variable indicating that
293
  // warnings have been emitted. It should be called last after other
294
  // conditions leading to a warning have been met.
295


18100
  if (env->options()->pending_deprecation && !value->IsString() &&
296

18072
      !value->IsNumber() && !value->IsBoolean() &&
297
1
      env->EmitProcessEnvWarning()) {
298
2
    if (ProcessEmitDeprecationWarning(
299
            env,
300
            "Assigning any value other than a string, number, or boolean to a "
301
            "process.env property is deprecated. Please make sure to convert "
302
            "the "
303
            "value to a string before setting process.env with it.",
304
            "DEP0104")
305
            .IsNothing())
306
2
      return;
307
  }
308
309
  Local<String> key;
310
  Local<String> value_string;
311

30114
  if (!property->ToString(env->context()).ToLocal(&key) ||
312
24089
      !value->ToString(env->context()).ToLocal(&value_string)) {
313
2
    return;
314
  }
315
316
6021
  env->env_vars()->Set(env->isolate(), key, value_string);
317
318
  // Whether it worked or not, always return value.
319
12042
  info.GetReturnValue().Set(value);
320
}
321
322
576926
static void EnvQuery(Local<Name> property,
323
                     const PropertyCallbackInfo<Integer>& info) {
324
576926
  Environment* env = Environment::GetCurrent(info);
325
576926
  CHECK(env->has_run_bootstrapping_code());
326
1153852
  if (property->IsString()) {
327
1153850
    int32_t rc = env->env_vars()->Query(env->isolate(), property.As<String>());
328
1719727
    if (rc != -1) info.GetReturnValue().Set(rc);
329
  }
330
576926
}
331
332
743
static void EnvDeleter(Local<Name> property,
333
                       const PropertyCallbackInfo<Boolean>& info) {
334
743
  Environment* env = Environment::GetCurrent(info);
335
743
  CHECK(env->has_run_bootstrapping_code());
336
1486
  if (property->IsString()) {
337
1484
    env->env_vars()->Delete(env->isolate(), property.As<String>());
338
  }
339
340
  // process.env never has non-configurable properties, so always
341
  // return true like the tc39 delete operator.
342
1486
  info.GetReturnValue().Set(true);
343
743
}
344
345
6677
static void EnvEnumerator(const PropertyCallbackInfo<Array>& info) {
346
6677
  Environment* env = Environment::GetCurrent(info);
347
6677
  CHECK(env->has_run_bootstrapping_code());
348
349
20031
  info.GetReturnValue().Set(
350
13354
      env->env_vars()->Enumerate(env->isolate()));
351
6677
}
352
353
4377
MaybeLocal<Object> CreateEnvVarProxy(Local<Context> context,
354
                                     Isolate* isolate,
355
                                     Local<Object> data) {
356
4377
  EscapableHandleScope scope(isolate);
357
4377
  Local<ObjectTemplate> env_proxy_template = ObjectTemplate::New(isolate);
358
13131
  env_proxy_template->SetHandler(NamedPropertyHandlerConfiguration(
359
      EnvGetter, EnvSetter, EnvQuery, EnvDeleter, EnvEnumerator, data,
360
4377
      PropertyHandlerFlags::kHasNoSideEffect));
361
8754
  return scope.EscapeMaybe(env_proxy_template->NewInstance(context));
362
}
363

12558
}  // namespace node