Skip to content

Commit 1bb5102

Browse files
addaleaxBethGriggs
authored andcommitted
src: use more explicit return type in Sign::SignFinal()
Using the non-indexed variant of `std::get<>` broke Travis CI. Also, this allows us to be a bit more concise when returning from `SignFinal()` due to some error condition. Refs: #23427 PR-URL: #23779 Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent 65e68d1 commit 1bb5102

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

src/node_crypto.cc

+11-12
Original file line numberDiff line numberDiff line change
@@ -3572,22 +3572,20 @@ static MallocedBuffer<unsigned char> Node_SignFinal(EVPMDPointer&& mdctx,
35723572
return MallocedBuffer<unsigned char>();
35733573
}
35743574

3575-
std::pair<SignBase::Error, MallocedBuffer<unsigned char>> Sign::SignFinal(
3575+
Sign::SignResult Sign::SignFinal(
35763576
const char* key_pem,
35773577
int key_pem_len,
35783578
const char* passphrase,
35793579
int padding,
35803580
int salt_len) {
3581-
MallocedBuffer<unsigned char> buffer;
3582-
35833581
if (!mdctx_)
3584-
return std::make_pair(kSignNotInitialised, std::move(buffer));
3582+
return SignResult(kSignNotInitialised);
35853583

35863584
EVPMDPointer mdctx = std::move(mdctx_);
35873585

35883586
BIOPointer bp(BIO_new_mem_buf(const_cast<char*>(key_pem), key_pem_len));
35893587
if (!bp)
3590-
return std::make_pair(kSignPrivateKey, std::move(buffer));
3588+
return SignResult(kSignPrivateKey);
35913589

35923590
EVPKeyPointer pkey(PEM_read_bio_PrivateKey(bp.get(),
35933591
nullptr,
@@ -3598,7 +3596,7 @@ std::pair<SignBase::Error, MallocedBuffer<unsigned char>> Sign::SignFinal(
35983596
// without `pkey` being set to nullptr;
35993597
// cf. the test of `test_bad_rsa_privkey.pem` for an example.
36003598
if (!pkey || 0 != ERR_peek_error())
3601-
return std::make_pair(kSignPrivateKey, std::move(buffer));
3599+
return SignResult(kSignPrivateKey);
36023600

36033601
#ifdef NODE_FIPS_MODE
36043602
/* Validate DSA2 parameters from FIPS 186-4 */
@@ -3622,9 +3620,10 @@ std::pair<SignBase::Error, MallocedBuffer<unsigned char>> Sign::SignFinal(
36223620
}
36233621
#endif // NODE_FIPS_MODE
36243622

3625-
buffer = Node_SignFinal(std::move(mdctx), pkey, padding, salt_len);
3623+
MallocedBuffer<unsigned char> buffer =
3624+
Node_SignFinal(std::move(mdctx), pkey, padding, salt_len);
36263625
Error error = buffer.is_empty() ? kSignPrivateKey : kSignOk;
3627-
return std::make_pair(error, std::move(buffer));
3626+
return SignResult(error, std::move(buffer));
36283627
}
36293628

36303629

@@ -3649,18 +3648,18 @@ void Sign::SignFinal(const FunctionCallbackInfo<Value>& args) {
36493648

36503649
ClearErrorOnReturn clear_error_on_return;
36513650

3652-
std::pair<Error, MallocedBuffer<unsigned char>> ret = sign->SignFinal(
3651+
SignResult ret = sign->SignFinal(
36533652
buf,
36543653
buf_len,
36553654
len >= 2 && !args[1]->IsNull() ? *passphrase : nullptr,
36563655
padding,
36573656
salt_len);
36583657

3659-
if (std::get<Error>(ret) != kSignOk)
3660-
return sign->CheckThrow(std::get<Error>(ret));
3658+
if (ret.error != kSignOk)
3659+
return sign->CheckThrow(ret.error);
36613660

36623661
MallocedBuffer<unsigned char> sig =
3663-
std::move(std::get<MallocedBuffer<unsigned char>>(ret));
3662+
std::move(ret.signature);
36643663

36653664
Local<Object> rc =
36663665
Buffer::New(env, reinterpret_cast<char*>(sig.release()), sig.size)

src/node_crypto.h

+11-1
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,17 @@ class Sign : public SignBase {
516516
public:
517517
static void Initialize(Environment* env, v8::Local<v8::Object> target);
518518

519-
std::pair<Error, MallocedBuffer<unsigned char>> SignFinal(
519+
struct SignResult {
520+
Error error;
521+
MallocedBuffer<unsigned char> signature;
522+
523+
explicit SignResult(
524+
Error err,
525+
MallocedBuffer<unsigned char>&& sig = MallocedBuffer<unsigned char>())
526+
: error(err), signature(std::move(sig)) {}
527+
};
528+
529+
SignResult SignFinal(
520530
const char* key_pem,
521531
int key_pem_len,
522532
const char* passphrase,

0 commit comments

Comments
 (0)