diff --git a/src/blob_serializer_deserializer-inl.h b/src/blob_serializer_deserializer-inl.h index ecf664fc807b68..7f5a3860b5b7e5 100644 --- a/src/blob_serializer_deserializer-inl.h +++ b/src/blob_serializer_deserializer-inl.h @@ -247,7 +247,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 de058d077d1f45..6ec43c0a461e78 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 860d5048db7611..0a01851076a776 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/module_wrap.cc b/src/module_wrap.cc index 1e2ceb931bbfac..f744ebec3828a9 100644 --- a/src/module_wrap.cc +++ b/src/module_wrap.cc @@ -125,7 +125,7 @@ v8::Maybe ModuleWrap::CheckUnsettledTopLevelAwait() { auto stalled_messages = std::get<1>(module->GetStalledTopLevelAwaitMessages(isolate)); - if (stalled_messages.size() == 0) { + if (stalled_messages.empty()) { return v8::Just(true); } diff --git a/src/node_file.cc b/src/node_file.cc index 3d53f84dfbbf83..ccfd6a740dd9cf 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -1580,7 +1580,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; @@ -1596,7 +1596,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; } @@ -1653,7 +1653,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 { @@ -1676,7 +1676,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 22834e05947d96..ced07e26b94ef2 100644 --- a/src/permission/fs_permission.cc +++ b/src/permission/fs_permission.cc @@ -177,7 +177,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;