@@ -1629,10 +1629,12 @@ _Py_write_noraise(int fd, const void *buf, size_t count)
16291629#ifdef HAVE_READLINK
16301630
16311631/* Read value of symbolic link. Encode the path to the locale encoding, decode
1632- the result from the locale encoding. Return -1 on error. */
1632+ the result from the locale encoding.
16331633
1634+ Return -1 on encoding error, on readlink() error, if the internal buffer is
1635+ too short, on decoding error, or if 'buf' is too short. */
16341636int
1635- _Py_wreadlink (const wchar_t * path , wchar_t * buf , size_t bufsiz )
1637+ _Py_wreadlink (const wchar_t * path , wchar_t * buf , size_t buflen )
16361638{
16371639 char * cpath ;
16381640 char cbuf [MAXPATHLEN ];
@@ -1659,12 +1661,13 @@ _Py_wreadlink(const wchar_t *path, wchar_t *buf, size_t bufsiz)
16591661 errno = EINVAL ;
16601662 return -1 ;
16611663 }
1662- if (bufsiz <= r1 ) {
1664+ /* wbuf must have space to store the trailing NUL character */
1665+ if (buflen <= r1 ) {
16631666 PyMem_RawFree (wbuf );
16641667 errno = EINVAL ;
16651668 return -1 ;
16661669 }
1667- wcsncpy (buf , wbuf , bufsiz );
1670+ wcsncpy (buf , wbuf , buflen );
16681671 PyMem_RawFree (wbuf );
16691672 return (int )r1 ;
16701673}
@@ -1674,11 +1677,12 @@ _Py_wreadlink(const wchar_t *path, wchar_t *buf, size_t bufsiz)
16741677
16751678/* Return the canonicalized absolute pathname. Encode path to the locale
16761679 encoding, decode the result from the locale encoding.
1677- Return NULL on error. */
16781680
1681+ Return NULL on encoding error, realpath() error, decoding error
1682+ or if 'resolved_path' is too short. */
16791683wchar_t *
16801684_Py_wrealpath (const wchar_t * path ,
1681- wchar_t * resolved_path , size_t resolved_path_size )
1685+ wchar_t * resolved_path , size_t resolved_path_len )
16821686{
16831687 char * cpath ;
16841688 char cresolved_path [MAXPATHLEN ];
@@ -1700,27 +1704,29 @@ _Py_wrealpath(const wchar_t *path,
17001704 errno = EINVAL ;
17011705 return NULL ;
17021706 }
1703- if (resolved_path_size <= r ) {
1707+ /* wresolved_path must have space to store the trailing NUL character */
1708+ if (resolved_path_len <= r ) {
17041709 PyMem_RawFree (wresolved_path );
17051710 errno = EINVAL ;
17061711 return NULL ;
17071712 }
1708- wcsncpy (resolved_path , wresolved_path , resolved_path_size );
1713+ wcsncpy (resolved_path , wresolved_path , resolved_path_len );
17091714 PyMem_RawFree (wresolved_path );
17101715 return resolved_path ;
17111716}
17121717#endif
17131718
17141719/* Get the current directory. size is the buffer size in wide characters
17151720 including the null character. Decode the path from the locale encoding.
1716- Return NULL on error. */
17171721
1722+ Return NULL on getcwd() error, on decoding error, or if 'buf' is
1723+ too short. */
17181724wchar_t *
1719- _Py_wgetcwd (wchar_t * buf , size_t size )
1725+ _Py_wgetcwd (wchar_t * buf , size_t buflen )
17201726{
17211727#ifdef MS_WINDOWS
1722- int isize = (int )Py_MIN (size , INT_MAX );
1723- return _wgetcwd (buf , isize );
1728+ int ibuflen = (int )Py_MIN (buflen , INT_MAX );
1729+ return _wgetcwd (buf , ibuflen );
17241730#else
17251731 char fname [MAXPATHLEN ];
17261732 wchar_t * wname ;
@@ -1731,11 +1737,12 @@ _Py_wgetcwd(wchar_t *buf, size_t size)
17311737 wname = Py_DecodeLocale (fname , & len );
17321738 if (wname == NULL )
17331739 return NULL ;
1734- if (size <= len ) {
1740+ /* wname must have space to store the trailing NUL character */
1741+ if (buflen <= len ) {
17351742 PyMem_RawFree (wname );
17361743 return NULL ;
17371744 }
1738- wcsncpy (buf , wname , size );
1745+ wcsncpy (buf , wname , buflen );
17391746 PyMem_RawFree (wname );
17401747 return buf ;
17411748#endif
0 commit comments