From 35ed40b031cbf14c772cd78f4ebf269b3ca25597 Mon Sep 17 00:00:00 2001 From: George Peter Banyard Date: Mon, 10 Apr 2023 14:40:41 +0100 Subject: [PATCH 1/2] FPM: refactor fpm_php_get_string_from_table() to better match usage Pass in length of the key to improve the existance of the key check --- sapi/fpm/fpm/fpm_php.c | 22 ++++++++++------------ sapi/fpm/fpm/fpm_php.h | 3 ++- sapi/fpm/fpm/fpm_status.c | 10 +++++----- 3 files changed, 17 insertions(+), 18 deletions(-) diff --git a/sapi/fpm/fpm/fpm_php.c b/sapi/fpm/fpm/fpm_php.c index 92b189668206e..aa8ab0d77fa48 100644 --- a/sapi/fpm/fpm/fpm_php.c +++ b/sapi/fpm/fpm/fpm_php.c @@ -252,13 +252,13 @@ int fpm_php_limit_extensions(char *path) /* {{{ */ } /* }}} */ -char* fpm_php_get_string_from_table(zend_string *table, char *key) /* {{{ */ +bool fpm_php_is_key_in_table(zend_string *table, char *key, size_t key_len) /* {{{ */ { - zval *data, *tmp; + zval *data; zend_string *str; - if (!table || !key) { - return NULL; - } + + ZEND_ASSERT(table); + ZEND_ASSERT(key); /* inspired from ext/standard/info.c */ @@ -266,16 +266,14 @@ char* fpm_php_get_string_from_table(zend_string *table, char *key) /* {{{ */ /* find the table and ensure it's an array */ data = zend_hash_find(&EG(symbol_table), table); - if (!data || Z_TYPE_P(data) != IS_ARRAY) { - return NULL; - } + ZEND_ASSERT(data && Z_TYPE_P(data) == IS_ARRAY); - ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(data), str, tmp) { - if (str && !strncmp(ZSTR_VAL(str), key, ZSTR_LEN(str))) { - return Z_STRVAL_P(tmp); + ZEND_HASH_FOREACH_STR_KEY(Z_ARRVAL_P(data), str) { + if (str && zend_string_equals_cstr(str, key, key_len)) { + return true; } } ZEND_HASH_FOREACH_END(); - return NULL; + return false; } /* }}} */ diff --git a/sapi/fpm/fpm/fpm_php.h b/sapi/fpm/fpm/fpm_php.h index 01ed65799170d..5f1aaa9d7d933 100644 --- a/sapi/fpm/fpm/fpm_php.h +++ b/sapi/fpm/fpm/fpm_php.h @@ -3,6 +3,7 @@ #ifndef FPM_PHP_H #define FPM_PHP_H 1 +#include #include #include "php.h" @@ -41,6 +42,6 @@ void fpm_php_soft_quit(void); int fpm_php_init_main(void); int fpm_php_apply_defines_ex(struct key_value_s *kv, int mode); int fpm_php_limit_extensions(char *path); -char* fpm_php_get_string_from_table(zend_string *table, char *key); +bool fpm_php_is_key_in_table(zend_string *table, char *key, size_t key_len); #endif diff --git a/sapi/fpm/fpm/fpm_status.c b/sapi/fpm/fpm/fpm_status.c index e78cbeab1105a..72c7ebb9093ba 100644 --- a/sapi/fpm/fpm/fpm_status.c +++ b/sapi/fpm/fpm/fpm_status.c @@ -172,7 +172,7 @@ int fpm_status_handle_request(void) /* {{{ */ /* full status ? */ _GET_str = ZSTR_INIT_LITERAL("_GET", 0); - full = (fpm_php_get_string_from_table(_GET_str, "full") != NULL); + full = fpm_php_is_key_in_table(_GET_str, "full", strlen("full")); short_syntax = short_post = NULL; full_separator = full_pre = full_syntax = full_post = NULL; encode = 0; @@ -215,7 +215,7 @@ int fpm_status_handle_request(void) /* {{{ */ } /* HTML */ - if (fpm_php_get_string_from_table(_GET_str, "html")) { + if (fpm_php_is_key_in_table(_GET_str, "html", strlen("html"))) { sapi_add_header_ex(ZEND_STRL("Content-Type: text/html"), 1, 1); time_format = "%d/%b/%Y:%H:%M:%S %z"; encode = 1; @@ -284,7 +284,7 @@ int fpm_status_handle_request(void) /* {{{ */ } /* XML */ - } else if (fpm_php_get_string_from_table(_GET_str, "xml")) { + } else if (fpm_php_is_key_in_table(_GET_str, "xml", strlen("xml"))) { sapi_add_header_ex(ZEND_STRL("Content-Type: text/xml"), 1, 1); time_format = "%s"; encode = 1; @@ -332,7 +332,7 @@ int fpm_status_handle_request(void) /* {{{ */ } /* JSON */ - } else if (fpm_php_get_string_from_table(_GET_str, "json")) { + } else if (fpm_php_is_key_in_table(_GET_str, "json", strlen("json"))) { sapi_add_header_ex(ZEND_STRL("Content-Type: application/json"), 1, 1); time_format = "%s"; @@ -379,7 +379,7 @@ int fpm_status_handle_request(void) /* {{{ */ } /* OpenMetrics */ - } else if (fpm_php_get_string_from_table(_GET_str, "openmetrics")) { + } else if (fpm_php_is_key_in_table(_GET_str, "openmetrics", strlen("openmetrics"))) { sapi_add_header_ex(ZEND_STRL("Content-Type: application/openmetrics-text; version=1.0.0; charset=utf-8"), 1, 1); time_format = "%s"; From 336037865ace5e3e5f9829fd9dad0399189c415f Mon Sep 17 00:00:00 2001 From: George Peter Banyard Date: Tue, 11 Apr 2023 11:43:11 +0100 Subject: [PATCH 2/2] Address review --- sapi/fpm/fpm/fpm_php.c | 6 ++++-- sapi/fpm/fpm/fpm_php.h | 3 +-- sapi/fpm/fpm/fpm_status.c | 13 +++++++------ 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/sapi/fpm/fpm/fpm_php.c b/sapi/fpm/fpm/fpm_php.c index aa8ab0d77fa48..aadaafbb05630 100644 --- a/sapi/fpm/fpm/fpm_php.c +++ b/sapi/fpm/fpm/fpm_php.c @@ -252,7 +252,7 @@ int fpm_php_limit_extensions(char *path) /* {{{ */ } /* }}} */ -bool fpm_php_is_key_in_table(zend_string *table, char *key, size_t key_len) /* {{{ */ +bool fpm_php_is_key_in_table(zend_string *table, const char *key, size_t key_len) /* {{{ */ { zval *data; zend_string *str; @@ -266,7 +266,9 @@ bool fpm_php_is_key_in_table(zend_string *table, char *key, size_t key_len) /* { /* find the table and ensure it's an array */ data = zend_hash_find(&EG(symbol_table), table); - ZEND_ASSERT(data && Z_TYPE_P(data) == IS_ARRAY); + if (!data || Z_TYPE_P(data) != IS_ARRAY) { + return NULL; + } ZEND_HASH_FOREACH_STR_KEY(Z_ARRVAL_P(data), str) { if (str && zend_string_equals_cstr(str, key, key_len)) { diff --git a/sapi/fpm/fpm/fpm_php.h b/sapi/fpm/fpm/fpm_php.h index 5f1aaa9d7d933..d61857c5e0e11 100644 --- a/sapi/fpm/fpm/fpm_php.h +++ b/sapi/fpm/fpm/fpm_php.h @@ -3,7 +3,6 @@ #ifndef FPM_PHP_H #define FPM_PHP_H 1 -#include #include #include "php.h" @@ -42,6 +41,6 @@ void fpm_php_soft_quit(void); int fpm_php_init_main(void); int fpm_php_apply_defines_ex(struct key_value_s *kv, int mode); int fpm_php_limit_extensions(char *path); -bool fpm_php_is_key_in_table(zend_string *table, char *key, size_t key_len); +bool fpm_php_is_key_in_table(zend_string *table, const char *key, size_t key_len); #endif diff --git a/sapi/fpm/fpm/fpm_status.c b/sapi/fpm/fpm/fpm_status.c index 72c7ebb9093ba..f52be9bb57518 100644 --- a/sapi/fpm/fpm/fpm_status.c +++ b/sapi/fpm/fpm/fpm_status.c @@ -143,7 +143,6 @@ int fpm_status_handle_request(void) /* {{{ */ int full, encode, has_start_time; char *short_syntax, *short_post; char *full_pre, *full_syntax, *full_post, *full_separator; - zend_string *_GET_str; if (!SG(request_info).request_uri) { return 0; @@ -168,11 +167,13 @@ int fpm_status_handle_request(void) /* {{{ */ /* STATUS */ if (fpm_status_uri && !strcmp(fpm_status_uri, SG(request_info).request_uri)) { + zend_string *_GET_str; + fpm_request_executing(); /* full status ? */ _GET_str = ZSTR_INIT_LITERAL("_GET", 0); - full = fpm_php_is_key_in_table(_GET_str, "full", strlen("full")); + full = fpm_php_is_key_in_table(_GET_str, ZEND_STRL("full")); short_syntax = short_post = NULL; full_separator = full_pre = full_syntax = full_post = NULL; encode = 0; @@ -215,7 +216,7 @@ int fpm_status_handle_request(void) /* {{{ */ } /* HTML */ - if (fpm_php_is_key_in_table(_GET_str, "html", strlen("html"))) { + if (fpm_php_is_key_in_table(_GET_str, ZEND_STRL("html"))) { sapi_add_header_ex(ZEND_STRL("Content-Type: text/html"), 1, 1); time_format = "%d/%b/%Y:%H:%M:%S %z"; encode = 1; @@ -284,7 +285,7 @@ int fpm_status_handle_request(void) /* {{{ */ } /* XML */ - } else if (fpm_php_is_key_in_table(_GET_str, "xml", strlen("xml"))) { + } else if (fpm_php_is_key_in_table(_GET_str, ZEND_STRL("xml"))) { sapi_add_header_ex(ZEND_STRL("Content-Type: text/xml"), 1, 1); time_format = "%s"; encode = 1; @@ -332,7 +333,7 @@ int fpm_status_handle_request(void) /* {{{ */ } /* JSON */ - } else if (fpm_php_is_key_in_table(_GET_str, "json", strlen("json"))) { + } else if (fpm_php_is_key_in_table(_GET_str, ZEND_STRL("json"))) { sapi_add_header_ex(ZEND_STRL("Content-Type: application/json"), 1, 1); time_format = "%s"; @@ -379,7 +380,7 @@ int fpm_status_handle_request(void) /* {{{ */ } /* OpenMetrics */ - } else if (fpm_php_is_key_in_table(_GET_str, "openmetrics", strlen("openmetrics"))) { + } else if (fpm_php_is_key_in_table(_GET_str, ZEND_STRL("openmetrics"))) { sapi_add_header_ex(ZEND_STRL("Content-Type: application/openmetrics-text; version=1.0.0; charset=utf-8"), 1, 1); time_format = "%s";