summaryrefslogtreecommitdiff
path: root/Python
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@redhat.com>2019-03-19 02:58:14 +0100
committerGitHub <noreply@github.com>2019-03-19 02:58:14 +0100
commitfaddaedd05ca81a9fed3f315e7bc8dcf455824a2 (patch)
tree90679ab39bba190bd5cfa055ae3f564255d29495 /Python
parent5f9cf23502febe0eb3bc02e45c7d2bfc79424757 (diff)
downloadcpython-git-faddaedd05ca81a9fed3f315e7bc8dcf455824a2.tar.gz
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')
-rw-r--r--Python/fileutils.c2
-rw-r--r--Python/pathconfig.c14
2 files changed, 9 insertions, 7 deletions
diff --git a/Python/fileutils.c b/Python/fileutils.c
index 0ac690a211..b933874193 100644
--- a/Python/fileutils.c
+++ b/Python/fileutils.c
@@ -1716,7 +1716,7 @@ _Py_wrealpath(const wchar_t *path,
}
#endif
-/* Get the current directory. size is the buffer size in wide characters
+/* Get the current directory. buflen is the buffer size in wide characters
including the null character. Decode the path from the locale encoding.
Return NULL on getcwd() error, on decoding error, or if 'buf' is
diff --git a/Python/pathconfig.c b/Python/pathconfig.c
index 87db66b752..f1818eb307 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;