File tree Expand file tree Collapse file tree 1 file changed +12
-2
lines changed Expand file tree Collapse file tree 1 file changed +12
-2
lines changed Original file line number Diff line number Diff line change @@ -60,10 +60,20 @@ void PngImg::ReadInfo_(PngReadStruct& rs) {
60
60
// /
61
61
void PngImg::InitStorage_ () {
62
62
rowPtrs_.resize (info_.height , nullptr );
63
- data_ = new png_byte[info_.height * info_.rowbytes ];
63
+ // Extend height and rowbytes from uint32_t to size_t to avoid multiplication overflow when size_t is larger
64
+ size_t h = info_.height ;
65
+ size_t rb = info_.rowbytes ;
66
+ // We need to make sure that info_.height * info_.rowbytes will not overflow size_t
67
+ // Unfotunately, there's no simple and portable way to do this in C++
68
+ // For integer division of positive numbers a * b > c <==> a > c / b holds
69
+ if (h > std::numeric_limits<size_t >::max () / rb) {
70
+ // TODO Propagate this exception to JS, and test it
71
+ throw std::runtime_error (" Image is too large to allocate single buffer" );
72
+ }
73
+ data_ = new png_byte[h * rb];
64
74
65
75
for (size_t i = 0 ; i < info_.height ; ++i) {
66
- rowPtrs_[i] = data_ + i * info_. rowbytes ;
76
+ rowPtrs_[i] = data_ + i * rb ;
67
77
}
68
78
}
69
79
You can’t perform that action at this time.
0 commit comments