From a4dbb5c58270959884c17d720185da06464fa256 Mon Sep 17 00:00:00 2001 From: peterbell10 Date: Wed, 2 May 2018 08:50:36 +0100 Subject: Prefer static_cast to reinterpret_cast (#4223) * Change reinterpret_cast -> static_cast wherever possible * Remove more unnecessary `const_cast`s. reinterpret_casts should be avoided for the same reason as c-style casts - they don't do any type-checking. reinterpret_cast was mainly being used for down-casting in inheritance hierarchies but static_cast works just as well while also making sure that there is actually an inheritance relationship there. --- src/mbedTLS++/BlockingSslClientSocket.cpp | 2 +- src/mbedTLS++/CryptoKey.cpp | 4 ++-- src/mbedTLS++/CtrDrbgContext.cpp | 2 +- src/mbedTLS++/SslContext.cpp | 4 ++-- src/mbedTLS++/SslContext.h | 4 ++-- src/mbedTLS++/X509Cert.cpp | 2 +- 6 files changed, 9 insertions(+), 9 deletions(-) (limited to 'src/mbedTLS++') diff --git a/src/mbedTLS++/BlockingSslClientSocket.cpp b/src/mbedTLS++/BlockingSslClientSocket.cpp index 9267975cb..e8f616258 100644 --- a/src/mbedTLS++/BlockingSslClientSocket.cpp +++ b/src/mbedTLS++/BlockingSslClientSocket.cpp @@ -208,7 +208,7 @@ bool cBlockingSslClientSocket::Send(const void * a_Data, size_t a_NumBytes) } // Keep sending the data until all of it is sent: - const char * Data = reinterpret_cast(a_Data); + const char * Data = static_cast(a_Data); size_t NumBytes = a_NumBytes; for (;;) { diff --git a/src/mbedTLS++/CryptoKey.cpp b/src/mbedTLS++/CryptoKey.cpp index 6615991d6..b0c0da167 100644 --- a/src/mbedTLS++/CryptoKey.cpp +++ b/src/mbedTLS++/CryptoKey.cpp @@ -109,7 +109,7 @@ int cCryptoKey::ParsePublic(const void * a_Data, size_t a_NumBytes) { ASSERT(!IsValid()); // Cannot parse a second key - return mbedtls_pk_parse_public_key(&m_Pk, reinterpret_cast(a_Data), a_NumBytes); + return mbedtls_pk_parse_public_key(&m_Pk, static_cast(a_Data), a_NumBytes); } @@ -122,7 +122,7 @@ int cCryptoKey::ParsePrivate(const void * a_Data, size_t a_NumBytes, const AStri ASSERT(!IsValid()); // Cannot parse a second key // mbedTLS requires that PEM-encoded data is passed including the terminating NUL byte, // and DER-encoded data is decoded properly even with an extra trailing NUL byte, so we simply add one to everything: - AString keyData(reinterpret_cast(a_Data), a_NumBytes); + AString keyData(static_cast(a_Data), a_NumBytes); if (a_Password.empty()) { diff --git a/src/mbedTLS++/CtrDrbgContext.cpp b/src/mbedTLS++/CtrDrbgContext.cpp index a2b9ee2dc..07f57001d 100644 --- a/src/mbedTLS++/CtrDrbgContext.cpp +++ b/src/mbedTLS++/CtrDrbgContext.cpp @@ -41,7 +41,7 @@ int cCtrDrbgContext::Initialize(const void * a_Custom, size_t a_CustomSize) return 0; } - int res = mbedtls_ctr_drbg_seed(&m_CtrDrbg, mbedtls_entropy_func, &(m_EntropyContext->m_Entropy), reinterpret_cast(a_Custom), a_CustomSize); + int res = mbedtls_ctr_drbg_seed(&m_CtrDrbg, mbedtls_entropy_func, &(m_EntropyContext->m_Entropy), static_cast(a_Custom), a_CustomSize); m_IsValid = (res == 0); return res; } diff --git a/src/mbedTLS++/SslContext.cpp b/src/mbedTLS++/SslContext.cpp index 679362c7a..d5b8525a6 100644 --- a/src/mbedTLS++/SslContext.cpp +++ b/src/mbedTLS++/SslContext.cpp @@ -104,7 +104,7 @@ int cSslContext::WritePlain(const void * a_Data, size_t a_NumBytes) } } - return mbedtls_ssl_write(&m_Ssl, reinterpret_cast(a_Data), a_NumBytes); + return mbedtls_ssl_write(&m_Ssl, static_cast(a_Data), a_NumBytes); } @@ -123,7 +123,7 @@ int cSslContext::ReadPlain(void * a_Data, size_t a_MaxBytes) } } - return mbedtls_ssl_read(&m_Ssl, reinterpret_cast(a_Data), a_MaxBytes); + return mbedtls_ssl_read(&m_Ssl, static_cast(a_Data), a_MaxBytes); } diff --git a/src/mbedTLS++/SslContext.h b/src/mbedTLS++/SslContext.h index d015b6587..033749bf3 100644 --- a/src/mbedTLS++/SslContext.h +++ b/src/mbedTLS++/SslContext.h @@ -103,13 +103,13 @@ protected: /** The callback used by mbedTLS when it wants to read encrypted data. */ static int ReceiveEncrypted(void * a_This, unsigned char * a_Buffer, size_t a_NumBytes) { - return (reinterpret_cast(a_This))->ReceiveEncrypted(a_Buffer, a_NumBytes); + return (static_cast(a_This))->ReceiveEncrypted(a_Buffer, a_NumBytes); } /** The callback used by mbedTLS when it wants to write encrypted data. */ static int SendEncrypted(void * a_This, const unsigned char * a_Buffer, size_t a_NumBytes) { - return (reinterpret_cast(a_This))->SendEncrypted(a_Buffer, a_NumBytes); + return (static_cast(a_This))->SendEncrypted(a_Buffer, a_NumBytes); } /** Called when mbedTLS wants to read encrypted data. */ diff --git a/src/mbedTLS++/X509Cert.cpp b/src/mbedTLS++/X509Cert.cpp index 7bcfec415..e7484af3b 100644 --- a/src/mbedTLS++/X509Cert.cpp +++ b/src/mbedTLS++/X509Cert.cpp @@ -32,7 +32,7 @@ int cX509Cert::Parse(const void * a_CertContents, size_t a_Size) { // mbedTLS requires that PEM-encoded data is passed including the terminating NUL byte, // and DER-encoded data is decoded properly even with an extra trailing NUL byte, so we simply add one to everything: - AString certContents(reinterpret_cast(a_CertContents), a_Size); + AString certContents(static_cast(a_CertContents), a_Size); return mbedtls_x509_crt_parse(&m_Cert, reinterpret_cast(certContents.data()), a_Size + 1); } -- cgit v1.2.3