Skip to content

Commit de02b52

Browse files
Fix Coverity TAINTED_SCALAR in ImageBMP::read (#997)
Resolves: CID-51802 BmpUtils::load_bmp_image[_8u] sets width_/height_ from the BMP file header and may then return false (e.g. when bounds checks fail or data read fails). The previous code called pixels_.resize(size(), 0) and copy_raw_data(data.get()) unconditionally, which means: - On the error path where width_/height_ were set from the file but data was not allocated (nullptr), copy_raw_data would perform std::copy from a null pointer — undefined behaviour. - A malformed BMP with very large dimensions could trigger an enormous allocation before the error was returned. Fix: guard pixels_.resize() and copy_raw_data() with if (!error) in both ImageBMP<T>::read (generic template) and the ImageBMP<uint8_t> specialization. The success path is unaffected; load_bmp_image's internal bounds checks (height/pitch <= 65536) ensure the dimensions are safe when !error.
1 parent 84f3174 commit de02b52

1 file changed

Lines changed: 8 additions & 4 deletions

File tree

utils/image/src/image.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,10 @@ template <typename T> bool ImageBMP<T>::read(const std::string &image_path) {
192192
bool error = !BmpUtils::load_bmp_image(tmp, width_, height_, pitch,
193193
bits_per_pixel, image_path.c_str());
194194
data.reset(tmp);
195-
pixels_.resize(size(), 0);
196-
copy_raw_data(reinterpret_cast<T *>(data.get()));
195+
if (!error) {
196+
pixels_.resize(size(), 0);
197+
copy_raw_data(reinterpret_cast<T *>(data.get()));
198+
}
197199
return error;
198200
}
199201

@@ -203,8 +205,10 @@ template <> bool ImageBMP<uint8_t>::read(const std::string &image_path) {
203205
bool error =
204206
!BmpUtils::load_bmp_image_8u(tmp, width_, height_, image_path.c_str());
205207
data.reset(tmp);
206-
pixels_.resize(size(), 0);
207-
copy_raw_data(data.get());
208+
if (!error) {
209+
pixels_.resize(size(), 0);
210+
copy_raw_data(data.get());
211+
}
208212
return error;
209213
}
210214

0 commit comments

Comments
 (0)