@@ -499,7 +499,7 @@ static char *read_file_into_buffer(struct acl_pkg_file *pkg,
499
499
fclose (f );
500
500
return NULL ;
501
501
}
502
- const size_t file_size = (size_t )ftell_return ;
502
+ file_size = (size_t )ftell_return ;
503
503
rewind (f );
504
504
505
505
// slurp the whole file into allocated buf
@@ -1315,6 +1315,11 @@ static acl_pack_kind add_directory(const char *out_file, FILE *of,
1315
1315
const char * dir_name , ZInfo * z_info ) {
1316
1316
#ifdef FULL_NAME_LENGTH
1317
1317
#undef FULL_NAME_LENGTH
1318
+ #endif
1319
+ #ifdef _WIN32
1320
+ #define FULL_NAME_LENGTH (2 * MAX_PATH)
1321
+ #else
1322
+ #define FULL_NAME_LENGTH (2 * PATH_MAX)
1318
1323
#endif
1319
1324
acl_pkg_pack_info info ;
1320
1325
size_t name_length = strlen (dir_name ) + 1 ;
@@ -1341,24 +1346,24 @@ static acl_pack_kind add_directory(const char *out_file, FILE *of,
1341
1346
1342
1347
// Now walk the directory processing each name.
1343
1348
{
1344
- #ifdef _WIN32
1345
- #define FULL_NAME_LENGTH (2 * MAX_PATH)
1346
1349
char full_name [FULL_NAME_LENGTH ];
1347
- if (FULL_NAME_LENGTH < name_length ) {
1350
+
1351
+ // Full name must be large enough to store dir_name plus a trailing path
1352
+ // separator
1353
+ if (name_length + 1 > FULL_NAME_LENGTH ) {
1348
1354
fprintf (stderr , "acl_pkg_pack: Failed to write to %s: %s\n" , out_file ,
1349
1355
"Directory name too long" );
1350
1356
return PACK_END ;
1351
1357
}
1358
+ #ifdef _WIN32
1352
1359
HANDLE file_handle ;
1353
1360
WIN32_FIND_DATA file_info ;
1354
1361
1355
1362
// Partially initialize the full path name.
1356
1363
strncpy (full_name , dir_name , FULL_NAME_LENGTH );
1357
1364
strncpy (full_name + name_length - 1 , "\\*.*" ,
1358
- FULL_NAME_LENGTH - name_length + 1 );
1359
- if (full_name [FULL_NAME_LENGTH - 1 ] != '\0' ) {
1360
- full_name [FULL_NAME_LENGTH - 1 ] = '\0' ;
1361
- }
1365
+ FULL_NAME_LENGTH - name_length );
1366
+ full_name [FULL_NAME_LENGTH - 1 ] = '\0' ;
1362
1367
1363
1368
// Walk through all the files in the directory.
1364
1369
file_handle = FindFirstFile (full_name , & file_info );
@@ -1372,10 +1377,9 @@ static acl_pack_kind add_directory(const char *out_file, FILE *of,
1372
1377
1373
1378
// Finish the full file name
1374
1379
strncpy (full_name + name_length , file_info .cFileName ,
1375
- FULL_NAME_LENGTH - name_length );
1376
- if (full_name [FULL_NAME_LENGTH - 1 ] != '\0' ) {
1377
- full_name [FULL_NAME_LENGTH - 1 ] = '\0' ;
1378
- }
1380
+ FULL_NAME_LENGTH - name_length - 1 );
1381
+ full_name [FULL_NAME_LENGTH - 1 ] = '\0' ;
1382
+
1379
1383
if (add_file_or_dir (out_file , of , full_name , z_info ) == PACK_END ) {
1380
1384
FindClose (file_handle );
1381
1385
return PACK_END ;
@@ -1386,14 +1390,6 @@ static acl_pack_kind add_directory(const char *out_file, FILE *of,
1386
1390
// Linux
1387
1391
DIR * dir ;
1388
1392
struct dirent * entry ;
1389
- #define FULL_NAME_LENGTH (2 * PATH_MAX)
1390
- char full_name [FULL_NAME_LENGTH ];
1391
- if (FULL_NAME_LENGTH < name_length ) {
1392
- fprintf (stderr , "acl_pkg_pack: Failed to write to %s: %s\n" , out_file ,
1393
- "Directory name too long" );
1394
- return PACK_END ;
1395
- }
1396
-
1397
1393
// Partially initialize the full path name.
1398
1394
strncpy (full_name , dir_name , FULL_NAME_LENGTH - 1 );
1399
1395
full_name [FULL_NAME_LENGTH - 1 ] = '\0' ;
@@ -1414,10 +1410,9 @@ static acl_pack_kind add_directory(const char *out_file, FILE *of,
1414
1410
// Finish the full file name, truncate name if the file is too long to
1415
1411
// avoid buffer overflow
1416
1412
size_t buffer_space_left = FULL_NAME_LENGTH - name_length ;
1417
- strncpy (full_name + name_length , entry -> d_name , buffer_space_left );
1418
- if (full_name [FULL_NAME_LENGTH - 1 ] != '\0' ) {
1419
- full_name [FULL_NAME_LENGTH - 1 ] = '\0' ;
1420
- }
1413
+ strncpy (full_name + name_length , entry -> d_name , buffer_space_left - 1 );
1414
+ full_name [FULL_NAME_LENGTH - 1 ] = '\0' ;
1415
+
1421
1416
if (add_file_or_dir (out_file , of , full_name , z_info ) == PACK_END ) {
1422
1417
closedir (dir );
1423
1418
return PACK_END ;
@@ -1518,6 +1513,17 @@ int acl_pkg_pack(const char *out_file, const char **input_files_dirs) {
1518
1513
return 1 /* success */ ;
1519
1514
}
1520
1515
1516
+ static void create_dir (char * dir_name ) {
1517
+ // Create output directory. We can ignore the error output since it will
1518
+ // only delay the failure to the first attempt of creating a file in the
1519
+ // (not) newly created directory.
1520
+ #ifdef _WIN32
1521
+ (void )CreateDirectory (dir_name , NULL );
1522
+ #else
1523
+ (void )mkdir (dir_name , 0755 );
1524
+ #endif
1525
+ }
1526
+
1521
1527
static int read_data (void * data , size_t size , ZInfo * z_info , FILE * in_fd ) {
1522
1528
// We want to fill 'data' with 'size' bytes.
1523
1529
z_info -> strm .next_out = data ;
@@ -1603,12 +1609,7 @@ static int acl_pkg_unpack_buffer_or_file(const char *buffer, size_t buffer_size,
1603
1609
return 0 ;
1604
1610
}
1605
1611
1606
- // Create output directory (ignore any errors).
1607
- #ifdef _WIN32
1608
- CreateDirectory (full_name , NULL );
1609
- #else
1610
- mkdir (full_name , 0755 );
1611
- #endif
1612
+ create_dir (full_name );
1612
1613
full_name [out_dir_length ] = '/' ;
1613
1614
1614
1615
// Process the file until we hit the PACK_END record (or finish the
@@ -1640,22 +1641,21 @@ static int acl_pkg_unpack_buffer_or_file(const char *buffer, size_t buffer_size,
1640
1641
return 0 ;
1641
1642
}
1642
1643
1643
- // Generate the full name, truncate or zero pad to avoid buffer overflow
1644
- if (FULL_NAME_LEN < out_dir_length ) {
1644
+ // Generate the full name, truncate or zero pad to avoid buffer overflow.
1645
+ // FULL_NAME_LEN must be large enough to store out_dir and a trailing path
1646
+ // separator and null terminator
1647
+ if (out_dir_length + 2 > FULL_NAME_LEN ) {
1645
1648
fprintf (stderr , "%s: Directory name too long\n" , routine_name );
1649
+ zlib_inflateEnd (& z_info .strm );
1650
+ return 0 ;
1646
1651
}
1652
+
1647
1653
strncpy (full_name + out_dir_length + 1 , name ,
1648
- FULL_NAME_LEN - out_dir_length - 1 );
1649
- if (full_name [FULL_NAME_LEN - 1 ] != '\0' ) {
1650
- full_name [FULL_NAME_LEN - 1 ] = '\0' ;
1651
- }
1654
+ FULL_NAME_LEN - out_dir_length - 2 );
1655
+ full_name [FULL_NAME_LEN - 1 ] = '\0' ;
1652
1656
1653
1657
if (info .kind == PACK_DIR ) {
1654
- #ifdef _WIN32
1655
- CreateDirectory (full_name , NULL );
1656
- #else
1657
- mkdir (full_name , 0755 );
1658
- #endif
1658
+ create_dir (full_name );
1659
1659
} else {
1660
1660
// Read file contents
1661
1661
FILE * out_file = fopen (full_name , "wb" );
0 commit comments