You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

200 lines
7.7 KiB

From 6ec30440f64de88a2ac1e909532c99ae0c3ee61f Mon Sep 17 00:00:00 2001
From: Evil Eye <malusluminis@hotmail.com>
Date: Sat, 12 Jul 2025 15:33:54 +0200
Subject: [PATCH 7/9] Add Nerixyz's test and fix for C++17
---
include/sol/usertype_storage.hpp | 4 ++
tests/regression_tests/1716/CMakeLists.txt | 45 ++++++++++++++
.../source/1716 - registry indexing leak.cpp | 61 +++++++++++++++++++
tests/regression_tests/1716/source/main.cpp | 31 ++++++++++
tests/regression_tests/CMakeLists.txt | 1 +
5 files changed, 142 insertions(+)
create mode 100644 tests/regression_tests/1716/CMakeLists.txt
create mode 100644 tests/regression_tests/1716/source/1716 - registry indexing leak.cpp
create mode 100644 tests/regression_tests/1716/source/main.cpp
diff --git include/sol/usertype_storage.hpp include/sol/usertype_storage.hpp
index bdca6146..691a025b 100644
--- include/sol/usertype_storage.hpp
+++ include/sol/usertype_storage.hpp
@@ -502,7 +502,11 @@ namespace sol { namespace u_detail {
stateless_reference* target = nullptr;
{
stack_reference k = stack::get<stack_reference>(L, 2);
+#if __cpp_lib_generic_unordered_lookup >= 201811L
auto it = self.auxiliary_keys.find(k);
+#else
+ auto it = self.auxiliary_keys.find(basic_reference(k));
+#endif
if (it != self.auxiliary_keys.cend()) {
target = &it->second;
}
diff --git tests/regression_tests/1716/CMakeLists.txt tests/regression_tests/1716/CMakeLists.txt
new file mode 100644
index 00000000..33ca975c
--- /dev/null
+++ tests/regression_tests/1716/CMakeLists.txt
@@ -0,0 +1,45 @@
+# # # # sol2
+# The MIT License (MIT)
+#
+# Copyright (c) 2013-2022 Rapptz, ThePhD, and contributors
+#
+# Permission is hereby granted, free of charge, to any person obtaining a copy of
+# this software and associated documentation files (the "Software"), to deal in
+# the Software without restriction, including without limitation the rights to
+# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+# the Software, and to permit persons to whom the Software is furnished to do so,
+# subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be included in all
+# copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+# # # # sol2 tests - simple regression tests
+
+file(GLOB_RECURSE sources
+ LIST_DIRECTORIES FALSE
+ CONFIG_DEPENDS source/*.cpp)
+
+sol2_create_basic_test(sol2.tests.regression_1716 sol2::sol2 ${sources})
+sol2_create_basic_test(sol2.tests.regression_1716.SOL_ALL_SAFETIES_ON sol2::sol2 ${sources})
+target_compile_options(sol2.tests.regression_1716
+ PRIVATE
+ ${--allow-unreachable-code})
+target_compile_definitions(sol2.tests.regression_1716.SOL_ALL_SAFETIES_ON
+ PRIVATE
+ SOL_ALL_SAFETIES_ON=1)
+target_compile_options(sol2.tests.regression_1716.SOL_ALL_SAFETIES_ON
+ PRIVATE
+ ${--allow-unreachable-code})
+if (SOL2_TESTS_SINGLE)
+ sol2_create_basic_test(sol2.single.tests.regression_1716 sol2::sol2::single ${sources})
+ target_compile_options(sol2.single.tests.regression_1716
+ PRIVATE
+ ${--allow-unreachable-code})
+endif()
diff --git tests/regression_tests/1716/source/1716 - registry indexing leak.cpp tests/regression_tests/1716/source/1716 - registry indexing leak.cpp
new file mode 100644
index 00000000..9bc263f6
--- /dev/null
+++ tests/regression_tests/1716/source/1716 - registry indexing leak.cpp
@@ -0,0 +1,61 @@
+// sol2
+
+// The MIT License (MIT)
+
+// Copyright (c) 2013-2022 Rapptz, ThePhD and contributors
+
+// Permission is hereby granted, free of charge, to any person obtaining a copy of
+// this software and associated documentation files (the "Software"), to deal in
+// the Software without restriction, including without limitation the rights to
+// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+// the Software, and to permit persons to whom the Software is furnished to do so,
+// subject to the following conditions:
+
+// The above copyright notice and this permission notice shall be included in all
+// copies or substantial portions of the Software.
+
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+#include <catch2/catch_all.hpp>
+
+#include <sol/sol.hpp>
+
+TEST_CASE("#1716 - index and newindex should not leak values into the registry", "[sol2][regression][issue1716]") {
+ class vector {
+ public:
+ vector() = default;
+
+ static int my_index(vector&, int) {
+ return 0;
+ }
+
+ static void my_new_index(vector&, int, int) {
+ return;
+ }
+ };
+
+ sol::state lua;
+ lua.open_libraries(sol::lib::base);
+ lua.new_usertype<vector>("vector",
+ sol::constructors<sol::types<>>(), //
+ sol::meta_function::index,
+ &vector::my_index, //
+ sol::meta_function::new_index,
+ &vector::my_new_index);
+ auto begin = lua.registry().size();
+ lua.script(R"(
+ local v = vector.new()
+ local unused = 0
+ for i=1,100 do
+ unused = v[1]
+ v[1] = 1
+ end
+ )");
+
+ REQUIRE(lua.registry().size() <= begin + 1);
+}
diff --git tests/regression_tests/1716/source/main.cpp tests/regression_tests/1716/source/main.cpp
new file mode 100644
index 00000000..43e1e320
--- /dev/null
+++ tests/regression_tests/1716/source/main.cpp
@@ -0,0 +1,31 @@
+// sol2
+
+// The MIT License (MIT)
+
+// Copyright (c) 2013-2022 Rapptz, ThePhD and contributors
+
+// Permission is hereby granted, free of charge, to any person obtaining a copy of
+// this software and associated documentation files (the "Software"), to deal in
+// the Software without restriction, including without limitation the rights to
+// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+// the Software, and to permit persons to whom the Software is furnished to do so,
+// subject to the following conditions:
+
+// The above copyright notice and this permission notice shall be included in all
+// copies or substantial portions of the Software.
+
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+#define CATCH_CONFIG_RUNNER
+
+#include <catch2/catch_all.hpp>
+
+int main(int argc, char* argv[]) {
+ int result = Catch::Session().run(argc, argv);
+ return result;
+}
diff --git tests/regression_tests/CMakeLists.txt tests/regression_tests/CMakeLists.txt
index 7af8ed61..31400503 100644
--- tests/regression_tests/CMakeLists.txt
+++ tests/regression_tests/CMakeLists.txt
@@ -23,4 +23,5 @@
# # # # sol2 tests
add_subdirectory(1011)
+add_subdirectory(1716)
add_subdirectory(simple)
--
2.48.1