aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2017-10-03 00:38:15 -0400
committerJack Lloyd <[email protected]>2017-10-03 00:38:15 -0400
commit04d64c3e0fe60a25b1f1a5c2eaf7e2986d2130dd (patch)
tree3dc2cc7e970fc5f1cdc94887b03704d82c37e07e
parent180540de74c58a72492692f58b63f32647e80bd8 (diff)
Add wrappers for reinterpret_cast between char* and uint8_t*
Generally speaking reinterpret_cast is sketchy stuff. But the special case of char*/uint8_t* is both common and safe. By isolating those, the remaining (likely sketchy) cases are easier to grep for.
-rw-r--r--src/lib/asn1/der_enc.cpp2
-rw-r--r--src/lib/base/buf_comp.h2
-rw-r--r--src/lib/ffi/ffi_mp.cpp2
-rw-r--r--src/lib/ffi/ffi_util.h9
-rw-r--r--src/lib/filters/codec_filt/b64_filt.cpp8
-rw-r--r--src/lib/filters/codec_filt/hex_filt.cpp6
-rw-r--r--src/lib/filters/data_snk.cpp2
-rw-r--r--src/lib/filters/pipe.cpp2
-rw-r--r--src/lib/filters/pipe_io.cpp4
-rw-r--r--src/lib/filters/pipe_rw.cpp4
-rw-r--r--src/lib/hash/skein/skein_512.cpp2
-rw-r--r--src/lib/kdf/hkdf/hkdf.cpp2
-rw-r--r--src/lib/kdf/kdf.h10
-rw-r--r--src/lib/math/bigint/big_code.cpp6
-rw-r--r--src/lib/math/bigint/big_io.cpp2
-rw-r--r--src/lib/math/bigint/bigint.cpp2
-rw-r--r--src/lib/misc/cryptobox/cryptobox.cpp6
-rw-r--r--src/lib/passhash/bcrypt/bcrypt.cpp4
-rw-r--r--src/lib/pbkdf/pbkdf2/pbkdf2.cpp2
-rw-r--r--src/lib/pbkdf/pgp_s2k/pgp_s2k.cpp2
-rw-r--r--src/lib/pubkey/pubkey.h8
-rw-r--r--src/lib/tls/tls_channel.cpp2
-rw-r--r--src/lib/tls/tls_extensions.cpp8
-rw-r--r--src/lib/tls/tls_reader.h4
-rw-r--r--src/lib/utils/data_src.cpp10
-rw-r--r--src/lib/utils/http_util/http_util.cpp12
-rw-r--r--src/lib/utils/locking_allocator/locking_allocator.cpp4
-rw-r--r--src/lib/utils/mem_ops.h20
-rw-r--r--src/lib/utils/os_utils.cpp4
29 files changed, 85 insertions, 66 deletions
diff --git a/src/lib/asn1/der_enc.cpp b/src/lib/asn1/der_enc.cpp
index 141391a1d..e791bce8c 100644
--- a/src/lib/asn1/der_enc.cpp
+++ b/src/lib/asn1/der_enc.cpp
@@ -394,7 +394,7 @@ DER_Encoder& DER_Encoder::add_object(ASN1_Tag type_tag, ASN1_Tag class_tag,
DER_Encoder& DER_Encoder::add_object(ASN1_Tag type_tag, ASN1_Tag class_tag,
const std::string& rep_str)
{
- const uint8_t* rep = reinterpret_cast<const uint8_t*>(rep_str.data());
+ const uint8_t* rep = cast_char_ptr_to_uint8(rep_str.data());
const size_t rep_len = rep_str.size();
return add_object(type_tag, class_tag, rep, rep_len);
}
diff --git a/src/lib/base/buf_comp.h b/src/lib/base/buf_comp.h
index 4c2b3a635..b4e1eb7a1 100644
--- a/src/lib/base/buf_comp.h
+++ b/src/lib/base/buf_comp.h
@@ -71,7 +71,7 @@ class BOTAN_PUBLIC_API(2,0) Buffered_Computation
*/
void update(const std::string& str)
{
- add_data(reinterpret_cast<const uint8_t*>(str.data()), str.size());
+ add_data(cast_char_ptr_to_uint8(str.data()), str.size());
}
/**
diff --git a/src/lib/ffi/ffi_mp.cpp b/src/lib/ffi/ffi_mp.cpp
index 00ad786ab..0b55c1d69 100644
--- a/src/lib/ffi/ffi_mp.cpp
+++ b/src/lib/ffi/ffi_mp.cpp
@@ -63,7 +63,7 @@ int botan_mp_set_from_radix_str(botan_mp_t mp, const char* str, size_t radix)
else
return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
- const uint8_t* bytes = reinterpret_cast<const uint8_t*>(str);
+ const uint8_t* bytes = Botan::cast_char_ptr_to_uint8(str);
const size_t len = strlen(str);
bn = Botan::BigInt::decode(bytes, len, base);
diff --git a/src/lib/ffi/ffi_util.h b/src/lib/ffi/ffi_util.h
index ae48ab01a..b52c319fe 100644
--- a/src/lib/ffi/ffi_util.h
+++ b/src/lib/ffi/ffi_util.h
@@ -176,19 +176,20 @@ int write_vec_output(uint8_t out[], size_t* out_len, const std::vector<uint8_t,
inline int write_str_output(uint8_t out[], size_t* out_len, const std::string& str)
{
return write_output(out, out_len,
- reinterpret_cast<const uint8_t*>(str.c_str()),
+ Botan::cast_char_ptr_to_uint8(str.data()),
str.size() + 1);
}
inline int write_str_output(char out[], size_t* out_len, const std::string& str)
{
- return write_str_output(reinterpret_cast<uint8_t*>(out), out_len, str);
+ return write_str_output(Botan::cast_char_ptr_to_uint8(out), out_len, str);
}
inline int write_str_output(char out[], size_t* out_len, const std::vector<uint8_t>& str_vec)
{
- return write_output(reinterpret_cast<uint8_t*>(out), out_len,
- reinterpret_cast<const uint8_t*>(str_vec.data()),
+ return write_output(Botan::cast_char_ptr_to_uint8(out),
+ out_len,
+ str_vec.data(),
str_vec.size());
}
diff --git a/src/lib/filters/codec_filt/b64_filt.cpp b/src/lib/filters/codec_filt/b64_filt.cpp
index bc9fc0f47..0ce158fb4 100644
--- a/src/lib/filters/codec_filt/b64_filt.cpp
+++ b/src/lib/filters/codec_filt/b64_filt.cpp
@@ -36,8 +36,8 @@ void Base64_Encoder::encode_and_send(const uint8_t input[], size_t length,
const size_t proc = std::min(length, m_in.size());
size_t consumed = 0;
- size_t produced = base64_encode(reinterpret_cast<char*>(m_out.data()), input,
- proc, consumed, final_inputs);
+ size_t produced = base64_encode(cast_uint8_ptr_to_char(m_out.data()),
+ input, proc, consumed, final_inputs);
do_output(m_out.data(), produced);
@@ -135,7 +135,7 @@ void Base64_Decoder::write(const uint8_t input[], size_t length)
size_t consumed = 0;
size_t written = base64_decode(m_out.data(),
- reinterpret_cast<const char*>(m_in.data()),
+ cast_uint8_ptr_to_char(m_in.data()),
m_position,
consumed,
false,
@@ -163,7 +163,7 @@ void Base64_Decoder::end_msg()
{
size_t consumed = 0;
size_t written = base64_decode(m_out.data(),
- reinterpret_cast<const char*>(m_in.data()),
+ cast_uint8_ptr_to_char(m_in.data()),
m_position,
consumed,
true,
diff --git a/src/lib/filters/codec_filt/hex_filt.cpp b/src/lib/filters/codec_filt/hex_filt.cpp
index a95a243dd..be1dbbc5c 100644
--- a/src/lib/filters/codec_filt/hex_filt.cpp
+++ b/src/lib/filters/codec_filt/hex_filt.cpp
@@ -43,7 +43,7 @@ Hex_Encoder::Hex_Encoder(Case c) : m_casing(c), m_line_length(0)
*/
void Hex_Encoder::encode_and_send(const uint8_t block[], size_t length)
{
- hex_encode(reinterpret_cast<char*>(m_out.data()),
+ hex_encode(cast_uint8_ptr_to_char(m_out.data()),
block, length,
m_casing == Uppercase);
@@ -125,7 +125,7 @@ void Hex_Decoder::write(const uint8_t input[], size_t length)
size_t consumed = 0;
size_t written = hex_decode(m_out.data(),
- reinterpret_cast<const char*>(m_in.data()),
+ cast_uint8_ptr_to_char(m_in.data()),
m_position,
consumed,
m_checking != FULL_CHECK);
@@ -152,7 +152,7 @@ void Hex_Decoder::end_msg()
{
size_t consumed = 0;
size_t written = hex_decode(m_out.data(),
- reinterpret_cast<const char*>(m_in.data()),
+ cast_uint8_ptr_to_char(m_in.data()),
m_position,
consumed,
m_checking != FULL_CHECK);
diff --git a/src/lib/filters/data_snk.cpp b/src/lib/filters/data_snk.cpp
index 38734f0e3..cdde5ffa1 100644
--- a/src/lib/filters/data_snk.cpp
+++ b/src/lib/filters/data_snk.cpp
@@ -21,7 +21,7 @@ namespace Botan {
*/
void DataSink_Stream::write(const uint8_t out[], size_t length)
{
- m_sink.write(reinterpret_cast<const char*>(out), length);
+ m_sink.write(cast_uint8_ptr_to_char(out), length);
if(!m_sink.good())
throw Stream_IO_Error("DataSink_Stream: Failure writing to " +
m_identifier);
diff --git a/src/lib/filters/pipe.cpp b/src/lib/filters/pipe.cpp
index 51f2cace5..ff3cb599b 100644
--- a/src/lib/filters/pipe.cpp
+++ b/src/lib/filters/pipe.cpp
@@ -133,7 +133,7 @@ void Pipe::process_msg(const std::vector<uint8_t>& input)
*/
void Pipe::process_msg(const std::string& input)
{
- process_msg(reinterpret_cast<const uint8_t*>(input.data()), input.length());
+ process_msg(cast_char_ptr_to_uint8(input.data()), input.length());
}
/*
diff --git a/src/lib/filters/pipe_io.cpp b/src/lib/filters/pipe_io.cpp
index 2f39e2e2c..f38a7d417 100644
--- a/src/lib/filters/pipe_io.cpp
+++ b/src/lib/filters/pipe_io.cpp
@@ -20,7 +20,7 @@ std::ostream& operator<<(std::ostream& stream, Pipe& pipe)
while(stream.good() && pipe.remaining())
{
const size_t got = pipe.read(buffer.data(), buffer.size());
- stream.write(reinterpret_cast<const char*>(buffer.data()), got);
+ stream.write(cast_uint8_ptr_to_char(buffer.data()), got);
}
if(!stream.good())
throw Stream_IO_Error("Pipe output operator (iostream) has failed");
@@ -35,7 +35,7 @@ std::istream& operator>>(std::istream& stream, Pipe& pipe)
secure_vector<uint8_t> buffer(DEFAULT_BUFFERSIZE);
while(stream.good())
{
- stream.read(reinterpret_cast<char*>(buffer.data()), buffer.size());
+ stream.read(cast_uint8_ptr_to_char(buffer.data()), buffer.size());
const size_t got = static_cast<size_t>(stream.gcount());
pipe.write(buffer.data(), got);
}
diff --git a/src/lib/filters/pipe_rw.cpp b/src/lib/filters/pipe_rw.cpp
index fa24198ab..082a215da 100644
--- a/src/lib/filters/pipe_rw.cpp
+++ b/src/lib/filters/pipe_rw.cpp
@@ -44,7 +44,7 @@ void Pipe::write(const uint8_t input[], size_t length)
*/
void Pipe::write(const std::string& str)
{
- write(reinterpret_cast<const uint8_t*>(str.data()), str.size());
+ write(cast_char_ptr_to_uint8(str.data()), str.size());
}
/*
@@ -119,7 +119,7 @@ std::string Pipe::read_all_as_string(message_id msg)
size_t got = read(buffer.data(), buffer.size(), msg);
if(got == 0)
break;
- str.append(reinterpret_cast<const char*>(buffer.data()), got);
+ str.append(cast_uint8_ptr_to_char(buffer.data()), got);
}
return str;
diff --git a/src/lib/hash/skein/skein_512.cpp b/src/lib/hash/skein/skein_512.cpp
index 1df3da62d..3bc23a91d 100644
--- a/src/lib/hash/skein/skein_512.cpp
+++ b/src/lib/hash/skein/skein_512.cpp
@@ -89,7 +89,7 @@ void Skein_512::initial_block()
if(m_personalization.length() > 64)
throw Invalid_Argument("Skein personalization must be less than 64 bytes");
- const uint8_t* bits = reinterpret_cast<const uint8_t*>(m_personalization.data());
+ const uint8_t* bits = cast_char_ptr_to_uint8(m_personalization.data());
reset_tweak(SKEIN_PERSONALIZATION, true);
ubi_512(bits, m_personalization.length());
}
diff --git a/src/lib/kdf/hkdf/hkdf.cpp b/src/lib/kdf/hkdf/hkdf.cpp
index 63085d191..6ccd786c3 100644
--- a/src/lib/kdf/hkdf/hkdf.cpp
+++ b/src/lib/kdf/hkdf/hkdf.cpp
@@ -103,7 +103,7 @@ hkdf_expand_label(const std::string& hash_fn,
prefix[2] = static_cast<uint8_t>(label.size());
copy_mem(prefix.data() + 3,
- reinterpret_cast<const uint8_t*>(label.data()),
+ cast_char_ptr_to_uint8(label.data()),
label.size());
prefix[3 + label.size()] = static_cast<uint8_t>(hash_val_len);
diff --git a/src/lib/kdf/kdf.h b/src/lib/kdf/kdf.h
index b99a3a83c..dd4cfedf6 100644
--- a/src/lib/kdf/kdf.h
+++ b/src/lib/kdf/kdf.h
@@ -107,9 +107,9 @@ class BOTAN_PUBLIC_API(2,0) KDF
const std::string& label = "") const
{
return derive_key(key_len, secret.data(), secret.size(),
- reinterpret_cast<const uint8_t*>(salt.data()),
+ cast_char_ptr_to_uint8(salt.data()),
salt.length(),
- reinterpret_cast<const uint8_t*>(label.data()),
+ cast_char_ptr_to_uint8(label.data()),
label.length());
}
@@ -152,7 +152,7 @@ class BOTAN_PUBLIC_API(2,0) KDF
return derive_key(key_len,
secret.data(), secret.size(),
salt, salt_len,
- reinterpret_cast<const uint8_t*>(label.data()),
+ cast_char_ptr_to_uint8(label.data()),
label.size());
}
@@ -172,9 +172,9 @@ class BOTAN_PUBLIC_API(2,0) KDF
const std::string& label = "") const
{
return derive_key(key_len, secret, secret_len,
- reinterpret_cast<const uint8_t*>(salt.data()),
+ cast_char_ptr_to_uint8(salt.data()),
salt.length(),
- reinterpret_cast<const uint8_t*>(label.data()),
+ cast_char_ptr_to_uint8(label.data()),
label.length());
}
diff --git a/src/lib/math/bigint/big_code.cpp b/src/lib/math/bigint/big_code.cpp
index f7ab53291..c42819965 100644
--- a/src/lib/math/bigint/big_code.cpp
+++ b/src/lib/math/bigint/big_code.cpp
@@ -26,7 +26,7 @@ void BigInt::encode(uint8_t output[], const BigInt& n, Base base)
secure_vector<uint8_t> binary(n.encoded_size(Binary));
n.binary_encode(binary.data());
- hex_encode(reinterpret_cast<char*>(output),
+ hex_encode(cast_uint8_ptr_to_char(output),
binary.data(), binary.size());
}
else if(base == Decimal)
@@ -128,12 +128,12 @@ BigInt BigInt::decode(const uint8_t buf[], size_t length, Base base)
binary = hex_decode_locked(buf0_with_leading_0, 2);
- binary += hex_decode_locked(reinterpret_cast<const char*>(&buf[1]),
+ binary += hex_decode_locked(cast_uint8_ptr_to_char(&buf[1]),
length - 1,
false);
}
else
- binary = hex_decode_locked(reinterpret_cast<const char*>(buf),
+ binary = hex_decode_locked(cast_uint8_ptr_to_char(buf),
length, false);
r.binary_decode(binary.data(), binary.size());
diff --git a/src/lib/math/bigint/big_io.cpp b/src/lib/math/bigint/big_io.cpp
index 7d990e25e..803e1cc4a 100644
--- a/src/lib/math/bigint/big_io.cpp
+++ b/src/lib/math/bigint/big_io.cpp
@@ -32,7 +32,7 @@ std::ostream& operator<<(std::ostream& stream, const BigInt& n)
size_t skip = 0;
while(skip < buffer.size() && buffer[skip] == '0')
++skip;
- stream.write(reinterpret_cast<const char*>(buffer.data()) + skip,
+ stream.write(cast_uint8_ptr_to_char(buffer.data()) + skip,
buffer.size() - skip);
}
if(!stream.good())
diff --git a/src/lib/math/bigint/bigint.cpp b/src/lib/math/bigint/bigint.cpp
index 47ff2482a..28fe68f00 100644
--- a/src/lib/math/bigint/bigint.cpp
+++ b/src/lib/math/bigint/bigint.cpp
@@ -68,7 +68,7 @@ BigInt::BigInt(const std::string& str)
base = Hexadecimal;
}
- *this = decode(reinterpret_cast<const uint8_t*>(str.data()) + markers,
+ *this = decode(cast_char_ptr_to_uint8(str.data()) + markers,
str.length() - markers, base);
if(negative) set_sign(Negative);
diff --git a/src/lib/misc/cryptobox/cryptobox.cpp b/src/lib/misc/cryptobox/cryptobox.cpp
index 195263368..5d2364871 100644
--- a/src/lib/misc/cryptobox/cryptobox.cpp
+++ b/src/lib/misc/cryptobox/cryptobox.cpp
@@ -154,7 +154,7 @@ decrypt_bin(const uint8_t input[], size_t input_len,
secure_vector<uint8_t> decrypt_bin(const std::string& input,
const std::string& passphrase)
{
- return decrypt_bin(reinterpret_cast<const uint8_t*>(input.data()),
+ return decrypt_bin(cast_char_ptr_to_uint8(input.data()),
input.size(),
passphrase);
}
@@ -164,14 +164,14 @@ std::string decrypt(const uint8_t input[], size_t input_len,
{
const secure_vector<uint8_t> bin = decrypt_bin(input, input_len, passphrase);
- return std::string(reinterpret_cast<const char*>(&bin[0]),
+ return std::string(cast_uint8_ptr_to_char(&bin[0]),
bin.size());
}
std::string decrypt(const std::string& input,
const std::string& passphrase)
{
- return decrypt(reinterpret_cast<const uint8_t*>(input.data()),
+ return decrypt(cast_char_ptr_to_uint8(input.data()),
input.size(), passphrase);
}
diff --git a/src/lib/passhash/bcrypt/bcrypt.cpp b/src/lib/passhash/bcrypt/bcrypt.cpp
index 8424de7e2..d4dc263e1 100644
--- a/src/lib/passhash/bcrypt/bcrypt.cpp
+++ b/src/lib/passhash/bcrypt/bcrypt.cpp
@@ -99,8 +99,8 @@ std::string make_bcrypt(const std::string& pass,
Blowfish blowfish;
- // Include the trailing NULL byte
- blowfish.eks_key_schedule(reinterpret_cast<const uint8_t*>(pass.c_str()),
+ // Include the trailing NULL byte, so we need c_str() not data()
+ blowfish.eks_key_schedule(cast_char_ptr_to_uint8(pass.c_str()),
pass.length() + 1,
salt.data(),
work_factor);
diff --git a/src/lib/pbkdf/pbkdf2/pbkdf2.cpp b/src/lib/pbkdf/pbkdf2/pbkdf2.cpp
index 4e1a73331..d4388bd10 100644
--- a/src/lib/pbkdf/pbkdf2/pbkdf2.cpp
+++ b/src/lib/pbkdf/pbkdf2/pbkdf2.cpp
@@ -26,7 +26,7 @@ pbkdf2(MessageAuthenticationCode& prf,
try
{
- prf.set_key(reinterpret_cast<const uint8_t*>(passphrase.data()), passphrase.size());
+ prf.set_key(cast_char_ptr_to_uint8(passphrase.data()), passphrase.size());
}
catch(Invalid_Key_Length&)
{
diff --git a/src/lib/pbkdf/pgp_s2k/pgp_s2k.cpp b/src/lib/pbkdf/pgp_s2k/pgp_s2k.cpp
index f298bb3e4..14f21eae2 100644
--- a/src/lib/pbkdf/pgp_s2k/pgp_s2k.cpp
+++ b/src/lib/pbkdf/pgp_s2k/pgp_s2k.cpp
@@ -91,7 +91,7 @@ size_t OpenPGP_S2K::pbkdf(uint8_t output_buf[], size_t output_len,
if(passphrase.empty() == false)
{
copy_mem(&input_buf[salt_len],
- reinterpret_cast<const uint8_t*>(passphrase.data()),
+ cast_char_ptr_to_uint8(passphrase.data()),
passphrase.size());
}
diff --git a/src/lib/pubkey/pubkey.h b/src/lib/pubkey/pubkey.h
index bfe94509e..a33142079 100644
--- a/src/lib/pubkey/pubkey.h
+++ b/src/lib/pubkey/pubkey.h
@@ -256,7 +256,7 @@ class BOTAN_PUBLIC_API(2,0) PK_Signer final
*/
void update(const std::string& in)
{
- update(reinterpret_cast<const uint8_t*>(in.data()), in.size());
+ update(cast_char_ptr_to_uint8(in.data()), in.size());
}
/**
@@ -356,7 +356,7 @@ class BOTAN_PUBLIC_API(2,0) PK_Verifier final
*/
void update(const std::string& in)
{
- update(reinterpret_cast<const uint8_t*>(in.data()), in.size());
+ update(cast_char_ptr_to_uint8(in.data()), in.size());
}
/**
@@ -478,7 +478,7 @@ class BOTAN_PUBLIC_API(2,0) PK_Key_Agreement final
const std::string& params = "") const
{
return derive_key(key_len, in, in_len,
- reinterpret_cast<const uint8_t*>(params.data()),
+ cast_char_ptr_to_uint8(params.data()),
params.length());
}
@@ -493,7 +493,7 @@ class BOTAN_PUBLIC_API(2,0) PK_Key_Agreement final
const std::string& params = "") const
{
return derive_key(key_len, in.data(), in.size(),
- reinterpret_cast<const uint8_t*>(params.data()),
+ cast_char_ptr_to_uint8(params.data()),
params.length());
}
diff --git a/src/lib/tls/tls_channel.cpp b/src/lib/tls/tls_channel.cpp
index 034976ec4..78f681765 100644
--- a/src/lib/tls/tls_channel.cpp
+++ b/src/lib/tls/tls_channel.cpp
@@ -548,7 +548,7 @@ void Channel::send(const uint8_t buf[], size_t buf_size)
void Channel::send(const std::string& string)
{
- this->send(reinterpret_cast<const uint8_t*>(string.c_str()), string.size());
+ this->send(cast_char_ptr_to_uint8(string.data()), string.size());
}
void Channel::send_alert(const Alert& alert)
diff --git a/src/lib/tls/tls_extensions.cpp b/src/lib/tls/tls_extensions.cpp
index c76128632..317d96b8d 100644
--- a/src/lib/tls/tls_extensions.cpp
+++ b/src/lib/tls/tls_extensions.cpp
@@ -178,7 +178,7 @@ std::vector<uint8_t> Server_Name_Indicator::serialize() const
buf.push_back(get_byte(1, static_cast<uint16_t>(name_len)));
buf += std::make_pair(
- reinterpret_cast<const uint8_t*>(m_sni_host_name.data()),
+ cast_char_ptr_to_uint8(m_sni_host_name.data()),
m_sni_host_name.size());
return buf;
@@ -197,9 +197,7 @@ std::vector<uint8_t> SRP_Identifier::serialize() const
{
std::vector<uint8_t> buf;
- const uint8_t* srp_bytes =
- reinterpret_cast<const uint8_t*>(m_srp_identifier.data());
-
+ const uint8_t* srp_bytes = cast_char_ptr_to_uint8(m_srp_identifier.data());
append_tls_length_value(buf, srp_bytes, m_srp_identifier.size(), 1);
return buf;
@@ -266,7 +264,7 @@ std::vector<uint8_t> Application_Layer_Protocol_Notification::serialize() const
throw TLS_Exception(Alert::INTERNAL_ERROR, "ALPN name too long");
if(p != "")
append_tls_length_value(buf,
- reinterpret_cast<const uint8_t*>(p.data()),
+ cast_char_ptr_to_uint8(p.data()),
p.size(),
1);
}
diff --git a/src/lib/tls/tls_reader.h b/src/lib/tls/tls_reader.h
index 588335144..8474f1308 100644
--- a/src/lib/tls/tls_reader.h
+++ b/src/lib/tls/tls_reader.h
@@ -119,7 +119,7 @@ class TLS_Data_Reader final
std::vector<uint8_t> v =
get_range_vector<uint8_t>(len_bytes, min_bytes, max_bytes);
- return std::string(reinterpret_cast<char*>(v.data()), v.size());
+ return std::string(cast_uint8_ptr_to_char(v.data()), v.size());
}
template<typename T>
@@ -219,7 +219,7 @@ void append_tls_length_value(std::vector<uint8_t, Alloc>& buf,
size_t tag_size)
{
append_tls_length_value(buf,
- reinterpret_cast<const uint8_t*>(str.data()),
+ cast_char_ptr_to_uint8(str.data()),
str.size(),
tag_size);
}
diff --git a/src/lib/utils/data_src.cpp b/src/lib/utils/data_src.cpp
index 078d3f2ea..f4645bb85 100644
--- a/src/lib/utils/data_src.cpp
+++ b/src/lib/utils/data_src.cpp
@@ -95,8 +95,8 @@ bool DataSource_Memory::end_of_data() const
* DataSource_Memory Constructor
*/
DataSource_Memory::DataSource_Memory(const std::string& in) :
- m_source(reinterpret_cast<const uint8_t*>(in.data()),
- reinterpret_cast<const uint8_t*>(in.data()) + in.length()),
+ m_source(cast_char_ptr_to_uint8(in.data()),
+ cast_char_ptr_to_uint8(in.data()) + in.length()),
m_offset(0)
{
}
@@ -106,7 +106,7 @@ DataSource_Memory::DataSource_Memory(const std::string& in) :
*/
size_t DataSource_Stream::read(uint8_t out[], size_t length)
{
- m_source.read(reinterpret_cast<char*>(out), length);
+ m_source.read(cast_uint8_ptr_to_char(out), length);
if(m_source.bad())
throw Stream_IO_Error("DataSource_Stream::read: Source failure");
@@ -137,7 +137,7 @@ size_t DataSource_Stream::peek(uint8_t out[], size_t length, size_t offset) cons
if(offset)
{
secure_vector<uint8_t> buf(offset);
- m_source.read(reinterpret_cast<char*>(buf.data()), buf.size());
+ m_source.read(cast_uint8_ptr_to_char(buf.data()), buf.size());
if(m_source.bad())
throw Stream_IO_Error("DataSource_Stream::peek: Source failure");
got = static_cast<size_t>(m_source.gcount());
@@ -145,7 +145,7 @@ size_t DataSource_Stream::peek(uint8_t out[], size_t length, size_t offset) cons
if(got == offset)
{
- m_source.read(reinterpret_cast<char*>(out), length);
+ m_source.read(cast_uint8_ptr_to_char(out), length);
if(m_source.bad())
throw Stream_IO_Error("DataSource_Stream::peek: Source failure");
got = static_cast<size_t>(m_source.gcount());
diff --git a/src/lib/utils/http_util/http_util.cpp b/src/lib/utils/http_util/http_util.cpp
index 73efb7adc..035176c17 100644
--- a/src/lib/utils/http_util/http_util.cpp
+++ b/src/lib/utils/http_util/http_util.cpp
@@ -40,7 +40,7 @@ std::string http_transact(const std::string& hostname,
}
// Blocks until entire message has been written
- socket->write(reinterpret_cast<const uint8_t*>(message.data()),
+ socket->write(cast_char_ptr_to_uint8(message.data()),
message.size());
std::ostringstream oss;
@@ -51,7 +51,7 @@ std::string http_transact(const std::string& hostname,
if(got == 0) // EOF
break;
- oss.write(reinterpret_cast<const char*>(buf.data()),
+ oss.write(cast_uint8_ptr_to_char(buf.data()),
static_cast<std::streamsize>(got));
}
@@ -75,7 +75,7 @@ std::string url_encode(const std::string& in)
else if(c == '-' || c == '_' || c == '.' || c == '~')
out << c;
else
- out << '%' << hex_encode(reinterpret_cast<uint8_t*>(&c), 1);
+ out << '%' << hex_encode(cast_char_ptr_to_uint8(&c), 1);
}
return out.str();
@@ -87,7 +87,7 @@ std::ostream& operator<<(std::ostream& o, const Response& resp)
for(auto h : resp.headers())
o << "Header '" << h.first << "' = '" << h.second << "'\n";
o << "Body " << std::to_string(resp.body().size()) << " bytes:\n";
- o.write(reinterpret_cast<const char*>(&resp.body()[0]), resp.body().size());
+ o.write(cast_uint8_ptr_to_char(resp.body().data()), resp.body().size());
return o;
}
@@ -136,7 +136,7 @@ Response http_sync(http_exch_fn http_transact,
if(!content_type.empty())
outbuf << "Content-Type: " << content_type << "\r\n";
outbuf << "Connection: close\r\n\r\n";
- outbuf.write(reinterpret_cast<const char*>(body.data()), body.size());
+ outbuf.write(cast_uint8_ptr_to_char(body.data()), body.size());
std::istringstream io(http_transact(hostname, outbuf.str()));
@@ -184,7 +184,7 @@ Response http_sync(http_exch_fn http_transact,
std::vector<uint8_t> buf(4096);
while(io.good())
{
- io.read(reinterpret_cast<char*>(buf.data()), buf.size());
+ io.read(cast_uint8_ptr_to_char(buf.data()), buf.size());
resp_body.insert(resp_body.end(), buf.data(), &buf[io.gcount()]);
}
diff --git a/src/lib/utils/locking_allocator/locking_allocator.cpp b/src/lib/utils/locking_allocator/locking_allocator.cpp
index f36fa9130..c7ca1662f 100644
--- a/src/lib/utils/locking_allocator/locking_allocator.cpp
+++ b/src/lib/utils/locking_allocator/locking_allocator.cpp
@@ -64,7 +64,7 @@ void* mlock_allocator::allocate(size_t num_elems, size_t elem_size)
m_freelist.erase(i);
clear_mem(m_pool + offset, n);
- BOTAN_ASSERT((reinterpret_cast<size_t>(m_pool) + offset) % alignment == 0,
+ BOTAN_ASSERT((reinterpret_cast<uintptr_t>(m_pool) + offset) % alignment == 0,
"Returning correctly aligned pointer");
return m_pool + offset;
@@ -107,7 +107,7 @@ void* mlock_allocator::allocate(size_t num_elems, size_t elem_size)
clear_mem(m_pool + offset + alignment_padding, n);
- BOTAN_ASSERT((reinterpret_cast<size_t>(m_pool) + offset + alignment_padding) % alignment == 0,
+ BOTAN_ASSERT((reinterpret_cast<uintptr_t>(m_pool) + offset + alignment_padding) % alignment == 0,
"Returning correctly aligned pointer");
return m_pool + offset + alignment_padding;
diff --git a/src/lib/utils/mem_ops.h b/src/lib/utils/mem_ops.h
index 3274bfaf6..ed4d6cb27 100644
--- a/src/lib/utils/mem_ops.h
+++ b/src/lib/utils/mem_ops.h
@@ -117,6 +117,26 @@ inline void set_mem(T* ptr, size_t n, uint8_t val)
}
}
+inline const uint8_t* cast_char_ptr_to_uint8(const char* s)
+ {
+ return reinterpret_cast<const uint8_t*>(s);
+ }
+
+inline const char* cast_uint8_ptr_to_char(const uint8_t* b)
+ {
+ return reinterpret_cast<const char*>(b);
+ }
+
+inline uint8_t* cast_char_ptr_to_uint8(char* s)
+ {
+ return reinterpret_cast<uint8_t*>(s);
+ }
+
+inline char* cast_uint8_ptr_to_char(uint8_t* b)
+ {
+ return reinterpret_cast<char*>(b);
+ }
+
/**
* Memory comparison, input insensitive
* @param p1 a pointer to an array
diff --git a/src/lib/utils/os_utils.cpp b/src/lib/utils/os_utils.cpp
index 3f7d3cfde..d516e7600 100644
--- a/src/lib/utils/os_utils.cpp
+++ b/src/lib/utils/os_utils.cpp
@@ -163,7 +163,7 @@ class Winsock_Socket final : public OS::Socket
{
const size_t left = len - sent_so_far;
int sent = ::send(m_socket,
- reinterpret_cast<const char*>(buf + sent_so_far),
+ cast_uint8_ptr_to_char(buf + sent_so_far),
static_cast<int>(left),
0);
@@ -178,7 +178,7 @@ class Winsock_Socket final : public OS::Socket
size_t read(uint8_t buf[], size_t len) override
{
int got = ::recv(m_socket,
- reinterpret_cast<char*>(buf),
+ cast_uint8_ptr_to_char(buf),
static_cast<int>(len), 0);
if(got == SOCKET_ERROR)