From 7db9f03cf67f5475fe894492a80c1aa2a6c7279b Mon Sep 17 00:00:00 2001 From: Devendra Satram Date: Fri, 19 Apr 2019 22:43:42 +0530 Subject: [PATCH] =?UTF-8?q?src:=20Modified=20src/node=5Fenv=5Fvar.cc=20as?= =?UTF-8?q?=20per=20Joyee=20Cheung=E2=80=99s=20review=20comments?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Below review comments are taken care in this submission: 1. followed snake case naming convention for local variables 2. dropped sizeof(char) since it is obvious value 1 3. Used MaybeLocal instead of Local to check
for empty strings and throw exception if empty. Fixes: https://github.com/nodejs/node/issues/27211 Refs: https://v8docs.nodesource.com/node-4.8/annotated.html --- src/node_env_var.cc | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/node_env_var.cc b/src/node_env_var.cc index e695e1a013f595..b9d44a768821fb 100644 --- a/src/node_env_var.cc +++ b/src/node_env_var.cc @@ -85,19 +85,19 @@ Local RealEnvStore::Get(Isolate* isolate, node::Utf8Value key(isolate, property); char* val = nullptr; - size_t initSz = 256; + size_t init_sz = 256; // Allocate 256 bytes initially, if not enough reallocate. - val = Malloc(sizeof(char) * initSz); + val = Malloc(init_sz); - int ret = uv_os_getenv(*key, val, &initSz); + int ret = uv_os_getenv(*key, val, &init_sz); if (UV_ENOBUFS == ret) { - // Buffer is not large enough, reallocate to the updated initSz + // Buffer is not large enough, reallocate to the updated init_sz // and fetch env value again. - val = Realloc(val, sizeof(char) * initSz); + val = Realloc(val, init_sz); - ret = uv_os_getenv(*key, val, &initSz); + ret = uv_os_getenv(*key, val, &init_sz); // Still failed to fetch env value return emptry string. if (UV_ENOBUFS == ret || UV_ENOENT == ret) { @@ -107,13 +107,17 @@ Local RealEnvStore::Get(Isolate* isolate, return Local(); } - Local valueString = - String::NewFromUtf8(isolate, val, NewStringType::kNormal) - .ToLocalChecked(); + MaybeLocal value_string = + String::NewFromUtf8(isolate, val, NewStringType::kNormal); + + if (value_string.IsEmpty()) { + isolate->ThrowException(ERR_STRING_TOO_LONG(isolate)); + return Local(); + } if (nullptr != val) free(val); - return valueString; + return value_string.ToLocalChecked(); } void RealEnvStore::Set(Isolate* isolate,