Skip to content

Commit

Permalink
os: assume UTF-8 for hostname
Browse files Browse the repository at this point in the history
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: #27848

PR-URL: #27849
Reviewed-By: Ben Noordhuis <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Sam Roberts <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Сковорода Никита Андреевич <[email protected]>
Reviewed-By: Gus Caplan <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Yongsheng Zhang <[email protected]>
  • Loading branch information
addaleax authored and targos committed May 28, 2019
1 parent e8fa067 commit 1da5acb
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/node_os.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -69,7 +70,9 @@ static void GetHostname(const FunctionCallbackInfo<Value>& args) {
return args.GetReturnValue().SetUndefined();
}

args.GetReturnValue().Set(OneByteString(env->isolate(), buf));
args.GetReturnValue().Set(
String::NewFromUtf8(env->isolate(), buf, NewStringType::kNormal)
.ToLocalChecked());
}


Expand All @@ -84,7 +87,9 @@ static void GetOSType(const FunctionCallbackInfo<Value>& 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());
}


Expand All @@ -99,7 +104,9 @@ static void GetOSRelease(const FunctionCallbackInfo<Value>& 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());
}


Expand Down

0 comments on commit 1da5acb

Please sign in to comment.