0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00

os: assume UTF-8 for hostname

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 <info@bnoordhuis.nl>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Sam Roberts <vieuxtech@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com>
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com>
This commit is contained in:
Anna Henningsen 2019-05-24 13:14:31 +02:00
parent 725a66a2f0
commit 6f924b60c5
No known key found for this signature in database
GPG Key ID: 9C63F3A6CD2AD8F9

View File

@ -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<Value>& 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<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());
}
@ -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());
}