From b05c56e4be2c48af44981d983cc2ba7eadba24c3 Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Sat, 15 Jun 2024 09:24:52 -0400 Subject: [PATCH] src: simplify `size() == 0` checks MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/53440 Reviewed-By: Paolo Insogna Reviewed-By: Matteo Collina Reviewed-By: Tobias Nießen Reviewed-By: Richard Lau Reviewed-By: Chemi Atlow Reviewed-By: James M Snell Reviewed-By: Chengzhong Wu Reviewed-By: Rafael Gonzaga --- src/blob_serializer_deserializer-inl.h | 2 +- src/crypto/crypto_aes.cc | 2 +- src/crypto/crypto_ec.cc | 3 +-- src/crypto/crypto_spkac.cc | 8 +++----- src/crypto/crypto_util.h | 17 +++++++++++------ src/node_file.cc | 8 ++++---- src/permission/fs_permission.cc | 2 +- src/permission/fs_permission.h | 2 +- 8 files changed, 23 insertions(+), 21 deletions(-) diff --git a/src/blob_serializer_deserializer-inl.h b/src/blob_serializer_deserializer-inl.h index f47a1e0cdf8a44..fc6b8c0c33d245 100644 --- a/src/blob_serializer_deserializer-inl.h +++ b/src/blob_serializer_deserializer-inl.h @@ -246,7 +246,7 @@ size_t BlobSerializer::WriteVector(const std::vector& data) { } size_t written_total = WriteArithmetic(data.size()); - if (data.size() == 0) { + if (data.empty()) { return written_total; } diff --git a/src/crypto/crypto_aes.cc b/src/crypto/crypto_aes.cc index c3f088024a9cb8..774030d408711c 100644 --- a/src/crypto/crypto_aes.cc +++ b/src/crypto/crypto_aes.cc @@ -134,7 +134,7 @@ WebCryptoCipherStatus AES_Cipher( // // Refs: https://github.com/openssl/openssl/commit/420cb707b880e4fb649094241371701013eeb15f // Refs: https://github.com/nodejs/node/pull/38913#issuecomment-866505244 - if (in.size() == 0) { + if (in.empty()) { out_len = 0; } else if (!EVP_CipherUpdate(ctx.get(), buf.data(), diff --git a/src/crypto/crypto_ec.cc b/src/crypto/crypto_ec.cc index 98273ac4eea91b..74656211ab0c10 100644 --- a/src/crypto/crypto_ec.cc +++ b/src/crypto/crypto_ec.cc @@ -398,8 +398,7 @@ void ECDH::ConvertKey(const FunctionCallbackInfo& args) { ArrayBufferOrViewContents args0(args[0]); if (UNLIKELY(!args0.CheckSizeInt32())) return THROW_ERR_OUT_OF_RANGE(env, "key is too big"); - if (args0.size() == 0) - return args.GetReturnValue().SetEmptyString(); + if (args0.empty()) return args.GetReturnValue().SetEmptyString(); node::Utf8Value curve(env->isolate(), args[1]); diff --git a/src/crypto/crypto_spkac.cc b/src/crypto/crypto_spkac.cc index 92484847afb1dc..a09a09ddd2f9d4 100644 --- a/src/crypto/crypto_spkac.cc +++ b/src/crypto/crypto_spkac.cc @@ -39,8 +39,7 @@ bool VerifySpkac(const ArrayBufferOrViewContents& input) { void VerifySpkac(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); ArrayBufferOrViewContents input(args[0]); - if (input.size() == 0) - return args.GetReturnValue().SetEmptyString(); + if (input.empty()) return args.GetReturnValue().SetEmptyString(); if (UNLIKELY(!input.CheckSizeInt32())) return THROW_ERR_OUT_OF_RANGE(env, "spkac is too large"); @@ -76,7 +75,7 @@ void ExportPublicKey(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); ArrayBufferOrViewContents input(args[0]); - if (input.size() == 0) return args.GetReturnValue().SetEmptyString(); + if (input.empty()) return args.GetReturnValue().SetEmptyString(); if (UNLIKELY(!input.CheckSizeInt32())) return THROW_ERR_OUT_OF_RANGE(env, "spkac is too large"); @@ -109,8 +108,7 @@ void ExportChallenge(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); ArrayBufferOrViewContents input(args[0]); - if (input.size() == 0) - return args.GetReturnValue().SetEmptyString(); + if (input.empty()) return args.GetReturnValue().SetEmptyString(); if (UNLIKELY(!input.CheckSizeInt32())) return THROW_ERR_OUT_OF_RANGE(env, "spkac is too large"); diff --git a/src/crypto/crypto_util.h b/src/crypto/crypto_util.h index 0ae2946e5e5884..16d2583d66eecf 100644 --- a/src/crypto/crypto_util.h +++ b/src/crypto/crypto_util.h @@ -233,6 +233,9 @@ class ByteSource { // Returns the (allocated) size in bytes. size_t size() const { return size_; } + // Returns if (allocated) size is zero. + bool empty() const { return size_ == 0; } + // Finalizes the Builder and returns a read-only view that is optionally // truncated. ByteSource release(std::optional resize = std::nullopt) && { @@ -271,6 +274,8 @@ class ByteSource { size_t size() const { return size_; } + bool empty() const { return size_ == 0; } + operator bool() const { return data_ != nullptr; } BignumPointer ToBN() const { @@ -718,8 +723,7 @@ class ArrayBufferOrViewContents { // Ideally, these would return nullptr if IsEmpty() or length_ is zero, // but some of the openssl API react badly if given a nullptr even when // length is zero, so we have to return something. - if (size() == 0) - return &buf; + if (empty()) return &buf; return reinterpret_cast(data_) + offset_; } @@ -727,13 +731,14 @@ class ArrayBufferOrViewContents { // Ideally, these would return nullptr if IsEmpty() or length_ is zero, // but some of the openssl API react badly if given a nullptr even when // length is zero, so we have to return something. - if (size() == 0) - return &buf; + if (empty()) return &buf; return reinterpret_cast(data_) + offset_; } inline size_t size() const { return length_; } + inline bool empty() const { return length_ == 0; } + // In most cases, input buffer sizes passed in to openssl need to // be limited to <= INT_MAX. This utility method helps us check. inline bool CheckSizeInt32() { return size() <= INT_MAX; } @@ -743,14 +748,14 @@ class ArrayBufferOrViewContents { } inline ByteSource ToCopy() const { - if (size() == 0) return ByteSource(); + if (empty()) return ByteSource(); ByteSource::Builder buf(size()); memcpy(buf.data(), data(), size()); return std::move(buf).release(); } inline ByteSource ToNullTerminatedCopy() const { - if (size() == 0) return ByteSource(); + if (empty()) return ByteSource(); ByteSource::Builder buf(size() + 1); memcpy(buf.data(), data(), size()); buf.data()[size()] = 0; diff --git a/src/node_file.cc b/src/node_file.cc index 80ff36f2463035..23c139b7041d8f 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -1705,7 +1705,7 @@ int MKDirpSync(uv_loop_t* loop, // ~FSReqWrapSync(): case 0: req_wrap->continuation_data()->MaybeSetFirstPath(next_path); - if (req_wrap->continuation_data()->paths().size() == 0) { + if (req_wrap->continuation_data()->paths().empty()) { return 0; } break; @@ -1721,7 +1721,7 @@ int MKDirpSync(uv_loop_t* loop, if (dirname != next_path) { req_wrap->continuation_data()->PushPath(std::move(next_path)); req_wrap->continuation_data()->PushPath(std::move(dirname)); - } else if (req_wrap->continuation_data()->paths().size() == 0) { + } else if (req_wrap->continuation_data()->paths().empty()) { err = UV_EEXIST; continue; } @@ -1778,7 +1778,7 @@ int MKDirpAsync(uv_loop_t* loop, // Note: uv_fs_req_cleanup in terminal paths will be called by // FSReqAfterScope::~FSReqAfterScope() case 0: { - if (req_wrap->continuation_data()->paths().size() == 0) { + if (req_wrap->continuation_data()->paths().empty()) { req_wrap->continuation_data()->MaybeSetFirstPath(path); req_wrap->continuation_data()->Done(0); } else { @@ -1801,7 +1801,7 @@ int MKDirpAsync(uv_loop_t* loop, if (dirname != path) { req_wrap->continuation_data()->PushPath(path); req_wrap->continuation_data()->PushPath(std::move(dirname)); - } else if (req_wrap->continuation_data()->paths().size() == 0) { + } else if (req_wrap->continuation_data()->paths().empty()) { err = UV_EEXIST; continue; } diff --git a/src/permission/fs_permission.cc b/src/permission/fs_permission.cc index 4f3fd06539943c..757b42b23a2d71 100644 --- a/src/permission/fs_permission.cc +++ b/src/permission/fs_permission.cc @@ -180,7 +180,7 @@ FSPermission::RadixTree::~RadixTree() { bool FSPermission::RadixTree::Lookup(const std::string_view& s, bool when_empty_return) const { FSPermission::RadixTree::Node* current_node = root_node_; - if (current_node->children.size() == 0) { + if (current_node->children.empty()) { return when_empty_return; } size_t parent_node_prefix_len = current_node->prefix.length(); diff --git a/src/permission/fs_permission.h b/src/permission/fs_permission.h index fea95369fc1bd2..22b29b017e2061 100644 --- a/src/permission/fs_permission.h +++ b/src/permission/fs_permission.h @@ -129,7 +129,7 @@ class FSPermission final : public PermissionBase { // ---> er // ---> n bool IsEndNode() const { - if (children.size() == 0) { + if (children.empty()) { return true; } return is_leaf;