diff options
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/posixmodule.c | 34 |
1 files changed, 27 insertions, 7 deletions
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index e538437..04f0d36 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -3418,12 +3418,15 @@ posix_lchown(PyObject *self, PyObject *args) static PyObject * posix_getcwd(int use_bytes) { - char buf[1026]; - char *res; + char *buf, *tmpbuf; + char *cwd; + const size_t chunk = 1024; + size_t buflen = 0; + PyObject *obj; #ifdef MS_WINDOWS if (!use_bytes) { - wchar_t wbuf[1026]; + wchar_t wbuf[MAXPATHLEN]; wchar_t *wbuf2 = wbuf; PyObject *resobj; DWORD len; @@ -3457,14 +3460,31 @@ posix_getcwd(int use_bytes) return NULL; #endif + buf = cwd = NULL; Py_BEGIN_ALLOW_THREADS - res = getcwd(buf, sizeof buf); + do { + buflen += chunk; + tmpbuf = PyMem_RawRealloc(buf, buflen); + if (tmpbuf == NULL) + break; + + buf = tmpbuf; + cwd = getcwd(buf, buflen); + } while (cwd == NULL && errno == ERANGE); Py_END_ALLOW_THREADS - if (res == NULL) + + if (cwd == NULL) { + PyMem_RawFree(buf); return posix_error(); + } + if (use_bytes) - return PyBytes_FromStringAndSize(buf, strlen(buf)); - return PyUnicode_DecodeFSDefault(buf); + obj = PyBytes_FromStringAndSize(buf, strlen(buf)); + else + obj = PyUnicode_DecodeFSDefault(buf); + PyMem_RawFree(buf); + + return obj; } PyDoc_STRVAR(posix_getcwd__doc__, |