From 7c3985f8a0113e62b0357c0a17a03ccaa7422b96 Mon Sep 17 00:00:00 2001 From: Mai-Lapyst Date: Tue, 25 Feb 2025 01:00:31 +0100 Subject: [PATCH] Add check for method limit; closes #1193 --- AUTHORS | 1 + src/vm/wren_common.h | 3 +++ src/vm/wren_compiler.c | 8 +++++++- src/vm/wren_vm.c | 1 + 4 files changed, 12 insertions(+), 1 deletion(-) diff --git a/AUTHORS b/AUTHORS index ecab48dc..ca1520d4 100644 --- a/AUTHORS +++ b/AUTHORS @@ -29,4 +29,5 @@ ruby0x1 Kolja Kube Alexander Klingenbeck Aviv Beeri +Mai Lapyst diff --git a/src/vm/wren_common.h b/src/vm/wren_common.h index 4ee9e7e2..8f3793bc 100644 --- a/src/vm/wren_common.h +++ b/src/vm/wren_common.h @@ -113,6 +113,9 @@ // field index*. #define MAX_FIELDS 255 +// The maximum number of methods the vm can handle. +#define MAX_METHODS 65535 + // Use the VM's allocator to allocate an object of [type]. #define ALLOCATE(vm, type) \ ((type*)wrenReallocate(vm, NULL, 0, sizeof(type))) diff --git a/src/vm/wren_compiler.c b/src/vm/wren_compiler.c index 92b16cac..47705f5e 100644 --- a/src/vm/wren_compiler.c +++ b/src/vm/wren_compiler.c @@ -1852,8 +1852,14 @@ static void finishParameterList(Compiler* compiler, Signature* signature) // Gets the symbol for a method [name] with [length]. static int methodSymbol(Compiler* compiler, const char* name, int length) { - return wrenSymbolTableEnsure(compiler->parser->vm, + int symbol = wrenSymbolTableEnsure(compiler->parser->vm, &compiler->parser->vm->methodNames, name, length); + + if (symbol > MAX_METHODS) { + error(compiler, "Method limit of %d reached.", MAX_METHODS); + } + + return symbol; } // Appends characters to [name] (and updates [length]) for [numParams] "_" diff --git a/src/vm/wren_vm.c b/src/vm/wren_vm.c index 254d0b03..f63ab8cd 100644 --- a/src/vm/wren_vm.c +++ b/src/vm/wren_vm.c @@ -1418,6 +1418,7 @@ WrenHandle* wrenMakeCallHandle(WrenVM* vm, const char* signature) // Add the signatue to the method table. int method = wrenSymbolTableEnsure(vm, &vm->methodNames, signature, signatureLength); + ASSERT(method <= MAX_METHODS, "Method limit reached."); // Create a little stub function that assumes the arguments are on the stack // and calls the method.