From 9f2dd48fc32bc19e07ff52f05ccf6e2cab059ac0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Nie=C3=9Fen?= Date: Mon, 10 Oct 2022 03:12:02 +0200 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/44914 Reviewed-By: Anna Henningsen Reviewed-By: Ben Noordhuis --- src/node_credentials.cc | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/node_credentials.cc b/src/node_credentials.cc index ccc77495449b0f..115c2fc877d5cc 100644 --- a/src/node_credentials.cc +++ b/src/node_credentials.cc @@ -215,7 +215,8 @@ static const char* name_by_gid(gid_t gid) { static uid_t uid_by_name(Isolate* isolate, Local value) { if (value->IsUint32()) { - return static_cast(value.As()->Value()); + static_assert(std::is_same::value); + return value.As()->Value(); } else { Utf8Value name(isolate, value); return uid_by_name(*name); @@ -224,7 +225,8 @@ static uid_t uid_by_name(Isolate* isolate, Local value) { static gid_t gid_by_name(Isolate* isolate, Local value) { if (value->IsUint32()) { - return static_cast(value.As()->Value()); + static_assert(std::is_same::value); + return value.As()->Value(); } else { Utf8Value name(isolate, value); return gid_by_name(*name);