Skip to content

Commit 0c7e69a

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 b3372a8 commit 0c7e69a

9 files changed

+24
-22
lines changed

src/blob_serializer_deserializer-inl.h

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

249249
size_t written_total = WriteArithmetic<size_t>(data.size());
250-
if (data.size() == 0) {
250+
if (data.empty()) {
251251
return written_total;
252252
}
253253

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/module_wrap.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ v8::Maybe<bool> ModuleWrap::CheckUnsettledTopLevelAwait() {
125125

126126
auto stalled_messages =
127127
std::get<1>(module->GetStalledTopLevelAwaitMessages(isolate));
128-
if (stalled_messages.size() == 0) {
128+
if (stalled_messages.empty()) {
129129
return v8::Just(true);
130130
}
131131

src/node_file.cc

+4-4
Original file line numberDiff line numberDiff line change
@@ -1580,7 +1580,7 @@ int MKDirpSync(uv_loop_t* loop,
15801580
// ~FSReqWrapSync():
15811581
case 0:
15821582
req_wrap->continuation_data()->MaybeSetFirstPath(next_path);
1583-
if (req_wrap->continuation_data()->paths().size() == 0) {
1583+
if (req_wrap->continuation_data()->paths().empty()) {
15841584
return 0;
15851585
}
15861586
break;
@@ -1596,7 +1596,7 @@ int MKDirpSync(uv_loop_t* loop,
15961596
if (dirname != next_path) {
15971597
req_wrap->continuation_data()->PushPath(std::move(next_path));
15981598
req_wrap->continuation_data()->PushPath(std::move(dirname));
1599-
} else if (req_wrap->continuation_data()->paths().size() == 0) {
1599+
} else if (req_wrap->continuation_data()->paths().empty()) {
16001600
err = UV_EEXIST;
16011601
continue;
16021602
}
@@ -1653,7 +1653,7 @@ int MKDirpAsync(uv_loop_t* loop,
16531653
// Note: uv_fs_req_cleanup in terminal paths will be called by
16541654
// FSReqAfterScope::~FSReqAfterScope()
16551655
case 0: {
1656-
if (req_wrap->continuation_data()->paths().size() == 0) {
1656+
if (req_wrap->continuation_data()->paths().empty()) {
16571657
req_wrap->continuation_data()->MaybeSetFirstPath(path);
16581658
req_wrap->continuation_data()->Done(0);
16591659
} else {
@@ -1676,7 +1676,7 @@ int MKDirpAsync(uv_loop_t* loop,
16761676
if (dirname != path) {
16771677
req_wrap->continuation_data()->PushPath(path);
16781678
req_wrap->continuation_data()->PushPath(std::move(dirname));
1679-
} else if (req_wrap->continuation_data()->paths().size() == 0) {
1679+
} else if (req_wrap->continuation_data()->paths().empty()) {
16801680
err = UV_EEXIST;
16811681
continue;
16821682
}

src/permission/fs_permission.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ FSPermission::RadixTree::~RadixTree() {
177177
bool FSPermission::RadixTree::Lookup(const std::string_view& s,
178178
bool when_empty_return) const {
179179
FSPermission::RadixTree::Node* current_node = root_node_;
180-
if (current_node->children.size() == 0) {
180+
if (current_node->children.empty()) {
181181
return when_empty_return;
182182
}
183183
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)