Skip to content

Commit

Permalink
Work around clang's clever null check
Browse files Browse the repository at this point in the history
  • Loading branch information
peadar committed Sep 12, 2023
1 parent 7a6cc2b commit b3e33a2
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,27 @@ MemReader::MemReader(const string &descr, size_t len_, const void *data_)
{
}

namespace {

// Clang can add checks to the likes of
// strcpy((char *)ptr + off);
// to validate that "ptr" is not a null pointer.
// In our case, for a memory reader in our own address space, we need to allow
// that, as the offset will be the raw pointer value. Use this to hide the
// [cn]astiness.
const char *ptroff(const void *base, uintptr_t off) {
return (char *)((uintptr_t)base + off);
}
}


size_t
MemReader::read(Off off, size_t count, char *ptr) const
{
if (off > Off(len))
throw (Exception() << "read past end of memory");
size_t rc = std::min(count, len - size_t(off));
memcpy(ptr, (const char *)data + off, rc);
memcpy(ptr, ptroff(data, off), rc);
return rc;
}

Expand All @@ -63,7 +77,7 @@ MemReader::describe(std::ostream &os) const

string
MemReader::readString(Off offset) const {
return string((char *)data + offset);
return string(ptroff(data, offset));
}


Expand Down Expand Up @@ -242,7 +256,7 @@ class MemOffsetReader final : public MemReader {
Reader::csptr upstream;
public:
MemOffsetReader(const std::string &name, const MemReader *upstream_, Off offset, Off size)
: MemReader(name, size, (char *)upstream_->data + offset)
: MemReader(name, size, ptroff(upstream_->data, offset))
, upstream(upstream_->shared_from_this())
{
}
Expand Down Expand Up @@ -301,15 +315,14 @@ Reader::readSLEB128(Off off) const
std::pair<uintmax_t, size_t>
MemReader::readULEB128(Off off) const
{
auto p = reinterpret_cast<const char *>(data) + off;
return readleb128<uintmax_t>(p);
return readleb128<uintmax_t>(ptroff(data, off));
}

std::pair<intmax_t, size_t>
MemReader::readSLEB128(Off off) const
{
auto p = reinterpret_cast<const char *>(data) + off;
return readleb128<intmax_t>(p);
return readleb128<intmax_t>(ptroff(data, off));
}

}

0 comments on commit b3e33a2

Please sign in to comment.