From e5da24f490ac0dd508fcccbd5ea80f8e55fe8575 Mon Sep 17 00:00:00 2001 From: Miha Lunar Date: Fri, 21 Nov 2014 00:57:33 +0100 Subject: [PATCH] Fix for crash when too many lines / vertices When a lot of lines are being drawn, this can overflow the vertex buffer, causing a crash in `nvgRenderFlush`. While the underlying code handles this case in an okay manner (truncating the vertex buffer instead of crashing), the `nvgRenderFlush` code is missing a check for this case, causing it to think it has more space than was allocated. I tried to fix it with multiple buffers as mentioned in #160, however it seems more complex than it appears. This fix just makes it truncate the output according to the actual allocated amount of bytes in the vertex buffer, so it doesn't crash at least. --- examples/common/nanovg/nanovg_bgfx.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/examples/common/nanovg/nanovg_bgfx.cpp b/examples/common/nanovg/nanovg_bgfx.cpp index 15aaaa621..c5416acb2 100644 --- a/examples/common/nanovg/nanovg_bgfx.cpp +++ b/examples/common/nanovg/nanovg_bgfx.cpp @@ -30,6 +30,8 @@ #include +#include + namespace { #include "vs_nanovg_fill.bin.h" @@ -686,6 +688,14 @@ namespace if (gl->ncalls > 0) { bgfx::allocTransientVertexBuffer(&gl->tvb, gl->nverts, s_nvgDecl); + + int allocated = gl->tvb.size/gl->tvb.stride; + + if (allocated < gl->nverts) { + gl->nverts = allocated; + BX_WARN(true, "Vertex number truncated due to transient vertex buffer overflow"); + } + memcpy(gl->tvb.data, gl->verts, gl->nverts * sizeof(struct NVGvertex) );