From 1da5acbf91f309f6860847c5e5b21864c5adcd7e Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Fri, 24 May 2019 13:14:31 +0200 Subject: [PATCH] os: assume UTF-8 for hostname MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Do not assume Latin-1, but rather UTF-8 for the result of getting the OS hostname. While in 99 % of cases these strings are stored in ASCII, the OS does not enforce an encoding on its own, and apparently the hostname is sometimes set to non-ASCII data (despite at least some versions of hostname(1) rejecting such input, making it even harder to write a test for this which would already require root privileges). In any case, these are short strings, so assuming UTF-8 comes with no significant overhead. Fixes: https://github.com/nodejs/node/issues/27848 PR-URL: https://github.com/nodejs/node/pull/27849 Reviewed-By: Ben Noordhuis Reviewed-By: Colin Ihrig Reviewed-By: Sam Roberts Reviewed-By: James M Snell Reviewed-By: Сковорода Никита Андреевич Reviewed-By: Gus Caplan Reviewed-By: Ruben Bridgewater Reviewed-By: Rich Trott Reviewed-By: Yongsheng Zhang --- src/node_os.cc | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/node_os.cc b/src/node_os.cc index d2387f2dc96bf7..b6fb305948e234 100644 --- a/src/node_os.cc +++ b/src/node_os.cc @@ -49,6 +49,7 @@ using v8::Integer; using v8::Isolate; using v8::Local; using v8::MaybeLocal; +using v8::NewStringType; using v8::Null; using v8::Number; using v8::Object; @@ -69,7 +70,9 @@ static void GetHostname(const FunctionCallbackInfo& args) { return args.GetReturnValue().SetUndefined(); } - args.GetReturnValue().Set(OneByteString(env->isolate(), buf)); + args.GetReturnValue().Set( + String::NewFromUtf8(env->isolate(), buf, NewStringType::kNormal) + .ToLocalChecked()); } @@ -84,7 +87,9 @@ static void GetOSType(const FunctionCallbackInfo& args) { return args.GetReturnValue().SetUndefined(); } - args.GetReturnValue().Set(OneByteString(env->isolate(), info.sysname)); + args.GetReturnValue().Set( + String::NewFromUtf8(env->isolate(), info.sysname, NewStringType::kNormal) + .ToLocalChecked()); } @@ -99,7 +104,9 @@ static void GetOSRelease(const FunctionCallbackInfo& args) { return args.GetReturnValue().SetUndefined(); } - args.GetReturnValue().Set(OneByteString(env->isolate(), info.release)); + args.GetReturnValue().Set( + String::NewFromUtf8(env->isolate(), info.release, NewStringType::kNormal) + .ToLocalChecked()); }