]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Fix] Catch all exceptions when loading fasttext model to avoid crash
authorVsevolod Stakhov <vsevolod@rspamd.com>
Mon, 16 Feb 2026 15:36:19 +0000 (15:36 +0000)
committerVsevolod Stakhov <vsevolod@rspamd.com>
Mon, 16 Feb 2026 15:44:23 +0000 (15:44 +0000)
When the fasttext language detection model file doesn't exist or is
corrupted, fasttext library throws an exception. Add a catch-all handler
to prevent crashes when the model path is invalid or the file is missing.

Also improved error messages to include the model path for debugging.

src/libmime/lang_detection_fasttext.cxx

index 75b17d8b32e5e4472cc48ec5ee1b5d5d96490f8a..6688aac8add315f11309f3f6578dc3b0bed6e3ff 100644 (file)
@@ -23,9 +23,9 @@
 #include "contrib/fmt/include/fmt/base.h"
 #include "stat_api.h"
 #include "libserver/word.h"
-#include <exception>
 #include <string_view>
 #include <vector>
+#include <unistd.h>
 #endif
 
 #ifdef WITH_FASTTEXT
@@ -53,13 +53,24 @@ public:
                        const auto *model = ucl_object_find_key(opts_section, "fasttext_model");
 
                        if (model) {
+                               const char *model_path = ucl_object_tostring(model);
+
+                               if (access(model_path, R_OK) != 0) {
+                                       msg_err_config("fasttext model '%s' is not readable: %s",
+                                                                  model_path, strerror(errno));
+                                       loaded = false;
+                                       return;
+                               }
+
                                try {
-                                       ft.loadModel(ucl_object_tostring(model));
+                                       ft.loadModel(model_path);
                                        loaded = true;
-                                       model_fname = std::string{ucl_object_tostring(model)};
-                               } catch (std::exception &e) {
-                                       auto err_message = fmt::format("cannot load fasttext model: {}", e.what());
-                                       msg_err_config("%s", err_message.c_str());
+                                       model_fname = std::string{model_path};
+                               } catch (const std::exception &e) {
+                                       msg_err_config("cannot load fasttext model '%s': %s", model_path, e.what());
+                                       loaded = false;
+                               } catch (...) {
+                                       msg_err_config("cannot load fasttext model '%s': unknown error", model_path);
                                        loaded = false;
                                }
                        }