Skip to content

Commit f4e4712

Browse files
committed
ext/zip: further micro optimisations.
- forgotten remaining libzip < 1.0.0 code path. - earlier return on invalid inputs from some ZipArchive methods.
1 parent d2a6773 commit f4e4712

File tree

2 files changed

+35
-23
lines changed

2 files changed

+35
-23
lines changed

ext/zip/php_zip.c

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2422,6 +2422,15 @@ PHP_METHOD(ZipArchive, setCompressionIndex)
24222422
RETURN_THROWS();
24232423
}
24242424

2425+
if (index < 0) {
2426+
RETURN_FALSE;
2427+
}
2428+
2429+
if (comp_flags < 0 || comp_flags > USHRT_MAX) {
2430+
// comp_flags is cast down accordingly in libzip, zip_entry_t compression_level is of zip_uint16_t
2431+
RETURN_FALSE;
2432+
}
2433+
24252434
ZIP_FROM_OBJECT(intern, this);
24262435

24272436
RETURN_BOOL(zip_set_file_compression(intern, (zip_uint64_t)index,
@@ -2444,13 +2453,13 @@ PHP_METHOD(ZipArchive, setMtimeName)
24442453
RETURN_THROWS();
24452454
}
24462455

2447-
ZIP_FROM_OBJECT(intern, this);
2448-
24492456
if (name_len == 0) {
24502457
zend_argument_must_not_be_empty_error(1);
24512458
RETURN_THROWS();
24522459
}
24532460

2461+
ZIP_FROM_OBJECT(intern, this);
2462+
24542463
idx = zip_name_locate(intern, name, 0);
24552464

24562465
if (idx < 0) {
@@ -2516,17 +2525,15 @@ PHP_METHOD(ZipArchive, deleteName)
25162525
RETURN_THROWS();
25172526
}
25182527

2519-
ZIP_FROM_OBJECT(intern, self);
2520-
25212528
if (name_len < 1) {
25222529
RETURN_FALSE;
25232530
}
25242531

2532+
ZIP_FROM_OBJECT(intern, self);
2533+
25252534
PHP_ZIP_STAT_PATH(intern, name, name_len, 0, sb);
2526-
if (zip_delete(intern, sb.index)) {
2527-
RETURN_FALSE;
2528-
}
2529-
RETURN_TRUE;
2535+
2536+
RETURN_BOOL(zip_delete(intern, sb.index) == 0);
25302537
}
25312538
/* }}} */
25322539

@@ -2547,13 +2554,13 @@ PHP_METHOD(ZipArchive, renameIndex)
25472554
RETURN_FALSE;
25482555
}
25492556

2550-
ZIP_FROM_OBJECT(intern, self);
2551-
25522557
if (new_name_len == 0) {
25532558
zend_argument_must_not_be_empty_error(2);
25542559
RETURN_THROWS();
25552560
}
25562561

2562+
ZIP_FROM_OBJECT(intern, self);
2563+
25572564
RETURN_BOOL(zip_file_rename(intern, index, (const char *)new_name, 0) == 0);
25582565
}
25592566
/* }}} */
@@ -2595,12 +2602,12 @@ PHP_METHOD(ZipArchive, unchangeIndex)
25952602
RETURN_THROWS();
25962603
}
25972604

2598-
ZIP_FROM_OBJECT(intern, self);
2599-
26002605
if (index < 0) {
26012606
RETURN_FALSE;
26022607
}
26032608

2609+
ZIP_FROM_OBJECT(intern, self);
2610+
26042611
RETURN_BOOL(zip_unchange(intern, index) == 0);
26052612
}
26062613
/* }}} */
@@ -2618,12 +2625,12 @@ PHP_METHOD(ZipArchive, unchangeName)
26182625
RETURN_THROWS();
26192626
}
26202627

2621-
ZIP_FROM_OBJECT(intern, self);
2622-
26232628
if (name_len < 1) {
26242629
RETURN_FALSE;
26252630
}
26262631

2632+
ZIP_FROM_OBJECT(intern, self);
2633+
26272634
PHP_ZIP_STAT_PATH(intern, name, name_len, 0, sb);
26282635

26292636
RETURN_BOOL(zip_unchange(intern, sb.index) == 0);
@@ -2687,8 +2694,6 @@ PHP_METHOD(ZipArchive, extractTo)
26872694
Z_PARAM_ARRAY_HT_OR_STR_OR_NULL(files_ht, files_str)
26882695
ZEND_PARSE_PARAMETERS_END();
26892696

2690-
ZIP_FROM_OBJECT(intern, self);
2691-
26922697
if (pathto_len < 1) {
26932698
RETURN_FALSE;
26942699
}
@@ -2701,6 +2706,7 @@ PHP_METHOD(ZipArchive, extractTo)
27012706
}
27022707

27032708
uint32_t nelems, i;
2709+
ZIP_FROM_OBJECT(intern, self);
27042710

27052711
if (files_str) {
27062712
if (!php_zip_extract_file(intern, pathto, ZSTR_VAL(files_str), ZSTR_LEN(files_str), -1)) {
@@ -2797,7 +2803,7 @@ static void php_zip_get_from(INTERNAL_FUNCTION_PARAMETERS, int type) /* {{{ */
27972803
RETURN_FALSE;
27982804
}
27992805

2800-
buffer = zend_string_safe_alloc(1, len, 0, 0);
2806+
buffer = zend_string_safe_alloc(1, len, 0, false);
28012807
n = zip_fread(zf, ZSTR_VAL(buffer), ZSTR_LEN(buffer));
28022808
if (n < 1) {
28032809
zend_string_efree(buffer);
@@ -3006,6 +3012,9 @@ PHP_METHOD(ZipArchive, isCompressionMethodSupported)
30063012
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l|b", &method, &enc) == FAILURE) {
30073013
return;
30083014
}
3015+
if (method < 0) {
3016+
RETURN_FALSE;
3017+
}
30093018
RETVAL_BOOL(zip_compression_method_supported((zip_int32_t)method, enc));
30103019
}
30113020
/* }}} */
@@ -3019,6 +3028,9 @@ PHP_METHOD(ZipArchive, isEncryptionMethodSupported)
30193028
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l|b", &method, &enc) == FAILURE) {
30203029
return;
30213030
}
3031+
if (method < 0) {
3032+
RETURN_FALSE;
3033+
}
30223034
RETVAL_BOOL(zip_encryption_method_supported((zip_uint16_t)method, enc));
30233035
}
30243036
/* }}} */

ext/zip/zip_stream.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ static int php_zip_ops_stat(php_stream *stream, php_stream_statbuf *ssb) /* {{{
150150
fragment++;
151151

152152
if (ZIP_OPENBASEDIR_CHECKPATH(file_dirname)) {
153-
zend_string_release_ex(file_basename, 0);
153+
zend_string_release_ex(file_basename, false);
154154
return -1;
155155
}
156156

@@ -159,7 +159,7 @@ static int php_zip_ops_stat(php_stream *stream, php_stream_statbuf *ssb) /* {{{
159159
memset(ssb, 0, sizeof(php_stream_statbuf));
160160
if (zip_stat(za, fragment, ZIP_FL_NOCASE, &sb) != 0) {
161161
zip_close(za);
162-
zend_string_release_ex(file_basename, 0);
162+
zend_string_release_ex(file_basename, false);
163163
return -1;
164164
}
165165
zip_close(za);
@@ -183,7 +183,7 @@ static int php_zip_ops_stat(php_stream *stream, php_stream_statbuf *ssb) /* {{{
183183
#endif
184184
ssb->sb.st_ino = -1;
185185
}
186-
zend_string_release_ex(file_basename, 0);
186+
zend_string_release_ex(file_basename, false);
187187
return 0;
188188
}
189189
/* }}} */
@@ -312,7 +312,7 @@ php_stream *php_stream_zip_opener(php_stream_wrapper *wrapper,
312312
fragment++;
313313

314314
if (ZIP_OPENBASEDIR_CHECKPATH(file_dirname)) {
315-
zend_string_release_ex(file_basename, 0);
315+
zend_string_release_ex(file_basename, false);
316316
return NULL;
317317
}
318318

@@ -344,14 +344,14 @@ php_stream *php_stream_zip_opener(php_stream_wrapper *wrapper,
344344
}
345345

346346
if (opened_path) {
347-
*opened_path = zend_string_init(path, strlen(path), 0);
347+
*opened_path = zend_string_init(path, path_len, false);
348348
}
349349
} else {
350350
zip_close(za);
351351
}
352352
}
353353

354-
zend_string_release_ex(file_basename, 0);
354+
zend_string_release_ex(file_basename, false);
355355

356356
if (!stream) {
357357
return NULL;

0 commit comments

Comments
 (0)