Use std::vector<> instead of new[] allocation (avoid memory leaks by using RAII idiom).

This commit is contained in:
Vincent Richard 2007-12-23 16:19:39 +00:00
parent 8eac1b40e8
commit bc0e5a9a48
2 changed files with 9 additions and 23 deletions

View File

@ -319,21 +319,19 @@ ref <security::cert::certificateChain> TLSSocket::getPeerCertificates() const
gnutls_x509_crt_export(x509Certs[i],
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_FMT_DER, data, &dataSize);
GNUTLS_X509_FMT_DER, &data[0], &dataSize);
ref <security::cert::X509Certificate> cert =
security::cert::X509Certificate::import(data, dataSize);
security::cert::X509Certificate::import(&data[0], dataSize);
if (cert != NULL)
certs.push_back(cert);
else
error = true;
delete [] data;
gnutls_x509_crt_deinit(x509Certs[i]);
}

View File

@ -128,19 +128,11 @@ void X509Certificate::write
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), dataSize);
}
catch (...)
{
delete [] data;
throw;
}
os.write(reinterpret_cast <utility::stream::value_type*>(&data[0]), dataSize);
}
@ -212,21 +204,17 @@ const byteArray X509Certificate::getFingerprint(const DigestAlgorithm algo) cons
gnutls_x509_crt_get_fingerprint
(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
(m_data->cert, galgo, buffer, &bufferSize) == 0)
(m_data->cert, galgo, &buffer[0], &bufferSize) == 0)
{
byteArray res;
res.insert(res.end(), buffer, buffer + bufferSize);
delete [] buffer;
res.insert(res.end(), &buffer[0], &buffer[0] + bufferSize);
return res;
}
delete [] buffer;
return byteArray();
}