From 00a1927c1fe7634fc017c023208019e00b51a853 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Sat, 31 Oct 2026 21:02:37 -0700 Subject: [PATCH] Fixed memory errors. (#4113) --- src/bgfx.cpp | 7 ++++--- src/bgfx_p.h | 3 ++- src/renderer_vk.cpp | 7 +++++-- src/renderer_vk.h | 2 ++ src/renderer_webgpu.cpp | 8 ++++++-- 5 files changed, 19 insertions(+), 8 deletions(-) diff --git a/src/bgfx.cpp b/src/bgfx.cpp index 6c7bb56f3..1c91e0f28 100644 --- a/src/bgfx.cpp +++ b/src/bgfx.cpp @@ -108,11 +108,11 @@ namespace bgfx int32_t len = bx::snprintf(out, sizeof(temp), "%s (%d): ", _filePath, _line); int32_t total = len + bx::vsnprintf(out + len, sizeof(temp)-len, _format, argListCopy); va_end(argListCopy); - if ( (int32_t)sizeof(temp) < total) + if ( (int32_t)sizeof(temp) <= total) { out = (char*)BX_STACK_ALLOC(total+1); bx::memCopy(out, temp, len); - bx::vsnprintf(out + len, total-len, _format, _argList); + bx::vsnprintf(out + len, total-len+1, _format, _argList); } out[total] = '\0'; bx::debugOutput(out); @@ -6118,7 +6118,6 @@ namespace bgfx { const Attachment& at = _attachment[ii]; const TextureHandle texHandle = at.handle; - const TextureRef& tr = s_ctx->m_textureRef[texHandle.idx]; BGFX_ERROR_CHECK(true && isValid(texHandle) @@ -6131,6 +6130,8 @@ namespace bgfx , texHandle.idx ); + const TextureRef& tr = s_ctx->m_textureRef[texHandle.idx]; + BGFX_ERROR_CHECK(true && at.mip < tr.m_numMips , _err diff --git a/src/bgfx_p.h b/src/bgfx_p.h index a52723e64..ee8ea89a7 100644 --- a/src/bgfx_p.h +++ b/src/bgfx_p.h @@ -7401,12 +7401,13 @@ namespace bgfx void setNameForHandle(Handle _handle, const bx::StringView& _name) { char tmp[1024]; - uint16_t len = 1+(uint16_t)bx::snprintf(tmp, BX_COUNTOF(tmp) + const int32_t total = bx::snprintf(tmp, BX_COUNTOF(tmp) , "%sH %d: %S" , _handle.getTypeName().abrvName , _handle.idx , &_name ); + const uint16_t len = uint16_t(bx::min(total, BX_COUNTOF(tmp) - 1) + 1); CommandBuffer& cmdbuf = getCommandBuffer(CommandBuffer::SetName); cmdbuf.write(_handle); diff --git a/src/renderer_vk.cpp b/src/renderer_vk.cpp index c84eb8e1f..bddd9e2e4 100644 --- a/src/renderer_vk.cpp +++ b/src/renderer_vk.cpp @@ -10096,7 +10096,7 @@ VK_DESTROY : m_backBufferColorImageView[ii] ; - if (NULL != m_backBufferDepthStencilImageView) + if (VK_NULL_HANDLE != m_backBufferDepthStencilImageView) { attachments[numAttachments++] = m_backBufferDepthStencilImageView; } @@ -10759,7 +10759,10 @@ VK_DESTROY const bool acquired = m_swapChain.acquire(_commandBuffer, _block); m_needPresent = m_swapChain.m_needPresent; - m_currentFramebuffer = m_swapChain.m_backBufferFrameBuffer[m_swapChain.m_backBufferColorIdx]; + m_currentFramebuffer = acquired + ? m_swapChain.m_backBufferFrameBuffer[m_swapChain.m_backBufferColorIdx] + : VkFramebuffer(VK_NULL_HANDLE) + ; return acquired; } diff --git a/src/renderer_vk.h b/src/renderer_vk.h index fcdaebe46..8517be7e6 100644 --- a/src/renderer_vk.h +++ b/src/renderer_vk.h @@ -796,6 +796,7 @@ VK_DESTROY_FUNC(DescriptorSet); , m_vsync(false) , m_surface(VK_NULL_HANDLE) , m_swapChain(VK_NULL_HANDLE) + , m_backBufferColorIdx(0) , m_lastImageRenderedSemaphore(VK_NULL_HANDLE) , m_lastImageAcquiredSemaphore(VK_NULL_HANDLE) , m_needPresent(false) @@ -895,6 +896,7 @@ VK_DESTROY_FUNC(DescriptorSet); , m_numTh(0) , m_nwh(NULL) , m_needPresent(false) + , m_needResolve(false) , m_framebuffer(VK_NULL_HANDLE) , m_currentFramebuffer(VK_NULL_HANDLE) { diff --git a/src/renderer_webgpu.cpp b/src/renderer_webgpu.cpp index 6ddfa1c70..3a4cf85ca 100644 --- a/src/renderer_webgpu.cpp +++ b/src/renderer_webgpu.cpp @@ -4421,12 +4421,16 @@ WGPU_IMPORT bool hasStencil(TextureFormat::Enum _format) { - return 0 < bimg::getBlockInfo(bimg::TextureFormat::Enum(_format) ).stencilBits; + return bimg::isDepth(bimg::TextureFormat::Enum(_format) ) + && 0 < bimg::getBlockInfo(bimg::TextureFormat::Enum(_format) ).stencilBits + ; } bool hasDepth(TextureFormat::Enum _format) { - return 0 < bimg::getBlockInfo(bimg::TextureFormat::Enum(_format) ).depthBits; + return bimg::isDepth(bimg::TextureFormat::Enum(_format) ) + && 0 < bimg::getBlockInfo(bimg::TextureFormat::Enum(_format) ).depthBits + ; } void addAttachmentState(bx::HashMurmur3& _murmur, const FrameBufferWGPU& _fb) const