diff options
author | Victor Stinner <vstinner@python.org> | 2019-10-04 17:53:43 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-10-04 17:53:43 (GMT) |
commit | c02b41b1fb115c87693530ea6a480b2e15460424 (patch) | |
tree | dd863614c77112d715aac8277083cdd8d119f905 /Python/pathconfig.c | |
parent | abd7cd856ba326bd7574135c7d034e98492ab695 (diff) | |
download | cpython-c02b41b1fb115c87693530ea6a480b2e15460424.zip cpython-c02b41b1fb115c87693530ea6a480b2e15460424.tar.gz cpython-c02b41b1fb115c87693530ea6a480b2e15460424.tar.bz2 |
bpo-38353: getpath.c: allocates strings on the heap (GH-16585)
* _Py_FindEnvConfigValue() now returns a string allocated
by PyMem_RawMalloc().
* calculate_init() now decodes VPATH macro.
* Add calculate_open_pyenv() function.
* Add substring() and joinpath2() functions.
* Fix add_exe_suffix()
And a few cleanup changes.
Diffstat (limited to 'Python/pathconfig.c')
-rw-r--r-- | Python/pathconfig.c | 29 |
1 files changed, 20 insertions, 9 deletions
diff --git a/Python/pathconfig.c b/Python/pathconfig.c index 61408e1..363b768 100644 --- a/Python/pathconfig.c +++ b/Python/pathconfig.c @@ -777,12 +777,17 @@ _PyPathConfig_ComputeSysPath0(const PyWideStringList *argv, PyObject **path0_p) #endif /* Search for a prefix value in an environment file (pyvenv.cfg). - If found, copy it into the provided buffer. */ -int + + - If found, copy it into *value_p: string which must be freed by + PyMem_RawFree(). + - If not found, *value_p is set to NULL. +*/ +PyStatus _Py_FindEnvConfigValue(FILE *env_file, const wchar_t *key, - wchar_t *value, size_t value_size) + wchar_t **value_p) { - int result = 0; /* meaning not found */ + *value_p = NULL; + char buffer[MAXPATHLEN * 2 + 1]; /* allow extra for key, '=', etc. */ buffer[Py_ARRAY_LENGTH(buffer)-1] = '\0'; @@ -812,18 +817,24 @@ _Py_FindEnvConfigValue(FILE *env_file, const wchar_t *key, if ((tok != NULL) && !wcscmp(tok, L"=")) { tok = WCSTOK(NULL, L"\r\n", &state); if (tok != NULL) { - wcsncpy(value, tok, value_size - 1); - value[value_size - 1] = L'\0'; - result = 1; + *value_p = _PyMem_RawWcsdup(tok); PyMem_RawFree(tmpbuffer); - break; + + if (*value_p == NULL) { + return _PyStatus_NO_MEMORY(); + } + + /* found */ + return _PyStatus_OK(); } } } PyMem_RawFree(tmpbuffer); } } - return result; + + /* not found */ + return _PyStatus_OK(); } #ifdef __cplusplus |