Skip to content

Commit a66b631

Browse files
authored
main/php_ini: various minor refactorings (#19339)
1 parent 961412d commit a66b631

File tree

3 files changed

+62
-70
lines changed

3 files changed

+62
-70
lines changed

main/main.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2234,9 +2234,7 @@ zend_result php_module_startup(sapi_module_struct *sf, zend_module_entry *additi
22342234
load zend extensions and register php function extensions
22352235
to be loaded later */
22362236
zend_stream_init();
2237-
if (php_init_config() == FAILURE) {
2238-
return FAILURE;
2239-
}
2237+
php_init_config();
22402238
zend_stream_shutdown();
22412239

22422240
/* Register PHP core ini entries */

main/php_ini.c

Lines changed: 52 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#include "php_ini.h"
2222
#include "ext/standard/dl.h"
2323
#include "zend_extensions.h"
24-
#include "zend_highlight.h"
2524
#include "SAPI.h"
2625
#include "php_main.h"
2726
#include "php_scandir.h"
@@ -49,17 +48,17 @@
4948
#endif
5049

5150

52-
typedef struct _php_extension_lists {
51+
typedef struct php_extension_lists {
5352
zend_llist engine;
5453
zend_llist functions;
5554
} php_extension_lists;
5655

5756
/* True globals */
58-
static int is_special_section = 0;
57+
static bool is_special_section = false;
5958
static HashTable *active_ini_hash;
6059
static HashTable configuration_hash;
61-
static int has_per_dir_config = 0;
62-
static int has_per_host_config = 0;
60+
static bool has_per_dir_config = false;
61+
static bool has_per_host_config = false;
6362
PHPAPI char *php_ini_opened_path=NULL;
6463
static php_extension_lists extension_lists;
6564
PHPAPI char *php_ini_scanned_path=NULL;
@@ -175,7 +174,7 @@ PHPAPI void config_zval_dtor(zval *zvalue)
175174
/* Reset / free active_ini_section global */
176175
#define RESET_ACTIVE_INI_HASH() do { \
177176
active_ini_hash = NULL; \
178-
is_special_section = 0; \
177+
is_special_section = false; \
179178
} while (0)
180179
/* }}} */
181180

@@ -211,7 +210,7 @@ static void php_ini_parser_cb(zval *arg1, zval *arg2, zval *arg3, int callback_t
211210
} else {
212211
/* Store in active hash */
213212
entry = zend_hash_update(active_hash, Z_STR_P(arg1), arg2);
214-
Z_STR_P(entry) = zend_string_dup(Z_STR_P(entry), 1);
213+
Z_STR_P(entry) = zend_string_dup(Z_STR_P(entry), true);
215214
}
216215
}
217216
break;
@@ -230,7 +229,7 @@ static void php_ini_parser_cb(zval *arg1, zval *arg2, zval *arg3, int callback_t
230229
/* If option not found in hash or is not an array -> create array, otherwise add to existing array */
231230
if ((find_arr = zend_hash_find(active_hash, Z_STR_P(arg1))) == NULL || Z_TYPE_P(find_arr) != IS_ARRAY) {
232231
ZVAL_NEW_PERSISTENT_ARR(&option_arr);
233-
zend_hash_init(Z_ARRVAL(option_arr), 8, NULL, config_zval_dtor, 1);
232+
zend_hash_init(Z_ARRVAL(option_arr), 8, NULL, config_zval_dtor, true);
234233
find_arr = zend_hash_update(active_hash, Z_STR_P(arg1), &option_arr);
235234
}
236235

@@ -240,7 +239,7 @@ static void php_ini_parser_cb(zval *arg1, zval *arg2, zval *arg3, int callback_t
240239
} else {
241240
entry = zend_hash_next_index_insert(Z_ARRVAL_P(find_arr), arg2);
242241
}
243-
Z_STR_P(entry) = zend_string_dup(Z_STR_P(entry), 1);
242+
Z_STR_P(entry) = zend_string_dup(Z_STR_P(entry), true);
244243
}
245244
break;
246245

@@ -252,27 +251,27 @@ static void php_ini_parser_cb(zval *arg1, zval *arg2, zval *arg3, int callback_t
252251
size_t key_len;
253252

254253
/* PATH sections */
255-
if (!zend_binary_strncasecmp(Z_STRVAL_P(arg1), Z_STRLEN_P(arg1), "PATH", sizeof("PATH") - 1, sizeof("PATH") - 1)) {
254+
if (zend_string_starts_with_literal_ci(Z_STR_P(arg1), "PATH")) {
256255
key = Z_STRVAL_P(arg1);
257256
key = key + sizeof("PATH") - 1;
258257
key_len = Z_STRLEN_P(arg1) - sizeof("PATH") + 1;
259-
is_special_section = 1;
260-
has_per_dir_config = 1;
258+
is_special_section = true;
259+
has_per_dir_config = true;
261260

262261
/* make the path lowercase on Windows, for case insensitivity. Does nothing for other platforms */
263262
TRANSLATE_SLASHES_LOWER(key);
264263

265264
/* HOST sections */
266-
} else if (!zend_binary_strncasecmp(Z_STRVAL_P(arg1), Z_STRLEN_P(arg1), "HOST", sizeof("HOST") - 1, sizeof("HOST") - 1)) {
265+
} else if (zend_string_starts_with_literal_ci(Z_STR_P(arg1), "HOST")) {
267266
key = Z_STRVAL_P(arg1);
268267
key = key + sizeof("HOST") - 1;
269268
key_len = Z_STRLEN_P(arg1) - sizeof("HOST") + 1;
270-
is_special_section = 1;
271-
has_per_host_config = 1;
269+
is_special_section = true;
270+
has_per_host_config = true;
272271
zend_str_tolower(key, key_len); /* host names are case-insensitive. */
273272

274273
} else {
275-
is_special_section = 0;
274+
is_special_section = false;
276275
}
277276

278277
if (key && key_len > 0) {
@@ -297,7 +296,7 @@ static void php_ini_parser_cb(zval *arg1, zval *arg2, zval *arg3, int callback_t
297296
zval section_arr;
298297

299298
ZVAL_NEW_PERSISTENT_ARR(&section_arr);
300-
zend_hash_init(Z_ARRVAL(section_arr), 8, NULL, (dtor_func_t) config_zval_dtor, 1);
299+
zend_hash_init(Z_ARRVAL(section_arr), 8, NULL, (dtor_func_t) config_zval_dtor, true);
301300
entry = zend_hash_str_update(target_hash, key, key_len, &section_arr);
302301
}
303302
if (Z_TYPE_P(entry) == IS_ARRAY) {
@@ -407,30 +406,30 @@ static void append_ini_path(char *php_ini_search_path, size_t search_path_size,
407406
}
408407

409408
/* {{{ php_init_config */
410-
int php_init_config(void)
409+
void php_init_config(void)
411410
{
412411
char *php_ini_file_name = NULL;
413412
char *php_ini_search_path = NULL;
414-
int php_ini_scanned_path_len;
413+
size_t php_ini_scanned_path_len;
415414
char *open_basedir;
416-
int free_ini_search_path = 0;
415+
bool free_ini_search_path = false;
417416
zend_string *opened_path = NULL;
418417

419-
zend_hash_init(&configuration_hash, 8, NULL, config_zval_dtor, 1);
418+
zend_hash_init(&configuration_hash, 8, NULL, config_zval_dtor, true);
420419

421420
if (sapi_module.ini_defaults) {
422421
sapi_module.ini_defaults(&configuration_hash);
423422
}
424423

425-
zend_llist_init(&extension_lists.engine, sizeof(char *), (llist_dtor_func_t) free_estring, 1);
426-
zend_llist_init(&extension_lists.functions, sizeof(char *), (llist_dtor_func_t) free_estring, 1);
424+
zend_llist_init(&extension_lists.engine, sizeof(char *), (llist_dtor_func_t) free_estring, true);
425+
zend_llist_init(&extension_lists.functions, sizeof(char *), (llist_dtor_func_t) free_estring, true);
427426

428427
open_basedir = PG(open_basedir);
429428

430429
if (sapi_module.php_ini_path_override) {
431430
php_ini_file_name = sapi_module.php_ini_path_override;
432431
php_ini_search_path = sapi_module.php_ini_path_override;
433-
free_ini_search_path = 0;
432+
free_ini_search_path = false;
434433
} else if (!sapi_module.php_ini_ignore) {
435434
size_t search_path_size;
436435
char *default_location;
@@ -480,7 +479,7 @@ int php_init_config(void)
480479

481480
search_path_size = MAXPATHLEN * 4 + strlen(env_location) + 3 + 1;
482481
php_ini_search_path = (char *) emalloc(search_path_size);
483-
free_ini_search_path = 1;
482+
free_ini_search_path = true;
484483
php_ini_search_path[0] = 0;
485484

486485
/* Add environment ___location */
@@ -601,15 +600,15 @@ int php_init_config(void)
601600
zend_stream_init_fp(&fh, fp, filename);
602601
RESET_ACTIVE_INI_HASH();
603602

604-
zend_parse_ini_file(&fh, 1, ZEND_INI_SCANNER_NORMAL, (zend_ini_parser_cb_t) php_ini_parser_cb, &configuration_hash);
603+
zend_parse_ini_file(&fh, true, ZEND_INI_SCANNER_NORMAL, (zend_ini_parser_cb_t) php_ini_parser_cb, &configuration_hash);
605604

606605
{
607606
zval tmp;
608607

609-
ZVAL_NEW_STR(&tmp, zend_string_init(filename, strlen(filename), 1));
608+
ZVAL_NEW_STR(&tmp, zend_string_init(filename, strlen(filename), true));
610609
zend_hash_str_update(&configuration_hash, "cfg_file_path", sizeof("cfg_file_path")-1, &tmp);
611610
if (opened_path) {
612-
zend_string_release_ex(opened_path, 0);
611+
zend_string_release_ex(opened_path, false);
613612
}
614613
php_ini_opened_path = zend_strndup(Z_STRVAL(tmp), Z_STRLEN(tmp));
615614
}
@@ -626,22 +625,20 @@ int php_init_config(void)
626625
/* Or fall back using possible --with-config-file-scan-dir setting (defaults to empty string!) */
627626
php_ini_scanned_path = PHP_CONFIG_FILE_SCAN_DIR;
628627
}
629-
php_ini_scanned_path_len = (int)strlen(php_ini_scanned_path);
628+
php_ini_scanned_path_len = strlen(php_ini_scanned_path);
630629

631630
/* Scan and parse any .ini files found in scan path if path not empty. */
632631
if (!sapi_module.php_ini_ignore && php_ini_scanned_path_len) {
633632
struct dirent **namelist;
634-
int ndir, i;
635633
zend_stat_t sb = {0};
636634
char ini_file[MAXPATHLEN];
637635
char *p;
638636
zend_llist scanned_ini_list;
639-
zend_llist_element *element;
640-
int l, total_l = 0;
637+
size_t total_l = 0;
641638
char *bufpath, *debpath, *endpath;
642-
int lenpath;
639+
size_t lenpath;
643640

644-
zend_llist_init(&scanned_ini_list, sizeof(char *), (llist_dtor_func_t) free_estring, 1);
641+
zend_llist_init(&scanned_ini_list, sizeof(char *), (llist_dtor_func_t) free_estring, true);
645642

646643
bufpath = estrdup(php_ini_scanned_path);
647644
for (debpath = bufpath ; debpath ; debpath=endpath) {
@@ -654,11 +651,11 @@ int php_init_config(void)
654651
to allow "/foo/php.d:" or ":/foo/php.d" */
655652
debpath = PHP_CONFIG_FILE_SCAN_DIR;
656653
}
657-
lenpath = (int)strlen(debpath);
654+
lenpath = strlen(debpath);
658655

659-
if (lenpath > 0 && (ndir = php_scandir(debpath, &namelist, 0, php_alphasort)) > 0) {
660-
661-
for (i = 0; i < ndir; i++) {
656+
int ndir;
657+
if (lenpath > 0 && (ndir = php_scandir(debpath, &namelist, NULL, php_alphasort)) > 0) {
658+
for (int i = 0; i < ndir; i++) {
662659

663660
/* check for any file with .ini extension */
664661
if (!(p = strrchr(namelist[i]->d_name, '.')) || (p && strcmp(p, ".ini"))) {
@@ -679,9 +676,9 @@ int php_init_config(void)
679676
FILE *file = VCWD_FOPEN(ini_file, "r");
680677
if (file) {
681678
zend_stream_init_fp(&fh, file, ini_file);
682-
if (zend_parse_ini_file(&fh, 1, ZEND_INI_SCANNER_NORMAL, (zend_ini_parser_cb_t) php_ini_parser_cb, &configuration_hash) == SUCCESS) {
679+
if (zend_parse_ini_file(&fh, true, ZEND_INI_SCANNER_NORMAL, (zend_ini_parser_cb_t) php_ini_parser_cb, &configuration_hash) == SUCCESS) {
683680
/* Here, add it to the list of ini files read */
684-
l = (int)strlen(ini_file);
681+
size_t l = strlen(ini_file);
685682
total_l += l + 2;
686683
p = estrndup(ini_file, l);
687684
zend_llist_add_element(&scanned_ini_list, &p);
@@ -698,13 +695,13 @@ int php_init_config(void)
698695
efree(bufpath);
699696

700697
if (total_l) {
701-
int php_ini_scanned_files_len = (php_ini_scanned_files) ? (int)strlen(php_ini_scanned_files) + 1 : 0;
698+
size_t php_ini_scanned_files_len = (php_ini_scanned_files) ? strlen(php_ini_scanned_files) + 1 : 0;
702699
php_ini_scanned_files = (char *) realloc(php_ini_scanned_files, php_ini_scanned_files_len + total_l + 1);
703700
if (!php_ini_scanned_files_len) {
704701
*php_ini_scanned_files = '\0';
705702
}
706703
total_l += php_ini_scanned_files_len;
707-
for (element = scanned_ini_list.head; element; element = element->next) {
704+
for (zend_llist_element *element = scanned_ini_list.head; element; element = element->next) {
708705
if (php_ini_scanned_files_len) {
709706
strlcat(php_ini_scanned_files, ",\n", total_l);
710707
}
@@ -721,15 +718,13 @@ int php_init_config(void)
721718
if (sapi_module.ini_entries) {
722719
/* Reset active ini section */
723720
RESET_ACTIVE_INI_HASH();
724-
zend_parse_ini_string(sapi_module.ini_entries, 1, ZEND_INI_SCANNER_NORMAL, (zend_ini_parser_cb_t) php_ini_parser_cb, &configuration_hash);
721+
zend_parse_ini_string(sapi_module.ini_entries, true, ZEND_INI_SCANNER_NORMAL, (zend_ini_parser_cb_t) php_ini_parser_cb, &configuration_hash);
725722
}
726-
727-
return SUCCESS;
728723
}
729724
/* }}} */
730725

731726
/* {{{ php_shutdown_config */
732-
int php_shutdown_config(void)
727+
void php_shutdown_config(void)
733728
{
734729
zend_hash_destroy(&configuration_hash);
735730
if (php_ini_opened_path) {
@@ -740,7 +735,6 @@ int php_shutdown_config(void)
740735
free(php_ini_scanned_files);
741736
php_ini_scanned_files = NULL;
742737
}
743-
return SUCCESS;
744738
}
745739
/* }}} */
746740

@@ -756,7 +750,7 @@ void php_ini_register_extensions(void)
756750
/* }}} */
757751

758752
/* {{{ php_parse_user_ini_file */
759-
PHPAPI int php_parse_user_ini_file(const char *dirname, const char *ini_filename, HashTable *target_hash)
753+
PHPAPI zend_result php_parse_user_ini_file(const char *dirname, const char *ini_filename, HashTable *target_hash)
760754
{
761755
zend_stat_t sb = {0};
762756
char ini_file[MAXPATHLEN];
@@ -766,7 +760,7 @@ PHPAPI int php_parse_user_ini_file(const char *dirname, const char *ini_filename
766760
if (VCWD_STAT(ini_file, &sb) == 0) {
767761
if (S_ISREG(sb.st_mode)) {
768762
zend_file_handle fh;
769-
int ret = FAILURE;
763+
zend_result ret = FAILURE;
770764

771765
zend_stream_init_fp(&fh, VCWD_FOPEN(ini_file, "r"), ini_file);
772766
if (fh.handle.fp) {
@@ -780,7 +774,7 @@ PHPAPI int php_parse_user_ini_file(const char *dirname, const char *ini_filename
780774
bool orig_rc_debug = zend_rc_debug;
781775
zend_rc_debug = false;
782776
#endif
783-
ret = zend_parse_ini_file(&fh, 1, ZEND_INI_SCANNER_NORMAL, (zend_ini_parser_cb_t) php_ini_parser_cb, target_hash);
777+
ret = zend_parse_ini_file(&fh, true, ZEND_INI_SCANNER_NORMAL, (zend_ini_parser_cb_t) php_ini_parser_cb, target_hash);
784778
#if ZEND_RC_DEBUG
785779
zend_rc_debug = orig_rc_debug;
786780
#endif
@@ -797,22 +791,22 @@ PHPAPI int php_parse_user_ini_file(const char *dirname, const char *ini_filename
797791
/* }}} */
798792

799793
/* {{{ php_ini_activate_config */
800-
PHPAPI void php_ini_activate_config(HashTable *source_hash, int modify_type, int stage)
794+
PHPAPI void php_ini_activate_config(const HashTable *source_hash, int modify_type, int stage)
801795
{
802796
zend_string *str;
803797
zval *data;
804798

805799
/* Walk through config hash and alter matching ini entries using the values found in the hash */
806800
ZEND_HASH_MAP_FOREACH_STR_KEY_VAL(source_hash, str, data) {
807-
zend_string *data_str = zend_string_dup(Z_STR_P(data), 0);
808-
zend_alter_ini_entry_ex(str, data_str, modify_type, stage, 0);
801+
zend_string *data_str = zend_string_dup(Z_STR_P(data), false);
802+
zend_alter_ini_entry_ex(str, data_str, modify_type, stage, false);
809803
zend_string_release(data_str);
810804
} ZEND_HASH_FOREACH_END();
811805
}
812806
/* }}} */
813807

814808
/* {{{ php_ini_has_per_dir_config */
815-
PHPAPI int php_ini_has_per_dir_config(void)
809+
PHPAPI bool php_ini_has_per_dir_config(void)
816810
{
817811
return has_per_dir_config;
818812
}
@@ -861,7 +855,7 @@ PHPAPI void php_ini_activate_per_dir_config(char *path, size_t path_len)
861855
/* }}} */
862856

863857
/* {{{ php_ini_has_per_host_config */
864-
PHPAPI int php_ini_has_per_host_config(void)
858+
PHPAPI bool php_ini_has_per_host_config(void)
865859
{
866860
return has_per_host_config;
867861
}
@@ -896,7 +890,7 @@ PHPAPI zval *cfg_get_entry(const char *name, size_t name_length)
896890
/* }}} */
897891

898892
/* {{{ cfg_get_long */
899-
PHPAPI int cfg_get_long(const char *varname, zend_long *result)
893+
PHPAPI zend_result cfg_get_long(const char *varname, zend_long *result)
900894
{
901895
zval *tmp;
902896

@@ -910,7 +904,7 @@ PHPAPI int cfg_get_long(const char *varname, zend_long *result)
910904
/* }}} */
911905

912906
/* {{{ cfg_get_double */
913-
PHPAPI int cfg_get_double(const char *varname, double *result)
907+
PHPAPI zend_result cfg_get_double(const char *varname, double *result)
914908
{
915909
zval *tmp;
916910

@@ -924,7 +918,7 @@ PHPAPI int cfg_get_double(const char *varname, double *result)
924918
/* }}} */
925919

926920
/* {{{ cfg_get_string */
927-
PHPAPI int cfg_get_string(const char *varname, char **result)
921+
PHPAPI zend_result cfg_get_string(const char *varname, char **result)
928922
{
929923
zval *tmp;
930924

0 commit comments

Comments
 (0)