Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions src/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class Buffer {
}

inline bool ReadU8(uint8_t *value) {
if (offset_ + 1 > length_) {
if (length_ < 1 || offset_ > length_ - 1) {
return FONT_COMPRESSION_FAILURE();
}
*value = buffer_[offset_];
Expand All @@ -91,7 +91,7 @@ class Buffer {
}

bool ReadU16(uint16_t *value) {
if (offset_ + 2 > length_) {
if (length_ < 2 || offset_ > length_ - 2) {
return FONT_COMPRESSION_FAILURE();
}
std::memcpy(value, buffer_ + offset_, sizeof(uint16_t));
Expand All @@ -105,7 +105,7 @@ class Buffer {
}

bool ReadU24(uint32_t *value) {
if (offset_ + 3 > length_) {
if (length_ < 3 || offset_ > length_ - 3) {
return FONT_COMPRESSION_FAILURE();
}
*value = static_cast<uint32_t>(buffer_[offset_]) << 16 |
Expand All @@ -116,7 +116,7 @@ class Buffer {
}

bool ReadU32(uint32_t *value) {
if (offset_ + 4 > length_) {
if (length_ < 4 || offset_ > length_ - 4) {
return FONT_COMPRESSION_FAILURE();
}
std::memcpy(value, buffer_ + offset_, sizeof(uint32_t));
Expand All @@ -130,7 +130,7 @@ class Buffer {
}

bool ReadTag(uint32_t *value) {
if (offset_ + 4 > length_) {
if (length_ < 4 || offset_ > length_ - 4) {
return FONT_COMPRESSION_FAILURE();
}
std::memcpy(value, buffer_ + offset_, sizeof(uint32_t));
Expand All @@ -139,7 +139,7 @@ class Buffer {
}

bool ReadR64(uint64_t *value) {
if (offset_ + 8 > length_) {
if (length_ < 8 || offset_ > length_ - 8) {
return FONT_COMPRESSION_FAILURE();
}
std::memcpy(value, buffer_ + offset_, sizeof(uint64_t));
Expand All @@ -151,7 +151,13 @@ class Buffer {
size_t offset() const { return offset_; }
size_t length() const { return length_; }

void set_offset(size_t newoffset) { offset_ = newoffset; }
bool set_offset(size_t newoffset) {
if (newoffset > length_) {
return FONT_COMPRESSION_FAILURE();
}
offset_ = newoffset;
return true;
}

private:
const uint8_t * const buffer_;
Expand Down
4 changes: 3 additions & 1 deletion src/font.cc
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,9 @@ bool ReadTrueTypeCollection(Buffer* file, const uint8_t* data, size_t len,

std::map<uint32_t, Font::Table*> all_tables;
for (const auto offset : offsets) {
file->set_offset(offset);
if (!file->set_offset(offset)) {
return FONT_COMPRESSION_FAILURE();
}
Font& font = *font_it++;
if (!ReadCollectionFont(file, data, len, &font, &all_tables)) {
return FONT_COMPRESSION_FAILURE();
Expand Down