From 4405fc879aadaa2f5c459023612c2a9c9c6978fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Nie=C3=9Fen?= Date: Wed, 5 Apr 2023 01:29:04 +0200 Subject: [PATCH] src: use stricter compile-time guidance SnapshotSerializerDeserializer::GetName() appears to confuse static analysis such as Coverity. This changes the function structure to a sequence of if-else blocks and marks all branch conditions as constexpr. (Unfortunately, this results in a dangling 'else' keyword in the V macro.) As per a request in the PR discussion, this change does _not_ ensure that GetName() can only be called for known types T and instead still returns an empty string in that case. Also use std::is_unsigned_v instead of !std::is_signed_v. PR-URL: https://github.com/nodejs/node/pull/46509 Reviewed-By: James M Snell Reviewed-By: Michael Dawson --- src/node_snapshotable.cc | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/src/node_snapshotable.cc b/src/node_snapshotable.cc index 693fad8dda40fd..0acfc46fe43953 100644 --- a/src/node_snapshotable.cc +++ b/src/node_snapshotable.cc @@ -166,22 +166,19 @@ class SnapshotSerializerDeserializer { V(std::string) #define V(TypeName) \ - if (std::is_same_v) { \ + if constexpr (std::is_same_v) { \ return #TypeName; \ - } + } else // NOLINT(readability/braces) TYPE_LIST(V) #undef V - std::string name; - if (std::is_arithmetic_v) { - if (!std::is_signed_v) { - name += "u"; - } - name += std::is_integral_v ? "int" : "float"; - name += std::to_string(sizeof(T) * 8); - name += "_t"; + if constexpr (std::is_arithmetic_v) { + return (std::is_unsigned_v ? "uint" + : std::is_integral_v ? "int" + : "float") + + std::to_string(sizeof(T) * 8) + "_t"; } - return name; + return ""; } bool is_debug = false;