Skip to content

Commit 1da5acb

Browse files
addaleaxtargos
authored andcommitted
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: #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]>
1 parent e8fa067 commit 1da5acb

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/node_os.cc

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ using v8::Integer;
4949
using v8::Isolate;
5050
using v8::Local;
5151
using v8::MaybeLocal;
52+
using v8::NewStringType;
5253
using v8::Null;
5354
using v8::Number;
5455
using v8::Object;
@@ -69,7 +70,9 @@ static void GetHostname(const FunctionCallbackInfo<Value>& args) {
6970
return args.GetReturnValue().SetUndefined();
7071
}
7172

72-
args.GetReturnValue().Set(OneByteString(env->isolate(), buf));
73+
args.GetReturnValue().Set(
74+
String::NewFromUtf8(env->isolate(), buf, NewStringType::kNormal)
75+
.ToLocalChecked());
7376
}
7477

7578

@@ -84,7 +87,9 @@ static void GetOSType(const FunctionCallbackInfo<Value>& args) {
8487
return args.GetReturnValue().SetUndefined();
8588
}
8689

87-
args.GetReturnValue().Set(OneByteString(env->isolate(), info.sysname));
90+
args.GetReturnValue().Set(
91+
String::NewFromUtf8(env->isolate(), info.sysname, NewStringType::kNormal)
92+
.ToLocalChecked());
8893
}
8994

9095

@@ -99,7 +104,9 @@ static void GetOSRelease(const FunctionCallbackInfo<Value>& args) {
99104
return args.GetReturnValue().SetUndefined();
100105
}
101106

102-
args.GetReturnValue().Set(OneByteString(env->isolate(), info.release));
107+
args.GetReturnValue().Set(
108+
String::NewFromUtf8(env->isolate(), info.release, NewStringType::kNormal)
109+
.ToLocalChecked());
103110
}
104111

105112

0 commit comments

Comments
 (0)