From 375f1b9334aecb02079b27d2628bfa8df7284f5f Mon Sep 17 00:00:00 2001 From: Filip Tibell Date: Fri, 11 Aug 2023 19:17:48 -0500 Subject: [PATCH] Make luau compile options more strict to avoid panics --- .vscode/settings.json | 2 +- src/lune/lua/luau/options.rs | 16 ++++++++++++--- src/tests.rs | 7 ++++--- tests/luau/options.luau | 38 ++++++++++++++++++++++++++++++++++++ types/Luau.luau | 6 ++---- 5 files changed, 58 insertions(+), 11 deletions(-) create mode 100644 tests/luau/options.luau diff --git a/.vscode/settings.json b/.vscode/settings.json index 75095c1..9652462 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -4,7 +4,7 @@ "luau-lsp.types.roblox": false, "luau-lsp.require.mode": "relativeToFile", "luau-lsp.require.directoryAliases": { - "@lune/": "~/.lune/.typedefs/0.7.5/" + "@lune/": "./types/" }, // Luau - ignore type defs file in docs dir and dev scripts we use "luau-lsp.ignoreGlobs": [ diff --git a/src/lune/lua/luau/options.rs b/src/lune/lua/luau/options.rs index a3c0cc0..d1febe6 100644 --- a/src/lune/lua/luau/options.rs +++ b/src/lune/lua/luau/options.rs @@ -37,13 +37,23 @@ impl<'lua> FromLua<'lua> for LuauCompileOptions { LuaValue::Table(t) => { let mut options = Self::default(); - if let Some(optimization_level) = t.get("optimizationLevel")? { + let get_and_check = |name: &'static str| -> LuaResult> { + match t.get(name)? { + Some(n @ (0 | 1 | 2)) => Ok(Some(n)), + Some(n) => Err(LuaError::runtime(format!( + "'{name}' must be one of: 0, 1, or 2 - got {n}" + ))), + None => Ok(None), + } + }; + + if let Some(optimization_level) = get_and_check("optimizationLevel")? { options.optimization_level = optimization_level; } - if let Some(coverage_level) = t.get("coverageLevel")? { + if let Some(coverage_level) = get_and_check("coverageLevel")? { options.coverage_level = coverage_level; } - if let Some(debug_level) = t.get("debugLevel")? { + if let Some(debug_level) = get_and_check("debugLevel")? { options.debug_level = debug_level; } diff --git a/src/tests.rs b/src/tests.rs index 270de16..d88c047 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -44,6 +44,10 @@ create_tests! { fs_metadata: "fs/metadata", fs_move: "fs/move", + luau_compile: "luau/compile", + luau_load: "luau/load", + luau_options: "luau/options", + net_request_codes: "net/request/codes", net_request_compression: "net/request/compression", net_request_methods: "net/request/methods", @@ -101,9 +105,6 @@ create_tests! { task_delay: "task/delay", task_spawn: "task/spawn", task_wait: "task/wait", - - luau_compile: "luau/compile", - luau_load: "luau/load", } #[cfg(feature = "roblox")] diff --git a/tests/luau/options.luau b/tests/luau/options.luau new file mode 100644 index 0000000..6a67008 --- /dev/null +++ b/tests/luau/options.luau @@ -0,0 +1,38 @@ +local luau = require("@lune/luau") + +local EMPTY_LUAU_CODE_BLOCK = "do end" + +local MIN_OPTION_VALUE = 0 +local MAX_OPTION_VALUE = 2 +local OPTION_NAMES = { + "optimizationLevel", + "coverageLevel", + "debugLevel", +} + +for _, optionName in OPTION_NAMES do + -- In range should work + for optionValue = MIN_OPTION_VALUE, MAX_OPTION_VALUE, 1 do + local options = { [optionName] = optionValue } + local success2 = pcall(function() + luau.compile(EMPTY_LUAU_CODE_BLOCK, options) + end) + assert(success2, "expected `luau.compile` to accept options within range") + end + -- Lower than min range should error + for optionValue = MIN_OPTION_VALUE - 16, MIN_OPTION_VALUE - 1, 1 do + local options = { [optionName] = optionValue } + local success2 = pcall(function() + luau.compile(EMPTY_LUAU_CODE_BLOCK, options) + end) + assert(not success2, "expected `luau.compile` to not accept options outside of range") + end + -- Higher than max range should error + for optionValue = MAX_OPTION_VALUE + 1, MAX_OPTION_VALUE + 16, 1 do + local options = { [optionName] = optionValue } + local success2 = pcall(function() + luau.compile(EMPTY_LUAU_CODE_BLOCK, options) + end) + assert(not success2, "expected `luau.compile` to not accept options outside of range") + end +end diff --git a/types/Luau.luau b/types/Luau.luau index d57aee9..a54b2fc 100644 --- a/types/Luau.luau +++ b/types/Luau.luau @@ -71,8 +71,6 @@ local luau = {} coverageLevel: 0, debugLevel: 1, }) - - ... ``` @param source The string that'll be compiled into bytecode @@ -80,7 +78,7 @@ local luau = {} @return luau bytecode ]=] -function luau.compile(source: string, CompileOptions: CompileOptions): string +function luau.compile(source: string, CompileOptions: CompileOptions?): string return nil :: any end @@ -109,7 +107,7 @@ end @return luau function ]=] -function luau.load(source: string, loadOptions: LoadOptions): string +function luau.load(source: string, loadOptions: LoadOptions?): (...any) -> ...any return nil :: any end