aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/utils
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 /src/lib/utils
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.
Diffstat (limited to 'src/lib/utils')
-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
5 files changed, 35 insertions, 15 deletions
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)