From fba0110073fac51d96b0cb13b9bfbde32934d304 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Wed, 5 Nov 2014 08:06:15 -0800 Subject: [PATCH] Properly clamp some iterative snprintf()s. --- GPU/GeDisasm.cpp | 18 +++++++++--------- Windows/Debugger/CtrlDisAsmView.cpp | 4 ++-- Windows/GEDebugger/CtrlDisplayListView.cpp | 4 ++-- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/GPU/GeDisasm.cpp b/GPU/GeDisasm.cpp index 94de3f73cb..549b4c3707 100644 --- a/GPU/GeDisasm.cpp +++ b/GPU/GeDisasm.cpp @@ -57,27 +57,27 @@ void GeDescribeVertexType(u32 op, char *buffer, int len) { char *w = buffer, *end = buffer + len; if (through) w += snprintf(w, end - w, "through, "); - if (typeNames[tc]) + if (typeNames[tc] && w < end) w += snprintf(w, end - w, "%s texcoords, ", typeNames[tc]); - if (colorNames[col]) + if (colorNames[col] && w < end) w += snprintf(w, end - w, "%s colors, ", colorNames[col]); - if (typeNames[nrm]) + if (typeNames[nrm] && w < end) w += snprintf(w, end - w, "%s normals, ", typeNamesS[nrm]); - if (typeNames[pos]) + if (typeNames[pos] && w < end) w += snprintf(w, end - w, "%s positions, ", typeNamesS[pos]); - if (typeNames[weight]) + if (typeNames[weight] && w < end) w += snprintf(w, end - w, "%s weights (%d), ", typeNames[weight], weightCount); - else if (weightCount > 0) + else if (weightCount > 0 && w < end) w += snprintf(w, end - w, "unknown weights (%d), ", weightCount); - if (morphCount > 0) + if (morphCount > 0 && w < end) w += snprintf(w, end - w, "%d morphs, ", morphCount); - if (typeNames[idx]) + if (typeNames[idx] && w < end) w += snprintf(w, end - w, "%s indexes, ", typeNames[idx]); if (w < buffer + 2) snprintf(buffer, len, "none"); // Otherwise, get rid of the pesky trailing comma. - else + else if (w < end) w[-2] = '\0'; } diff --git a/Windows/Debugger/CtrlDisAsmView.cpp b/Windows/Debugger/CtrlDisAsmView.cpp index 3fe114c135..1e7076cb2f 100644 --- a/Windows/Debugger/CtrlDisAsmView.cpp +++ b/Windows/Debugger/CtrlDisAsmView.cpp @@ -905,12 +905,12 @@ void CtrlDisAsmView::copyInstructions(u32 startAddr, u32 endAddr, bool withDisas char *temp = new char[space]; char *p = temp, *end = temp + space; - for (u32 pos = startAddr; pos < endAddr; pos += instructionSize) + for (u32 pos = startAddr; pos < endAddr && p < end; pos += instructionSize) { p += snprintf(p, end - p, "%08X", debugger->readMemory(pos)); // Don't leave a trailing newline. - if (pos + instructionSize < endAddr) + if (pos + instructionSize < endAddr && p < end) p += snprintf(p, end - p, "\r\n"); } W32Util::CopyTextToClipboard(wnd, temp); diff --git a/Windows/GEDebugger/CtrlDisplayListView.cpp b/Windows/GEDebugger/CtrlDisplayListView.cpp index 54d8dccf12..8c1e12c112 100644 --- a/Windows/GEDebugger/CtrlDisplayListView.cpp +++ b/Windows/GEDebugger/CtrlDisplayListView.cpp @@ -314,7 +314,7 @@ void CtrlDisplayListView::onMouseUp(WPARAM wParam, LPARAM lParam, int button) char *temp = new char[space]; char *p = temp, *end = temp + space; - for (u32 pos = selectRangeStart; pos < selectRangeEnd; pos += instructionSize) + for (u32 pos = selectRangeStart; pos < selectRangeEnd && p < end; pos += instructionSize) { GPUDebugOp op = gpuDebug->DissassembleOp(pos); p += snprintf(p, end - p, "%s\r\n", op.desc.c_str()); @@ -351,7 +351,7 @@ void CtrlDisplayListView::onMouseUp(WPARAM wParam, LPARAM lParam, int button) char *temp = new char[space]; char *p = temp, *end = temp + space; - for (u32 pos = selectRangeStart; pos < selectRangeEnd; pos += instructionSize) + for (u32 pos = selectRangeStart; pos < selectRangeEnd && p < end; pos += instructionSize) p += snprintf(p, end - p, "%08X\r\n", Memory::ReadUnchecked_U32(pos)); W32Util::CopyTextToClipboard(wnd, temp);