Skip to content

Commit 20282b1

Browse files
committed
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 0feb21f commit 20282b1

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

src/node_crypto.cc

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3562,22 +3562,20 @@ static MallocedBuffer<unsigned char> Node_SignFinal(EVPMDPointer&& mdctx,
35623562
return MallocedBuffer<unsigned char>();
35633563
}
35643564

3565-
std::pair<SignBase::Error, MallocedBuffer<unsigned char>> Sign::SignFinal(
3565+
Sign::SignResult Sign::SignFinal(
35663566
const char* key_pem,
35673567
int key_pem_len,
35683568
const char* passphrase,
35693569
int padding,
35703570
int salt_len) {
3571-
MallocedBuffer<unsigned char> buffer;
3572-
35733571
if (!mdctx_)
3574-
return std::make_pair(kSignNotInitialised, std::move(buffer));
3572+
return SignResult(kSignNotInitialised);
35753573

35763574
EVPMDPointer mdctx = std::move(mdctx_);
35773575

35783576
BIOPointer bp(BIO_new_mem_buf(const_cast<char*>(key_pem), key_pem_len));
35793577
if (!bp)
3580-
return std::make_pair(kSignPrivateKey, std::move(buffer));
3578+
return SignResult(kSignPrivateKey);
35813579

35823580
EVPKeyPointer pkey(PEM_read_bio_PrivateKey(bp.get(),
35833581
nullptr,
@@ -3588,7 +3586,7 @@ std::pair<SignBase::Error, MallocedBuffer<unsigned char>> Sign::SignFinal(
35883586
// without `pkey` being set to nullptr;
35893587
// cf. the test of `test_bad_rsa_privkey.pem` for an example.
35903588
if (!pkey || 0 != ERR_peek_error())
3591-
return std::make_pair(kSignPrivateKey, std::move(buffer));
3589+
return SignResult(kSignPrivateKey);
35923590

35933591
#ifdef NODE_FIPS_MODE
35943592
/* Validate DSA2 parameters from FIPS 186-4 */
@@ -3612,9 +3610,10 @@ std::pair<SignBase::Error, MallocedBuffer<unsigned char>> Sign::SignFinal(
36123610
}
36133611
#endif // NODE_FIPS_MODE
36143612

3615-
buffer = Node_SignFinal(std::move(mdctx), pkey, padding, salt_len);
3613+
MallocedBuffer<unsigned char> buffer =
3614+
Node_SignFinal(std::move(mdctx), pkey, padding, salt_len);
36163615
Error error = buffer.is_empty() ? kSignPrivateKey : kSignOk;
3617-
return std::make_pair(error, std::move(buffer));
3616+
return SignResult(error, std::move(buffer));
36183617
}
36193618

36203619

@@ -3639,18 +3638,18 @@ void Sign::SignFinal(const FunctionCallbackInfo<Value>& args) {
36393638

36403639
ClearErrorOnReturn clear_error_on_return;
36413640

3642-
std::pair<Error, MallocedBuffer<unsigned char>> ret = sign->SignFinal(
3641+
SignResult ret = sign->SignFinal(
36433642
buf,
36443643
buf_len,
36453644
len >= 2 && !args[1]->IsNull() ? *passphrase : nullptr,
36463645
padding,
36473646
salt_len);
36483647

3649-
if (std::get<Error>(ret) != kSignOk)
3650-
return sign->CheckThrow(std::get<Error>(ret));
3648+
if (ret.error != kSignOk)
3649+
return sign->CheckThrow(ret.error);
36513650

36523651
MallocedBuffer<unsigned char> sig =
3653-
std::move(std::get<MallocedBuffer<unsigned char>>(ret));
3652+
std::move(ret.signature);
36543653

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

src/node_crypto.h

Lines changed: 11 additions & 1 deletion
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)