Use std::vector<> instead of new[] allocation (avoid memory leaks by using RAII idiom).
This commit is contained in:
parent
8eac1b40e8
commit
bc0e5a9a48
@ -319,21 +319,19 @@ ref <security::cert::certificateChain> TLSSocket::getPeerCertificates() const
|
|||||||
gnutls_x509_crt_export(x509Certs[i],
|
gnutls_x509_crt_export(x509Certs[i],
|
||||||
GNUTLS_X509_FMT_DER, NULL, &dataSize);
|
GNUTLS_X509_FMT_DER, NULL, &dataSize);
|
||||||
|
|
||||||
byte_t* data = new byte_t[dataSize];
|
std::vector <byte_t> data(dataSize);
|
||||||
|
|
||||||
gnutls_x509_crt_export(x509Certs[i],
|
gnutls_x509_crt_export(x509Certs[i],
|
||||||
GNUTLS_X509_FMT_DER, data, &dataSize);
|
GNUTLS_X509_FMT_DER, &data[0], &dataSize);
|
||||||
|
|
||||||
ref <security::cert::X509Certificate> cert =
|
ref <security::cert::X509Certificate> cert =
|
||||||
security::cert::X509Certificate::import(data, dataSize);
|
security::cert::X509Certificate::import(&data[0], dataSize);
|
||||||
|
|
||||||
if (cert != NULL)
|
if (cert != NULL)
|
||||||
certs.push_back(cert);
|
certs.push_back(cert);
|
||||||
else
|
else
|
||||||
error = true;
|
error = true;
|
||||||
|
|
||||||
delete [] data;
|
|
||||||
|
|
||||||
gnutls_x509_crt_deinit(x509Certs[i]);
|
gnutls_x509_crt_deinit(x509Certs[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -128,19 +128,11 @@ void X509Certificate::write
|
|||||||
|
|
||||||
gnutls_x509_crt_export(m_data->cert, fmt, NULL, &dataSize);
|
gnutls_x509_crt_export(m_data->cert, fmt, NULL, &dataSize);
|
||||||
|
|
||||||
byte_t* data = new byte_t[dataSize];
|
std::vector <byte_t> data(dataSize);
|
||||||
|
|
||||||
gnutls_x509_crt_export(m_data->cert, fmt, data, &dataSize);
|
gnutls_x509_crt_export(m_data->cert, fmt, &data[0], &dataSize);
|
||||||
|
|
||||||
try
|
os.write(reinterpret_cast <utility::stream::value_type*>(&data[0]), dataSize);
|
||||||
{
|
|
||||||
os.write(reinterpret_cast <utility::stream::value_type*>(data), dataSize);
|
|
||||||
}
|
|
||||||
catch (...)
|
|
||||||
{
|
|
||||||
delete [] data;
|
|
||||||
throw;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -212,21 +204,17 @@ const byteArray X509Certificate::getFingerprint(const DigestAlgorithm algo) cons
|
|||||||
gnutls_x509_crt_get_fingerprint
|
gnutls_x509_crt_get_fingerprint
|
||||||
(m_data->cert, galgo, NULL, &bufferSize);
|
(m_data->cert, galgo, NULL, &bufferSize);
|
||||||
|
|
||||||
byte_t* buffer = new byte_t[bufferSize];
|
std::vector <byte_t> buffer(bufferSize);
|
||||||
|
|
||||||
if (gnutls_x509_crt_get_fingerprint
|
if (gnutls_x509_crt_get_fingerprint
|
||||||
(m_data->cert, galgo, buffer, &bufferSize) == 0)
|
(m_data->cert, galgo, &buffer[0], &bufferSize) == 0)
|
||||||
{
|
{
|
||||||
byteArray res;
|
byteArray res;
|
||||||
res.insert(res.end(), buffer, buffer + bufferSize);
|
res.insert(res.end(), &buffer[0], &buffer[0] + bufferSize);
|
||||||
|
|
||||||
delete [] buffer;
|
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
delete [] buffer;
|
|
||||||
|
|
||||||
return byteArray();
|
return byteArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user