diff options
author | Pablo Galindo Salgado <Pablogsal@gmail.com> | 2021-10-13 17:08:19 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-13 17:08:19 (GMT) |
commit | db2b6a20cd35781b2f5e798e880e57e6cf9b97aa (patch) | |
tree | 316124de83275184a817e1d543f9b8200c785466 /Python | |
parent | 1c831353816ff699b54e804047a7242a09e98f5b (diff) | |
download | cpython-db2b6a20cd35781b2f5e798e880e57e6cf9b97aa.zip cpython-db2b6a20cd35781b2f5e798e880e57e6cf9b97aa.tar.gz cpython-db2b6a20cd35781b2f5e798e880e57e6cf9b97aa.tar.bz2 |
bpo-45445: Fail if an invalid X-option is provided in the command line (GH-28823)
Diffstat (limited to 'Python')
-rw-r--r-- | Python/initconfig.c | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/Python/initconfig.c b/Python/initconfig.c index b91d280..ba6d19d 100644 --- a/Python/initconfig.c +++ b/Python/initconfig.c @@ -2129,6 +2129,49 @@ _PyConfig_InitImportConfig(PyConfig *config) return config_init_import(config, 1); } +// List of known xoptions to validate against the provided ones. Note that all +// options are listed, even if they are only available if a specific macro is +// set, like -X showrefcount which requires a debug build. In this case unknown +// options are silently ignored. +const wchar_t* known_xoptions[] = { + L"faulthandler", + L"showrefcount", + L"tracemalloc", + L"importtime", + L"dev", + L"utf8", + L"pycache_prefix", + L"warn_default_encoding", + L"no_debug_ranges", + L"frozen_modules", + NULL, +}; + +static const wchar_t* +_Py_check_xoptions(const PyWideStringList *xoptions, const wchar_t **names) +{ + for (Py_ssize_t i=0; i < xoptions->length; i++) { + const wchar_t *option = xoptions->items[i]; + size_t len; + wchar_t *sep = wcschr(option, L'='); + if (sep != NULL) { + len = (sep - option); + } + else { + len = wcslen(option); + } + int found = 0; + for (const wchar_t** name = names; *name != NULL; name++) { + if (wcsncmp(option, *name, len) == 0 && (*name)[len] == L'\0') { + found = 1; + } + } + if (found == 0) { + return option; + } + } + return NULL; +} static PyStatus config_read(PyConfig *config, int compute_path_config) @@ -2144,6 +2187,11 @@ config_read(PyConfig *config, int compute_path_config) } /* -X options */ + const wchar_t* option = _Py_check_xoptions(&config->xoptions, known_xoptions); + if (option != NULL) { + return PyStatus_Error("Unknown value for option -X"); + } + if (config_get_xoption(config, L"showrefcount")) { config->show_ref_count = 1; } |