From 8df1e14b6fd56aa6059620cb5c819854e2d42c5f Mon Sep 17 00:00:00 2001 From: Qingping Hou Date: Fri, 21 Apr 2017 04:04:02 -0700 Subject: [PATCH] plugin(fix): catch crash with pcall in event handlers --- frontend/pluginloader.lua | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/frontend/pluginloader.lua b/frontend/pluginloader.lua index 1ae456f11..18c158d53 100644 --- a/frontend/pluginloader.lua +++ b/frontend/pluginloader.lua @@ -3,6 +3,25 @@ local logger = require("logger") local DEFAULT_PLUGIN_PATH = "plugins" + + +local function sandboxPluginEventHandlers(plugin) + for key, value in pairs(plugin) do + if key:sub(1, 2) == "on" and type(value) == "function" then + plugin[key] = function(self, ...) + local ok, re = pcall(value, self, ...) + if ok then + return re + else + logger.err("failed to call event handler", key, re) + return false + end + end + end + end +end + + local PluginLoader = {} function PluginLoader:loadPlugins() @@ -47,6 +66,7 @@ function PluginLoader:loadPlugins() elseif type(plugin_module.disabled) ~= "boolean" or not plugin_module.disabled then plugin_module.path = plugin_root plugin_module.name = plugin_module.name or plugin_root:match("/(.-)%.koplugin") + sandboxPluginEventHandlers(plugin_module) table.insert(self.plugins, plugin_module) else logger.info("Plugin ", mainfile, " has been disabled.")