Skip to content

Commit 52abb92

Browse files
committed
refactor: keep vecBytes always properly initialized in bls
1 parent 5906dfb commit 52abb92

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

src/bls/bls.h

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ class CBLSLazyWrapper
390390
private:
391391
mutable std::mutex mutex;
392392

393-
mutable std::array<uint8_t, BLSObject::SerSize> vecBytes;
393+
mutable std::array<uint8_t, BLSObject::SerSize> vecBytes{};
394394

395395
mutable BLSObject obj;
396396
mutable bool objInitialized{false};
@@ -403,7 +403,6 @@ class CBLSLazyWrapper
403403

404404
public:
405405
CBLSLazyWrapper() :
406-
vecBytes{},
407406
bufLegacyScheme(bls::bls_legacy_scheme.load())
408407
{}
409408

@@ -418,16 +417,10 @@ class CBLSLazyWrapper
418417
std::unique_lock<std::mutex> l(r.mutex);
419418
bufValid = r.bufValid;
420419
bufLegacyScheme = r.bufLegacyScheme;
421-
if (r.bufValid) {
422-
vecBytes = r.vecBytes;
423-
} else {
424-
std::fill(vecBytes.begin(), vecBytes.end(), 0);
425-
}
420+
vecBytes = r.vecBytes;
426421
objInitialized = r.objInitialized;
427422
if (r.objInitialized) {
428423
obj = r.obj;
429-
} else {
430-
obj.Reset();
431424
}
432425
hash = r.hash;
433426
return *this;
@@ -442,13 +435,18 @@ class CBLSLazyWrapper
442435
inline void Serialize(Stream& s, const bool specificLegacyScheme) const
443436
{
444437
std::unique_lock<std::mutex> l(mutex);
445-
if (!objInitialized && !bufValid) {
446-
std::fill(vecBytes.begin(), vecBytes.end(), 0);
447-
} else if (!bufValid || (bufLegacyScheme != specificLegacyScheme)) {
448-
vecBytes = obj.ToBytes(specificLegacyScheme);
449-
bufValid = true;
450-
bufLegacyScheme = specificLegacyScheme;
451-
hash.SetNull();
438+
if (!objInitialized) {
439+
if (bufValid && bufLegacyScheme != specificLegacyScheme) {
440+
obj.SetBytes(vecBytes, bufLegacyScheme);
441+
vecBytes = obj.ToBytes(specificLegacyScheme);
442+
}
443+
} else {
444+
if (!bufValid || (bufLegacyScheme != specificLegacyScheme)) {
445+
vecBytes = obj.ToBytes(specificLegacyScheme);
446+
bufValid = true;
447+
bufLegacyScheme = specificLegacyScheme;
448+
hash.SetNull();
449+
}
452450
}
453451
s.write(MakeByteSpan(vecBytes));
454452
}
@@ -483,6 +481,7 @@ class CBLSLazyWrapper
483481
bufLegacyScheme = specificLegacyScheme;
484482
objInitialized = true;
485483
obj = _obj;
484+
std::fill(vecBytes.begin(), vecBytes.end(), 0);
486485
hash.SetNull();
487486
}
488487
const BLSObject& Get() const
@@ -539,7 +538,6 @@ class CBLSLazyWrapper
539538
{
540539
std::unique_lock<std::mutex> l(mutex);
541540
if (!objInitialized && !bufValid) {
542-
std::fill(vecBytes.begin(), vecBytes.end(), 0);
543541
hash.SetNull();
544542
} else if (!bufValid) {
545543
vecBytes = obj.ToBytes(bufLegacyScheme);

0 commit comments

Comments
 (0)