GCC Code Coverage Report | |||||||||||||||||||||
|
|||||||||||||||||||||
Line | Branch | Exec | Source |
1 |
#include "debug_utils-inl.h" |
||
2 |
#include "env-inl.h" |
||
3 |
#include "node_errors.h" |
||
4 |
#include "node_external_reference.h" |
||
5 |
#include "node_process.h" |
||
6 |
|||
7 |
#include <time.h> // tzset(), _tzset() |
||
8 |
|||
9 |
namespace node { |
||
10 |
using v8::Array; |
||
11 |
using v8::Boolean; |
||
12 |
using v8::Context; |
||
13 |
using v8::DontDelete; |
||
14 |
using v8::DontEnum; |
||
15 |
using v8::EscapableHandleScope; |
||
16 |
using v8::HandleScope; |
||
17 |
using v8::Integer; |
||
18 |
using v8::Isolate; |
||
19 |
using v8::Just; |
||
20 |
using v8::Local; |
||
21 |
using v8::Maybe; |
||
22 |
using v8::MaybeLocal; |
||
23 |
using v8::Name; |
||
24 |
using v8::NamedPropertyHandlerConfiguration; |
||
25 |
using v8::NewStringType; |
||
26 |
using v8::Nothing; |
||
27 |
using v8::Object; |
||
28 |
using v8::ObjectTemplate; |
||
29 |
using v8::PropertyCallbackInfo; |
||
30 |
using v8::PropertyHandlerFlags; |
||
31 |
using v8::ReadOnly; |
||
32 |
using v8::String; |
||
33 |
using v8::Value; |
||
34 |
|||
35 |
8885 |
class RealEnvStore final : public KVStore { |
|
36 |
public: |
||
37 |
MaybeLocal<String> Get(Isolate* isolate, Local<String> key) const override; |
||
38 |
Maybe<std::string> Get(const char* key) const override; |
||
39 |
void Set(Isolate* isolate, Local<String> key, Local<String> value) override; |
||
40 |
int32_t Query(Isolate* isolate, Local<String> key) const override; |
||
41 |
int32_t Query(const char* key) const override; |
||
42 |
void Delete(Isolate* isolate, Local<String> key) override; |
||
43 |
Local<Array> Enumerate(Isolate* isolate) const override; |
||
44 |
}; |
||
45 |
|||
46 |
633 |
class MapKVStore final : public KVStore { |
|
47 |
public: |
||
48 |
MaybeLocal<String> Get(Isolate* isolate, Local<String> key) const override; |
||
49 |
Maybe<std::string> Get(const char* key) const override; |
||
50 |
void Set(Isolate* isolate, Local<String> key, Local<String> value) override; |
||
51 |
int32_t Query(Isolate* isolate, Local<String> key) const override; |
||
52 |
int32_t Query(const char* key) const override; |
||
53 |
void Delete(Isolate* isolate, Local<String> key) override; |
||
54 |
Local<Array> Enumerate(Isolate* isolate) const override; |
||
55 |
|||
56 |
std::shared_ptr<KVStore> Clone(Isolate* isolate) const override; |
||
57 |
|||
58 |
626 |
MapKVStore() = default; |
|
59 |
7 |
MapKVStore(const MapKVStore& other) : KVStore(), map_(other.map_) {} |
|
60 |
|||
61 |
private: |
||
62 |
mutable Mutex mutex_; |
||
63 |
std::unordered_map<std::string, std::string> map_; |
||
64 |
}; |
||
65 |
|||
66 |
namespace per_process { |
||
67 |
4678 |
Mutex env_var_mutex; |
|
68 |
4678 |
std::shared_ptr<KVStore> system_environment = std::make_shared<RealEnvStore>(); |
|
69 |
} // namespace per_process |
||
70 |
|||
71 |
template <typename T> |
||
72 |
7595 |
void DateTimeConfigurationChangeNotification(Isolate* isolate, const T& key) { |
|
73 |
✓✓✓✗ ✓✗✓✓ |
7595 |
if (key.length() == 2 && key[0] == 'T' && key[1] == 'Z') { |
74 |
#ifdef __POSIX__ |
||
75 |
4 |
tzset(); |
|
76 |
#else |
||
77 |
_tzset(); |
||
78 |
#endif |
||
79 |
4 |
auto constexpr time_zone_detection = Isolate::TimeZoneDetection::kRedetect; |
|
80 |
4 |
isolate->DateTimeConfigurationChangeNotification(time_zone_detection); |
|
81 |
} |
||
82 |
7595 |
} |
|
83 |
|||
84 |
583977 |
Maybe<std::string> RealEnvStore::Get(const char* key) const { |
|
85 |
1167954 |
Mutex::ScopedLock lock(per_process::env_var_mutex); |
|
86 |
|||
87 |
583977 |
size_t init_sz = 256; |
|
88 |
1167954 |
MaybeStackBuffer<char, 256> val; |
|
89 |
583977 |
int ret = uv_os_getenv(key, *val, &init_sz); |
|
90 |
|||
91 |
✓✓ | 583977 |
if (ret == UV_ENOBUFS) { |
92 |
// Buffer is not large enough, reallocate to the updated init_sz |
||
93 |
// and fetch env value again. |
||
94 |
6034 |
val.AllocateSufficientStorage(init_sz); |
|
95 |
6034 |
ret = uv_os_getenv(key, *val, &init_sz); |
|
96 |
} |
||
97 |
|||
98 |
✓✓ | 583977 |
if (ret >= 0) { // Env key value fetch success. |
99 |
523867 |
return Just(std::string(*val, init_sz)); |
|
100 |
} |
||
101 |
|||
102 |
60110 |
return Nothing<std::string>(); |
|
103 |
} |
||
104 |
|||
105 |
583976 |
MaybeLocal<String> RealEnvStore::Get(Isolate* isolate, |
|
106 |
Local<String> property) const { |
||
107 |
1167953 |
node::Utf8Value key(isolate, property); |
|
108 |
1167954 |
Maybe<std::string> value = Get(*key); |
|
109 |
|||
110 |
✓✓ | 583977 |
if (value.IsJust()) { |
111 |
523867 |
std::string val = value.FromJust(); |
|
112 |
return String::NewFromUtf8( |
||
113 |
523867 |
isolate, val.data(), NewStringType::kNormal, val.size()); |
|
114 |
} |
||
115 |
|||
116 |
60110 |
return MaybeLocal<String>(); |
|
117 |
} |
||
118 |
|||
119 |
6576 |
void RealEnvStore::Set(Isolate* isolate, |
|
120 |
Local<String> property, |
||
121 |
Local<String> value) { |
||
122 |
13152 |
Mutex::ScopedLock lock(per_process::env_var_mutex); |
|
123 |
|||
124 |
13152 |
node::Utf8Value key(isolate, property); |
|
125 |
13152 |
node::Utf8Value val(isolate, value); |
|
126 |
|||
127 |
#ifdef _WIN32 |
||
128 |
if (key.length() > 0 && key[0] == '=') return; |
||
129 |
#endif |
||
130 |
6576 |
uv_os_setenv(*key, *val); |
|
131 |
6576 |
DateTimeConfigurationChangeNotification(isolate, key); |
|
132 |
6576 |
} |
|
133 |
|||
134 |
638687 |
int32_t RealEnvStore::Query(const char* key) const { |
|
135 |
1277374 |
Mutex::ScopedLock lock(per_process::env_var_mutex); |
|
136 |
|||
137 |
char val[2]; |
||
138 |
638687 |
size_t init_sz = sizeof(val); |
|
139 |
638687 |
int ret = uv_os_getenv(key, val, &init_sz); |
|
140 |
|||
141 |
✓✓ | 638687 |
if (ret == UV_ENOENT) { |
142 |
5664 |
return -1; |
|
143 |
} |
||
144 |
|||
145 |
#ifdef _WIN32 |
||
146 |
if (key[0] == '=') { |
||
147 |
return static_cast<int32_t>(ReadOnly) | |
||
148 |
static_cast<int32_t>(DontDelete) | |
||
149 |
static_cast<int32_t>(DontEnum); |
||
150 |
} |
||
151 |
#endif |
||
152 |
|||
153 |
633023 |
return 0; |
|
154 |
} |
||
155 |
|||
156 |
638687 |
int32_t RealEnvStore::Query(Isolate* isolate, Local<String> property) const { |
|
157 |
1277374 |
node::Utf8Value key(isolate, property); |
|
158 |
1277374 |
return Query(*key); |
|
159 |
} |
||
160 |
|||
161 |
1019 |
void RealEnvStore::Delete(Isolate* isolate, Local<String> property) { |
|
162 |
2038 |
Mutex::ScopedLock lock(per_process::env_var_mutex); |
|
163 |
|||
164 |
2038 |
node::Utf8Value key(isolate, property); |
|
165 |
1019 |
uv_os_unsetenv(*key); |
|
166 |
1019 |
DateTimeConfigurationChangeNotification(isolate, key); |
|
167 |
1019 |
} |
|
168 |
|||
169 |
7765 |
Local<Array> RealEnvStore::Enumerate(Isolate* isolate) const { |
|
170 |
15530 |
Mutex::ScopedLock lock(per_process::env_var_mutex); |
|
171 |
uv_env_item_t* items; |
||
172 |
int count; |
||
173 |
|||
174 |
23295 |
auto cleanup = OnScopeLeave([&]() { uv_os_free_environ(items, count); }); |
|
175 |
✗✓ | 7765 |
CHECK_EQ(uv_os_environ(&items, &count), 0); |
176 |
|||
177 |
15530 |
MaybeStackBuffer<Local<Value>, 256> env_v(count); |
|
178 |
7765 |
int env_v_index = 0; |
|
179 |
✓✓ | 567705 |
for (int i = 0; i < count; i++) { |
180 |
#ifdef _WIN32 |
||
181 |
// If the key starts with '=' it is a hidden environment variable. |
||
182 |
// The '\0' check is a workaround for the bug behind |
||
183 |
// https://github.com/libuv/libuv/pull/2473 and can be removed later. |
||
184 |
if (items[i].name[0] == '=' || items[i].name[0] == '\0') continue; |
||
185 |
#endif |
||
186 |
559940 |
MaybeLocal<String> str = String::NewFromUtf8(isolate, items[i].name); |
|
187 |
✗✓ | 559940 |
if (str.IsEmpty()) { |
188 |
isolate->ThrowException(ERR_STRING_TOO_LONG(isolate)); |
||
189 |
return Local<Array>(); |
||
190 |
} |
||
191 |
1119880 |
env_v[env_v_index++] = str.ToLocalChecked(); |
|
192 |
} |
||
193 |
|||
194 |
7765 |
return Array::New(isolate, env_v.out(), env_v_index); |
|
195 |
} |
||
196 |
|||
197 |
602 |
std::shared_ptr<KVStore> KVStore::Clone(Isolate* isolate) const { |
|
198 |
1204 |
HandleScope handle_scope(isolate); |
|
199 |
602 |
Local<Context> context = isolate->GetCurrentContext(); |
|
200 |
|||
201 |
602 |
std::shared_ptr<KVStore> copy = KVStore::CreateMapKVStore(); |
|
202 |
602 |
Local<Array> keys = Enumerate(isolate); |
|
203 |
602 |
uint32_t keys_length = keys->Length(); |
|
204 |
✓✓ | 43387 |
for (uint32_t i = 0; i < keys_length; i++) { |
205 |
85570 |
Local<Value> key = keys->Get(context, i).ToLocalChecked(); |
|
206 |
✗✓ | 85570 |
CHECK(key->IsString()); |
207 |
42785 |
copy->Set(isolate, |
|
208 |
key.As<String>(), |
||
209 |
128355 |
Get(isolate, key.As<String>()).ToLocalChecked()); |
|
210 |
} |
||
211 |
1204 |
return copy; |
|
212 |
} |
||
213 |
|||
214 |
31085 |
Maybe<std::string> MapKVStore::Get(const char* key) const { |
|
215 |
62170 |
Mutex::ScopedLock lock(mutex_); |
|
216 |
31085 |
auto it = map_.find(key); |
|
217 |
✓✓ | 62170 |
return it == map_.end() ? Nothing<std::string>() : Just(it->second); |
218 |
} |
||
219 |
|||
220 |
30673 |
MaybeLocal<String> MapKVStore::Get(Isolate* isolate, Local<String> key) const { |
|
221 |
61346 |
Utf8Value str(isolate, key); |
|
222 |
61346 |
Maybe<std::string> value = Get(*str); |
|
223 |
✓✓ | 32792 |
if (value.IsNothing()) return Local<String>(); |
224 |
28554 |
std::string val = value.FromJust(); |
|
225 |
return String::NewFromUtf8( |
||
226 |
28554 |
isolate, val.data(), NewStringType::kNormal, val.size()); |
|
227 |
} |
||
228 |
|||
229 |
44427 |
void MapKVStore::Set(Isolate* isolate, Local<String> key, Local<String> value) { |
|
230 |
88854 |
Mutex::ScopedLock lock(mutex_); |
|
231 |
88854 |
Utf8Value key_str(isolate, key); |
|
232 |
88854 |
Utf8Value value_str(isolate, value); |
|
233 |
✓✗✓✗ ✓✗✓✗ |
44427 |
if (*key_str != nullptr && key_str.length() > 0 && *value_str != nullptr) { |
234 |
88854 |
map_[std::string(*key_str, key_str.length())] = |
|
235 |
133281 |
std::string(*value_str, value_str.length()); |
|
236 |
} |
||
237 |
44427 |
} |
|
238 |
|||
239 |
52861 |
int32_t MapKVStore::Query(const char* key) const { |
|
240 |
105722 |
Mutex::ScopedLock lock(mutex_); |
|
241 |
✓✓ | 105722 |
return map_.find(key) == map_.end() ? -1 : 0; |
242 |
} |
||
243 |
|||
244 |
52861 |
int32_t MapKVStore::Query(Isolate* isolate, Local<String> key) const { |
|
245 |
105722 |
Utf8Value str(isolate, key); |
|
246 |
105722 |
return Query(*str); |
|
247 |
} |
||
248 |
|||
249 |
void MapKVStore::Delete(Isolate* isolate, Local<String> key) { |
||
250 |
Mutex::ScopedLock lock(mutex_); |
||
251 |
Utf8Value str(isolate, key); |
||
252 |
map_.erase(std::string(*str, str.length())); |
||
253 |
} |
||
254 |
|||
255 |
720 |
Local<Array> MapKVStore::Enumerate(Isolate* isolate) const { |
|
256 |
1440 |
Mutex::ScopedLock lock(mutex_); |
|
257 |
1440 |
std::vector<Local<Value>> values; |
|
258 |
720 |
values.reserve(map_.size()); |
|
259 |
✓✓ | 53003 |
for (const auto& pair : map_) { |
260 |
values.emplace_back( |
||
261 |
104566 |
String::NewFromUtf8(isolate, pair.first.data(), |
|
262 |
52283 |
NewStringType::kNormal, pair.first.size()) |
|
263 |
52283 |
.ToLocalChecked()); |
|
264 |
} |
||
265 |
1440 |
return Array::New(isolate, values.data(), values.size()); |
|
266 |
} |
||
267 |
|||
268 |
7 |
std::shared_ptr<KVStore> MapKVStore::Clone(Isolate* isolate) const { |
|
269 |
7 |
return std::make_shared<MapKVStore>(*this); |
|
270 |
} |
||
271 |
|||
272 |
626 |
std::shared_ptr<KVStore> KVStore::CreateMapKVStore() { |
|
273 |
626 |
return std::make_shared<MapKVStore>(); |
|
274 |
} |
||
275 |
|||
276 |
24 |
Maybe<bool> KVStore::AssignFromObject(Local<Context> context, |
|
277 |
Local<Object> entries) { |
||
278 |
24 |
Isolate* isolate = context->GetIsolate(); |
|
279 |
48 |
HandleScope handle_scope(isolate); |
|
280 |
Local<Array> keys; |
||
281 |
✗✓ | 48 |
if (!entries->GetOwnPropertyNames(context).ToLocal(&keys)) |
282 |
return Nothing<bool>(); |
||
283 |
24 |
uint32_t keys_length = keys->Length(); |
|
284 |
✓✓ | 1660 |
for (uint32_t i = 0; i < keys_length; i++) { |
285 |
Local<Value> key; |
||
286 |
✗✓ | 3272 |
if (!keys->Get(context, i).ToLocal(&key)) |
287 |
return Nothing<bool>(); |
||
288 |
✗✓ | 3272 |
if (!key->IsString()) continue; |
289 |
|||
290 |
Local<Value> value; |
||
291 |
Local<String> value_string; |
||
292 |
✓✗✗✓ ✗✓ |
6544 |
if (!entries->Get(context, key).ToLocal(&value) || |
293 |
4908 |
!value->ToString(context).ToLocal(&value_string)) { |
|
294 |
return Nothing<bool>(); |
||
295 |
} |
||
296 |
|||
297 |
3272 |
Set(isolate, key.As<String>(), value_string); |
|
298 |
} |
||
299 |
24 |
return Just(true); |
|
300 |
} |
||
301 |
|||
302 |
559342 |
static void EnvGetter(Local<Name> property, |
|
303 |
const PropertyCallbackInfo<Value>& info) { |
||
304 |
559342 |
Environment* env = Environment::GetCurrent(info); |
|
305 |
✗✓ | 559342 |
CHECK(env->has_run_bootstrapping_code()); |
306 |
✓✓ | 559342 |
if (property->IsSymbol()) { |
307 |
16044 |
return info.GetReturnValue().SetUndefined(); |
|
308 |
} |
||
309 |
✗✓ | 1102638 |
CHECK(property->IsString()); |
310 |
MaybeLocal<String> value_string = |
||
311 |
1102639 |
env->env_vars()->Get(env->isolate(), property.As<String>()); |
|
312 |
✓✓ | 551320 |
if (!value_string.IsEmpty()) { |
313 |
998722 |
info.GetReturnValue().Set(value_string.ToLocalChecked()); |
|
314 |
} |
||
315 |
} |
||
316 |
|||
317 |
6584 |
static void EnvSetter(Local<Name> property, |
|
318 |
Local<Value> value, |
||
319 |
const PropertyCallbackInfo<Value>& info) { |
||
320 |
6584 |
Environment* env = Environment::GetCurrent(info); |
|
321 |
✗✓ | 6584 |
CHECK(env->has_run_bootstrapping_code()); |
322 |
// calling env->EmitProcessEnvWarning() sets a variable indicating that |
||
323 |
// warnings have been emitted. It should be called last after other |
||
324 |
// conditions leading to a warning have been met. |
||
325 |
✓✓✓✗ ✓✗✓✓ |
19795 |
if (env->options()->pending_deprecation && !value->IsString() && |
326 |
✓✓✓✗ ✓✗ |
19755 |
!value->IsNumber() && !value->IsBoolean() && |
327 |
1 |
env->EmitProcessEnvWarning()) { |
|
328 |
✗✓ | 2 |
if (ProcessEmitDeprecationWarning( |
329 |
env, |
||
330 |
"Assigning any value other than a string, number, or boolean to a " |
||
331 |
"process.env property is deprecated. Please make sure to convert " |
||
332 |
"the " |
||
333 |
"value to a string before setting process.env with it.", |
||
334 |
"DEP0104") |
||
335 |
.IsNothing()) |
||
336 |
2 |
return; |
|
337 |
} |
||
338 |
|||
339 |
Local<String> key; |
||
340 |
Local<String> value_string; |
||
341 |
✓✓✓✓ ✓✓ |
32919 |
if (!property->ToString(env->context()).ToLocal(&key) || |
342 |
26333 |
!value->ToString(env->context()).ToLocal(&value_string)) { |
|
343 |
2 |
return; |
|
344 |
} |
||
345 |
|||
346 |
6582 |
env->env_vars()->Set(env->isolate(), key, value_string); |
|
347 |
|||
348 |
// Whether it worked or not, always return value. |
||
349 |
13164 |
info.GetReturnValue().Set(value); |
|
350 |
} |
||
351 |
|||
352 |
691549 |
static void EnvQuery(Local<Name> property, |
|
353 |
const PropertyCallbackInfo<Integer>& info) { |
||
354 |
691549 |
Environment* env = Environment::GetCurrent(info); |
|
355 |
✗✓ | 691549 |
CHECK(env->has_run_bootstrapping_code()); |
356 |
✓✓ | 1383098 |
if (property->IsString()) { |
357 |
1383096 |
int32_t rc = env->env_vars()->Query(env->isolate(), property.As<String>()); |
|
358 |
✓✓ | 2062592 |
if (rc != -1) info.GetReturnValue().Set(rc); |
359 |
} |
||
360 |
691549 |
} |
|
361 |
|||
362 |
1020 |
static void EnvDeleter(Local<Name> property, |
|
363 |
const PropertyCallbackInfo<Boolean>& info) { |
||
364 |
1020 |
Environment* env = Environment::GetCurrent(info); |
|
365 |
✗✓ | 1020 |
CHECK(env->has_run_bootstrapping_code()); |
366 |
✓✓ | 2040 |
if (property->IsString()) { |
367 |
2038 |
env->env_vars()->Delete(env->isolate(), property.As<String>()); |
|
368 |
} |
||
369 |
|||
370 |
// process.env never has non-configurable properties, so always |
||
371 |
// return true like the tc39 delete operator. |
||
372 |
2040 |
info.GetReturnValue().Set(true); |
|
373 |
1020 |
} |
|
374 |
|||
375 |
7883 |
static void EnvEnumerator(const PropertyCallbackInfo<Array>& info) { |
|
376 |
7883 |
Environment* env = Environment::GetCurrent(info); |
|
377 |
✗✓ | 7883 |
CHECK(env->has_run_bootstrapping_code()); |
378 |
|||
379 |
23649 |
info.GetReturnValue().Set( |
|
380 |
15766 |
env->env_vars()->Enumerate(env->isolate())); |
|
381 |
7883 |
} |
|
382 |
|||
383 |
445 |
MaybeLocal<Object> CreateEnvVarProxy(Local<Context> context, Isolate* isolate) { |
|
384 |
445 |
EscapableHandleScope scope(isolate); |
|
385 |
445 |
Local<ObjectTemplate> env_proxy_template = ObjectTemplate::New(isolate); |
|
386 |
1335 |
env_proxy_template->SetHandler(NamedPropertyHandlerConfiguration( |
|
387 |
EnvGetter, EnvSetter, EnvQuery, EnvDeleter, EnvEnumerator, Local<Value>(), |
||
388 |
445 |
PropertyHandlerFlags::kHasNoSideEffect)); |
|
389 |
890 |
return scope.EscapeMaybe(env_proxy_template->NewInstance(context)); |
|
390 |
} |
||
391 |
|||
392 |
4601 |
void RegisterEnvVarExternalReferences(ExternalReferenceRegistry* registry) { |
|
393 |
4601 |
registry->Register(EnvGetter); |
|
394 |
4601 |
registry->Register(EnvSetter); |
|
395 |
4601 |
registry->Register(EnvQuery); |
|
396 |
4601 |
registry->Register(EnvDeleter); |
|
397 |
4601 |
registry->Register(EnvEnumerator); |
|
398 |
4601 |
} |
|
399 |
} // namespace node |
||
400 |
|||
401 |
✓✗✓✗ |
18635 |
NODE_MODULE_EXTERNAL_REFERENCE(env_var, node::RegisterEnvVarExternalReferences) |
Generated by: GCOVR (Version 3.4) |