commit 5137b79: [Project] Fix on conditions

Vsevolod Stakhov vsevolod at rspamd.com
Sat Apr 30 19:21:16 UTC 2022


Author: Vsevolod Stakhov
Date: 2022-04-04 21:33:46 +0100
URL: https://github.com/rspamd/rspamd/commit/5137b7942b4eb82a7321913b24025289396bd3f0

[Project] Fix on conditions

---
 src/libserver/symcache/symcache_impl.cxx     | 43 +++++++++++++++++++---------
 src/libserver/symcache/symcache_internal.hxx | 31 +++++++++++++++-----
 2 files changed, 53 insertions(+), 21 deletions(-)

diff --git a/src/libserver/symcache/symcache_impl.cxx b/src/libserver/symcache/symcache_impl.cxx
index 76bbf70b4..e1f3cafbb 100644
--- a/src/libserver/symcache/symcache_impl.cxx
+++ b/src/libserver/symcache/symcache_impl.cxx
@@ -57,27 +57,26 @@ auto symcache::init() -> bool
 	delayed_deps.reset();
 
 
-	cur = cache->delayed_conditions;
-	while (cur) {
-		dcond = cur->data;
+	/* Deal with the delayed conditions */
+	for (const auto &delayed_cond : *delayed_conditions) {
+		auto it = get_item_by_name_mut(delayed_cond.sym, true);
 
-		it = rspamd_symcache_find_filter(cache, dcond->sym, true);
-
-		if (it == NULL) {
+		if (it == nullptr) {
 			msg_err_cache (
 					"cannot register delayed condition for %s",
-					dcond->sym);
-			luaL_unref(dcond->L, LUA_REGISTRYINDEX, dcond->cbref);
+					delayed_cond.sym.c_str());
+			luaL_unref(delayed_cond.L, LUA_REGISTRYINDEX, delayed_cond.cbref);
 		}
 		else {
-			struct rspamd_symcache_condition *ncond = rspamd_mempool_alloc0 (cache->static_pool,
-					sizeof(*ncond));
-			ncond->cb = dcond->cbref;
-			DL_APPEND(it->specific.normal.conditions, ncond);
+			if (!it->add_condition(delayed_cond.L, delayed_cond.cbref)) {
+				msg_err_cache (
+						"cannot register delayed condition for %s: virtual parent; qed",
+						delayed_cond.sym.c_str());
+				g_abort();
+			}
 		}
-
-		cur = g_list_next (cur);
 	}
+	delayed_conditions.reset();
 
 	PTR_ARRAY_FOREACH (cache->items_by_id, i, it) {
 
@@ -353,6 +352,21 @@ auto symcache::get_item_by_name(std::string_view name, bool resolve_parent) cons
 	return it->second.get();
 }
 
+auto symcache::get_item_by_name_mut(std::string_view name, bool resolve_parent) const -> cache_item *
+{
+	auto it = items_by_symbol.find(name);
+
+	if (it == items_by_symbol.end()) {
+		return nullptr;
+	}
+
+	if (resolve_parent && it->second->is_virtual()) {
+		return (cache_item *) it->second->get_parent(*this);
+	}
+
+	return it->second.get();
+}
+
 auto symcache::add_dependency(int id_from, std::string_view to, int virtual_id_from)-> void
 {
 	g_assert (id_from >= 0 && id_from < (gint)items_by_id.size());
@@ -382,6 +396,7 @@ auto symcache::add_dependency(int id_from, std::string_view to, int virtual_id_f
 }
 
 
+
 auto cache_item::get_parent(const symcache &cache) const -> const cache_item *
 {
 	if (is_virtual()) {
diff --git a/src/libserver/symcache/symcache_internal.hxx b/src/libserver/symcache/symcache_internal.hxx
index b41c4a3df..b7e18ec1e 100644
--- a/src/libserver/symcache/symcache_internal.hxx
+++ b/src/libserver/symcache/symcache_internal.hxx
@@ -177,14 +177,14 @@ class symcache;
 
 struct item_condition {
 private:
-	gint cb;
 	lua_State *L;
+	int cb;
 public:
-	item_condition() {
-		// TODO
-	}
+	item_condition(lua_State *_L, int _cb) : L(_L), cb(_cb) {}
 	virtual ~item_condition() {
-		// TODO
+		if (cb != -1 && L != nullptr) {
+			luaL_unref(L, LUA_REGISTRYINDEX, cb);
+		}
 	}
 };
 
@@ -197,8 +197,8 @@ public:
 	explicit normal_item() {
 		// TODO
 	}
-	auto add_condition() -> void {
-		// TODO
+	auto add_condition(lua_State *L, int cbref) -> void {
+		conditions.emplace_back(L, cbref);
 	}
 	auto call() -> void {
 		// TODO
@@ -261,6 +261,16 @@ struct cache_item {
 
 	auto is_virtual() const -> bool { return std::holds_alternative<virtual_item>(specific); }
 	auto get_parent(const symcache &cache) const -> const cache_item *;
+	auto add_condition(lua_State *L, int cbref) -> bool {
+		if (!is_virtual()) {
+			auto &normal = std::get<normal_item>(specific);
+			normal.add_condition(L, cbref);
+
+			return true;
+		}
+
+		return false;
+	}
 };
 
 struct delayed_cache_dependency {
@@ -350,6 +360,13 @@ public:
 	 * @return
 	 */
 	auto get_item_by_name(std::string_view name, bool resolve_parent) const -> const cache_item *;
+	/**
+	 * Get an item by it's name, mutable pointer
+	 * @param name
+	 * @param resolve_parent
+	 * @return
+	 */
+	auto get_item_by_name_mut(std::string_view name, bool resolve_parent) const -> cache_item *;
 
 	/**
 	 * Add a direct dependency


More information about the Commits mailing list