Skip to content

Commit 318152f

Browse files
committed
Convert some Session globals from char* to zend_string*
However, the code is somewhat disgusting as I'm hitting Zend/zend_types.h:1222: zend_gc_delref: Assertion (zval_gc_flags((p)->u.type_info) & ((1<<7)|(1<<8))) != (1<<7) failed. Failures that I don't understand.
1 parent adb45a6 commit 318152f

File tree

2 files changed

+39
-26
lines changed

2 files changed

+39
-26
lines changed

ext/session/php_session.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,14 @@ typedef struct _php_ps_globals {
143143
char *save_path;
144144
char *session_name;
145145
zend_string *id;
146-
char *extern_referer_chk;
147-
char *cache_limiter;
146+
zend_string *extern_referer_chk;
147+
zend_string *cache_limiter;
148148
zend_long cookie_lifetime;
149-
char *cookie_path;
150-
char *cookie_domain;
149+
zend_string *cookie_path;
150+
zend_string *cookie_domain;
151151
bool cookie_secure;
152152
bool cookie_httponly;
153-
char *cookie_samesite;
153+
zend_string *cookie_samesite;
154154
const ps_module *mod;
155155
const ps_module *default_mod;
156156
void *mod_data;

ext/session/session.c

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -692,11 +692,14 @@ static PHP_INI_MH(OnUpdateSessionLong) /* {{{ */
692692
/* }}} */
693693

694694

695-
static PHP_INI_MH(OnUpdateSessionString) /* {{{ */
695+
static PHP_INI_MH(OnUpdateSessionStr) /* {{{ */
696696
{
697697
SESSION_CHECK_ACTIVE_STATE;
698698
SESSION_CHECK_OUTPUT_STATE;
699-
return OnUpdateString(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
699+
700+
zend_string **p = (zend_string **) ZEND_INI_GET_ADDR();
701+
*p = new_value ? new_value : NULL;
702+
return SUCCESS;
700703
}
701704
/* }}} */
702705

@@ -780,16 +783,16 @@ PHP_INI_BEGIN()
780783
STD_PHP_INI_ENTRY("session.gc_maxlifetime", "1440", PHP_INI_ALL, OnUpdateSessionLong, gc_maxlifetime, php_ps_globals, ps_globals)
781784
PHP_INI_ENTRY("session.serialize_handler", "php", PHP_INI_ALL, OnUpdateSerializer)
782785
STD_PHP_INI_ENTRY("session.cookie_lifetime", "0", PHP_INI_ALL, OnUpdateCookieLifetime,cookie_lifetime, php_ps_globals, ps_globals)
783-
STD_PHP_INI_ENTRY("session.cookie_path", "/", PHP_INI_ALL, OnUpdateSessionString, cookie_path, php_ps_globals, ps_globals)
784-
STD_PHP_INI_ENTRY("session.cookie_domain", "", PHP_INI_ALL, OnUpdateSessionString, cookie_domain, php_ps_globals, ps_globals)
786+
STD_PHP_INI_ENTRY("session.cookie_path", "/", PHP_INI_ALL, OnUpdateSessionStr, cookie_path, php_ps_globals, ps_globals)
787+
STD_PHP_INI_ENTRY("session.cookie_domain", "", PHP_INI_ALL, OnUpdateSessionStr, cookie_domain, php_ps_globals, ps_globals)
785788
STD_PHP_INI_ENTRY("session.cookie_secure", "0", PHP_INI_ALL, OnUpdateSessionBool, cookie_secure, php_ps_globals, ps_globals)
786789
STD_PHP_INI_ENTRY("session.cookie_httponly", "0", PHP_INI_ALL, OnUpdateSessionBool, cookie_httponly, php_ps_globals, ps_globals)
787-
STD_PHP_INI_ENTRY("session.cookie_samesite", "", PHP_INI_ALL, OnUpdateSessionString, cookie_samesite, php_ps_globals, ps_globals)
790+
STD_PHP_INI_ENTRY("session.cookie_samesite", "", PHP_INI_ALL, OnUpdateSessionStr, cookie_samesite, php_ps_globals, ps_globals)
788791
STD_PHP_INI_ENTRY("session.use_cookies", "1", PHP_INI_ALL, OnUpdateSessionBool, use_cookies, php_ps_globals, ps_globals)
789792
STD_PHP_INI_ENTRY("session.use_only_cookies", "1", PHP_INI_ALL, OnUpdateSessionBool, use_only_cookies, php_ps_globals, ps_globals)
790793
STD_PHP_INI_ENTRY("session.use_strict_mode", "0", PHP_INI_ALL, OnUpdateSessionBool, use_strict_mode, php_ps_globals, ps_globals)
791-
STD_PHP_INI_ENTRY("session.referer_check", "", PHP_INI_ALL, OnUpdateSessionString, extern_referer_chk, php_ps_globals, ps_globals)
792-
STD_PHP_INI_ENTRY("session.cache_limiter", "nocache", PHP_INI_ALL, OnUpdateSessionString, cache_limiter, php_ps_globals, ps_globals)
794+
STD_PHP_INI_ENTRY("session.referer_check", "", PHP_INI_ALL, OnUpdateSessionStr, extern_referer_chk, php_ps_globals, ps_globals)
795+
STD_PHP_INI_ENTRY("session.cache_limiter", "nocache", PHP_INI_ALL, OnUpdateSessionStr, cache_limiter, php_ps_globals, ps_globals)
793796
STD_PHP_INI_ENTRY("session.cache_expire", "180", PHP_INI_ALL, OnUpdateSessionLong, cache_expire, php_ps_globals, ps_globals)
794797
STD_PHP_INI_BOOLEAN("session.use_trans_sid", "0", PHP_INI_ALL, OnUpdateSessionBool, use_trans_sid, php_ps_globals, ps_globals)
795798
PHP_INI_ENTRY("session.sid_length", "32", PHP_INI_ALL, OnUpdateSidLength)
@@ -1202,7 +1205,7 @@ static int php_session_cache_limiter(void) /* {{{ */
12021205
{
12031206
const php_session_cache_limiter_t *lim;
12041207

1205-
if (PS(cache_limiter)[0] == '\0') return 0;
1208+
if (!PS(cache_limiter) || ZSTR_LEN(PS(cache_limiter)) == 0) return 0;
12061209
if (PS(session_status) != php_session_active) return -1;
12071210

12081211
if (SG(headers_sent)) {
@@ -1219,7 +1222,8 @@ static int php_session_cache_limiter(void) /* {{{ */
12191222
}
12201223

12211224
for (lim = php_session_cache_limiters; lim->name; lim++) {
1222-
if (!strcasecmp(lim->name, PS(cache_limiter))) {
1225+
// TODO Use zend_string_cmp API?
1226+
if (!strcasecmp(lim->name, ZSTR_VAL(PS(cache_limiter)))) {
12231227
lim->func();
12241228
return 0;
12251229
}
@@ -1328,14 +1332,14 @@ static zend_result php_session_send_cookie(void) /* {{{ */
13281332
}
13291333
}
13301334

1331-
if (PS(cookie_path)[0]) {
1335+
if (PS(cookie_path) && ZSTR_LEN(PS(cookie_path)) != 0) {
13321336
smart_str_appends(&ncookie, COOKIE_PATH);
1333-
smart_str_appends(&ncookie, PS(cookie_path));
1337+
smart_str_append(&ncookie, PS(cookie_path));
13341338
}
13351339

1336-
if (PS(cookie_domain)[0]) {
1340+
if (PS(cookie_domain) && ZSTR_LEN(PS(cookie_domain)) != 0) {
13371341
smart_str_appends(&ncookie, COOKIE_DOMAIN);
1338-
smart_str_appends(&ncookie, PS(cookie_domain));
1342+
smart_str_append(&ncookie, PS(cookie_domain));
13391343
}
13401344

13411345
if (PS(cookie_secure)) {
@@ -1346,9 +1350,9 @@ static zend_result php_session_send_cookie(void) /* {{{ */
13461350
smart_str_appends(&ncookie, COOKIE_HTTPONLY);
13471351
}
13481352

1349-
if (PS(cookie_samesite)[0]) {
1353+
if (PS(cookie_samesite) && ZSTR_LEN(PS(cookie_samesite)) != 0) {
13501354
smart_str_appends(&ncookie, COOKIE_SAMESITE);
1351-
smart_str_appends(&ncookie, PS(cookie_samesite));
1355+
smart_str_append(&ncookie, PS(cookie_samesite));
13521356
}
13531357

13541358
smart_str_0(&ncookie);
@@ -1566,12 +1570,12 @@ PHPAPI zend_result php_session_start(void) /* {{{ */
15661570
}
15671571
/* Check whether the current request was referred to by
15681572
* an external site which invalidates the previously found id. */
1569-
if (PS(id) && PS(extern_referer_chk)[0] != '\0' &&
1573+
if (PS(id) && PS(extern_referer_chk) && ZSTR_LEN(PS(extern_referer_chk)) != 0 &&
15701574
!Z_ISUNDEF(PG(http_globals)[TRACK_VARS_SERVER]) &&
15711575
(data = zend_hash_str_find(Z_ARRVAL(PG(http_globals)[TRACK_VARS_SERVER]), "HTTP_REFERER", sizeof("HTTP_REFERER") - 1)) &&
15721576
Z_TYPE_P(data) == IS_STRING &&
15731577
Z_STRLEN_P(data) != 0 &&
1574-
strstr(Z_STRVAL_P(data), PS(extern_referer_chk)) == NULL
1578+
strstr(Z_STRVAL_P(data), ZSTR_VAL(PS(extern_referer_chk))) == NULL
15751579
) {
15761580
zend_string_release_ex(PS(id), 0);
15771581
PS(id) = NULL;
@@ -1829,11 +1833,15 @@ PHP_FUNCTION(session_get_cookie_params)
18291833
array_init(return_value);
18301834

18311835
add_assoc_long(return_value, "lifetime", PS(cookie_lifetime));
1832-
add_assoc_string(return_value, "path", PS(cookie_path));
1833-
add_assoc_string(return_value, "domain", PS(cookie_domain));
1836+
// TODO Use add_assoc_str() but figure out why it emits a
1837+
// Zend/zend_types.h:1222: zend_gc_delref: Assertion `(zval_gc_flags((p)->u.type_info) & ((1<<7)|(1<<8))) != (1<<7)' failed.
1838+
add_assoc_string(return_value, "path", ZSTR_VAL(PS(cookie_path)));
1839+
add_assoc_string(return_value, "domain", ZSTR_VAL(PS(cookie_domain)));
18341840
add_assoc_bool(return_value, "secure", PS(cookie_secure));
18351841
add_assoc_bool(return_value, "httponly", PS(cookie_httponly));
1836-
add_assoc_string(return_value, "samesite", PS(cookie_samesite));
1842+
// TODO Use add_assoc_str() but figure out why it emits a
1843+
// Zend/zend_types.h:1222: zend_gc_delref: Assertion `(zval_gc_flags((p)->u.type_info) & ((1<<7)|(1<<8))) != (1<<7)' failed.
1844+
add_assoc_string(return_value, "samesite", ZSTR_VAL(PS(cookie_samesite)));
18371845
}
18381846
/* }}} */
18391847

@@ -2358,7 +2366,12 @@ PHP_FUNCTION(session_cache_limiter)
23582366
RETURN_FALSE;
23592367
}
23602368

2361-
RETVAL_STRING(PS(cache_limiter));
2369+
zend_string *result_str = PS(cache_limiter);
2370+
if (!result_str) {
2371+
result_str = zend_empty_string;
2372+
}
2373+
// TODO Prevent duplication???
2374+
RETVAL_STR(zend_string_dup(result_str, false));
23622375

23632376
if (limiter) {
23642377
ini_name = zend_string_init("session.cache_limiter", sizeof("session.cache_limiter") - 1, 0);

0 commit comments

Comments
 (0)