按源保护 PythonPlugin inittab 初始化边界
This commit is contained in:
@@ -113,10 +113,27 @@ int check_python_path_entries(const char *iniFilename,
|
||||
|
||||
} // namespace
|
||||
|
||||
PythonPlugin *PythonPlugin::instantiate(struct _inittab *) {
|
||||
PythonPlugin *PythonPlugin::instantiate(struct _inittab *inittab) {
|
||||
static PythonPlugin plugin;
|
||||
python_plugin = &plugin;
|
||||
python_plugin->status = PLUGIN_OK;
|
||||
if (!python_plugin) {
|
||||
python_plugin = &plugin;
|
||||
python_plugin->status = PLUGIN_OK;
|
||||
if (inittab) {
|
||||
for (int i = 0; inittab[i].name != nullptr; ++i) {
|
||||
// LinuxCNC source basis: python_plugin.cc reports
|
||||
// PLUGIN_INITTAB_FAILED when a built-in module from the
|
||||
// _inittab cannot be initialized.
|
||||
if (!inittab[i].initfunc || inittab[i].initfunc() == nullptr) {
|
||||
python_plugin->error_msg = "failed to initialize built-in module";
|
||||
python_plugin->status = PLUGIN_INITTAB_FAILED;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!python_plugin->usable()) {
|
||||
return nullptr;
|
||||
}
|
||||
return python_plugin;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
#include "pythonplugin/python_plugin.hh"
|
||||
|
||||
namespace {
|
||||
|
||||
PyObject *init_bad_module() {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
// LinuxCNC source basis: src/emc/pythonplugin/python_plugin.cc
|
||||
// PythonPlugin::PythonPlugin(struct _inittab*) reports PLUGIN_INITTAB_FAILED
|
||||
// when a built-in Python module from the first caller's inittab cannot be
|
||||
// initialized; instantiate() then returns nullptr for the unusable plugin.
|
||||
int main() {
|
||||
_inittab bad_modules[] = {
|
||||
{"cnc_sim_bad_module", init_bad_module},
|
||||
{nullptr, nullptr},
|
||||
};
|
||||
|
||||
PythonPlugin *plugin = PythonPlugin::instantiate(bad_modules);
|
||||
if (plugin != nullptr) {
|
||||
return 1;
|
||||
}
|
||||
if (python_plugin == nullptr) {
|
||||
return 2;
|
||||
}
|
||||
if (python_plugin->plugin_status() != PLUGIN_INITTAB_FAILED) {
|
||||
return 3;
|
||||
}
|
||||
if (python_plugin->usable()) {
|
||||
return 4;
|
||||
}
|
||||
if (python_plugin->last_errmsg() != "failed to initialize built-in module") {
|
||||
return 5;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user