Skip to content

Commit 9719d6c

Browse files
committed
Make Phar $format and $compression arguments nullable
Rather than using Greg's birthday, use null to indicate that the existing format/compression should be retained. For the format simply using zero would be sufficient, but as the documentation explicitly says that NULL is allowed here, we may as well make that the truth.
1 parent 9e32e13 commit 9719d6c

File tree

4 files changed

+41
-23
lines changed

4 files changed

+41
-23
lines changed

ext/phar/phar_object.c

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2336,10 +2336,10 @@ PHP_METHOD(Phar, convertToExecutable)
23362336
size_t ext_len = 0;
23372337
uint32_t flags;
23382338
zend_object *ret;
2339-
/* a number that is not 0, 1 or 2 (Which is also Greg's birthday, so there) */
2340-
zend_long format = 9021976, method = 9021976;
2339+
zend_long format, method;
2340+
zend_bool format_is_null = 1, method_is_null = 1;
23412341

2342-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|lls!", &format, &method, &ext, &ext_len) == FAILURE) {
2342+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l!l!s!", &format, &format_is_null, &method, &method_is_null, &ext, &ext_len) == FAILURE) {
23432343
RETURN_THROWS();
23442344
}
23452345

@@ -2351,9 +2351,12 @@ PHP_METHOD(Phar, convertToExecutable)
23512351
RETURN_THROWS();
23522352
}
23532353

2354+
if (format_is_null) {
2355+
format = PHAR_FORMAT_SAME;
2356+
}
23542357
switch (format) {
2355-
case 9021976:
2356-
case PHAR_FORMAT_SAME: /* null is converted to 0 */
2358+
case 9021976: /* Retained for BC */
2359+
case PHAR_FORMAT_SAME:
23572360
/* by default, use the existing format */
23582361
if (phar_obj->archive->is_tar) {
23592362
format = PHAR_FORMAT_TAR;
@@ -2373,8 +2376,11 @@ PHP_METHOD(Phar, convertToExecutable)
23732376
RETURN_THROWS();
23742377
}
23752378

2376-
switch (method) {
2377-
case 9021976:
2379+
if (method_is_null) {
2380+
flags = phar_obj->archive->flags & PHAR_FILE_COMPRESSION_MASK;
2381+
} else {
2382+
switch (method) {
2383+
case 9021976: /* Retained for BC */
23782384
flags = phar_obj->archive->flags & PHAR_FILE_COMPRESSION_MASK;
23792385
break;
23802386
case 0:
@@ -2414,6 +2420,7 @@ PHP_METHOD(Phar, convertToExecutable)
24142420
zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
24152421
"Unknown compression specified, please pass one of Phar::GZ or Phar::BZ2");
24162422
RETURN_THROWS();
2423+
}
24172424
}
24182425

24192426
is_data = phar_obj->archive->is_data;
@@ -2440,18 +2447,21 @@ PHP_METHOD(Phar, convertToData)
24402447
size_t ext_len = 0;
24412448
uint32_t flags;
24422449
zend_object *ret;
2443-
/* a number that is not 0, 1 or 2 (Which is also Greg's birthday so there) */
2444-
zend_long format = 9021976, method = 9021976;
2450+
zend_long format, method;
2451+
zend_bool format_is_null = 1, method_is_null = 1;
24452452

2446-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|lls!", &format, &method, &ext, &ext_len) == FAILURE) {
2453+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|l!l!s!", &format, &format_is_null, &method, &method_is_null, &ext, &ext_len) == FAILURE) {
24472454
RETURN_THROWS();
24482455
}
24492456

24502457
PHAR_ARCHIVE_OBJECT();
24512458

2459+
if (format_is_null) {
2460+
format = PHAR_FORMAT_SAME;
2461+
}
24522462
switch (format) {
2453-
case 9021976:
2454-
case PHAR_FORMAT_SAME: /* null is converted to 0 */
2463+
case 9021976: /* Retained for BC */
2464+
case PHAR_FORMAT_SAME:
24552465
/* by default, use the existing format */
24562466
if (phar_obj->archive->is_tar) {
24572467
format = PHAR_FORMAT_TAR;
@@ -2476,8 +2486,11 @@ PHP_METHOD(Phar, convertToData)
24762486
RETURN_THROWS();
24772487
}
24782488

2479-
switch (method) {
2480-
case 9021976:
2489+
if (method_is_null) {
2490+
flags = phar_obj->archive->flags & PHAR_FILE_COMPRESSION_MASK;
2491+
} else {
2492+
switch (method) {
2493+
case 9021976: /* Retained for BC */
24812494
flags = phar_obj->archive->flags & PHAR_FILE_COMPRESSION_MASK;
24822495
break;
24832496
case 0:
@@ -2517,6 +2530,7 @@ PHP_METHOD(Phar, convertToData)
25172530
zend_throw_exception_ex(spl_ce_BadMethodCallException, 0,
25182531
"Unknown compression specified, please pass one of Phar::GZ or Phar::BZ2");
25192532
RETURN_THROWS();
2533+
}
25202534
}
25212535

25222536
is_data = phar_obj->archive->is_data;

ext/phar/phar_object.stub.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ public function compress(int $compression_type, ?string $file_ext = null) {}
4040
public function decompress(?string $file_ext = null) {}
4141

4242
/** @return Phar|null */
43-
public function convertToExecutable(int $format = 9021976, int $compression_type = 9021976, ?string $file_ext = null) {}
43+
public function convertToExecutable(?int $format = null, ?int $compression_type = null, ?string $file_ext = null) {}
4444

4545
/** @return Phar|null */
46-
public function convertToData(int $format = 9021976, int $compression_type = 9021976, ?string $file_ext = null) {}
46+
public function convertToData(?int $format = null, ?int $compression_type = null, ?string $file_ext = null) {}
4747

4848
/** @return bool */
4949
public function copy(string $newfile, string $oldfile) {}

ext/phar/phar_object_arginfo.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: e67cd4d59555843688a1bdd90ecd7d3924f1ee29 */
2+
* Stub hash: 7c9fbbc6da2c4d7196583d82102fa857ce8eda6b */
33

44
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Phar___construct, 0, 0, 1)
55
ZEND_ARG_TYPE_INFO(0, filename, IS_STRING, 0)
@@ -50,8 +50,8 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Phar_decompress, 0, 0, 0)
5050
ZEND_END_ARG_INFO()
5151

5252
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_Phar_convertToExecutable, 0, 0, 0)
53-
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, format, IS_LONG, 0, "9021976")
54-
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, compression_type, IS_LONG, 0, "9021976")
53+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, format, IS_LONG, 1, "null")
54+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, compression_type, IS_LONG, 1, "null")
5555
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, file_ext, IS_STRING, 1, "null")
5656
ZEND_END_ARG_INFO()
5757

@@ -236,9 +236,13 @@ ZEND_END_ARG_INFO()
236236

237237
#define arginfo_class_PharData_decompress arginfo_class_Phar_decompress
238238

239-
#define arginfo_class_PharData_convertToExecutable arginfo_class_Phar_convertToExecutable
239+
ZEND_BEGIN_ARG_INFO_EX(arginfo_class_PharData_convertToExecutable, 0, 0, 0)
240+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, format, IS_LONG, 0, "9021976")
241+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, compression_type, IS_LONG, 0, "9021976")
242+
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, file_ext, IS_STRING, 1, "null")
243+
ZEND_END_ARG_INFO()
240244

241-
#define arginfo_class_PharData_convertToData arginfo_class_Phar_convertToExecutable
245+
#define arginfo_class_PharData_convertToData arginfo_class_PharData_convertToExecutable
242246

243247
#define arginfo_class_PharData_copy arginfo_class_Phar_copy
244248

ext/phar/tests/badparameters.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,8 @@ Phar::createDefaultStub(): Argument #1 ($index) must be of type ?string, array g
233233
Phar::loadPhar(): Argument #1 ($filename) must be of type string, array given
234234
Phar::canCompress(): Argument #1 ($method) must be of type int, string given
235235
Phar::__construct(): Argument #1 ($filename) must be of type string, array given
236-
Phar::convertToExecutable(): Argument #1 ($format) must be of type int, array given
237-
Phar::convertToData(): Argument #1 ($format) must be of type int, array given
236+
Phar::convertToExecutable(): Argument #1 ($format) must be of type ?int, array given
237+
Phar::convertToData(): Argument #1 ($format) must be of type ?int, array given
238238
PharData::delete(): Argument #1 ($entry) must be of type string, array given
239239
Cannot write out phar archive, phar is read-only
240240
Entry oops does not exist and cannot be deleted

0 commit comments

Comments
 (0)