Add further bounds checks when accessing image_buffer

This commit is contained in:
Isaac Connor 2021-05-13 09:31:59 -04:00
parent 70946921bc
commit d52b53e690
1 changed files with 36 additions and 22 deletions

View File

@ -1163,39 +1163,49 @@ void Monitor::AddPrivacyBitmask() {
int Monitor::GetImage(int32_t index, int scale) { int Monitor::GetImage(int32_t index, int scale) {
if (index < 0 || index > image_buffer_count) { if (index < 0 || index > image_buffer_count) {
Warning("Invalid index %d passed. image_buffer_count = %d", index, image_buffer_count);
index = shared_data->last_write_index; index = shared_data->last_write_index;
} }
if ( index != image_buffer_count ) { if (!image_buffer.size() or static_cast<size_t>(index) >= image_buffer.size()) {
Image *image; Error("Image Buffer has not been allocated");
// If we are going to be modifying the snapshot before writing, then we need to copy it return -1;
if ( ( scale != ZM_SCALE_BASE ) || ( !config.timestamp_on_capture ) ) { }
alarm_image.Assign(*image_buffer[index]); if ( index == image_buffer_count ) {
Error("Unable to generate image, no images in buffer");
return 0;
}
if (scale != ZM_SCALE_BASE) { Image *image;
alarm_image.Scale(scale); // If we are going to be modifying the snapshot before writing, then we need to copy it
} if ((scale != ZM_SCALE_BASE) || (!config.timestamp_on_capture)) {
alarm_image.Assign(*image_buffer[index]);
if ( !config.timestamp_on_capture ) { if (scale != ZM_SCALE_BASE) {
TimestampImage(&alarm_image, shared_timestamps[index]); alarm_image.Scale(scale);
}
image = &alarm_image;
} else {
image = image_buffer[index];
} }
static char filename[PATH_MAX]; if (!config.timestamp_on_capture) {
snprintf(filename, sizeof(filename), "Monitor%u.jpg", id); TimestampImage(&alarm_image, shared_timestamps[index]);
image->WriteJpeg(filename); }
image = &alarm_image;
} else { } else {
Error("Unable to generate image, no images in buffer"); image = image_buffer[index];
} }
return 0;
static char filename[PATH_MAX];
snprintf(filename, sizeof(filename), "Monitor%u.jpg", id);
image->WriteJpeg(filename);
return 1;
} }
ZMPacket *Monitor::getSnapshot(int index) const { ZMPacket *Monitor::getSnapshot(int index) const {
if ((index < 0) || (index >= image_buffer_count)) { if ((index < 0) || (index >= image_buffer_count)) {
index = shared_data->last_write_index; index = shared_data->last_write_index;
} }
if (!image_buffer.size() or static_cast<size_t>(index) >= image_buffer.size()) {
Error("Image Buffer has not been allocated");
return nullptr;
}
if (index != image_buffer_count) { if (index != image_buffer_count) {
return new ZMPacket(image_buffer[index], shared_timestamps[index]); return new ZMPacket(image_buffer[index], shared_timestamps[index]);
} else { } else {
@ -2462,6 +2472,10 @@ std::vector<std::shared_ptr<Monitor>> Monitor::LoadFfmpegMonitors(const char *fi
*/ */
int Monitor::Capture() { int Monitor::Capture() {
unsigned int index = image_count % image_buffer_count; unsigned int index = image_count % image_buffer_count;
if (!image_buffer.size() or index >= image_buffer.size()) {
Error("Image Buffer is invalid. Check ImageBufferCount");
return -1;
}
std::shared_ptr<ZMPacket> packet = std::make_shared<ZMPacket>(); std::shared_ptr<ZMPacket> packet = std::make_shared<ZMPacket>();
packet->image_index = image_count; packet->image_index = image_count;
@ -2663,9 +2677,9 @@ bool Monitor::Decode() {
} }
} }
if ( orientation != ROTATE_0 ) { if (orientation != ROTATE_0) {
Debug(2, "Doing rotation"); Debug(2, "Doing rotation");
switch ( orientation ) { switch (orientation) {
case ROTATE_0 : case ROTATE_0 :
// No action required // No action required
break; break;
@ -2703,7 +2717,7 @@ bool Monitor::Decode() {
} // end bool Monitor::Decode() } // end bool Monitor::Decode()
void Monitor::TimestampImage(Image *ts_image, const timeval &ts_time) const { void Monitor::TimestampImage(Image *ts_image, const timeval &ts_time) const {
if ( !label_format[0] ) if (!label_format[0])
return; return;
// Expand the strftime macros first // Expand the strftime macros first