diff options
author | Victor Stinner <vstinner@redhat.com> | 2019-03-19 01:58:14 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-03-19 01:58:14 (GMT) |
commit | faddaedd05ca81a9fed3f315e7bc8dcf455824a2 (patch) | |
tree | 90679ab39bba190bd5cfa055ae3f564255d29495 /Python/pathconfig.c | |
parent | 5f9cf23502febe0eb3bc02e45c7d2bfc79424757 (diff) | |
download | cpython-faddaedd05ca81a9fed3f315e7bc8dcf455824a2.zip cpython-faddaedd05ca81a9fed3f315e7bc8dcf455824a2.tar.gz cpython-faddaedd05ca81a9fed3f315e7bc8dcf455824a2.tar.bz2 |
bpo-36352: Avoid hardcoded MAXPATHLEN size in getpath.c (GH-12423)
* Use Py_ARRAY_LENGTH() rather than hardcoded MAXPATHLEN in getpath.c.
* Pass string length to functions modifying strings.
Diffstat (limited to 'Python/pathconfig.c')
-rw-r--r-- | Python/pathconfig.c | 14 |
1 files changed, 8 insertions, 6 deletions
diff --git a/Python/pathconfig.c b/Python/pathconfig.c index 87db66b..f1818eb 100644 --- a/Python/pathconfig.c +++ b/Python/pathconfig.c @@ -578,8 +578,8 @@ _PyPathConfig_ComputeArgv0(const _PyWstrList *argv) int have_script_arg = 0; int have_module_arg = 0; #ifdef HAVE_READLINK - wchar_t link[MAXPATHLEN+1]; - wchar_t argv0copy[2*MAXPATHLEN+1]; + wchar_t link[MAXPATHLEN + 1]; + wchar_t argv0copy[2 * MAXPATHLEN + 1]; int nr = 0; #endif #if defined(HAVE_REALPATH) @@ -607,7 +607,7 @@ _PyPathConfig_ComputeArgv0(const _PyWstrList *argv) #ifdef HAVE_READLINK if (have_script_arg) - nr = _Py_wreadlink(argv0, link, MAXPATHLEN); + nr = _Py_wreadlink(argv0, link, Py_ARRAY_LENGTH(link)); if (nr > 0) { /* It's a symlink */ link[nr] = '\0'; @@ -692,11 +692,12 @@ _Py_FindEnvConfigValue(FILE *env_file, const wchar_t *key, wchar_t *value, size_t value_size) { int result = 0; /* meaning not found */ - char buffer[MAXPATHLEN*2+1]; /* allow extra for key, '=', etc. */ + char buffer[MAXPATHLEN * 2 + 1]; /* allow extra for key, '=', etc. */ + buffer[Py_ARRAY_LENGTH(buffer)-1] = '\0'; fseek(env_file, 0, SEEK_SET); while (!feof(env_file)) { - char * p = fgets(buffer, MAXPATHLEN*2, env_file); + char * p = fgets(buffer, Py_ARRAY_LENGTH(buffer) - 1, env_file); if (p == NULL) { break; @@ -721,7 +722,8 @@ _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, MAXPATHLEN); + wcsncpy(value, tok, value_size - 1); + value[value_size - 1] = L'\0'; result = 1; PyMem_RawFree(tmpbuffer); break; |