From f80373e7a22101c318baa2dd2027fa4dbf2feb45 Mon Sep 17 00:00:00 2001 From: Jonathan Coates Date: Wed, 24 Jul 2024 19:34:34 +0100 Subject: [PATCH] Add bounds check to cc.strings.wrap Fixes #1905, closes #1906. Co-authored-by: Lupus590 --- .../data/computercraft/lua/rom/modules/main/cc/strings.lua | 5 +++-- .../test/resources/test-rom/spec/modules/cc/strings_spec.lua | 4 +++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/projects/core/src/main/resources/data/computercraft/lua/rom/modules/main/cc/strings.lua b/projects/core/src/main/resources/data/computercraft/lua/rom/modules/main/cc/strings.lua index 054673f36..09fc057e7 100644 --- a/projects/core/src/main/resources/data/computercraft/lua/rom/modules/main/cc/strings.lua +++ b/projects/core/src/main/resources/data/computercraft/lua/rom/modules/main/cc/strings.lua @@ -8,7 +8,8 @@ -- @since 1.95.0 -- @see textutils For additional string related utilities. -local expect = require("cc.expect").expect +local expect = require("cc.expect") +local expect, range = expect.expect, expect.range --[[- Wraps a block of text, so that each line fits within the given width. @@ -32,7 +33,7 @@ local function wrap(text, width) expect(1, text, "string") expect(2, width, "number", "nil") width = width or term.getSize() - + range(width, 1) local lines, lines_n, current_line = {}, 0, "" local function push_line() diff --git a/projects/core/src/test/resources/test-rom/spec/modules/cc/strings_spec.lua b/projects/core/src/test/resources/test-rom/spec/modules/cc/strings_spec.lua index 585f8ef1a..32d26d514 100644 --- a/projects/core/src/test/resources/test-rom/spec/modules/cc/strings_spec.lua +++ b/projects/core/src/test/resources/test-rom/spec/modules/cc/strings_spec.lua @@ -2,7 +2,7 @@ -- -- SPDX-License-Identifier: MPL-2.0 -describe("cc.pretty", function() +describe("cc.strings", function() local str = require("cc.strings") describe("wrap", function() @@ -11,6 +11,8 @@ describe("cc.pretty", function() str.wrap("test string is long", 11) expect.error(str.wrap, nil):eq("bad argument #1 (string expected, got nil)") expect.error(str.wrap, "", false):eq("bad argument #2 (number expected, got boolean)") + + expect.error(str.wrap, "", 0):eq("number outside of range (expected 0 to be within 1 and inf)") end) it("wraps lines", function()