commit eb6f412: [Minor] Allow rspamd_lua_require_function to work without function

Vsevolod Stakhov vsevolod at rspamd.com
Fri Aug 18 13:00:04 UTC 2023


Author: Vsevolod Stakhov
Date: 2023-08-18 13:15:20 +0100
URL: https://github.com/rspamd/rspamd/commit/eb6f412f8f2002b16f25dadf0464b4cde5797d7d

[Minor] Allow rspamd_lua_require_function to work without function

---
 src/libutil/cxx/hash_util.hxx |  9 ++++----
 src/lua/lua_common.c          | 48 +++++++++++++++++++++++++++----------------
 2 files changed, 35 insertions(+), 22 deletions(-)

diff --git a/src/libutil/cxx/hash_util.hxx b/src/libutil/cxx/hash_util.hxx
index 972165850..05f3d97ae 100644
--- a/src/libutil/cxx/hash_util.hxx
+++ b/src/libutil/cxx/hash_util.hxx
@@ -1,11 +1,11 @@
-/*-
- * Copyright 2021 Vsevolod Stakhov
+/*
+ * Copyright 2023 Vsevolod Stakhov
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
- *   http://www.apache.org/licenses/LICENSE-2.0
+ *    http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
@@ -59,6 +59,7 @@ struct smart_ptr_equal {
 template<typename T>
 struct smart_ptr_hash {
 	using is_transparent = void; /* We want to find values in a set of shared_ptr by reference */
+	using is_avalanching = void;
 	auto operator()(const std::shared_ptr<T> &a) const
 	{
 		return std::hash<T>()(*a);
@@ -92,7 +93,7 @@ struct smart_str_equal {
 
 struct smart_str_hash {
 	using is_transparent = void;
-	using is_avalanching = typename ankerl::unordered_dense::hash<std::string_view>::is_avalanching;
+	using is_avalanching = void;
 	auto operator()(const std::string &a) const
 	{
 		return ankerl::unordered_dense::hash<std::string>()(a);
diff --git a/src/lua/lua_common.c b/src/lua/lua_common.c
index d41e69822..5991af656 100644
--- a/src/lua/lua_common.c
+++ b/src/lua/lua_common.c
@@ -2251,33 +2251,45 @@ rspamd_lua_require_function(lua_State *L, const gchar *modname,
 	lua_remove(L, err_pos);
 
 	/* Now we should have a table with results */
-	if (!lua_istable(L, -1)) {
-		msg_warn("require of %s.%s failed: not a table but %s", modname,
-				 funcname, lua_typename(L, lua_type(L, -1)));
+	if (funcname) {
+		if (!lua_istable(L, -1)) {
+			msg_warn("require of %s.%s failed: not a table but %s", modname,
+					 funcname, lua_typename(L, lua_type(L, -1)));
 
-		lua_pop(L, 1);
+			lua_pop(L, 1);
 
-		return FALSE;
-	}
+			return FALSE;
+		}
 
-	table_pos = lua_gettop(L);
-	lua_pushstring(L, funcname);
-	lua_gettable(L, -2);
+		table_pos = lua_gettop(L);
+		lua_pushstring(L, funcname);
+		lua_gettable(L, -2);
+
+		if (lua_type(L, -1) == LUA_TFUNCTION) {
+			/* Remove table, preserve just a function */
+			lua_remove(L, table_pos);
+
+			return TRUE;
+		}
+		else {
+			msg_warn("require of %s.%s failed: not a function but %s", modname,
+					 funcname, lua_typename(L, lua_type(L, -1)));
+		}
 
-	if (lua_type(L, -1) == LUA_TFUNCTION) {
-		/* Remove table, preserve just a function */
-		lua_remove(L, table_pos);
+		lua_pop(L, 2);
 
+		return FALSE;
+	}
+	else if (lua_isfunction(L, -1)) {
 		return TRUE;
 	}
 	else {
-		msg_warn("require of %s.%s failed: not a function but %s", modname,
-				 funcname, lua_typename(L, lua_type(L, -1)));
-	}
-
-	lua_pop(L, 2);
+		msg_warn("require of %s failed: not a function but %s", modname,
+				 lua_typename(L, lua_type(L, -1)));
+		lua_pop(L, 1);
 
-	return FALSE;
+		return FALSE;
+	}
 }
 
 gint rspamd_lua_function_ref_from_str(lua_State *L, const gchar *str, gsize slen,


More information about the Commits mailing list