From 964e2b3a9e35dc91c3c1b6688f743922f9660b87 Mon Sep 17 00:00:00 2001 From: demolke Date: Thu, 5 Dec 2024 23:23:53 +0100 Subject: [PATCH] Fix handling of leading `..` in simplify_path Prior to this `..\..\texture.png` was incorrectly simplified to `texture.png` --- core/string/ustring.cpp | 2 +- tests/core/string/test_string.h | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/core/string/ustring.cpp b/core/string/ustring.cpp index 9238e74ab18..30b30f5a84d 100644 --- a/core/string/ustring.cpp +++ b/core/string/ustring.cpp @@ -4608,7 +4608,7 @@ String String::simplify_path() const { dirs.remove_at(i); i--; } else if (d == "..") { - if (i != 0) { + if (i != 0 && dirs[i - 1] != "..") { dirs.remove_at(i); dirs.remove_at(i - 1); i -= 2; diff --git a/tests/core/string/test_string.h b/tests/core/string/test_string.h index 9adc97e8450..ea60230947d 100644 --- a/tests/core/string/test_string.h +++ b/tests/core/string/test_string.h @@ -1687,6 +1687,10 @@ TEST_CASE("[String] Path functions") { for (int i = 0; i < 3; i++) { CHECK(String(file_name[i]).is_valid_filename() == valid[i]); } + + CHECK(String("res://texture.png") == String("res://folder/../folder/../texture.png").simplify_path()); + CHECK(String("res://texture.png") == String("res://folder/sub/../../texture.png").simplify_path()); + CHECK(String("res://../../texture.png") == String("res://../../texture.png").simplify_path()); } TEST_CASE("[String] hash") {