From 6140b4b33ef0c1d87e233c8fb064142233f9f4b9 Mon Sep 17 00:00:00 2001 From: Michael Hansen Date: Mon, 16 Aug 2021 15:25:09 +0000 Subject: [PATCH] Protect against out of range lookups in pycdas Fixes #198. --- bytecode.cpp | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/bytecode.cpp b/bytecode.cpp index ca4d9de..7081eec 100644 --- a/bytecode.cpp +++ b/bytecode.cpp @@ -1,5 +1,6 @@ #include "pyc_numeric.h" #include "bytecode.h" +#include #include #ifdef _MSC_VER @@ -356,14 +357,31 @@ void bc_disasm(PycRef code, PycModule* mod, int indent) if (opcode >= Pyc::PYC_HAVE_ARG) { if (Pyc::IsConstArg(opcode)) { - fprintf(pyc_output, "%d: ", operand); - print_const(code->getConst(operand), mod); + try { + auto constParam = code->getConst(operand); + fprintf(pyc_output, "%d: ", operand); + print_const(constParam, mod); + } catch (const std::out_of_range &) { + fprintf(pyc_output, "%d ", operand); + } } else if (Pyc::IsNameArg(opcode)) { - fprintf(pyc_output, "%d: %s", operand, code->getName(operand)->value()); + try { + fprintf(pyc_output, "%d: %s", operand, code->getName(operand)->value()); + } catch (const std::out_of_range &) { + fprintf(pyc_output, "%d ", operand); + } } else if (Pyc::IsVarNameArg(opcode)) { - fprintf(pyc_output, "%d: %s", operand, code->getVarName(operand)->value()); + try { + fprintf(pyc_output, "%d: %s", operand, code->getVarName(operand)->value()); + } catch (const std::out_of_range &) { + fprintf(pyc_output, "%d ", operand); + } } else if (Pyc::IsCellArg(opcode)) { - fprintf(pyc_output, "%d: %s", operand, code->getCellVar(operand)->value()); + try { + fprintf(pyc_output, "%d: %s", operand, code->getCellVar(operand)->value()); + } catch (const std::out_of_range &) { + fprintf(pyc_output, "%d ", operand); + } } else if (Pyc::IsJumpOffsetArg(opcode)) { fprintf(pyc_output, "%d (to %d)", operand, pos+operand); } else if (Pyc::IsCompareArg(opcode)) {