From a7f56f0bfb1d6db3fce24926470415e3c811ab31 Mon Sep 17 00:00:00 2001 From: David Benjamin Date: Sun, 23 Oct 2016 13:05:02 -0400 Subject: [PATCH] Use scopers a little more. Change-Id: I51e5a7dac3ceffc41d3a7a57157a11258e65bc42 Reviewed-on: https://boringssl-review.googlesource.com/11721 Commit-Queue: David Benjamin Reviewed-by: Adam Langley --- crypto/bn/bn_test.cc | 19 +++++++------------ crypto/x509/x509_test.cc | 16 +++++++++------- 2 files changed, 16 insertions(+), 19 deletions(-) diff --git a/crypto/bn/bn_test.cc b/crypto/bn/bn_test.cc index 04888100..efb6bc5b 100644 --- a/crypto/bn/bn_test.cc +++ b/crypto/bn/bn_test.cc @@ -1024,14 +1024,12 @@ static bool TestASN1() { } // Test the value serializes correctly. - CBB cbb; + bssl::ScopedCBB cbb; uint8_t *der; size_t der_len; - CBB_zero(&cbb); - if (!CBB_init(&cbb, 0) || - !BN_marshal_asn1(&cbb, bn.get()) || - !CBB_finish(&cbb, &der, &der_len)) { - CBB_cleanup(&cbb); + if (!CBB_init(cbb.get(), 0) || + !BN_marshal_asn1(cbb.get(), bn.get()) || + !CBB_finish(cbb.get(), &der, &der_len)) { return false; } bssl::UniquePtr delete_der(der); @@ -1114,16 +1112,13 @@ static bool TestASN1() { if (!bn) { return false; } - CBB cbb; - CBB_zero(&cbb); - if (!CBB_init(&cbb, 0) || - BN_marshal_asn1(&cbb, bn.get())) { + bssl::ScopedCBB cbb; + if (!CBB_init(cbb.get(), 0) || + BN_marshal_asn1(cbb.get(), bn.get())) { fprintf(stderr, "Serialized negative number.\n"); - CBB_cleanup(&cbb); return false; } ERR_clear_error(); - CBB_cleanup(&cbb); return true; } diff --git a/crypto/x509/x509_test.cc b/crypto/x509/x509_test.cc index 17e9bffd..ed09c846 100644 --- a/crypto/x509/x509_test.cc +++ b/crypto/x509/x509_test.cc @@ -382,9 +382,10 @@ static bssl::UniquePtr PrivateKeyFromPEM(const char *pem) { PEM_read_bio_PrivateKey(bio.get(), nullptr, nullptr, nullptr)); } -// CertsToStack converts a vector of |X509*| to an OpenSSL STACK_OF(X509*), +// CertsToStack converts a vector of |X509*| to an OpenSSL STACK_OF(X509), // bumping the reference counts for each certificate in question. -static STACK_OF(X509)* CertsToStack(const std::vector &certs) { +static bssl::UniquePtr CertsToStack( + const std::vector &certs) { bssl::UniquePtr stack(sk_X509_new_null()); if (!stack) { return nullptr; @@ -396,12 +397,13 @@ static STACK_OF(X509)* CertsToStack(const std::vector &certs) { X509_up_ref(cert); } - return stack.release(); + return stack; } -// CRLsToStack converts a vector of |X509_CRL*| to an OpenSSL STACK_OF(X509_CRL*), -// bumping the reference counts for each CRL in question. -static STACK_OF(X509_CRL)* CRLsToStack(const std::vector &crls) { +// CRLsToStack converts a vector of |X509_CRL*| to an OpenSSL +// STACK_OF(X509_CRL), bumping the reference counts for each CRL in question. +static bssl::UniquePtr CRLsToStack( + const std::vector &crls) { bssl::UniquePtr stack(sk_X509_CRL_new_null()); if (!stack) { return nullptr; @@ -413,7 +415,7 @@ static STACK_OF(X509_CRL)* CRLsToStack(const std::vector &crls) { X509_CRL_up_ref(crl); } - return stack.release(); + return stack; } static int Verify(X509 *leaf, const std::vector &roots,