Skip to content

Commit 8e9fa2b

Browse files
committed
Convert mod user FINISH macro to static function
1 parent 3917018 commit 8e9fa2b

File tree

2 files changed

+81
-31
lines changed

2 files changed

+81
-31
lines changed

ext/session/mod_user.c

Lines changed: 49 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -47,31 +47,37 @@ static void ps_call_handler(zval *func, int argc, zval *argv, zval *retval)
4747

4848
#define PSF(a) PS(mod_user_names).ps_##a
4949

50-
#define FINISH(return_value) \
51-
if (Z_TYPE(return_value) != IS_UNDEF) { \
52-
if (Z_TYPE(return_value) == IS_TRUE) { \
53-
ret = SUCCESS; \
54-
} else if (Z_TYPE(return_value) == IS_FALSE) { \
55-
ret = FAILURE; \
56-
} else if ((Z_TYPE(return_value) == IS_LONG) && (Z_LVAL(return_value) == -1)) { \
57-
if (!EG(exception)) { \
58-
php_error_docref(NULL, E_DEPRECATED, "Session callback must have a return value of type bool, %s returned", zend_zval_type_name(&return_value)); \
59-
} \
60-
ret = FAILURE; \
61-
} else if ((Z_TYPE(return_value) == IS_LONG) && (Z_LVAL(return_value) == 0)) { \
62-
if (!EG(exception)) { \
63-
php_error_docref(NULL, E_DEPRECATED, "Session callback must have a return value of type bool, %s returned", zend_zval_type_name(&return_value)); \
64-
} \
65-
ret = SUCCESS; \
66-
} else { \
67-
if (!EG(exception)) { \
68-
zend_type_error("Session callback must have a return value of type bool, %s returned", zend_zval_type_name(&return_value)); \
69-
} \
70-
ret = FAILURE; \
71-
zval_ptr_dtor(&return_value); \
72-
} \
73-
} \
74-
return ret
50+
static zend_result verify_bool_return_type_userland_calls(const zval* value)
51+
{
52+
/* Exit or exception in userland call */
53+
if (Z_TYPE_P(value) == IS_UNDEF) {
54+
return FAILURE;
55+
}
56+
if (Z_TYPE_P(value) == IS_TRUE) {
57+
return SUCCESS;
58+
}
59+
if (Z_TYPE_P(value) == IS_FALSE) {
60+
return FAILURE;
61+
}
62+
if ((Z_TYPE_P(value) == IS_LONG) && (Z_LVAL_P(value) == -1)) {
63+
/* TODO Why are exception cheked? */
64+
if (!EG(exception)) {
65+
php_error_docref(NULL, E_DEPRECATED, "Session callback must have a return value of type bool, %s returned", zend_zval_type_name(value));
66+
}
67+
return FAILURE;
68+
}
69+
if ((Z_TYPE_P(value) == IS_LONG) && (Z_LVAL_P(value) == 0)) {
70+
/* TODO Why are exception cheked? */
71+
if (!EG(exception)) {
72+
php_error_docref(NULL, E_DEPRECATED, "Session callback must have a return value of type bool, %s returned", zend_zval_type_name(value));
73+
}
74+
return SUCCESS;
75+
}
76+
if (!EG(exception)) {
77+
zend_type_error("Session callback must have a return value of type bool, %s returned", zend_zval_type_name(value)); \
78+
}
79+
return FAILURE;
80+
}
7581

7682
PS_OPEN_FUNC(user)
7783
{
@@ -96,7 +102,9 @@ PS_OPEN_FUNC(user)
96102

97103
PS(mod_user_implemented) = 1;
98104

99-
FINISH(retval);
105+
ret = verify_bool_return_type_userland_calls(&retval);
106+
zval_ptr_dtor(&retval);
107+
return ret;
100108
}
101109

102110
PS_CLOSE_FUNC(user)
@@ -127,7 +135,9 @@ PS_CLOSE_FUNC(user)
127135
zend_bailout();
128136
}
129137

130-
FINISH(retval);
138+
ret = verify_bool_return_type_userland_calls(&retval);
139+
zval_ptr_dtor(&retval);
140+
return ret;
131141
}
132142

133143
PS_READ_FUNC(user)
@@ -166,7 +176,9 @@ PS_WRITE_FUNC(user)
166176

167177
ps_call_handler(&PSF(write), 2, args, &retval);
168178

169-
FINISH(retval);
179+
ret = verify_bool_return_type_userland_calls(&retval);
180+
zval_ptr_dtor(&retval);
181+
return ret;
170182
}
171183

172184
PS_DESTROY_FUNC(user)
@@ -181,7 +193,9 @@ PS_DESTROY_FUNC(user)
181193

182194
ps_call_handler(&PSF(destroy), 1, args, &retval);
183195

184-
FINISH(retval);
196+
ret = verify_bool_return_type_userland_calls(&retval);
197+
zval_ptr_dtor(&retval);
198+
return ret;
185199
}
186200

187201
PS_GC_FUNC(user)
@@ -250,7 +264,9 @@ PS_VALIDATE_SID_FUNC(user)
250264

251265
ps_call_handler(&PSF(validate_sid), 1, args, &retval);
252266

253-
FINISH(retval);
267+
ret = verify_bool_return_type_userland_calls(&retval);
268+
zval_ptr_dtor(&retval);
269+
return ret;
254270
}
255271

256272
/* dummy function defined by PS_MOD */
@@ -273,5 +289,7 @@ PS_UPDATE_TIMESTAMP_FUNC(user)
273289
ps_call_handler(&PSF(write), 2, args, &retval);
274290
}
275291

276-
FINISH(retval);
292+
ret = verify_bool_return_type_userland_calls(&retval);
293+
zval_ptr_dtor(&retval);
294+
return ret;
277295
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
--TEST--
2+
Test session_set_save_handler() function: Incorrect bool returns
3+
--EXTENSIONS--
4+
session
5+
--FILE--
6+
<?php
7+
8+
$validCallback = function () { return true; };
9+
$nullCallback = function () { return; };
10+
$oneCallback = function () { return 1; };
11+
12+
ob_start();
13+
14+
try {
15+
$ret = session_set_save_handler($nullCallback, $validCallback, $validCallback, $validCallback, $validCallback, $validCallback);
16+
session_start();
17+
} catch (TypeError $exception) {
18+
echo $exception->getMessage() . "\n";
19+
}
20+
try {
21+
$ret = session_set_save_handler($oneCallback, $validCallback, $validCallback, $validCallback, $validCallback, $validCallback);
22+
session_start();
23+
} catch (TypeError $exception) {
24+
echo $exception->getMessage() . "\n";
25+
}
26+
27+
ob_end_flush();
28+
29+
?>
30+
--EXPECT--
31+
Session callback must have a return value of type bool, null returned
32+
Session callback must have a return value of type bool, int returned

0 commit comments

Comments
 (0)