Skip to content

Commit b05c56e

Browse files
anonrigtargos
authored andcommitted
src: simplify size() == 0 checks
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>
1 parent 3f78d4e commit b05c56e

8 files changed

+23
-21
lines changed

src/blob_serializer_deserializer-inl.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ size_t BlobSerializer<Impl>::WriteVector(const std::vector<T>& data) {
246246
}
247247

248248
size_t written_total = WriteArithmetic<size_t>(data.size());
249-
if (data.size() == 0) {
249+
if (data.empty()) {
250250
return written_total;
251251
}
252252

src/crypto/crypto_aes.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ WebCryptoCipherStatus AES_Cipher(
134134
//
135135
// Refs: https://github.com/openssl/openssl/commit/420cb707b880e4fb649094241371701013eeb15f
136136
// Refs: https://github.com/nodejs/node/pull/38913#issuecomment-866505244
137-
if (in.size() == 0) {
137+
if (in.empty()) {
138138
out_len = 0;
139139
} else if (!EVP_CipherUpdate(ctx.get(),
140140
buf.data<unsigned char>(),

src/crypto/crypto_ec.cc

+1-2
Original file line numberDiff line numberDiff line change
@@ -398,8 +398,7 @@ void ECDH::ConvertKey(const FunctionCallbackInfo<Value>& args) {
398398
ArrayBufferOrViewContents<char> args0(args[0]);
399399
if (UNLIKELY(!args0.CheckSizeInt32()))
400400
return THROW_ERR_OUT_OF_RANGE(env, "key is too big");
401-
if (args0.size() == 0)
402-
return args.GetReturnValue().SetEmptyString();
401+
if (args0.empty()) return args.GetReturnValue().SetEmptyString();
403402

404403
node::Utf8Value curve(env->isolate(), args[1]);
405404

src/crypto/crypto_spkac.cc

+3-5
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ bool VerifySpkac(const ArrayBufferOrViewContents<char>& input) {
3939
void VerifySpkac(const FunctionCallbackInfo<Value>& args) {
4040
Environment* env = Environment::GetCurrent(args);
4141
ArrayBufferOrViewContents<char> input(args[0]);
42-
if (input.size() == 0)
43-
return args.GetReturnValue().SetEmptyString();
42+
if (input.empty()) return args.GetReturnValue().SetEmptyString();
4443

4544
if (UNLIKELY(!input.CheckSizeInt32()))
4645
return THROW_ERR_OUT_OF_RANGE(env, "spkac is too large");
@@ -76,7 +75,7 @@ void ExportPublicKey(const FunctionCallbackInfo<Value>& args) {
7675
Environment* env = Environment::GetCurrent(args);
7776

7877
ArrayBufferOrViewContents<char> input(args[0]);
79-
if (input.size() == 0) return args.GetReturnValue().SetEmptyString();
78+
if (input.empty()) return args.GetReturnValue().SetEmptyString();
8079

8180
if (UNLIKELY(!input.CheckSizeInt32()))
8281
return THROW_ERR_OUT_OF_RANGE(env, "spkac is too large");
@@ -109,8 +108,7 @@ void ExportChallenge(const FunctionCallbackInfo<Value>& args) {
109108
Environment* env = Environment::GetCurrent(args);
110109

111110
ArrayBufferOrViewContents<char> input(args[0]);
112-
if (input.size() == 0)
113-
return args.GetReturnValue().SetEmptyString();
111+
if (input.empty()) return args.GetReturnValue().SetEmptyString();
114112

115113
if (UNLIKELY(!input.CheckSizeInt32()))
116114
return THROW_ERR_OUT_OF_RANGE(env, "spkac is too large");

src/crypto/crypto_util.h

+11-6
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,9 @@ class ByteSource {
233233
// Returns the (allocated) size in bytes.
234234
size_t size() const { return size_; }
235235

236+
// Returns if (allocated) size is zero.
237+
bool empty() const { return size_ == 0; }
238+
236239
// Finalizes the Builder and returns a read-only view that is optionally
237240
// truncated.
238241
ByteSource release(std::optional<size_t> resize = std::nullopt) && {
@@ -271,6 +274,8 @@ class ByteSource {
271274

272275
size_t size() const { return size_; }
273276

277+
bool empty() const { return size_ == 0; }
278+
274279
operator bool() const { return data_ != nullptr; }
275280

276281
BignumPointer ToBN() const {
@@ -718,22 +723,22 @@ class ArrayBufferOrViewContents {
718723
// Ideally, these would return nullptr if IsEmpty() or length_ is zero,
719724
// but some of the openssl API react badly if given a nullptr even when
720725
// length is zero, so we have to return something.
721-
if (size() == 0)
722-
return &buf;
726+
if (empty()) return &buf;
723727
return reinterpret_cast<T*>(data_) + offset_;
724728
}
725729

726730
inline T* data() {
727731
// Ideally, these would return nullptr if IsEmpty() or length_ is zero,
728732
// but some of the openssl API react badly if given a nullptr even when
729733
// length is zero, so we have to return something.
730-
if (size() == 0)
731-
return &buf;
734+
if (empty()) return &buf;
732735
return reinterpret_cast<T*>(data_) + offset_;
733736
}
734737

735738
inline size_t size() const { return length_; }
736739

740+
inline bool empty() const { return length_ == 0; }
741+
737742
// In most cases, input buffer sizes passed in to openssl need to
738743
// be limited to <= INT_MAX. This utility method helps us check.
739744
inline bool CheckSizeInt32() { return size() <= INT_MAX; }
@@ -743,14 +748,14 @@ class ArrayBufferOrViewContents {
743748
}
744749

745750
inline ByteSource ToCopy() const {
746-
if (size() == 0) return ByteSource();
751+
if (empty()) return ByteSource();
747752
ByteSource::Builder buf(size());
748753
memcpy(buf.data<void>(), data(), size());
749754
return std::move(buf).release();
750755
}
751756

752757
inline ByteSource ToNullTerminatedCopy() const {
753-
if (size() == 0) return ByteSource();
758+
if (empty()) return ByteSource();
754759
ByteSource::Builder buf(size() + 1);
755760
memcpy(buf.data<void>(), data(), size());
756761
buf.data<char>()[size()] = 0;

src/node_file.cc

+4-4
Original file line numberDiff line numberDiff line change
@@ -1705,7 +1705,7 @@ int MKDirpSync(uv_loop_t* loop,
17051705
// ~FSReqWrapSync():
17061706
case 0:
17071707
req_wrap->continuation_data()->MaybeSetFirstPath(next_path);
1708-
if (req_wrap->continuation_data()->paths().size() == 0) {
1708+
if (req_wrap->continuation_data()->paths().empty()) {
17091709
return 0;
17101710
}
17111711
break;
@@ -1721,7 +1721,7 @@ int MKDirpSync(uv_loop_t* loop,
17211721
if (dirname != next_path) {
17221722
req_wrap->continuation_data()->PushPath(std::move(next_path));
17231723
req_wrap->continuation_data()->PushPath(std::move(dirname));
1724-
} else if (req_wrap->continuation_data()->paths().size() == 0) {
1724+
} else if (req_wrap->continuation_data()->paths().empty()) {
17251725
err = UV_EEXIST;
17261726
continue;
17271727
}
@@ -1778,7 +1778,7 @@ int MKDirpAsync(uv_loop_t* loop,
17781778
// Note: uv_fs_req_cleanup in terminal paths will be called by
17791779
// FSReqAfterScope::~FSReqAfterScope()
17801780
case 0: {
1781-
if (req_wrap->continuation_data()->paths().size() == 0) {
1781+
if (req_wrap->continuation_data()->paths().empty()) {
17821782
req_wrap->continuation_data()->MaybeSetFirstPath(path);
17831783
req_wrap->continuation_data()->Done(0);
17841784
} else {
@@ -1801,7 +1801,7 @@ int MKDirpAsync(uv_loop_t* loop,
18011801
if (dirname != path) {
18021802
req_wrap->continuation_data()->PushPath(path);
18031803
req_wrap->continuation_data()->PushPath(std::move(dirname));
1804-
} else if (req_wrap->continuation_data()->paths().size() == 0) {
1804+
} else if (req_wrap->continuation_data()->paths().empty()) {
18051805
err = UV_EEXIST;
18061806
continue;
18071807
}

src/permission/fs_permission.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ FSPermission::RadixTree::~RadixTree() {
180180
bool FSPermission::RadixTree::Lookup(const std::string_view& s,
181181
bool when_empty_return) const {
182182
FSPermission::RadixTree::Node* current_node = root_node_;
183-
if (current_node->children.size() == 0) {
183+
if (current_node->children.empty()) {
184184
return when_empty_return;
185185
}
186186
size_t parent_node_prefix_len = current_node->prefix.length();

src/permission/fs_permission.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ class FSPermission final : public PermissionBase {
129129
// ---> er
130130
// ---> n
131131
bool IsEndNode() const {
132-
if (children.size() == 0) {
132+
if (children.empty()) {
133133
return true;
134134
}
135135
return is_leaf;

0 commit comments

Comments
 (0)