From 274c0f2e0a4afd6e3c3999cde57d2b17db0715b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Nie=C3=9Fen?= Date: Fri, 5 May 2023 00:35:11 +0200 Subject: [PATCH] src: avoid strcmp() with Utf8Value Having Utf8Value::operator==() without operator!=() is awkward in C++17, so add the negated equality operator. Then, use either instead of strcmp() where appropriate. PR-URL: https://github.com/nodejs/node/pull/47827 Reviewed-By: Yagiz Nizipli Reviewed-By: Ben Noordhuis Reviewed-By: Mohammed Keyvanzadeh Reviewed-By: Darshan Sen Reviewed-By: Filip Skokan --- src/crypto/crypto_context.cc | 3 +-- src/crypto/crypto_tls.cc | 3 +-- src/node_report.cc | 2 +- src/util.h | 5 ++--- 4 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/crypto/crypto_context.cc b/src/crypto/crypto_context.cc index 77763853a99d87..d4aa76d1d8c50e 100644 --- a/src/crypto/crypto_context.cc +++ b/src/crypto/crypto_context.cc @@ -841,8 +841,7 @@ void SecureContext::SetECDHCurve(const FunctionCallbackInfo& args) { Utf8Value curve(env->isolate(), args[0]); - if (strcmp(*curve, "auto") != 0 && - !SSL_CTX_set1_curves_list(sc->ctx_.get(), *curve)) { + if (curve != "auto" && !SSL_CTX_set1_curves_list(sc->ctx_.get(), *curve)) { return THROW_ERR_CRYPTO_OPERATION_FAILED(env, "Failed to set ECDH curve"); } } diff --git a/src/crypto/crypto_tls.cc b/src/crypto/crypto_tls.cc index 3815dc12fff972..71d59b511e1736 100644 --- a/src/crypto/crypto_tls.cc +++ b/src/crypto/crypto_tls.cc @@ -1350,8 +1350,7 @@ unsigned int TLSWrap::PskServerCallback( // Make sure there are no utf8 replacement symbols. Utf8Value identity_utf8(env->isolate(), identity_str); - if (strcmp(*identity_utf8, identity) != 0) - return 0; + if (identity_utf8 != identity) return 0; Local argv[] = { identity_str, diff --git a/src/node_report.cc b/src/node_report.cc index f6439623de01e4..94f31663d2c736 100644 --- a/src/node_report.cc +++ b/src/node_report.cc @@ -401,7 +401,7 @@ static void PrintJavaScriptErrorProperties(JSONWriter* writer, continue; } node::Utf8Value k(isolate, key); - if (!strcmp(*k, "stack") || !strcmp(*k, "message")) continue; + if (k == "stack" || k == "message") continue; node::Utf8Value v(isolate, value_string); writer->json_keyvalue(k.ToStringView(), v.ToStringView()); } diff --git a/src/util.h b/src/util.h index b1750f7b718f66..a50d92601bf0ac 100644 --- a/src/util.h +++ b/src/util.h @@ -525,9 +525,8 @@ class Utf8Value : public MaybeStackBuffer { public: explicit Utf8Value(v8::Isolate* isolate, v8::Local value); - inline bool operator==(const char* a) const { - return strcmp(out(), a) == 0; - } + inline bool operator==(const char* a) const { return strcmp(out(), a) == 0; } + inline bool operator!=(const char* a) const { return !(*this == a); } }; class TwoByteValue : public MaybeStackBuffer {