Fix various undefined behaviours detected by UBSan

This commit is contained in:
Maxime Coste 2017-06-26 11:27:18 +01:00
parent 8a2ece78b7
commit 4e7a357a47
6 changed files with 25 additions and 16 deletions

View File

@ -34,7 +34,7 @@ public:
template<typename Alloc, typename U,
typename = typename std::enable_if<sizeof(U) == sizeof(T)>::type>
constexpr ArrayView(const std::vector<U, Alloc>& v)
: m_pointer(&v[0]), m_size(v.size()) {}
: m_pointer(v.data()), m_size(v.size()) {}
constexpr ArrayView(const std::initializer_list<T>& v)
: m_pointer(v.begin()), m_size(v.size()) {}

View File

@ -1,6 +1,7 @@
#include "hash.hh"
#include <cstdint>
#include <cstring>
namespace Kakoune
{
@ -32,11 +33,12 @@ size_t hash_data(const char* input, size_t len)
constexpr uint32_t c2 = 0x1b873593;
const int nblocks = len / 4;
const uint32_t* blocks = reinterpret_cast<const uint32_t*>(data + nblocks*4);
const uint8_t* blocks = data + nblocks*4;
for (int i = -nblocks; i; ++i)
{
uint32_t key = blocks[i];
uint32_t key;
memcpy(&key, blocks + 4*i, 4);
key *= c1;
key = rotl(key, 15);
key *= c2;

View File

@ -1014,7 +1014,7 @@ struct LineNumbersHighlighter : Highlighter
}
private:
void do_highlight(const Context& context, HighlightPass, DisplayBuffer& display_buffer, BufferRange)
void do_highlight(const Context& context, HighlightPass, DisplayBuffer& display_buffer, BufferRange) override
{
const Face face = get_face("LineNumbers");
const Face face_wrapped = get_face("LineNumbersWrapped");

View File

@ -23,7 +23,7 @@
namespace Kakoune
{
enum class MessageType : char
enum class MessageType : uint8_t
{
Unknown,
Connect,
@ -54,7 +54,7 @@ public:
~MsgWriter() noexcept(false)
{
uint32_t count = (uint32_t)m_buffer.size() - m_start;
*reinterpret_cast<uint32_t*>(m_buffer.data() + m_start + 1) = count;
memcpy(m_buffer.data() + m_start + sizeof(MessageType), &count, sizeof(uint32_t));
}
void write(const char* val, size_t size)
@ -172,7 +172,9 @@ public:
uint32_t size() const
{
kak_assert(m_write_pos >= header_size);
return *reinterpret_cast<const uint32_t*>(m_stream.data()+1);
uint32_t res;
memcpy(&res, m_stream.data() + sizeof(MessageType), sizeof(uint32_t));
return res;
}
MessageType type() const

View File

@ -22,6 +22,7 @@ String::Data::Data(const char* data, size_t size, size_t capacity)
l.size = size;
l.capacity = capacity;
if (data != nullptr)
memcpy(l.ptr, data, size);
l.ptr[size] = 0;
}
@ -101,6 +102,9 @@ void String::Data::force_size(size_t new_size)
void String::Data::append(const char* str, size_t len)
{
if (len == 0)
return;
const size_t new_size = size() + len;
reserve(new_size);
@ -147,6 +151,7 @@ void String::Data::set_size(size_t size)
void String::Data::set_short(const char* data, size_t size)
{
s.size = (size << 1) | 1;
if (data != nullptr)
memcpy(s.string, data, size);
s.string[size] = 0;
}
@ -308,7 +313,7 @@ Optional<int> str_to_int_ifp(StringView str)
return {};
res = res * 10 + c - '0';
}
return negative ? -(int)res : (int)res;
return negative ? -res : res;
}
int str_to_int(StringView str)

View File

@ -22,7 +22,7 @@ public:
static T& instance()
{
kak_assert(ms_instance);
return *ms_instance;
return *static_cast<T*>(ms_instance);
}
static bool has_instance()
@ -33,8 +33,8 @@ public:
protected:
Singleton()
{
kak_assert(not ms_instance);
ms_instance = static_cast<T*>(this);
kak_assert(ms_instance == nullptr);
ms_instance = this;
}
~Singleton()
@ -44,11 +44,11 @@ protected:
}
private:
static T* ms_instance;
static Singleton* ms_instance;
};
template<typename T>
T* Singleton<T>::ms_instance = nullptr;
Singleton<T>* Singleton<T>::ms_instance = nullptr;
// *** On scope end ***
//