Skip to content

Commit b54d2e5

Browse files
committed
ext/phar: Use zend_string instead of char* len pair
1 parent 7f858fb commit b54d2e5

File tree

1 file changed

+56
-56
lines changed

1 file changed

+56
-56
lines changed

ext/phar/phar_object.c

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2600,12 +2600,11 @@ PHP_METHOD(Phar, isWritable)
26002600
/* {{{ Deletes a named file within the archive. */
26012601
PHP_METHOD(Phar, delete)
26022602
{
2603-
char *fname;
2604-
size_t fname_len;
2603+
zend_string *file_name;
26052604
char *error;
26062605
phar_entry_info *entry;
26072606

2608-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "p", &fname, &fname_len) == FAILURE) {
2607+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "P", &file_name) == FAILURE) {
26092608
RETURN_THROWS();
26102609
}
26112610

@@ -2621,7 +2620,7 @@ PHP_METHOD(Phar, delete)
26212620
zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar_obj->archive->fname);
26222621
RETURN_THROWS();
26232622
}
2624-
if (NULL != (entry = zend_hash_str_find_ptr(&phar_obj->archive->manifest, fname, fname_len))) {
2623+
if (NULL != (entry = zend_hash_find_ptr(&phar_obj->archive->manifest, file_name))) {
26252624
if (entry->is_deleted) {
26262625
/* entry is deleted, but has not been flushed to disk yet */
26272626
RETURN_TRUE;
@@ -2631,7 +2630,7 @@ PHP_METHOD(Phar, delete)
26312630
phar_obj->archive->is_modified = 1;
26322631
}
26332632
} else {
2634-
zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Entry %s does not exist and cannot be deleted", fname);
2633+
zend_throw_exception_ex(spl_ce_BadMethodCallException, 0, "Entry %s does not exist and cannot be deleted", ZSTR_VAL(file_name));
26352634
RETURN_THROWS();
26362635
}
26372636

@@ -2679,12 +2678,19 @@ PHP_METHOD(Phar, getPath)
26792678
*/
26802679
PHP_METHOD(Phar, setAlias)
26812680
{
2682-
char *alias, *error, *oldalias;
2681+
zend_string *new_alias = NULL;
2682+
char *error, *oldalias;
26832683
phar_archive_data *fd_ptr;
2684-
size_t alias_len, oldalias_len;
2684+
size_t oldalias_len;
26852685
int old_temp, readd = 0;
26862686

2687-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &alias, &alias_len) == FAILURE) {
2687+
// TODO Should this be using a "P" modifier?
2688+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &new_alias) == FAILURE) {
2689+
RETURN_THROWS();
2690+
}
2691+
2692+
if (ZSTR_LEN(new_alias) == 0) {
2693+
zend_argument_value_error(1, "cannot be empty");
26882694
RETURN_THROWS();
26892695
}
26902696

@@ -2711,22 +2717,22 @@ PHP_METHOD(Phar, setAlias)
27112717
RETURN_THROWS();
27122718
}
27132719

2714-
if (alias_len == phar_obj->archive->alias_len && memcmp(phar_obj->archive->alias, alias, alias_len) == 0) {
2720+
if (zend_string_equals_cstr(new_alias, phar_obj->archive->alias, phar_obj->archive->alias_len)) {
27152721
RETURN_TRUE;
27162722
}
2717-
if (alias_len && NULL != (fd_ptr = zend_hash_str_find_ptr(&(PHAR_G(phar_alias_map)), alias, alias_len))) {
2718-
spprintf(&error, 0, "alias \"%s\" is already used for archive \"%s\" and cannot be used for other archives", alias, fd_ptr->fname);
2719-
if (SUCCESS == phar_free_alias(fd_ptr, alias, alias_len)) {
2723+
if (NULL != (fd_ptr = zend_hash_find_ptr(&(PHAR_G(phar_alias_map)), new_alias))) {
2724+
spprintf(&error, 0, "alias \"%s\" is already used for archive \"%s\" and cannot be used for other archives", ZSTR_VAL(new_alias), fd_ptr->fname);
2725+
if (SUCCESS == phar_free_alias(fd_ptr, ZSTR_VAL(new_alias), ZSTR_LEN(new_alias))) {
27202726
efree(error);
27212727
goto valid_alias;
27222728
}
27232729
zend_throw_exception_ex(phar_ce_PharException, 0, "%s", error);
27242730
efree(error);
27252731
RETURN_THROWS();
27262732
}
2727-
if (!phar_validate_alias(alias, alias_len)) {
2733+
if (!phar_validate_alias(ZSTR_VAL(new_alias), ZSTR_LEN(new_alias))) {
27282734
zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
2729-
"Invalid alias \"%s\" specified for phar \"%s\"", alias, phar_obj->archive->fname);
2735+
"Invalid alias \"%s\" specified for phar \"%s\"", ZSTR_VAL(new_alias), phar_obj->archive->fname);
27302736
RETURN_THROWS();
27312737
}
27322738
valid_alias:
@@ -2743,13 +2749,8 @@ PHP_METHOD(Phar, setAlias)
27432749
oldalias_len = phar_obj->archive->alias_len;
27442750
old_temp = phar_obj->archive->is_temporary_alias;
27452751

2746-
if (alias_len) {
2747-
phar_obj->archive->alias = estrndup(alias, alias_len);
2748-
} else {
2749-
phar_obj->archive->alias = NULL;
2750-
}
2751-
2752-
phar_obj->archive->alias_len = alias_len;
2752+
phar_obj->archive->alias = estrndup(ZSTR_VAL(new_alias), ZSTR_LEN(new_alias));
2753+
phar_obj->archive->alias_len = ZSTR_LEN(new_alias);
27532754
phar_obj->archive->is_temporary_alias = 0;
27542755
phar_flush(phar_obj->archive, NULL, 0, 0, &error);
27552756

@@ -2765,7 +2766,7 @@ PHP_METHOD(Phar, setAlias)
27652766
RETURN_THROWS();
27662767
}
27672768

2768-
zend_hash_str_add_ptr(&(PHAR_G(phar_alias_map)), alias, alias_len, phar_obj->archive);
2769+
zend_hash_add_ptr(&(PHAR_G(phar_alias_map)), new_alias, phar_obj->archive);
27692770

27702771
if (oldalias) {
27712772
efree(oldalias);
@@ -3408,73 +3409,73 @@ PHP_METHOD(Phar, decompressFiles)
34083409
/* {{{ copy a file internal to the phar archive to another new file within the phar */
34093410
PHP_METHOD(Phar, copy)
34103411
{
3411-
char *oldfile, *newfile, *error;
3412+
char *error;
34123413
const char *pcr_error;
3413-
size_t oldfile_len, newfile_len;
34143414
phar_entry_info *oldentry, newentry = {0}, *temp;
3415-
size_t tmp_len = 0;
3415+
zend_string *new_file = NULL;
3416+
zend_string *old_file = NULL;
34163417

3417-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "pp", &oldfile, &oldfile_len, &newfile, &newfile_len) == FAILURE) {
3418+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "PP", &old_file, &new_file) == FAILURE) {
34183419
RETURN_THROWS();
34193420
}
34203421

34213422
PHAR_ARCHIVE_OBJECT();
34223423

34233424
if (PHAR_G(readonly) && !phar_obj->archive->is_data) {
34243425
zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
3425-
"Cannot copy \"%s\" to \"%s\", phar is read-only", oldfile, newfile);
3426+
"Cannot copy \"%s\" to \"%s\", phar is read-only", ZSTR_VAL(old_file), ZSTR_VAL(new_file));
34263427
RETURN_THROWS();
34273428
}
34283429

3429-
if (oldfile_len >= sizeof(".phar")-1 && !memcmp(oldfile, ".phar", sizeof(".phar")-1)) {
3430+
if (ZSTR_LEN(old_file) >= sizeof(".phar")-1 && !memcmp(ZSTR_VAL(old_file), ".phar", sizeof(".phar")-1)) {
34303431
/* can't copy a meta file */
34313432
zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
3432-
"file \"%s\" cannot be copied to file \"%s\", cannot copy Phar meta-file in %s", oldfile, newfile, phar_obj->archive->fname);
3433+
"file \"%s\" cannot be copied to file \"%s\", cannot copy Phar meta-file in %s", ZSTR_VAL(old_file), ZSTR_VAL(new_file), phar_obj->archive->fname);
34333434
RETURN_THROWS();
34343435
}
34353436

3436-
if (newfile_len >= sizeof(".phar")-1 && !memcmp(newfile, ".phar", sizeof(".phar")-1)) {
3437+
if (ZSTR_LEN(new_file) >= sizeof(".phar")-1 && !memcmp(ZSTR_VAL(new_file), ".phar", sizeof(".phar")-1)) {
34373438
/* can't copy a meta file */
34383439
zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
3439-
"file \"%s\" cannot be copied to file \"%s\", cannot copy to Phar meta-file in %s", oldfile, newfile, phar_obj->archive->fname);
3440+
"file \"%s\" cannot be copied to file \"%s\", cannot copy to Phar meta-file in %s", ZSTR_VAL(old_file), ZSTR_VAL(new_file), phar_obj->archive->fname);
34403441
RETURN_THROWS();
34413442
}
34423443

3443-
if (NULL == (oldentry = zend_hash_str_find_ptr(&phar_obj->archive->manifest, oldfile, oldfile_len)) || oldentry->is_deleted) {
3444+
if (NULL == (oldentry = zend_hash_str_find_ptr(&phar_obj->archive->manifest, ZSTR_VAL(old_file), ZSTR_LEN(old_file))) || oldentry->is_deleted) {
34443445
zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
3445-
"file \"%s\" cannot be copied to file \"%s\", file does not exist in %s", oldfile, newfile, phar_obj->archive->fname);
3446+
"file \"%s\" cannot be copied to file \"%s\", file does not exist in %s", ZSTR_VAL(old_file), ZSTR_VAL(new_file), phar_obj->archive->fname);
34463447
RETURN_THROWS();
34473448
}
34483449

3449-
if (NULL != (temp = zend_hash_str_find_ptr(&phar_obj->archive->manifest, newfile, newfile_len)) && !temp->is_deleted) {
3450+
if (NULL != (temp = zend_hash_find_ptr(&phar_obj->archive->manifest, new_file)) && !temp->is_deleted) {
34503451
zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
3451-
"file \"%s\" cannot be copied to file \"%s\", file must not already exist in phar %s", oldfile, newfile, phar_obj->archive->fname);
3452+
"file \"%s\" cannot be copied to file \"%s\", file must not already exist in phar %s", ZSTR_VAL(old_file), ZSTR_VAL(new_file), phar_obj->archive->fname);
34523453
RETURN_THROWS();
34533454
}
34543455

3455-
tmp_len = newfile_len;
3456-
if (phar_path_check(&newfile, &tmp_len, &pcr_error) > pcr_is_ok) {
3456+
size_t tmp_len = ZSTR_LEN(new_file);
3457+
char *tmp_new_file = ZSTR_VAL(new_file);
3458+
if (phar_path_check(&tmp_new_file, &tmp_len, &pcr_error) > pcr_is_ok) {
34573459
zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0,
3458-
"file \"%s\" contains invalid characters %s, cannot be copied from \"%s\" in phar %s", newfile, pcr_error, oldfile, phar_obj->archive->fname);
3460+
"file \"%s\" contains invalid characters %s, cannot be copied from \"%s\" in phar %s", ZSTR_VAL(new_file), pcr_error, ZSTR_VAL(old_file), phar_obj->archive->fname);
34593461
RETURN_THROWS();
34603462
}
3461-
newfile_len = tmp_len;
34623463

34633464
if (phar_obj->archive->is_persistent) {
34643465
if (FAILURE == phar_copy_on_write(&(phar_obj->archive))) {
34653466
zend_throw_exception_ex(phar_ce_PharException, 0, "phar \"%s\" is persistent, unable to copy on write", phar_obj->archive->fname);
34663467
RETURN_THROWS();
34673468
}
34683469
/* re-populate with copied-on-write entry */
3469-
oldentry = zend_hash_str_find_ptr(&phar_obj->archive->manifest, oldfile, oldfile_len);
3470+
oldentry = zend_hash_find_ptr(&phar_obj->archive->manifest, old_file);
34703471
}
34713472

34723473
memcpy((void *) &newentry, oldentry, sizeof(phar_entry_info));
34733474

34743475
phar_metadata_tracker_clone(&newentry.metadata_tracker);
34753476

3476-
newentry.filename = estrndup(newfile, newfile_len);
3477-
newentry.filename_len = newfile_len;
3477+
newentry.filename = estrndup(tmp_new_file, tmp_len);
3478+
newentry.filename_len = tmp_len;
34783479
newentry.fp_refcount = 0;
34793480

34803481
if (oldentry->fp_type != PHAR_FP) {
@@ -3487,7 +3488,7 @@ PHP_METHOD(Phar, copy)
34873488
}
34883489
}
34893490

3490-
zend_hash_str_add_mem(&oldentry->phar->manifest, newfile, newfile_len, &newentry, sizeof(phar_entry_info));
3491+
zend_hash_str_add_mem(&oldentry->phar->manifest, ZSTR_VAL(new_file), tmp_len, &newentry, sizeof(phar_entry_info));
34913492
phar_obj->archive->is_modified = 1;
34923493
phar_flush(phar_obj->archive, 0, 0, 0, &error);
34933494

@@ -3503,31 +3504,30 @@ PHP_METHOD(Phar, copy)
35033504
/* {{{ determines whether a file exists in the phar */
35043505
PHP_METHOD(Phar, offsetExists)
35053506
{
3506-
char *fname;
3507-
size_t fname_len;
3507+
zend_string *file_name;
35083508
phar_entry_info *entry;
35093509

3510-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "p", &fname, &fname_len) == FAILURE) {
3510+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "P", &file_name) == FAILURE) {
35113511
RETURN_THROWS();
35123512
}
35133513

35143514
PHAR_ARCHIVE_OBJECT();
35153515

3516-
if (zend_hash_str_exists(&phar_obj->archive->manifest, fname, fname_len)) {
3517-
if (NULL != (entry = zend_hash_str_find_ptr(&phar_obj->archive->manifest, fname, fname_len))) {
3516+
if (zend_hash_exists(&phar_obj->archive->manifest, file_name)) {
3517+
if (NULL != (entry = zend_hash_find_ptr(&phar_obj->archive->manifest, file_name))) {
35183518
if (entry->is_deleted) {
35193519
/* entry is deleted, but has not been flushed to disk yet */
35203520
RETURN_FALSE;
35213521
}
35223522
}
35233523

3524-
if (fname_len >= sizeof(".phar")-1 && !memcmp(fname, ".phar", sizeof(".phar")-1)) {
3524+
if (ZSTR_LEN(file_name) >= sizeof(".phar")-1 && !memcmp(ZSTR_VAL(file_name), ".phar", sizeof(".phar")-1)) {
35253525
/* none of these are real files, so they don't exist */
35263526
RETURN_FALSE;
35273527
}
35283528
RETURN_TRUE;
35293529
} else {
3530-
RETURN_BOOL(zend_hash_str_exists(&phar_obj->archive->virtual_dirs, fname, fname_len));
3530+
RETURN_BOOL(zend_hash_exists(&phar_obj->archive->virtual_dirs, file_name));
35313531
}
35323532
}
35333533
/* }}} */
@@ -3754,11 +3754,11 @@ PHP_METHOD(Phar, offsetSet)
37543754
/* {{{ remove a file from a phar */
37553755
PHP_METHOD(Phar, offsetUnset)
37563756
{
3757-
char *fname, *error;
3758-
size_t fname_len;
3757+
char *error;
3758+
zend_string *file_name;
37593759
phar_entry_info *entry;
37603760

3761-
if (zend_parse_parameters(ZEND_NUM_ARGS(), "p", &fname, &fname_len) == FAILURE) {
3761+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "P", &file_name) == FAILURE) {
37623762
RETURN_THROWS();
37633763
}
37643764

@@ -3769,8 +3769,8 @@ PHP_METHOD(Phar, offsetUnset)
37693769
RETURN_THROWS();
37703770
}
37713771

3772-
if (zend_hash_str_exists(&phar_obj->archive->manifest, fname, fname_len)) {
3773-
if (NULL != (entry = zend_hash_str_find_ptr(&phar_obj->archive->manifest, fname, fname_len))) {
3772+
if (zend_hash_exists(&phar_obj->archive->manifest, file_name)) {
3773+
if (NULL != (entry = zend_hash_find_ptr(&phar_obj->archive->manifest, file_name))) {
37743774
if (entry->is_deleted) {
37753775
/* entry is deleted, but has not been flushed to disk yet */
37763776
return;
@@ -3782,7 +3782,7 @@ PHP_METHOD(Phar, offsetUnset)
37823782
RETURN_THROWS();
37833783
}
37843784
/* re-populate entry after copy on write */
3785-
entry = zend_hash_str_find_ptr(&phar_obj->archive->manifest, fname, fname_len);
3785+
entry = zend_hash_find_ptr(&phar_obj->archive->manifest, file_name);
37863786
}
37873787
entry->is_modified = 0;
37883788
entry->is_deleted = 1;

0 commit comments

Comments
 (0)