@@ -1311,9 +1311,6 @@ static acl_pack_kind add_file(const char *out_file, FILE *of, const char *file,
1311
1311
1312
1312
static acl_pack_kind add_directory (const char * out_file , FILE * of ,
1313
1313
const char * dir_name , ZInfo * z_info ) {
1314
- #ifdef FULL_NAME_LENGTH
1315
- #undef FULL_NAME_LENGTH
1316
- #endif
1317
1314
acl_pkg_pack_info info ;
1318
1315
size_t name_length = strlen (dir_name ) + 1 ;
1319
1316
@@ -1339,24 +1336,31 @@ static acl_pack_kind add_directory(const char *out_file, FILE *of,
1339
1336
1340
1337
// Now walk the directory processing each name.
1341
1338
{
1339
+
1342
1340
#ifdef _WIN32
1343
1341
#define FULL_NAME_LENGTH (2 * MAX_PATH)
1342
+ #else
1343
+ #define FULL_NAME_LENGTH (2 * PATH_MAX)
1344
+ #endif
1345
+
1344
1346
char full_name [FULL_NAME_LENGTH ];
1345
- if (FULL_NAME_LENGTH < name_length ) {
1347
+
1348
+ // Full name must be large enough to store dir_name plus a trailing path
1349
+ // separator
1350
+ if (name_length + 1 > FULL_NAME_LENGTH ) {
1346
1351
fprintf (stderr , "acl_pkg_pack: Failed to write to %s: %s\n" , out_file ,
1347
1352
"Directory name too long" );
1348
1353
return PACK_END ;
1349
1354
}
1355
+ #ifdef _WIN32
1350
1356
HANDLE file_handle ;
1351
1357
WIN32_FIND_DATA file_info ;
1352
1358
1353
1359
// Partially initialize the full path name.
1354
- strncpy (full_name , dir_name , FULL_NAME_LENGTH );
1360
+ strncpy (full_name , dir_name , FULL_NAME_LENGTH - 1 );
1355
1361
strncpy (full_name + name_length - 1 , "\\*.*" ,
1356
- FULL_NAME_LENGTH - name_length + 1 );
1357
- if (full_name [FULL_NAME_LENGTH - 1 ] != '\0' ) {
1358
- full_name [FULL_NAME_LENGTH - 1 ] = '\0' ;
1359
- }
1362
+ FULL_NAME_LENGTH - name_length );
1363
+ full_name [FULL_NAME_LENGTH - 1 ] = '\0' ;
1360
1364
1361
1365
// Walk through all the files in the directory.
1362
1366
file_handle = FindFirstFile (full_name , & file_info );
@@ -1370,10 +1374,9 @@ static acl_pack_kind add_directory(const char *out_file, FILE *of,
1370
1374
1371
1375
// Finish the full file name
1372
1376
strncpy (full_name + name_length , file_info .cFileName ,
1373
- FULL_NAME_LENGTH - name_length );
1374
- if (full_name [FULL_NAME_LENGTH - 1 ] != '\0' ) {
1375
- full_name [FULL_NAME_LENGTH - 1 ] = '\0' ;
1376
- }
1377
+ FULL_NAME_LENGTH - name_length - 1 );
1378
+ full_name [FULL_NAME_LENGTH - 1 ] = '\0' ;
1379
+
1377
1380
if (add_file_or_dir (out_file , of , full_name , z_info ) == PACK_END ) {
1378
1381
FindClose (file_handle );
1379
1382
return PACK_END ;
@@ -1384,14 +1387,6 @@ static acl_pack_kind add_directory(const char *out_file, FILE *of,
1384
1387
// Linux
1385
1388
DIR * dir ;
1386
1389
struct dirent * entry ;
1387
- #define FULL_NAME_LENGTH (2 * PATH_MAX)
1388
- char full_name [FULL_NAME_LENGTH ];
1389
- if (FULL_NAME_LENGTH < name_length ) {
1390
- fprintf (stderr , "acl_pkg_pack: Failed to write to %s: %s\n" , out_file ,
1391
- "Directory name too long" );
1392
- return PACK_END ;
1393
- }
1394
-
1395
1390
// Partially initialize the full path name.
1396
1391
strncpy (full_name , dir_name , FULL_NAME_LENGTH - 1 );
1397
1392
full_name [FULL_NAME_LENGTH - 1 ] = '\0' ;
@@ -1412,10 +1407,9 @@ static acl_pack_kind add_directory(const char *out_file, FILE *of,
1412
1407
// Finish the full file name, truncate name if the file is too long to
1413
1408
// avoid buffer overflow
1414
1409
size_t buffer_space_left = FULL_NAME_LENGTH - name_length ;
1415
- strncpy (full_name + name_length , entry -> d_name , buffer_space_left );
1416
- if (full_name [FULL_NAME_LENGTH - 1 ] != '\0' ) {
1417
- full_name [FULL_NAME_LENGTH - 1 ] = '\0' ;
1418
- }
1410
+ strncpy (full_name + name_length , entry -> d_name , buffer_space_left - 1 );
1411
+ full_name [FULL_NAME_LENGTH - 1 ] = '\0' ;
1412
+
1419
1413
if (add_file_or_dir (out_file , of , full_name , z_info ) == PACK_END ) {
1420
1414
closedir (dir );
1421
1415
return PACK_END ;
@@ -1639,15 +1633,18 @@ static int acl_pkg_unpack_buffer_or_file(const char *buffer, size_t buffer_size,
1639
1633
return 0 ;
1640
1634
}
1641
1635
1642
- // Generate the full name, truncate or zero pad to avoid buffer overflow
1643
- if (FULL_NAME_LEN < out_dir_length ) {
1636
+ // Generate the full name, truncate or zero pad to avoid buffer overflow.
1637
+ // FULL_NAME_LEN must be large enough to store out_dir and a trailing path
1638
+ // separator and null terminator
1639
+ if (out_dir_length + 2 > FULL_NAME_LEN ) {
1644
1640
fprintf (stderr , "%s: Directory name too long\n" , routine_name );
1641
+ inflateEnd (& z_info .strm );
1642
+ return 0 ;
1645
1643
}
1644
+
1646
1645
strncpy (full_name + out_dir_length + 1 , name ,
1647
- FULL_NAME_LEN - out_dir_length - 1 );
1648
- if (full_name [FULL_NAME_LEN - 1 ] != '\0' ) {
1649
- full_name [FULL_NAME_LEN - 1 ] = '\0' ;
1650
- }
1646
+ FULL_NAME_LEN - out_dir_length - 2 );
1647
+ full_name [FULL_NAME_LEN - 1 ] = '\0' ;
1651
1648
1652
1649
if (info .kind == PACK_DIR ) {
1653
1650
#ifdef _WIN32
0 commit comments