From a9323e33e69a05ae6815047bb26ecc227420bb79 Mon Sep 17 00:00:00 2001 From: Oleksandr Nemesh Date: Sun, 15 Dec 2024 02:11:53 +0200 Subject: [PATCH] fix string multiplication bug --- src/value.cpp | 6 +++++- test/main.cpp | 1 + 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/value.cpp b/src/value.cpp index 7a3b891..9844a1f 100644 --- a/src/value.cpp +++ b/src/value.cpp @@ -259,7 +259,11 @@ namespace rift { } auto const& str = isString() ? getString() : other.getString(); - auto const& num = isString() ? other.toInteger() : toInteger(); + auto num = isString() ? other.toInteger() : toInteger(); + + if (num <= 0) { + return geode::Ok(Value("")); + } std::string result; result.reserve(str.size() * num); diff --git a/test/main.cpp b/test/main.cpp index 87e03ff..4ef4c85 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -111,6 +111,7 @@ int main() { RIFT_EVAL("('hello world' - 'hello ')[2] == 'r'", true); // string subtraction RIFT_TEST("{middlePad('#' * (progress * 4 / 10), 40, '-')} {progress}%", "----------####################---------- 50%", {{"progress", 50}}); RIFT_TEST("{min(20, 40)}", "20"); + RIFT_EVAL("-1 * 'hello'", ""); // making sure string multiplication with negative number is empty fmt::println("\nResults:\nTests passed: {}/{}\nTests failed: {}/{}", TEST_PASSED, TEST_COUNT, TEST_FAILED, TEST_COUNT); return TEST_FAILED;