From f7bb4611e9a47a29fc85dc5029f0cc8283a1fa25 Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Sat, 23 Sep 2023 11:15:21 -0400 Subject: [PATCH] Replace uses of strcpy() and sprintf() --- app/streaming/session.cpp | 13 +--- app/streaming/video/ffmpeg.cpp | 103 ++++++++++++++++--------- app/streaming/video/ffmpeg.h | 2 +- app/streaming/video/overlaymanager.cpp | 13 ++++ app/streaming/video/overlaymanager.h | 2 + 5 files changed, 87 insertions(+), 46 deletions(-) diff --git a/app/streaming/session.cpp b/app/streaming/session.cpp index 53ab2d04..5704c20e 100644 --- a/app/streaming/session.cpp +++ b/app/streaming/session.cpp @@ -190,13 +190,9 @@ void Session::clConnectionStatusUpdate(int connectionStatus) switch (connectionStatus) { case CONN_STATUS_POOR: - if (s_ActiveSession->m_StreamConfig.bitrate > 5000) { - strcpy(s_ActiveSession->m_OverlayManager.getOverlayText(Overlay::OverlayStatusUpdate), "Slow connection to PC\nReduce your bitrate"); - } - else { - strcpy(s_ActiveSession->m_OverlayManager.getOverlayText(Overlay::OverlayStatusUpdate), "Poor connection to PC"); - } - s_ActiveSession->m_OverlayManager.setOverlayTextUpdated(Overlay::OverlayStatusUpdate); + s_ActiveSession->m_OverlayManager.updateOverlayText(Overlay::OverlayStatusUpdate, + s_ActiveSession->m_StreamConfig.bitrate > 5000 ? + "Slow connection to PC\nReduce your bitrate" : "Poor connection to PC"); s_ActiveSession->m_OverlayManager.setOverlayState(Overlay::OverlayStatusUpdate, true); break; case CONN_STATUS_OKAY: @@ -1288,8 +1284,7 @@ void Session::notifyMouseEmulationMode(bool enabled) // We re-use the status update overlay for mouse mode notification if (m_MouseEmulationRefCount > 0) { - strcpy(m_OverlayManager.getOverlayText(Overlay::OverlayStatusUpdate), "Gamepad mouse mode active\nLong press Start to deactivate"); - m_OverlayManager.setOverlayTextUpdated(Overlay::OverlayStatusUpdate); + m_OverlayManager.updateOverlayText(Overlay::OverlayStatusUpdate, "Gamepad mouse mode active\nLong press Start to deactivate"); m_OverlayManager.setOverlayState(Overlay::OverlayStatusUpdate, true); } else { diff --git a/app/streaming/video/ffmpeg.cpp b/app/streaming/video/ffmpeg.cpp index 0fac812f..558dcd22 100644 --- a/app/streaming/video/ffmpeg.cpp +++ b/app/streaming/video/ffmpeg.cpp @@ -604,10 +604,11 @@ void FFmpegVideoDecoder::addVideoStats(VIDEO_STATS& src, VIDEO_STATS& dst) dst.renderedFps = (float)dst.renderedFrames / ((float)(now - dst.measurementStartTimestamp) / 1000); } -void FFmpegVideoDecoder::stringifyVideoStats(VIDEO_STATS& stats, char* output) +void FFmpegVideoDecoder::stringifyVideoStats(VIDEO_STATS& stats, char* output, ssize_t length) { int offset = 0; const char* codecString; + int ret; // Start with an empty string output[offset] = 0; @@ -652,54 +653,82 @@ void FFmpegVideoDecoder::stringifyVideoStats(VIDEO_STATS& stats, char* output) if (stats.receivedFps > 0) { if (m_VideoDecoderCtx != nullptr) { - offset += sprintf(&output[offset], - "Video stream: %dx%d %.2f FPS (Codec: %s)\n", - m_VideoDecoderCtx->width, - m_VideoDecoderCtx->height, - stats.totalFps, - codecString); + ret = snprintf(&output[offset], + length - offset, + "Video stream: %dx%d %.2f FPS (Codec: %s)\n", + m_VideoDecoderCtx->width, + m_VideoDecoderCtx->height, + stats.totalFps, + codecString); + if (ret < 0 || ret >= length - offset) { + SDL_assert(false); + return; + } + + offset += ret; } - offset += sprintf(&output[offset], - "Incoming frame rate from network: %.2f FPS\n" - "Decoding frame rate: %.2f FPS\n" - "Rendering frame rate: %.2f FPS\n", - stats.receivedFps, - stats.decodedFps, - stats.renderedFps); + ret = snprintf(&output[offset], + length - offset, + "Incoming frame rate from network: %.2f FPS\n" + "Decoding frame rate: %.2f FPS\n" + "Rendering frame rate: %.2f FPS\n", + stats.receivedFps, + stats.decodedFps, + stats.renderedFps); + if (ret < 0 || ret >= length - offset) { + SDL_assert(false); + return; + } + + offset += ret; } if (stats.framesWithHostProcessingLatency > 0) { - offset += sprintf(&output[offset], - "Host processing latency min/max/average: %.1f/%.1f/%.1f ms\n", - (float)stats.minHostProcessingLatency / 10, - (float)stats.maxHostProcessingLatency / 10, - (float)stats.totalHostProcessingLatency / 10 / stats.framesWithHostProcessingLatency); + ret = snprintf(&output[offset], + length - offset, + "Host processing latency min/max/average: %.1f/%.1f/%.1f ms\n", + (float)stats.minHostProcessingLatency / 10, + (float)stats.maxHostProcessingLatency / 10, + (float)stats.totalHostProcessingLatency / 10 / stats.framesWithHostProcessingLatency); + if (ret < 0 || ret >= length - offset) { + SDL_assert(false); + return; + } + + offset += ret; } if (stats.renderedFrames != 0) { char rttString[32]; if (stats.lastRtt != 0) { - sprintf(rttString, "%u ms (variance: %u ms)", stats.lastRtt, stats.lastRttVariance); + snprintf(rttString, sizeof(rttString), "%u ms (variance: %u ms)", stats.lastRtt, stats.lastRttVariance); } else { - sprintf(rttString, "N/A"); + snprintf(rttString, sizeof(rttString), "N/A"); } - offset += sprintf(&output[offset], - "Frames dropped by your network connection: %.2f%%\n" - "Frames dropped due to network jitter: %.2f%%\n" - "Average network latency: %s\n" - "Average decoding time: %.2f ms\n" - "Average frame queue delay: %.2f ms\n" - "Average rendering time (including monitor V-sync latency): %.2f ms\n", - (float)stats.networkDroppedFrames / stats.totalFrames * 100, - (float)stats.pacerDroppedFrames / stats.decodedFrames * 100, - rttString, - (float)stats.totalDecodeTime / stats.decodedFrames, - (float)stats.totalPacerTime / stats.renderedFrames, - (float)stats.totalRenderTime / stats.renderedFrames); + ret = snprintf(&output[offset], + length - offset, + "Frames dropped by your network connection: %.2f%%\n" + "Frames dropped due to network jitter: %.2f%%\n" + "Average network latency: %s\n" + "Average decoding time: %.2f ms\n" + "Average frame queue delay: %.2f ms\n" + "Average rendering time (including monitor V-sync latency): %.2f ms\n", + (float)stats.networkDroppedFrames / stats.totalFrames * 100, + (float)stats.pacerDroppedFrames / stats.decodedFrames * 100, + rttString, + (float)stats.totalDecodeTime / stats.decodedFrames, + (float)stats.totalPacerTime / stats.renderedFrames, + (float)stats.totalRenderTime / stats.renderedFrames); + if (ret < 0 || ret >= length - offset) { + SDL_assert(false); + return; + } + + offset += ret; } } @@ -707,7 +736,7 @@ void FFmpegVideoDecoder::logVideoStats(VIDEO_STATS& stats, const char* title) { if (stats.renderedFps > 0 || stats.renderedFrames != 0) { char videoStatsStr[512]; - stringifyVideoStats(stats, videoStatsStr); + stringifyVideoStats(stats, videoStatsStr, sizeof(videoStatsStr)); SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "%s", title); @@ -1393,7 +1422,9 @@ int FFmpegVideoDecoder::submitDecodeUnit(PDECODE_UNIT du) addVideoStats(m_LastWndVideoStats, lastTwoWndStats); addVideoStats(m_ActiveWndVideoStats, lastTwoWndStats); - stringifyVideoStats(lastTwoWndStats, Session::get()->getOverlayManager().getOverlayText(Overlay::OverlayDebug)); + stringifyVideoStats(lastTwoWndStats, + Session::get()->getOverlayManager().getOverlayText(Overlay::OverlayDebug), + Session::get()->getOverlayManager().getOverlayMaxTextLength()); Session::get()->getOverlayManager().setOverlayTextUpdated(Overlay::OverlayDebug); } diff --git a/app/streaming/video/ffmpeg.h b/app/streaming/video/ffmpeg.h index 0f99e8c1..fdd45b15 100644 --- a/app/streaming/video/ffmpeg.h +++ b/app/streaming/video/ffmpeg.h @@ -32,7 +32,7 @@ public: private: bool completeInitialization(const AVCodec* decoder, PDECODER_PARAMETERS params, bool testFrame, bool useAlternateFrontend); - void stringifyVideoStats(VIDEO_STATS& stats, char* output); + void stringifyVideoStats(VIDEO_STATS& stats, char* output, ssize_t length); void logVideoStats(VIDEO_STATS& stats, const char* title); diff --git a/app/streaming/video/overlaymanager.cpp b/app/streaming/video/overlaymanager.cpp index 69c53fb3..168e5233 100644 --- a/app/streaming/video/overlaymanager.cpp +++ b/app/streaming/video/overlaymanager.cpp @@ -58,6 +58,19 @@ char* OverlayManager::getOverlayText(OverlayType type) return m_Overlays[type].text; } +void OverlayManager::updateOverlayText(OverlayType type, const char* text) +{ + strncpy(m_Overlays[type].text, text, sizeof(m_Overlays[0].text)); + m_Overlays[type].text[getOverlayMaxTextLength() - 1] = '\0'; + + setOverlayTextUpdated(type); +} + +int OverlayManager::getOverlayMaxTextLength() +{ + return sizeof(m_Overlays[0].text); +} + int OverlayManager::getOverlayFontSize(OverlayType type) { return m_Overlays[type].fontSize; diff --git a/app/streaming/video/overlaymanager.h b/app/streaming/video/overlaymanager.h index 3c7805ab..59c808b9 100644 --- a/app/streaming/video/overlaymanager.h +++ b/app/streaming/video/overlaymanager.h @@ -29,6 +29,8 @@ public: bool isOverlayEnabled(OverlayType type); char* getOverlayText(OverlayType type); + void updateOverlayText(OverlayType type, const char* text); + int getOverlayMaxTextLength(); void setOverlayTextUpdated(OverlayType type); void setOverlayState(OverlayType type, bool enabled); SDL_Color getOverlayColor(OverlayType type);