diff --git a/src/zm_camera.cpp b/src/zm_camera.cpp index 8d31f8e82..e7bdd42a5 100644 --- a/src/zm_camera.cpp +++ b/src/zm_camera.cpp @@ -63,7 +63,7 @@ Camera::Camera( { linesize = width * colours; pixels = width * height; - imagesize = height * linesize; + imagesize = static_cast(height) * linesize; Debug(2, "New camera id: %d width: %d line size: %d height: %d colours: %d subpixelorder: %d capture: %d", monitor->Id(), width, linesize, height, colours, subpixelorder, capture); diff --git a/src/zm_eventstream.cpp b/src/zm_eventstream.cpp index d3076c170..9ea1d074a 100644 --- a/src/zm_eventstream.cpp +++ b/src/zm_eventstream.cpp @@ -773,13 +773,7 @@ bool EventStream::sendFrame(Microseconds delta_us) { } Image *send_image = prepareImage(image); - if (temp_img_buffer_size < send_image->Size()) { - Debug(1, "Resizing image buffer from %zu to %u", - temp_img_buffer_size, send_image->Size()); - delete[] temp_img_buffer; - temp_img_buffer = new uint8_t[send_image->Size()]; - temp_img_buffer_size = send_image->Size(); - } + reserveTempImgBuffer(send_image->Size()); int img_buffer_size = 0; uint8_t *img_buffer = temp_img_buffer; diff --git a/src/zm_fifo_debug.cpp b/src/zm_fifo_debug.cpp index 1b1950e3c..0beaeb6b1 100644 --- a/src/zm_fifo_debug.cpp +++ b/src/zm_fifo_debug.cpp @@ -81,7 +81,11 @@ void zmFifoDbgOutput( int len = va_arg(arg_ptr, int); dbg_ptr += snprintf(dbg_ptr, str_size-(dbg_ptr-dbg_string), "%d:", len); for ( int i = 0; i < len; i++ ) { - dbg_ptr += snprintf(dbg_ptr, str_size-(dbg_ptr-dbg_string), " %02x", data[i]); + const auto max_len = str_size - (dbg_ptr - dbg_string); + int rc = snprintf(dbg_ptr, max_len, " %02x", data[i]); + if (rc < 0 || rc > max_len) + break; + dbg_ptr += rc; } } else { dbg_ptr += vsnprintf(dbg_ptr, str_size-(dbg_ptr-dbg_string), fstring, arg_ptr); diff --git a/src/zm_font.cpp b/src/zm_font.cpp index 7ecaaa6b1..43a26283a 100644 --- a/src/zm_font.cpp +++ b/src/zm_font.cpp @@ -101,7 +101,7 @@ FontLoadError ZmFont::LoadFontFile(const std::string &loc) { } std::vector bitmap; - bitmap.resize(bitmap_header.number_of_code_points * bitmap_header.char_height); + bitmap.resize(static_cast(bitmap_header.number_of_code_points) * bitmap_header.char_height); std::size_t bitmap_bytes = bitmap.size() * sizeof(uint64); font_file.read(reinterpret_cast(bitmap.data()), static_cast(bitmap_bytes)); diff --git a/src/zm_image.cpp b/src/zm_image.cpp index 65aec46f8..c62b89ebb 100644 --- a/src/zm_image.cpp +++ b/src/zm_image.cpp @@ -667,7 +667,7 @@ void Image::AssignDirect( return; } - size_t new_buffer_size = p_width * p_height * p_colours; + size_t new_buffer_size = static_cast(p_width) * p_height * p_colours; if ( buffer_size < new_buffer_size ) { Error("Attempt to directly assign buffer from an undersized buffer of size: %zu, needed %dx%d*%d colours = %zu", @@ -2759,7 +2759,7 @@ void Image::Scale(const unsigned int new_width, const unsigned int new_height) { if (width == new_width and height == new_height) return; // Why larger than we need? - size_t scale_buffer_size = (new_width+1) * (new_height+1) * colours; + size_t scale_buffer_size = static_cast(new_width+1) * (new_height+1) * colours; uint8_t* scale_buffer = AllocBuffer(scale_buffer_size); AVPixelFormat format = AVPixFormat(); @@ -2789,7 +2789,7 @@ void Image::Scale(const unsigned int factor) { unsigned int new_height = (height*factor)/ZM_SCALE_BASE; // Why larger than we need? - size_t scale_buffer_size = (new_width+1) * (new_height+1) * colours; + size_t scale_buffer_size = static_cast(new_width+1) * (new_height+1) * colours; uint8_t* scale_buffer = AllocBuffer(scale_buffer_size); diff --git a/src/zm_logger.cpp b/src/zm_logger.cpp index 4db535a78..87b2f692e 100644 --- a/src/zm_logger.cpp +++ b/src/zm_logger.cpp @@ -481,7 +481,11 @@ void Logger::logPrint(bool hex, const char *filepath, int line, int level, const int i; logPtr += snprintf(logPtr, sizeof(logString)-(logPtr-logString), "%d:", len); for ( i = 0; i < len; i++ ) { - logPtr += snprintf(logPtr, sizeof(logString)-(logPtr-logString), " %02x", data[i]); + const size_t max_len = sizeof(logString) - (logPtr - logString); + int rc = snprintf(logPtr, max_len, " %02x", data[i]); + if (rc < 0 || static_cast(rc) > max_len) + break; + logPtr += rc; } } else { logPtr += vsnprintf(logPtr, sizeof(logString)-(logPtr-logString), fstring, argPtr); diff --git a/src/zm_monitor.cpp b/src/zm_monitor.cpp index c9d368019..680b465e7 100644 --- a/src/zm_monitor.cpp +++ b/src/zm_monitor.cpp @@ -2748,24 +2748,29 @@ void Monitor::TimestampImage(Image *ts_image, SystemTimePoint ts_time) const { while (*s_ptr && ((unsigned int)(d_ptr - label_text) < (unsigned int) sizeof(label_text))) { if ( *s_ptr == config.timestamp_code_char[0] ) { + const auto max_len = sizeof(label_text) - (d_ptr - label_text); bool found_macro = false; + int rc = 0; switch ( *(s_ptr+1) ) { case 'N' : - d_ptr += snprintf(d_ptr, sizeof(label_text)-(d_ptr-label_text), "%s", name.c_str()); + rc = snprintf(d_ptr, max_len, "%s", name.c_str()); found_macro = true; break; case 'Q' : - d_ptr += snprintf(d_ptr, sizeof(label_text)-(d_ptr-label_text), "%s", trigger_data->trigger_showtext); + rc = snprintf(d_ptr, max_len, "%s", trigger_data->trigger_showtext); found_macro = true; break; case 'f' : typedef std::chrono::duration Centiseconds; Centiseconds centi_sec = std::chrono::duration_cast( ts_time.time_since_epoch() - std::chrono::duration_cast(ts_time.time_since_epoch())); - d_ptr += snprintf(d_ptr, sizeof(label_text) - (d_ptr - label_text), "%02lld", static_cast(centi_sec.count())); + rc = snprintf(d_ptr, max_len, "%02lld", static_cast(centi_sec.count())); found_macro = true; break; } + if (rc < 0 || static_cast(rc) > max_len) + break; + d_ptr += rc; if ( found_macro ) { s_ptr += 2; continue; diff --git a/src/zm_monitorstream.cpp b/src/zm_monitorstream.cpp index f8e7de132..062ece605 100644 --- a/src/zm_monitorstream.cpp +++ b/src/zm_monitorstream.cpp @@ -385,13 +385,7 @@ bool MonitorStream::sendFrame(Image *image, SystemTimePoint timestamp) { /* double pts = */ vid_stream->EncodeFrame(send_image->Buffer(), send_image->Size(), config.mpeg_timed_frames, delta_time.count()); } else { - if (temp_img_buffer_size < send_image->Size()) { - Debug(1, "Resizing image buffer from %zu to %u", - temp_img_buffer_size, send_image->Size()); - delete[] temp_img_buffer; - temp_img_buffer = new uint8_t[send_image->Size()]; - temp_img_buffer_size = send_image->Size(); - } + reserveTempImgBuffer(send_image->Size()); int img_buffer_size = 0; unsigned char *img_buffer = temp_img_buffer; diff --git a/src/zm_packetqueue.cpp b/src/zm_packetqueue.cpp index fcad7b0ef..77b0e41ae 100644 --- a/src/zm_packetqueue.cpp +++ b/src/zm_packetqueue.cpp @@ -334,6 +334,7 @@ void PacketQueue::clearPackets(const std::shared_ptr &add_packet) { ++it; } // end while + Debug(1, "Resulting it pointing at latest packet? %d, next front points to begin? %d, Keyframe interval %d", ( *it == add_packet ), ( next_front == pktQueue.begin() ), diff --git a/src/zm_stream.cpp b/src/zm_stream.cpp index 16f30795a..4164946c9 100644 --- a/src/zm_stream.cpp +++ b/src/zm_stream.cpp @@ -34,7 +34,7 @@ constexpr Milliseconds StreamBase::MAX_SLEEP; StreamBase::~StreamBase() { delete vid_stream; - delete temp_img_buffer; + delete[] temp_img_buffer; closeComms(); } @@ -403,3 +403,13 @@ void StreamBase::closeComms() { } } } // end void StreamBase::closeComms + +void StreamBase::reserveTempImgBuffer(size_t size) +{ + if (temp_img_buffer_size < size) { + Debug(1, "Resizing image buffer from %zu to %zu", temp_img_buffer_size, size); + delete[] temp_img_buffer; + temp_img_buffer = new uint8_t[size]; + temp_img_buffer_size = size; + } +} diff --git a/src/zm_stream.h b/src/zm_stream.h index d46e89893..a4b03a3a3 100644 --- a/src/zm_stream.h +++ b/src/zm_stream.h @@ -148,7 +148,7 @@ protected: CmdMsg msg; bool got_command = false; // commands like zoom should output a frame even if paused - unsigned char *temp_img_buffer; // Used when encoding or sending file data + uint8_t *temp_img_buffer; // Used when encoding or sending file data size_t temp_img_buffer_size; protected: @@ -158,6 +158,7 @@ protected: Image *prepareImage(Image *image); void checkCommandQueue(); virtual void processCommand(const CmdMsg *msg)=0; + void reserveTempImgBuffer(size_t size); public: StreamBase():