Skip to content

Commit 2abf97c

Browse files
Copilotxiaozhuai
andauthored
Fix memory safety and integer overflow bugs in image parsing
* Initial plan * Fix 5 critical bugs in imageinfo library Co-authored-by: xiaozhuai <4773701+xiaozhuai@users.noreply.github.com> * Improve bounds checking to prevent integer overflow Co-authored-by: xiaozhuai <4773701+xiaozhuai@users.noreply.github.com> * Task complete: All bugs fixed and verified Co-authored-by: xiaozhuai <4773701+xiaozhuai@users.noreply.github.com> * Clean up build artifacts and update gitignore Co-authored-by: xiaozhuai <4773701+xiaozhuai@users.noreply.github.com> * Revert .gitignore changes as requested Co-authored-by: xiaozhuai <4773701+xiaozhuai@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: xiaozhuai <4773701+xiaozhuai@users.noreply.github.com>
1 parent 5cab2df commit 2abf97c

1 file changed

Lines changed: 11 additions & 8 deletions

File tree

include/imageinfo.hpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,10 @@ class RawDataReader {
218218

219219
inline size_t size() const { return data_.length; }
220220

221-
inline void read(void *buf, off_t offset, size_t size) const { memcpy(buf, ((char *)data_.data) + offset, size); }
221+
inline void read(void *buf, off_t offset, size_t size) const {
222+
assert(offset >= 0 && size <= data_.length && (size_t)offset <= data_.length - size);
223+
memcpy(buf, ((char *)data_.data) + offset, size);
224+
}
222225

223226
private:
224227
RawData data_;
@@ -241,9 +244,9 @@ class Buffer {
241244

242245
inline size_t size() const { return size_; }
243246

244-
inline uint8_t &operator[](int offset) { return data_.get()[offset]; }
247+
inline uint8_t &operator[](size_t offset) { return data_.get()[offset]; }
245248

246-
inline uint8_t operator[](int offset) const { return data_.get()[offset]; }
249+
inline uint8_t operator[](size_t offset) const { return data_.get()[offset]; }
247250

248251
public:
249252
inline uint8_t read_u8(off_t offset) { return read_int<uint8_t>(offset, false); }
@@ -549,17 +552,17 @@ inline bool try_avif_heic(ReadInterface &ri, size_t length, ImageInfo &info) {
549552
uint16_t entry_count = buffer.read_u16_be(offset + 14);
550553
off_t t = offset + 16;
551554
for (uint16_t i = 0; i < entry_count; ++i) {
552-
if (box_size < 18) {
555+
if (t + 2 > offset + box_size) {
553556
return false;
554557
}
555558
uint16_t item_id = buffer.read_u16_be(t);
556559
t += 2;
557-
if (box_size < 19) {
560+
if (t + 1 > offset + box_size) {
558561
return false;
559562
}
560563
uint8_t index_count = buffer.read_u8(t);
561564
t += 1;
562-
if (box_size < 19 + index_count) {
565+
if (t + index_count > offset + box_size) {
563566
return false;
564567
}
565568
std::unordered_set<uint8_t> indices;
@@ -700,7 +703,7 @@ inline bool try_cur_ico(ReadInterface &ri, size_t length, ImageInfo &info) {
700703
int64_t h2 = h1 == 0 ? 256 : h1;
701704
sizes.emplace_back(w2, h2);
702705

703-
uint32_t bytes = buffer.read_s32_le(i * entry_size + 8);
706+
uint32_t bytes = buffer.read_u32_le(i * entry_size + 8);
704707
offset += bytes;
705708
}
706709

@@ -1057,7 +1060,7 @@ inline bool try_jpg(ReadInterface &ri, size_t length, ImageInfo &info) {
10571060
}
10581061
auto ifd_main_entries_count = buffer.read_int<uint16_t>(first_ifd_offset + 10, big_endian);
10591062
for (uint16_t i = 0; i < ifd_main_entries_count; ++i) {
1060-
off_t entry_offset = first_ifd_offset + 12 + i * 12;
1063+
off_t entry_offset = first_ifd_offset + 12 + (off_t)i * 12;
10611064
if (entry_offset + 12 > section_size + 2) {
10621065
return false;
10631066
}

0 commit comments

Comments
 (0)