Skip to content

Commit

Permalink
Protect against empty requires
Browse files Browse the repository at this point in the history
  • Loading branch information
ThePhD committed Jan 25, 2021
1 parent e1950b9 commit 27c3521
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 9 deletions.
9 changes: 9 additions & 0 deletions include/sol/state_view.hpp
Expand Up @@ -86,7 +86,16 @@ namespace sol {
optional<object> loaded = is_loaded_package(key);
if (loaded && loaded->valid())
return std::move(*loaded);
int before = lua_gettop(L);
action();
int after = lua_gettop(L);
if (before == after) {
// I mean, you were supposed to return
// something, ANYTHING, from your requires script. I guess I'll just
// but some trash in here, it's on you after that?
ensure_package(key, static_cast<void*>(L));
return object(L, lua_nil);
}
stack_reference sr(L, -1);
if (create_global)
set(key, sr);
Expand Down
2 changes: 1 addition & 1 deletion include/sol/table_proxy.hpp
Expand Up @@ -85,7 +85,7 @@ namespace sol {

template <typename T>
table_proxy&& set(T&& item) && {
tuple_set(std::make_index_sequence<std::tuple_size_v<meta::unqualified_t<key_type>>>(), std::forward<T>(item));
std::move(*this).tuple_set(std::make_index_sequence<std::tuple_size_v<meta::unqualified_t<key_type>>>(), std::forward<T>(item));
return std::move(*this);
}

Expand Down
4 changes: 2 additions & 2 deletions single/include/sol/config.hpp
Expand Up @@ -20,8 +20,8 @@
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

// This file was generated with a script.
// Generated 2021-01-24 17:38:14.787587 UTC
// This header was generated with sol v3.2.3 (revision d363ccd7)
// Generated 2021-01-25 02:52:07.886776 UTC
// This header was generated with sol v3.2.3 (revision e1950b9a)
// https://github.com/ThePhD/sol2

#ifndef SOL_SINGLE_CONFIG_HPP
Expand Down
4 changes: 2 additions & 2 deletions single/include/sol/forward.hpp
Expand Up @@ -20,8 +20,8 @@
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

// This file was generated with a script.
// Generated 2021-01-24 17:38:14.725587 UTC
// This header was generated with sol v3.2.3 (revision d363ccd7)
// Generated 2021-01-25 02:52:07.863772 UTC
// This header was generated with sol v3.2.3 (revision e1950b9a)
// https://github.com/ThePhD/sol2

#ifndef SOL_SINGLE_INCLUDE_FORWARD_HPP
Expand Down
15 changes: 12 additions & 3 deletions single/include/sol/sol.hpp
Expand Up @@ -20,8 +20,8 @@
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

// This file was generated with a script.
// Generated 2021-01-24 17:38:13.604128 UTC
// This header was generated with sol v3.2.3 (revision d363ccd7)
// Generated 2021-01-25 02:52:07.000664 UTC
// This header was generated with sol v3.2.3 (revision e1950b9a)
// https://github.com/ThePhD/sol2

#ifndef SOL_SINGLE_INCLUDE_HPP
Expand Down Expand Up @@ -24145,7 +24145,7 @@ namespace sol {

template <typename T>
table_proxy&& set(T&& item) && {
tuple_set(std::make_index_sequence<std::tuple_size_v<meta::unqualified_t<key_type>>>(), std::forward<T>(item));
std::move(*this).tuple_set(std::make_index_sequence<std::tuple_size_v<meta::unqualified_t<key_type>>>(), std::forward<T>(item));
return std::move(*this);
}

Expand Down Expand Up @@ -26553,7 +26553,16 @@ namespace sol {
optional<object> loaded = is_loaded_package(key);
if (loaded && loaded->valid())
return std::move(*loaded);
int before = lua_gettop(L);
action();
int after = lua_gettop(L);
if (before == after) {
// I mean, you were supposed to return
// something, ANYTHING, from your requires script. I guess I'll just
// but some trash in here, it's on you after that?
ensure_package(key, static_cast<void*>(L));
return object(L, lua_nil);
}
stack_reference sr(L, -1);
if (create_global)
set(key, sr);
Expand Down
25 changes: 24 additions & 1 deletion tests/runtime_tests/source/state.cpp
Expand Up @@ -145,7 +145,7 @@ TEST_CASE("state/require_file", "opening files as 'requires'") {
}

TEST_CASE("state/require_script", "opening strings as 'requires' clauses") {
std::string code = "return { modfunc = function () return 221 end }";
const std::string code = "return { modfunc = function () return 221 end }";

sol::state lua;
sol::stack_guard luasg(lua);
Expand All @@ -160,6 +160,29 @@ TEST_CASE("state/require_script", "opening strings as 'requires' clauses") {
REQUIRE((thingy1 == thingy2));
}

TEST_CASE("state/require_script empty", "opening strings as 'requires' clauses that return nothing insert a bogus value") {
sol::state lua;
lua.open_libraries(sol::lib::package, sol::lib::base);

lua.set_function("print_to_console", [](std::string const& message) { std::cout << message << std::endl; });

const std::string code = "print_to_console('hello from required file') i = 20";

{
int begintop = 0, endtop = 0;
test_stack_guard guard(lua, begintop, endtop);
lua.require_script("require_test", code, false);
}

sol::optional<sol::error> maybe_error = lua.safe_script(R"(
require ("require_test")
print_to_console("hello from script")
assert(i == 20)
)",
sol::script_pass_on_error);
REQUIRE_FALSE(maybe_error.has_value());
}

TEST_CASE("state/require", "require using a function") {
struct open {
static int open_func(lua_State* L) {
Expand Down

0 comments on commit 27c3521

Please sign in to comment.