Skip to content

Commit 548e061

Browse files
authored
FPM: refactor fpm_php_get_string_from_table() to better match usage (#11051)
Pass the key length to improve the existence check for the key
1 parent a94fe87 commit 548e061

File tree

3 files changed

+17
-16
lines changed

3 files changed

+17
-16
lines changed

sapi/fpm/fpm/fpm_php.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -252,13 +252,13 @@ int fpm_php_limit_extensions(char *path) /* {{{ */
252252
}
253253
/* }}} */
254254

255-
char* fpm_php_get_string_from_table(zend_string *table, char *key) /* {{{ */
255+
bool fpm_php_is_key_in_table(zend_string *table, const char *key, size_t key_len) /* {{{ */
256256
{
257-
zval *data, *tmp;
257+
zval *data;
258258
zend_string *str;
259-
if (!table || !key) {
260-
return NULL;
261-
}
259+
260+
ZEND_ASSERT(table);
261+
ZEND_ASSERT(key);
262262

263263
/* inspired from ext/standard/info.c */
264264

@@ -270,12 +270,12 @@ char* fpm_php_get_string_from_table(zend_string *table, char *key) /* {{{ */
270270
return NULL;
271271
}
272272

273-
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(data), str, tmp) {
274-
if (str && !strncmp(ZSTR_VAL(str), key, ZSTR_LEN(str))) {
275-
return Z_STRVAL_P(tmp);
273+
ZEND_HASH_FOREACH_STR_KEY(Z_ARRVAL_P(data), str) {
274+
if (str && zend_string_equals_cstr(str, key, key_len)) {
275+
return true;
276276
}
277277
} ZEND_HASH_FOREACH_END();
278278

279-
return NULL;
279+
return false;
280280
}
281281
/* }}} */

sapi/fpm/fpm/fpm_php.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,6 @@ void fpm_php_soft_quit(void);
4141
int fpm_php_init_main(void);
4242
int fpm_php_apply_defines_ex(struct key_value_s *kv, int mode);
4343
int fpm_php_limit_extensions(char *path);
44-
char* fpm_php_get_string_from_table(zend_string *table, char *key);
44+
bool fpm_php_is_key_in_table(zend_string *table, const char *key, size_t key_len);
4545

4646
#endif

sapi/fpm/fpm/fpm_status.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ int fpm_status_handle_request(void) /* {{{ */
145145
bool encode_html, encode_json;
146146
char *short_syntax, *short_post;
147147
char *full_pre, *full_syntax, *full_post, *full_separator;
148-
zend_string *_GET_str;
149148

150149
if (!SG(request_info).request_uri) {
151150
return 0;
@@ -170,11 +169,13 @@ int fpm_status_handle_request(void) /* {{{ */
170169

171170
/* STATUS */
172171
if (fpm_status_uri && !strcmp(fpm_status_uri, SG(request_info).request_uri)) {
172+
zend_string *_GET_str;
173+
173174
fpm_request_executing();
174175

175176
/* full status ? */
176177
_GET_str = ZSTR_INIT_LITERAL("_GET", 0);
177-
full = (fpm_php_get_string_from_table(_GET_str, "full") != NULL);
178+
full = fpm_php_is_key_in_table(_GET_str, ZEND_STRL("full"));
178179
short_syntax = short_post = NULL;
179180
full_separator = full_pre = full_syntax = full_post = NULL;
180181
encode_html = false;
@@ -218,7 +219,7 @@ int fpm_status_handle_request(void) /* {{{ */
218219
}
219220

220221
/* HTML */
221-
if (fpm_php_get_string_from_table(_GET_str, "html")) {
222+
if (fpm_php_is_key_in_table(_GET_str, ZEND_STRL("html"))) {
222223
sapi_add_header_ex(ZEND_STRL("Content-Type: text/html"), 1, 1);
223224
time_format = "%d/%b/%Y:%H:%M:%S %z";
224225
encode_html = true;
@@ -287,7 +288,7 @@ int fpm_status_handle_request(void) /* {{{ */
287288
}
288289

289290
/* XML */
290-
} else if (fpm_php_get_string_from_table(_GET_str, "xml")) {
291+
} else if (fpm_php_is_key_in_table(_GET_str, ZEND_STRL("xml"))) {
291292
sapi_add_header_ex(ZEND_STRL("Content-Type: text/xml"), 1, 1);
292293
time_format = "%s";
293294
encode_html = true;
@@ -335,7 +336,7 @@ int fpm_status_handle_request(void) /* {{{ */
335336
}
336337

337338
/* JSON */
338-
} else if (fpm_php_get_string_from_table(_GET_str, "json")) {
339+
} else if (fpm_php_is_key_in_table(_GET_str, ZEND_STRL("json"))) {
339340
sapi_add_header_ex(ZEND_STRL("Content-Type: application/json"), 1, 1);
340341
time_format = "%s";
341342

@@ -384,7 +385,7 @@ int fpm_status_handle_request(void) /* {{{ */
384385
}
385386

386387
/* OpenMetrics */
387-
} else if (fpm_php_get_string_from_table(_GET_str, "openmetrics")) {
388+
} else if (fpm_php_is_key_in_table(_GET_str, ZEND_STRL("openmetrics"))) {
388389
sapi_add_header_ex(ZEND_STRL("Content-Type: application/openmetrics-text; version=1.0.0; charset=utf-8"), 1, 1);
389390
time_format = "%s";
390391

0 commit comments

Comments
 (0)