@@ -7259,90 +7259,98 @@ check_CreateSymbolicLink(void)
7259
7259
return (Py_CreateSymbolicLinkW && Py_CreateSymbolicLinkA );
7260
7260
}
7261
7261
7262
- /* Remove the last portion of the path */
7263
- static void
7262
+ /* Remove the last portion of the path - return 0 on success */
7263
+ static int
7264
7264
_dirnameW (WCHAR * path )
7265
7265
{
7266
7266
WCHAR * ptr ;
7267
+ size_t length = wcsnlen_s (path , MAX_PATH );
7268
+ if (length == MAX_PATH ) {
7269
+ return -1 ;
7270
+ }
7267
7271
7268
7272
/* walk the path from the end until a backslash is encountered */
7269
- for (ptr = path + wcslen ( path ) ; ptr != path ; ptr -- ) {
7273
+ for (ptr = path + length ; ptr != path ; ptr -- ) {
7270
7274
if (* ptr == L'\\' || * ptr == L'/' )
7271
7275
break ;
7272
7276
}
7273
7277
* ptr = 0 ;
7278
+ return 0 ;
7274
7279
}
7275
7280
7276
- /* Remove the last portion of the path */
7277
- static void
7281
+ /* Remove the last portion of the path - return 0 on success */
7282
+ static int
7278
7283
_dirnameA (char * path )
7279
7284
{
7280
7285
char * ptr ;
7286
+ size_t length = strnlen_s (path , MAX_PATH );
7287
+ if (length == MAX_PATH ) {
7288
+ return -1 ;
7289
+ }
7281
7290
7282
7291
/* walk the path from the end until a backslash is encountered */
7283
- for (ptr = path + strlen ( path ) ; ptr != path ; ptr -- ) {
7292
+ for (ptr = path + length ; ptr != path ; ptr -- ) {
7284
7293
if (* ptr == '\\' || * ptr == '/' )
7285
7294
break ;
7286
7295
}
7287
7296
* ptr = 0 ;
7297
+ return 0 ;
7288
7298
}
7289
7299
7290
7300
/* Is this path absolute? */
7291
7301
static int
7292
7302
_is_absW (const WCHAR * path )
7293
7303
{
7294
- return path [0 ] == L'\\' || path [0 ] == L'/' || path [1 ] == L':' ;
7304
+ return path [0 ] == L'\\' || path [0 ] == L'/' ||
7305
+ (path [0 ] && path [1 ] == L':' );
7295
7306
7296
7307
}
7297
7308
7298
7309
/* Is this path absolute? */
7299
7310
static int
7300
7311
_is_absA (const char * path )
7301
7312
{
7302
- return path [0 ] == '\\' || path [0 ] == '/' || path [1 ] == ':' ;
7313
+ return path [0 ] == '\\' || path [0 ] == '/' ||
7314
+ (path [0 ] && path [1 ] == ':' );
7303
7315
7304
7316
}
7305
7317
7306
- /* join root and rest with a backslash */
7307
- static void
7318
+ /* join root and rest with a backslash - return 0 on success */
7319
+ static int
7308
7320
_joinW (WCHAR * dest_path , const WCHAR * root , const WCHAR * rest )
7309
7321
{
7310
- size_t root_len ;
7311
-
7312
7322
if (_is_absW (rest )) {
7313
- wcscpy (dest_path , rest );
7314
- return ;
7323
+ return wcscpy_s (dest_path , MAX_PATH , rest );
7315
7324
}
7316
7325
7317
- root_len = wcslen (root );
7326
+ if (wcscpy_s (dest_path , MAX_PATH , root )) {
7327
+ return -1 ;
7328
+ }
7318
7329
7319
- wcscpy (dest_path , root );
7320
- if (root_len ) {
7321
- dest_path [root_len ] = L'\\' ;
7322
- root_len ++ ;
7330
+ if (dest_path [0 ] && wcscat_s (dest_path , MAX_PATH , L"\\" )) {
7331
+ return -1 ;
7323
7332
}
7324
- wcscpy (dest_path + root_len , rest );
7333
+
7334
+ return wcscat_s (dest_path , MAX_PATH , rest );
7325
7335
}
7326
7336
7327
- /* join root and rest with a backslash */
7328
- static void
7337
+ /* join root and rest with a backslash - return 0 on success */
7338
+ static int
7329
7339
_joinA (char * dest_path , const char * root , const char * rest )
7330
7340
{
7331
- size_t root_len ;
7332
-
7333
7341
if (_is_absA (rest )) {
7334
- strcpy (dest_path , rest );
7335
- return ;
7342
+ return strcpy_s (dest_path , MAX_PATH , rest );
7336
7343
}
7337
7344
7338
- root_len = strlen (root );
7345
+ if (strcpy_s (dest_path , MAX_PATH , root )) {
7346
+ return -1 ;
7347
+ }
7339
7348
7340
- strcpy (dest_path , root );
7341
- if (root_len ) {
7342
- dest_path [root_len ] = '\\' ;
7343
- root_len ++ ;
7349
+ if (dest_path [0 ] && strcat_s (dest_path , MAX_PATH , "\\" )) {
7350
+ return -1 ;
7344
7351
}
7345
- strcpy (dest_path + root_len , rest );
7352
+
7353
+ return strcat_s (dest_path , MAX_PATH , rest );
7346
7354
}
7347
7355
7348
7356
/* Return True if the path at src relative to dest is a directory */
@@ -7354,10 +7362,14 @@ _check_dirW(WCHAR *src, WCHAR *dest)
7354
7362
WCHAR src_resolved [MAX_PATH ] = L"" ;
7355
7363
7356
7364
/* dest_parent = os.path.dirname(dest) */
7357
- wcscpy (dest_parent , dest );
7358
- _dirnameW (dest_parent );
7365
+ if (wcscpy_s (dest_parent , MAX_PATH , dest ) ||
7366
+ _dirnameW (dest_parent )) {
7367
+ return 0 ;
7368
+ }
7359
7369
/* src_resolved = os.path.join(dest_parent, src) */
7360
- _joinW (src_resolved , dest_parent , src );
7370
+ if (_joinW (src_resolved , dest_parent , src )) {
7371
+ return 0 ;
7372
+ }
7361
7373
return (
7362
7374
GetFileAttributesExW (src_resolved , GetFileExInfoStandard , & src_info )
7363
7375
&& src_info .dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY
@@ -7373,10 +7385,14 @@ _check_dirA(char *src, char *dest)
7373
7385
char src_resolved [MAX_PATH ] = "" ;
7374
7386
7375
7387
/* dest_parent = os.path.dirname(dest) */
7376
- strcpy (dest_parent , dest );
7377
- _dirnameA (dest_parent );
7388
+ if (strcpy_s (dest_parent , MAX_PATH , dest ) ||
7389
+ _dirnameA (dest_parent )) {
7390
+ return 0 ;
7391
+ }
7378
7392
/* src_resolved = os.path.join(dest_parent, src) */
7379
- _joinA (src_resolved , dest_parent , src );
7393
+ if (_joinA (src_resolved , dest_parent , src )) {
7394
+ return 0 ;
7395
+ }
7380
7396
return (
7381
7397
GetFileAttributesExA (src_resolved , GetFileExInfoStandard , & src_info )
7382
7398
&& src_info .dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY
@@ -7441,6 +7457,7 @@ os_symlink_impl(PyObject *module, path_t *src, path_t *dst,
7441
7457
#ifdef MS_WINDOWS
7442
7458
7443
7459
Py_BEGIN_ALLOW_THREADS
7460
+ _Py_BEGIN_SUPPRESS_IPH
7444
7461
if (dst -> wide ) {
7445
7462
/* if src is a directory, ensure target_is_directory==1 */
7446
7463
target_is_directory |= _check_dirW (src -> wide , dst -> wide );
@@ -7453,6 +7470,7 @@ os_symlink_impl(PyObject *module, path_t *src, path_t *dst,
7453
7470
result = Py_CreateSymbolicLinkA (dst -> narrow , src -> narrow ,
7454
7471
target_is_directory );
7455
7472
}
7473
+ _Py_END_SUPPRESS_IPH
7456
7474
Py_END_ALLOW_THREADS
7457
7475
7458
7476
if (!result )
0 commit comments