This issue is similar to issue 2722 (http://bugs.python.org/issue2722#), where the char buffer support that the path string has not fixed length in the function posix_getcwd(). In the function posix_getcwdu(), the char buffer is still fix length. But I think the same change should also apply to this function. A patch is attached to allow the char buffer in posix_getcwdu() to be not fixed length.
The "#ifdef MS_WINDOWS" is not a the right place in posix_getcwdu(): buf and res are declared but unused, and there is dead code on Windows. The patch only fixes the UNIX implementation, not the Windows implementation. In the py3k branch, bytes and unicode versions of getcwd are factorized in a common function. The same should be done in python trunk.
Victor, I corrected both issues of the patch according to your first comment. This patch did not fix the Windows implementation. It seems that there will not be buffer overflow in the Windows implementation, since if the buffer is small for GetCurrentDirectoryW(), the code allocates a new buffer for it with enough length by the following code: len = GetCurrentDirectoryW(sizeof wbuf/ sizeof wbuf[0], wbuf); if (len >= sizeof wbuf/ sizeof wbuf[0]) { wbuf2 = malloc(len * sizeof(wchar_t)); if (wbuf2) len = GetCurrentDirectoryW(len, wbuf2); }