Skip to content

Commit 71d066b

Browse files
committed
feedback
1 parent a951792 commit 71d066b

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

ext/zip/php_zip.c

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,11 @@ static zend_result php_zip_parse_options(HashTable *options, zip_options *opts)
378378
php_error_docref(NULL, E_WARNING, "Option \"comp_method\" must be of type int, %s given",
379379
zend_zval_value_name(option));
380380
}
381-
opts->comp_method = zval_get_long(option);
381+
zend_long comp_method = zval_get_long(option);
382+
if (comp_method < 0 || comp_method > INT_MAX) {
383+
php_error_docref(NULL, E_WARNING, "Option \"comp_method\" must be between 0 and %d", INT_MAX);
384+
}
385+
opts->comp_method = (zip_int32_t)comp_method;
382386

383387
if ((option = zend_hash_str_find(options, "comp_flags", sizeof("comp_flags") - 1)) != NULL) {
384388
if (Z_TYPE_P(option) != IS_LONG) {
@@ -2388,15 +2392,19 @@ PHP_METHOD(ZipArchive, setCompressionName)
23882392
RETURN_THROWS();
23892393
}
23902394

2395+
if (name_len == 0) {
2396+
zend_argument_must_not_be_empty_error(1);
2397+
RETURN_THROWS();
2398+
}
23912399

2392-
if (comp_flags < 0 || comp_flags > USHRT_MAX) {
2393-
// comp_flags is cast down accordingly in libzip, zip_entry_t compression_level is of zip_uint16_t
2394-
zend_argument_value_error(3, "must be between 0 and %u", USHRT_MAX);
2400+
if (comp_method < -1 || comp_method > INT_MAX) {
2401+
zend_argument_value_error(2, "must be between 0 and %d", INT_MAX);
23952402
RETURN_THROWS();
23962403
}
23972404

2398-
if (name_len == 0) {
2399-
zend_argument_must_not_be_empty_error(1);
2405+
if (comp_flags < 0 || comp_flags > USHRT_MAX) {
2406+
// comp_flags is cast down accordingly in libzip, zip_entry_t compression_level is of zip_uint16_t
2407+
zend_argument_value_error(3, "must be between 0 and %u", USHRT_MAX);
24002408
RETURN_THROWS();
24012409
}
24022410

@@ -2429,6 +2437,11 @@ PHP_METHOD(ZipArchive, setCompressionIndex)
24292437
RETURN_FALSE;
24302438
}
24312439

2440+
if (comp_method < -1 || comp_method > INT_MAX) {
2441+
zend_argument_value_error(2, "must be between -1 and %d", INT_MAX);
2442+
RETURN_THROWS();
2443+
}
2444+
24322445
if (comp_flags < 0 || comp_flags > USHRT_MAX) {
24332446
// comp_flags is cast down accordingly in libzip, zip_entry_t compression_level is of zip_uint16_t
24342447
zend_argument_value_error(3, "must be between 0 and %u", USHRT_MAX);
@@ -3016,8 +3029,9 @@ PHP_METHOD(ZipArchive, isCompressionMethodSupported)
30163029
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l|b", &method, &enc) == FAILURE) {
30173030
return;
30183031
}
3019-
if (method < 0) {
3020-
RETURN_FALSE;
3032+
if (method < -1 || method > INT_MAX) {
3033+
zend_argument_value_error(1, "must be between -1 and %d", INT_MAX);
3034+
RETURN_THROWS();
30213035
}
30223036
RETVAL_BOOL(zip_compression_method_supported((zip_int32_t)method, enc));
30233037
}
@@ -3032,8 +3046,9 @@ PHP_METHOD(ZipArchive, isEncryptionMethodSupported)
30323046
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l|b", &method, &enc) == FAILURE) {
30333047
return;
30343048
}
3035-
if (method < 0) {
3036-
RETURN_FALSE;
3049+
if (method < 0 || method > USHRT_MAX) {
3050+
zend_argument_value_error(1, "must be between 0 and %u", USHRT_MAX);
3051+
RETURN_THROWS();
30373052
}
30383053
RETVAL_BOOL(zip_encryption_method_supported((zip_uint16_t)method, enc));
30393054
}

ext/zip/tests/oo_setcompression.phpt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ var_dump($zip->setCompressionName('dir/entry3.txt', ZipArchive::CM_STORE));
2929
var_dump($zip->setCompressionName('entry4.txt', ZipArchive::CM_DEFLATE));
3030

3131
try {
32-
$zip->setCompressionName('entry5.txt', -1);
32+
$zip->setCompressionName('entry5.txt', PHP_INT_MIN);
3333
} catch (\ValueError $e) {
3434
echo $e->getMessage(), PHP_EOL;
3535
}
@@ -41,7 +41,7 @@ try {
4141
}
4242

4343
try {
44-
$zip->setCompressionIndex(4, -1);
44+
$zip->setCompressionIndex(4, PHP_INT_MIN);
4545
} catch (\ValueError $e) {
4646
echo $e->getMessage(), PHP_EOL;
4747
}
@@ -81,6 +81,10 @@ unlink($tmpfile);
8181
bool(true)
8282
bool(true)
8383
bool(true)
84+
ZipArchive::setCompressionName(): Argument #2 ($method) must be between 0 and %d
85+
ZipArchive::setCompressionName(): Argument #2 ($method) must be between 0 and %d
86+
ZipArchive::setCompressionIndex(): Argument #2 ($method) must be between -1 and %d
87+
ZipArchive::setCompressionIndex(): Argument #2 ($method) must be between -1 and %d
8488
bool(true)
8589
bool(true)
8690
bool(true)

0 commit comments

Comments
 (0)