Skip to content

Commit b8cb60f

Browse files
addaleaxjasnell
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 <joyeec9h3@gmail.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Refael Ackermann <refack@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
1 parent 39fcda0 commit b8cb60f

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
@@ -3553,22 +3553,20 @@ static MallocedBuffer<unsigned char> Node_SignFinal(EVPMDPointer&& mdctx,
35533553
return MallocedBuffer<unsigned char>();
35543554
}
35553555

3556-
std::pair<SignBase::Error, MallocedBuffer<unsigned char>> Sign::SignFinal(
3556+
Sign::SignResult Sign::SignFinal(
35573557
const char* key_pem,
35583558
int key_pem_len,
35593559
const char* passphrase,
35603560
int padding,
35613561
int salt_len) {
3562-
MallocedBuffer<unsigned char> buffer;
3563-
35643562
if (!mdctx_)
3565-
return std::make_pair(kSignNotInitialised, std::move(buffer));
3563+
return SignResult(kSignNotInitialised);
35663564

35673565
EVPMDPointer mdctx = std::move(mdctx_);
35683566

35693567
BIOPointer bp(BIO_new_mem_buf(const_cast<char*>(key_pem), key_pem_len));
35703568
if (!bp)
3571-
return std::make_pair(kSignPrivateKey, std::move(buffer));
3569+
return SignResult(kSignPrivateKey);
35723570

35733571
EVPKeyPointer pkey(PEM_read_bio_PrivateKey(bp.get(),
35743572
nullptr,
@@ -3579,7 +3577,7 @@ std::pair<SignBase::Error, MallocedBuffer<unsigned char>> Sign::SignFinal(
35793577
// without `pkey` being set to nullptr;
35803578
// cf. the test of `test_bad_rsa_privkey.pem` for an example.
35813579
if (!pkey || 0 != ERR_peek_error())
3582-
return std::make_pair(kSignPrivateKey, std::move(buffer));
3580+
return SignResult(kSignPrivateKey);
35833581

35843582
#ifdef NODE_FIPS_MODE
35853583
/* Validate DSA2 parameters from FIPS 186-4 */
@@ -3603,9 +3601,10 @@ std::pair<SignBase::Error, MallocedBuffer<unsigned char>> Sign::SignFinal(
36033601
}
36043602
#endif // NODE_FIPS_MODE
36053603

3606-
buffer = Node_SignFinal(std::move(mdctx), pkey, padding, salt_len);
3604+
MallocedBuffer<unsigned char> buffer =
3605+
Node_SignFinal(std::move(mdctx), pkey, padding, salt_len);
36073606
Error error = buffer.is_empty() ? kSignPrivateKey : kSignOk;
3608-
return std::make_pair(error, std::move(buffer));
3607+
return SignResult(error, std::move(buffer));
36093608
}
36103609

36113610

@@ -3630,18 +3629,18 @@ void Sign::SignFinal(const FunctionCallbackInfo<Value>& args) {
36303629

36313630
ClearErrorOnReturn clear_error_on_return;
36323631

3633-
std::pair<Error, MallocedBuffer<unsigned char>> ret = sign->SignFinal(
3632+
SignResult ret = sign->SignFinal(
36343633
buf,
36353634
buf_len,
36363635
len >= 2 && !args[1]->IsNull() ? *passphrase : nullptr,
36373636
padding,
36383637
salt_len);
36393638

3640-
if (std::get<Error>(ret) != kSignOk)
3641-
return sign->CheckThrow(std::get<Error>(ret));
3639+
if (ret.error != kSignOk)
3640+
return sign->CheckThrow(ret.error);
36423641

36433642
MallocedBuffer<unsigned char> sig =
3644-
std::move(std::get<MallocedBuffer<unsigned char>>(ret));
3643+
std::move(ret.signature);
36453644

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

src/node_crypto.h

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

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

0 commit comments

Comments
 (0)