@@ -286,14 +286,20 @@ is_stdlibdir(wchar_t *stdlibdir)
286
286
static int
287
287
search_for_prefix (wchar_t * prefix , const wchar_t * argv0_path )
288
288
{
289
- wchar_t stdlibdir [MAXPATHLEN + 1 ];
290
289
/* Search from argv0_path, until LANDMARK is found.
291
290
We guarantee 'prefix' is null terminated in bounds. */
292
291
wcscpy_s (prefix , MAXPATHLEN + 1 , argv0_path );
292
+ wchar_t stdlibdir [MAXPATHLEN + 1 ];
293
+ memset (stdlibdir , 0 , sizeof (stdlibdir ));
294
+ wcscpy_s (stdlibdir , Py_ARRAY_LENGTH (stdlibdir ), prefix );
295
+ /* We initialize with the longest possible path, in case it doesn't fit.
296
+ This also gives us an initial SEP at stdlibdir[wcslen(prefix)]. */
297
+ join (stdlibdir , L"lib" );
293
298
do {
294
- memset (stdlibdir , 0 , sizeof (stdlibdir ));
295
- wcscpy_s (stdlibdir , Py_ARRAY_LENGTH (stdlibdir ), prefix );
296
- join (stdlibdir , L"lib" );
299
+ assert (stdlibdir [wcslen (prefix )] == SEP );
300
+ /* Due to reduce() and our initial value, this result
301
+ is guaranteed to fit. */
302
+ wcscpy (& stdlibdir [wcslen (prefix ) + 1 ], L"lib" );
297
303
if (is_stdlibdir (stdlibdir )) {
298
304
return 1 ;
299
305
}
0 commit comments