按源保护 PythonPlugin INI 节解析边界

This commit is contained in:
cnc
2026-06-06 05:56:10 +08:00
parent ba2d680500
commit e320d6d420
4 changed files with 56 additions and 2 deletions

View File

@@ -32,6 +32,21 @@ std::string trim_ini_value(std::string value) {
return std::string(first, last);
}
bool read_ini_section(const std::string &text, std::string &section_name) {
if (text.empty() || text.front() != '[') {
return false;
}
const auto close = text.find(']');
if (close == std::string::npos) {
return false;
}
// LinuxCNC source basis: IniFile::processLine() trims the section name
// between '[' and ']' and ignores text after the closing bracket.
section_name = trim_ini_value(text.substr(1, close - 1));
return true;
}
std::vector<std::string> find_ini_values(const char *iniFilename,
const char *section,
const char *key) {
@@ -48,8 +63,9 @@ std::vector<std::string> find_ini_values(const char *iniFilename,
if (text.empty() || text[0] == ';' || text[0] == '#') {
continue;
}
if (text.front() == '[' && text.back() == ']') {
in_section = text.substr(1, text.size() - 2) == section;
std::string section_name;
if (read_ini_section(text, section_name)) {
in_section = section_name == section;
continue;
}
if (!in_section) {

View File

@@ -128,6 +128,37 @@ int main() {
setenv("HOME", saved_home_value.c_str(), 1);
}
std::remove(path_prepend_ini);
const char *spaced_section_ini = "/tmp/cnc_sim_python_plugin_probe_spaced_section.ini";
{
FILE *file = std::fopen(spaced_section_ini, "w");
if (!file) {
std::remove(ini);
return 47;
}
std::fputs("[ PYTHON ] ; section comment\nPATH_PREPEND = ~/linuxcnc-python\n", file);
std::fclose(file);
}
unsetenv("HOME");
if (plugin->configure(spaced_section_ini, "PYTHON") != PLUGIN_EXCEPTION_DURING_PATH_PREPEND) {
if (saved_home) {
setenv("HOME", saved_home_value.c_str(), 1);
}
std::remove(ini);
std::remove(spaced_section_ini);
return 48;
}
if (plugin->last_errmsg() != "bad path prepend") {
if (saved_home) {
setenv("HOME", saved_home_value.c_str(), 1);
}
std::remove(ini);
std::remove(spaced_section_ini);
return 49;
}
if (saved_home) {
setenv("HOME", saved_home_value.c_str(), 1);
}
std::remove(spaced_section_ini);
const char *path_append_ini = "/tmp/cnc_sim_python_plugin_probe_path_append.ini";
{
FILE *file = std::fopen(path_append_ini, "w");