Skip to content

Commit 18fca34

Browse files
committed
ext/ldap: Improve type check for option value
1 parent b79866d commit 18fca34

File tree

1 file changed

+32
-17
lines changed

1 file changed

+32
-17
lines changed

ext/ldap/ldap.c

+32-17
Original file line numberDiff line numberDiff line change
@@ -3063,14 +3063,18 @@ PHP_FUNCTION(ldap_set_option)
30633063
case LDAP_OPT_X_KEEPALIVE_INTERVAL:
30643064
#endif
30653065
{
3066-
int val;
3066+
bool failed = false;
3067+
zend_long lval = zval_try_get_long(newval, &failed);
3068+
if (failed) {
3069+
zend_argument_type_error(3, "must be of type int for the given option, %s given", zend_zval_value_name(newval));
3070+
RETURN_THROWS();
3071+
}
30673072

3068-
convert_to_long(newval);
3069-
if (ZEND_LONG_EXCEEDS_INT(Z_LVAL_P(newval))) {
3073+
if (ZEND_LONG_EXCEEDS_INT(lval)) {
30703074
zend_argument_value_error(3, "is too large");
30713075
RETURN_THROWS();
30723076
}
3073-
val = (int)Z_LVAL_P(newval);
3077+
int val = (int)lval;
30743078
if (ldap_set_option(ldap, option, &val)) {
30753079
RETURN_FALSE;
30763080
}
@@ -3079,9 +3083,13 @@ PHP_FUNCTION(ldap_set_option)
30793083
case LDAP_OPT_NETWORK_TIMEOUT:
30803084
{
30813085
struct timeval timeout;
3082-
3083-
convert_to_long(newval);
3084-
timeout.tv_sec = Z_LVAL_P(newval);
3086+
bool failed = false;
3087+
zend_long lval = zval_try_get_long(newval, &failed);
3088+
if (failed) {
3089+
zend_argument_type_error(3, "must be of type int for the LDAP_OPT_NETWORK_TIMEOUT option, %s given", zend_zval_value_name(newval));
3090+
RETURN_THROWS();
3091+
}
3092+
timeout.tv_sec = lval;
30853093
timeout.tv_usec = 0;
30863094
if (ldap_set_option(ldap, LDAP_OPT_NETWORK_TIMEOUT, (void *) &timeout)) {
30873095
RETURN_FALSE;
@@ -3091,9 +3099,13 @@ PHP_FUNCTION(ldap_set_option)
30913099
case LDAP_X_OPT_CONNECT_TIMEOUT:
30923100
{
30933101
int timeout;
3094-
3095-
convert_to_long(newval);
3096-
timeout = 1000 * Z_LVAL_P(newval); /* Convert to milliseconds */
3102+
bool failed = false;
3103+
zend_long lval = zval_try_get_long(newval, &failed);
3104+
if (failed) {
3105+
zend_argument_type_error(3, "must be of type int for the LDAP_X_OPT_CONNECT_TIMEOUT option, %s given", zend_zval_value_name(newval));
3106+
RETURN_THROWS();
3107+
}
3108+
timeout = 1000 * lval; /* Convert to milliseconds */
30973109
if (ldap_set_option(ldap, LDAP_X_OPT_CONNECT_TIMEOUT, &timeout)) {
30983110
RETURN_FALSE;
30993111
}
@@ -3104,8 +3116,13 @@ PHP_FUNCTION(ldap_set_option)
31043116
{
31053117
struct timeval timeout;
31063118

3107-
convert_to_long(newval);
3108-
timeout.tv_sec = Z_LVAL_P(newval);
3119+
bool failed = false;
3120+
zend_long lval = zval_try_get_long(newval, &failed);
3121+
if (failed) {
3122+
zend_argument_type_error(3, "must be of type int for the LDAP_OPT_TIMEOUT option, %s given", zend_zval_value_name(newval));
3123+
RETURN_THROWS();
3124+
}
3125+
timeout.tv_sec = lval;
31093126
timeout.tv_usec = 0;
31103127
if (ldap_set_option(ldap, LDAP_OPT_TIMEOUT, (void *) &timeout)) {
31113128
RETURN_FALSE;
@@ -3141,9 +3158,8 @@ PHP_FUNCTION(ldap_set_option)
31413158
case LDAP_OPT_MATCHED_DN:
31423159
#endif
31433160
{
3144-
zend_string *val;
3145-
val = zval_get_string(newval);
3146-
if (EG(exception)) {
3161+
zend_string *val = zval_try_get_string(newval);
3162+
if (val == NULL) {
31473163
RETURN_THROWS();
31483164
}
31493165
if (ldap_set_option(ldap, option, ZSTR_VAL(val))) {
@@ -3161,8 +3177,7 @@ PHP_FUNCTION(ldap_set_option)
31613177
case LDAP_OPT_X_SASL_NOCANON:
31623178
#endif
31633179
{
3164-
void *val;
3165-
val = zend_is_true(newval) ? LDAP_OPT_ON : LDAP_OPT_OFF;
3180+
void *val = zend_is_true(newval) ? LDAP_OPT_ON : LDAP_OPT_OFF;
31663181
if (ldap_set_option(ldap, option, val)) {
31673182
RETURN_FALSE;
31683183
}

0 commit comments

Comments
 (0)