Skip to content

Commit 99c57b3

Browse files
committed
Merge branch 'PHP-7.4'
* PHP-7.4: Replace deprecated libzip functions
2 parents c1c8538 + 8f897f1 commit 99c57b3

File tree

3 files changed

+87
-25
lines changed

3 files changed

+87
-25
lines changed

ext/zip/php_zip.c

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ static int le_zip_entry;
8181
#define PHP_ZIP_SET_FILE_COMMENT(za, index, comment, comment_len) \
8282
if (comment_len == 0) { \
8383
/* Passing NULL remove the existing comment */ \
84-
if (zip_set_file_comment(za, index, NULL, 0) < 0) { \
84+
if (zip_file_set_comment(za, index, NULL, 0, 0) < 0) { \
8585
RETURN_FALSE; \
8686
} \
87-
} else if (zip_set_file_comment(za, index, comment, comment_len) < 0) { \
87+
} else if (zip_file_set_comment(za, index, comment, comment_len, 0) < 0) { \
8888
RETURN_FALSE; \
8989
} \
9090
RETURN_TRUE;
@@ -409,7 +409,7 @@ static int php_zip_parse_options(zval *options, zend_long *remove_all_path, char
409409
#endif
410410
/* }}} */
411411

412-
static int php_zip_status(struct zip *za) /* {{{ */
412+
static zend_long php_zip_status(struct zip *za) /* {{{ */
413413
{
414414
#if LIBZIP_VERSION_MAJOR < 1
415415
int zep, syp;
@@ -427,7 +427,7 @@ static int php_zip_status(struct zip *za) /* {{{ */
427427
}
428428
/* }}} */
429429

430-
static int php_zip_status_sys(struct zip *za) /* {{{ */
430+
static zend_long php_zip_status_sys(struct zip *za) /* {{{ */
431431
{
432432
#if LIBZIP_VERSION_MAJOR < 1
433433
int zep, syp;
@@ -445,9 +445,10 @@ static int php_zip_status_sys(struct zip *za) /* {{{ */
445445
}
446446
/* }}} */
447447

448-
static int php_zip_get_num_files(struct zip *za) /* {{{ */
448+
static zend_long php_zip_get_num_files(struct zip *za) /* {{{ */
449449
{
450-
return zip_get_num_files(za);
450+
zip_int64_t num = zip_get_num_entries(za, 0);
451+
return MIN(num, ZEND_LONG_MAX);
451452
}
452453
/* }}} */
453454

@@ -740,7 +741,7 @@ static zend_object_handlers zip_object_handlers;
740741

741742
static HashTable zip_prop_handlers;
742743

743-
typedef int (*zip_read_int_t)(struct zip *za);
744+
typedef zend_long (*zip_read_int_t)(struct zip *za);
744745
typedef char *(*zip_read_const_char_t)(struct zip *za, int *len);
745746
typedef char *(*zip_read_const_char_from_ze_t)(ze_zip_object *obj);
746747

@@ -1088,7 +1089,7 @@ static PHP_NAMED_FUNCTION(zif_zip_open)
10881089
}
10891090

10901091
rsrc_int->index_current = 0;
1091-
rsrc_int->num_files = zip_get_num_files(rsrc_int->za);
1092+
rsrc_int->num_files = zip_get_num_entries(rsrc_int->za, 0);
10921093

10931094
RETURN_RES(zend_register_resource(rsrc_int, le_zip_dir));
10941095
}
@@ -1464,10 +1465,12 @@ static ZIPARCHIVE_METHOD(count)
14641465
{
14651466
struct zip *intern;
14661467
zval *self = ZEND_THIS;
1468+
zip_int64_t num;
14671469

14681470
ZIP_FROM_OBJECT(intern, self);
14691471

1470-
RETVAL_LONG(zip_get_num_files(intern));
1472+
num = zip_get_num_entries(intern, 0);
1473+
RETVAL_LONG(MIN(num, ZEND_LONG_MAX));
14711474
}
14721475
/* }}} */
14731476

@@ -1751,7 +1754,7 @@ static ZIPARCHIVE_METHOD(addFromString)
17511754
}
17521755
}
17531756

1754-
if (zip_add(intern, name, zs) == -1) {
1757+
if (zip_file_add(intern, name, zs, 0) == -1) {
17551758
zip_source_free(zs);
17561759
RETURN_FALSE;
17571760
} else {
@@ -1877,7 +1880,13 @@ static ZIPARCHIVE_METHOD(setArchiveComment)
18771880
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &comment, &comment_len) == FAILURE) {
18781881
return;
18791882
}
1880-
if (zip_set_archive_comment(intern, (const char *)comment, (int)comment_len)) {
1883+
1884+
if (comment_len > 0xffff) {
1885+
php_error_docref(NULL, E_WARNING, "Comment must not exceed 65535 bytes");
1886+
RETURN_FALSE;
1887+
}
1888+
1889+
if (zip_set_archive_comment(intern, (const char *)comment, comment_len)) {
18811890
RETURN_FALSE;
18821891
} else {
18831892
RETURN_TRUE;
@@ -1930,6 +1939,11 @@ static ZIPARCHIVE_METHOD(setCommentName)
19301939
php_error_docref(NULL, E_NOTICE, "Empty string as entry name");
19311940
}
19321941

1942+
if (comment_len > 0xffff) {
1943+
php_error_docref(NULL, E_WARNING, "Comment must not exceed 65535 bytes");
1944+
RETURN_FALSE;
1945+
}
1946+
19331947
idx = zip_name_locate(intern, name, 0);
19341948
if (idx < 0) {
19351949
RETURN_FALSE;
@@ -1956,6 +1970,11 @@ static ZIPARCHIVE_METHOD(setCommentIndex)
19561970
return;
19571971
}
19581972

1973+
if (comment_len > 0xffff) {
1974+
php_error_docref(NULL, E_WARNING, "Comment must not exceed 65535 bytes");
1975+
RETURN_FALSE;
1976+
}
1977+
19591978
PHP_ZIP_STAT_INDEX(intern, index, 0, sb);
19601979
PHP_ZIP_SET_FILE_COMMENT(intern, index, comment, comment_len);
19611980
}
@@ -2160,7 +2179,7 @@ static ZIPARCHIVE_METHOD(getCommentName)
21602179
size_t name_len;
21612180
int idx;
21622181
zend_long flags = 0;
2163-
int comment_len = 0;
2182+
zip_uint32_t comment_len = 0;
21642183
const char * comment;
21652184
char *name;
21662185

@@ -2180,8 +2199,8 @@ static ZIPARCHIVE_METHOD(getCommentName)
21802199
RETURN_FALSE;
21812200
}
21822201

2183-
comment = zip_get_file_comment(intern, idx, &comment_len, (int)flags);
2184-
RETURN_STRINGL((char *)comment, (zend_long)comment_len);
2202+
comment = zip_file_get_comment(intern, idx, &comment_len, (zip_flags_t)flags);
2203+
RETURN_STRINGL((char *)comment, comment_len);
21852204
}
21862205
/* }}} */
21872206

@@ -2193,7 +2212,7 @@ static ZIPARCHIVE_METHOD(getCommentIndex)
21932212
zval *self = ZEND_THIS;
21942213
zend_long index, flags = 0;
21952214
const char * comment;
2196-
int comment_len = 0;
2215+
zip_uint32_t comment_len = 0;
21972216
struct zip_stat sb;
21982217

21992218
ZIP_FROM_OBJECT(intern, self);
@@ -2204,8 +2223,8 @@ static ZIPARCHIVE_METHOD(getCommentIndex)
22042223
}
22052224

22062225
PHP_ZIP_STAT_INDEX(intern, index, 0, sb);
2207-
comment = zip_get_file_comment(intern, index, &comment_len, (int)flags);
2208-
RETURN_STRINGL((char *)comment, (zend_long)comment_len);
2226+
comment = zip_file_get_comment(intern, index, &comment_len, (zip_flags_t)flags);
2227+
RETURN_STRINGL((char *)comment, comment_len);
22092228
}
22102229
/* }}} */
22112230

@@ -2346,7 +2365,7 @@ static ZIPARCHIVE_METHOD(renameIndex)
23462365
php_error_docref(NULL, E_NOTICE, "Empty string as new entry name");
23472366
RETURN_FALSE;
23482367
}
2349-
if (zip_rename(intern, index, (const char *)new_name) != 0) {
2368+
if (zip_file_rename(intern, index, (const char *)new_name, 0) != 0) {
23502369
RETURN_FALSE;
23512370
}
23522371
RETURN_TRUE;
@@ -2376,7 +2395,7 @@ static ZIPARCHIVE_METHOD(renameName)
23762395

23772396
PHP_ZIP_STAT_PATH(intern, name, name_len, 0, sb);
23782397

2379-
if (zip_rename(intern, sb.index, (const char *)new_name)) {
2398+
if (zip_file_rename(intern, sb.index, (const char *)new_name, 0)) {
23802399
RETURN_FALSE;
23812400
}
23822401
RETURN_TRUE;
@@ -2490,9 +2509,7 @@ static ZIPARCHIVE_METHOD(extractTo)
24902509
php_stream_statbuf ssb;
24912510
char *pathto;
24922511
size_t pathto_len;
2493-
int ret, i;
2494-
2495-
int nelems;
2512+
int ret;
24962513

24972514
if (zend_parse_parameters(ZEND_NUM_ARGS(), "p|z", &pathto, &pathto_len, &zval_files) == FAILURE) {
24982515
return;
@@ -2511,6 +2528,8 @@ static ZIPARCHIVE_METHOD(extractTo)
25112528

25122529
ZIP_FROM_OBJECT(intern, self);
25132530
if (zval_files && (Z_TYPE_P(zval_files) != IS_NULL)) {
2531+
uint32_t nelems, i;
2532+
25142533
switch (Z_TYPE_P(zval_files)) {
25152534
case IS_STRING:
25162535
if (!php_zip_extract_file(intern, pathto, Z_STRVAL_P(zval_files), Z_STRLEN_P(zval_files))) {
@@ -2543,7 +2562,7 @@ static ZIPARCHIVE_METHOD(extractTo)
25432562
}
25442563
} else {
25452564
/* Extract all files */
2546-
int filecount = zip_get_num_files(intern);
2565+
zip_int64_t i, filecount = zip_get_num_entries(intern, 0);
25472566

25482567
if (filecount == -1) {
25492568
php_error_docref(NULL, E_WARNING, "Illegal archive");

ext/zip/php_zip.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ extern zend_module_entry zip_module_entry;
3939

4040
typedef struct _ze_zip_rsrc {
4141
struct zip *za;
42-
int index_current;
43-
int num_files;
42+
zip_uint64_t index_current;
43+
zip_int64_t num_files;
4444
} zip_rsrc;
4545

4646
typedef zip_rsrc * zip_rsrc_ptr;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
--TEST--
2+
setComment error behavior
3+
--SKIPIF--
4+
<?php
5+
if(!extension_loaded('zip')) die('skip zip extension not available');
6+
?>
7+
--FILE--
8+
<?php
9+
$file = __DIR__ . '/__tmp_oo_set_comment_error.zip';
10+
11+
@unlink($file);
12+
13+
$zip = new ZipArchive;
14+
if (!$zip->open($file, ZIPARCHIVE::CREATE)) {
15+
exit('failed');
16+
}
17+
18+
$zip->addFromString('entry1.txt', 'entry #1');
19+
$zip->addFromString('entry2.txt', 'entry #2');
20+
21+
$longComment = str_repeat('a', 0x10000);
22+
23+
var_dump($zip->setArchiveComment($longComment));
24+
var_dump($zip->setCommentName('entry1.txt', $longComment));
25+
var_dump($zip->setCommentIndex(1, $longComment));
26+
27+
$zip->close();
28+
?>
29+
===DONE===
30+
--EXPECTF--
31+
Warning: ZipArchive::setArchiveComment(): Comment must not exceed 65535 bytes in %s on line %d
32+
bool(false)
33+
34+
Warning: ZipArchive::setCommentName(): Comment must not exceed 65535 bytes in %s on line %d
35+
bool(false)
36+
37+
Warning: ZipArchive::setCommentIndex(): Comment must not exceed 65535 bytes in %s on line %d
38+
bool(false)
39+
===DONE===
40+
--CLEAN--
41+
<?php
42+
@unlink(__DIR__ . '/__tmp_oo_set_comment_error.zip');
43+
?>

0 commit comments

Comments
 (0)