@@ -899,7 +899,7 @@ path_converter(PyObject *o, void *p) {
899899
900900 length = PyBytes_GET_SIZE (bytes );
901901#ifdef MS_WINDOWS
902- if (length > MAX_PATH ) {
902+ if (length > MAX_PATH - 1 ) {
903903 FORMAT_EXCEPTION (PyExc_ValueError , "%s too long for Windows" );
904904 Py_DECREF (bytes );
905905 return 0 ;
@@ -1378,18 +1378,18 @@ posix_1str(const char *func_name, PyObject *args, char *format,
13781378static BOOL __stdcall
13791379win32_chdir (LPCSTR path )
13801380{
1381- char new_path [MAX_PATH + 1 ];
1381+ char new_path [MAX_PATH ];
13821382 int result ;
13831383 char env [4 ] = "=x:" ;
13841384
13851385 if (!SetCurrentDirectoryA (path ))
13861386 return FALSE;
1387- result = GetCurrentDirectoryA (MAX_PATH + 1 , new_path );
1387+ result = GetCurrentDirectoryA (Py_ARRAY_LENGTH ( new_path ) , new_path );
13881388 if (!result )
13891389 return FALSE;
13901390 /* In the ANSI API, there should not be any paths longer
1391- than MAX_PATH. */
1392- assert (result <= MAX_PATH + 1 );
1391+ than MAX_PATH-1 (not including the final null character) . */
1392+ assert (result < Py_ARRAY_LENGTH ( new_path ) );
13931393 if (strncmp (new_path , "\\\\" , 2 ) == 0 ||
13941394 strncmp (new_path , "//" , 2 ) == 0 )
13951395 /* UNC path, nothing to do. */
@@ -1403,16 +1403,16 @@ win32_chdir(LPCSTR path)
14031403static BOOL __stdcall
14041404win32_wchdir (LPCWSTR path )
14051405{
1406- wchar_t _new_path [MAX_PATH + 1 ], * new_path = _new_path ;
1406+ wchar_t _new_path [MAX_PATH ], * new_path = _new_path ;
14071407 int result ;
14081408 wchar_t env [4 ] = L"=x:" ;
14091409
14101410 if (!SetCurrentDirectoryW (path ))
14111411 return FALSE;
1412- result = GetCurrentDirectoryW (MAX_PATH + 1 , new_path );
1412+ result = GetCurrentDirectoryW (Py_ARRAY_LENGTH ( new_path ) , new_path );
14131413 if (!result )
14141414 return FALSE;
1415- if (result > MAX_PATH + 1 ) {
1415+ if (result > Py_ARRAY_LENGTH ( new_path ) ) {
14161416 new_path = PyMem_RawMalloc (result * sizeof (wchar_t ));
14171417 if (!new_path ) {
14181418 SetLastError (ERROR_OUTOFMEMORY );
@@ -3398,11 +3398,11 @@ posix_getcwd(int use_bytes)
33983398 PyObject * resobj ;
33993399 DWORD len ;
34003400 Py_BEGIN_ALLOW_THREADS
3401- len = GetCurrentDirectoryW (sizeof wbuf / sizeof wbuf [ 0 ] , wbuf );
3401+ len = GetCurrentDirectoryW (Py_ARRAY_LENGTH ( wbuf ) , wbuf );
34023402 /* If the buffer is large enough, len does not include the
34033403 terminating \0. If the buffer is too small, len includes
34043404 the space needed for the terminator. */
3405- if (len >= sizeof wbuf / sizeof wbuf [ 0 ] ) {
3405+ if (len >= Py_ARRAY_LENGTH ( wbuf ) ) {
34063406 wbuf2 = PyMem_RawMalloc (len * sizeof (wchar_t ));
34073407 if (wbuf2 )
34083408 len = GetCurrentDirectoryW (len , wbuf2 );
@@ -3583,10 +3583,10 @@ _listdir_windows_no_opendir(path_t *path, PyObject *list)
35833583 HANDLE hFindFile = INVALID_HANDLE_VALUE ;
35843584 BOOL result ;
35853585 WIN32_FIND_DATA FileData ;
3586- char namebuf [MAX_PATH + 5 ]; /* Overallocate for \\ *.*\0 */
3586+ char namebuf [MAX_PATH + 4 ]; /* Overallocate for "\ *.*" */
35873587 char * bufptr = namebuf ;
35883588 /* only claim to have space for MAX_PATH */
3589- Py_ssize_t len = sizeof (namebuf )- 5 ;
3589+ Py_ssize_t len = Py_ARRAY_LENGTH (namebuf )- 4 ;
35903590 PyObject * po = NULL ;
35913591 wchar_t * wnamebuf = NULL ;
35923592
@@ -3875,14 +3875,14 @@ static PyObject *
38753875posix__getfullpathname (PyObject * self , PyObject * args )
38763876{
38773877 const char * path ;
3878- char outbuf [MAX_PATH * 2 ];
3878+ char outbuf [MAX_PATH ];
38793879 char * temp ;
38803880 PyObject * po ;
38813881
38823882 if (PyArg_ParseTuple (args , "U|:_getfullpathname" , & po ))
38833883 {
38843884 wchar_t * wpath ;
3885- wchar_t woutbuf [MAX_PATH * 2 ], * woutbufp = woutbuf ;
3885+ wchar_t woutbuf [MAX_PATH ], * woutbufp = woutbuf ;
38863886 wchar_t * wtemp ;
38873887 DWORD result ;
38883888 PyObject * v ;
0 commit comments