Skip to content

Commit 9f2dd48

Browse files
tniessendanielleadams
authored andcommitted
src: remove uid_t/gid_t casts
If uid_t/gid_t are uint32_t, then the casts are unnecessary. This appears to be true in all recent versions of all supported platforms, so this change makes that assumption explicit and removes the casts. Conversely, if uid_t/gid_t are smaller unsigned integer types (such as uint16_t in earlier versions of Linux) or signed integer types (such as int32_t), then the casts are potentially dangerous because they might change the value of the uid/gid. If this happens on any platform, the added static_assert will fail, and additional bound checks should be introduced. PR-URL: #44914 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
1 parent 8c69da8 commit 9f2dd48

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/node_credentials.cc

+4-2
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,8 @@ static const char* name_by_gid(gid_t gid) {
215215

216216
static uid_t uid_by_name(Isolate* isolate, Local<Value> value) {
217217
if (value->IsUint32()) {
218-
return static_cast<uid_t>(value.As<Uint32>()->Value());
218+
static_assert(std::is_same<uid_t, uint32_t>::value);
219+
return value.As<Uint32>()->Value();
219220
} else {
220221
Utf8Value name(isolate, value);
221222
return uid_by_name(*name);
@@ -224,7 +225,8 @@ static uid_t uid_by_name(Isolate* isolate, Local<Value> value) {
224225

225226
static gid_t gid_by_name(Isolate* isolate, Local<Value> value) {
226227
if (value->IsUint32()) {
227-
return static_cast<gid_t>(value.As<Uint32>()->Value());
228+
static_assert(std::is_same<gid_t, uint32_t>::value);
229+
return value.As<Uint32>()->Value();
228230
} else {
229231
Utf8Value name(isolate, value);
230232
return gid_by_name(*name);

0 commit comments

Comments
 (0)