Skip to content

Commit

Permalink
src: simplify size() == 0 checks
Browse files Browse the repository at this point in the history
PR-URL: #53440
Reviewed-By: Paolo Insogna <paolo@cowtech.it>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Richard Lau <rlau@redhat.com>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
  • Loading branch information
anonrig authored and targos committed Sep 21, 2024
1 parent 3f78d4e commit b05c56e
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/blob_serializer_deserializer-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ size_t BlobSerializer<Impl>::WriteVector(const std::vector<T>& data) {
}

size_t written_total = WriteArithmetic<size_t>(data.size());
if (data.size() == 0) {
if (data.empty()) {
return written_total;
}

Expand Down
2 changes: 1 addition & 1 deletion src/crypto/crypto_aes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<unsigned char>(),
Expand Down
3 changes: 1 addition & 2 deletions src/crypto/crypto_ec.cc
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,7 @@ void ECDH::ConvertKey(const FunctionCallbackInfo<Value>& args) {
ArrayBufferOrViewContents<char> 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]);

Expand Down
8 changes: 3 additions & 5 deletions src/crypto/crypto_spkac.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ bool VerifySpkac(const ArrayBufferOrViewContents<char>& input) {
void VerifySpkac(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
ArrayBufferOrViewContents<char> 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");
Expand Down Expand Up @@ -76,7 +75,7 @@ void ExportPublicKey(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

ArrayBufferOrViewContents<char> 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");
Expand Down Expand Up @@ -109,8 +108,7 @@ void ExportChallenge(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

ArrayBufferOrViewContents<char> 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");
Expand Down
17 changes: 11 additions & 6 deletions src/crypto/crypto_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<size_t> resize = std::nullopt) && {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -718,22 +723,22 @@ 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<T*>(data_) + offset_;
}

inline T* data() {
// 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<T*>(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; }
Expand All @@ -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<void>(), 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<void>(), data(), size());
buf.data<char>()[size()] = 0;
Expand Down
8 changes: 4 additions & 4 deletions src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/permission/fs_permission.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/permission/fs_permission.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit b05c56e

Please sign in to comment.