Skip to content

Commit 3799dfa

Browse files
committed
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
1 parent 4082d42 commit 3799dfa

File tree

3 files changed

+17
-18
lines changed

3 files changed

+17
-18
lines changed

sapi/fpm/fpm/fpm_php.c

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -252,30 +252,28 @@ 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, 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

265265
zend_is_auto_global(table);
266266

267267
/* find the table and ensure it's an array */
268268
data = zend_hash_find(&EG(symbol_table), table);
269-
if (!data || Z_TYPE_P(data) != IS_ARRAY) {
270-
return NULL;
271-
}
269+
ZEND_ASSERT(data && Z_TYPE_P(data) == IS_ARRAY);
272270

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);
271+
ZEND_HASH_FOREACH_STR_KEY(Z_ARRVAL_P(data), str) {
272+
if (str && zend_string_equals_cstr(str, key, key_len)) {
273+
return true;
276274
}
277275
} ZEND_HASH_FOREACH_END();
278276

279-
return NULL;
277+
return false;
280278
}
281279
/* }}} */

sapi/fpm/fpm/fpm_php.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#ifndef FPM_PHP_H
44
#define FPM_PHP_H 1
55

6+
#include <stdbool.h>
67
#include <TSRM.h>
78

89
#include "php.h"
@@ -41,6 +42,6 @@ void fpm_php_soft_quit(void);
4142
int fpm_php_init_main(void);
4243
int fpm_php_apply_defines_ex(struct key_value_s *kv, int mode);
4344
int fpm_php_limit_extensions(char *path);
44-
char* fpm_php_get_string_from_table(zend_string *table, char *key);
45+
bool fpm_php_is_key_in_table(zend_string *table, char *key, size_t key_len);
4546

4647
#endif

sapi/fpm/fpm/fpm_status.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ int fpm_status_handle_request(void) /* {{{ */
172172

173173
/* full status ? */
174174
_GET_str = ZSTR_INIT_LITERAL("_GET", 0);
175-
full = (fpm_php_get_string_from_table(_GET_str, "full") != NULL);
175+
full = fpm_php_is_key_in_table(_GET_str, "full", strlen("full"));
176176
short_syntax = short_post = NULL;
177177
full_separator = full_pre = full_syntax = full_post = NULL;
178178
encode = 0;
@@ -215,7 +215,7 @@ int fpm_status_handle_request(void) /* {{{ */
215215
}
216216

217217
/* HTML */
218-
if (fpm_php_get_string_from_table(_GET_str, "html")) {
218+
if (fpm_php_is_key_in_table(_GET_str, "html", strlen("html"))) {
219219
sapi_add_header_ex(ZEND_STRL("Content-Type: text/html"), 1, 1);
220220
time_format = "%d/%b/%Y:%H:%M:%S %z";
221221
encode = 1;
@@ -284,7 +284,7 @@ int fpm_status_handle_request(void) /* {{{ */
284284
}
285285

286286
/* XML */
287-
} else if (fpm_php_get_string_from_table(_GET_str, "xml")) {
287+
} else if (fpm_php_is_key_in_table(_GET_str, "xml", strlen("xml"))) {
288288
sapi_add_header_ex(ZEND_STRL("Content-Type: text/xml"), 1, 1);
289289
time_format = "%s";
290290
encode = 1;
@@ -332,7 +332,7 @@ int fpm_status_handle_request(void) /* {{{ */
332332
}
333333

334334
/* JSON */
335-
} else if (fpm_php_get_string_from_table(_GET_str, "json")) {
335+
} else if (fpm_php_is_key_in_table(_GET_str, "json", strlen("json"))) {
336336
sapi_add_header_ex(ZEND_STRL("Content-Type: application/json"), 1, 1);
337337
time_format = "%s";
338338

@@ -379,7 +379,7 @@ int fpm_status_handle_request(void) /* {{{ */
379379
}
380380

381381
/* OpenMetrics */
382-
} else if (fpm_php_get_string_from_table(_GET_str, "openmetrics")) {
382+
} else if (fpm_php_is_key_in_table(_GET_str, "openmetrics", strlen("openmetrics"))) {
383383
sapi_add_header_ex(ZEND_STRL("Content-Type: application/openmetrics-text; version=1.0.0; charset=utf-8"), 1, 1);
384384
time_format = "%s";
385385

0 commit comments

Comments
 (0)