ext/ldap: Improve type check for option value

This commit is contained in:
Gina Peter Banyard 2024-09-28 00:16:52 +01:00
parent b79866d01a
commit 18fca34ef3

View file

@ -3063,14 +3063,18 @@ PHP_FUNCTION(ldap_set_option)
case LDAP_OPT_X_KEEPALIVE_INTERVAL: case LDAP_OPT_X_KEEPALIVE_INTERVAL:
#endif #endif
{ {
int val; bool failed = false;
zend_long lval = zval_try_get_long(newval, &failed);
if (failed) {
zend_argument_type_error(3, "must be of type int for the given option, %s given", zend_zval_value_name(newval));
RETURN_THROWS();
}
convert_to_long(newval); if (ZEND_LONG_EXCEEDS_INT(lval)) {
if (ZEND_LONG_EXCEEDS_INT(Z_LVAL_P(newval))) {
zend_argument_value_error(3, "is too large"); zend_argument_value_error(3, "is too large");
RETURN_THROWS(); RETURN_THROWS();
} }
val = (int)Z_LVAL_P(newval); int val = (int)lval;
if (ldap_set_option(ldap, option, &val)) { if (ldap_set_option(ldap, option, &val)) {
RETURN_FALSE; RETURN_FALSE;
} }
@ -3079,9 +3083,13 @@ PHP_FUNCTION(ldap_set_option)
case LDAP_OPT_NETWORK_TIMEOUT: case LDAP_OPT_NETWORK_TIMEOUT:
{ {
struct timeval timeout; struct timeval timeout;
bool failed = false;
convert_to_long(newval); zend_long lval = zval_try_get_long(newval, &failed);
timeout.tv_sec = Z_LVAL_P(newval); if (failed) {
zend_argument_type_error(3, "must be of type int for the LDAP_OPT_NETWORK_TIMEOUT option, %s given", zend_zval_value_name(newval));
RETURN_THROWS();
}
timeout.tv_sec = lval;
timeout.tv_usec = 0; timeout.tv_usec = 0;
if (ldap_set_option(ldap, LDAP_OPT_NETWORK_TIMEOUT, (void *) &timeout)) { if (ldap_set_option(ldap, LDAP_OPT_NETWORK_TIMEOUT, (void *) &timeout)) {
RETURN_FALSE; RETURN_FALSE;
@ -3091,9 +3099,13 @@ PHP_FUNCTION(ldap_set_option)
case LDAP_X_OPT_CONNECT_TIMEOUT: case LDAP_X_OPT_CONNECT_TIMEOUT:
{ {
int timeout; int timeout;
bool failed = false;
convert_to_long(newval); zend_long lval = zval_try_get_long(newval, &failed);
timeout = 1000 * Z_LVAL_P(newval); /* Convert to milliseconds */ if (failed) {
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));
RETURN_THROWS();
}
timeout = 1000 * lval; /* Convert to milliseconds */
if (ldap_set_option(ldap, LDAP_X_OPT_CONNECT_TIMEOUT, &timeout)) { if (ldap_set_option(ldap, LDAP_X_OPT_CONNECT_TIMEOUT, &timeout)) {
RETURN_FALSE; RETURN_FALSE;
} }
@ -3104,8 +3116,13 @@ PHP_FUNCTION(ldap_set_option)
{ {
struct timeval timeout; struct timeval timeout;
convert_to_long(newval); bool failed = false;
timeout.tv_sec = Z_LVAL_P(newval); zend_long lval = zval_try_get_long(newval, &failed);
if (failed) {
zend_argument_type_error(3, "must be of type int for the LDAP_OPT_TIMEOUT option, %s given", zend_zval_value_name(newval));
RETURN_THROWS();
}
timeout.tv_sec = lval;
timeout.tv_usec = 0; timeout.tv_usec = 0;
if (ldap_set_option(ldap, LDAP_OPT_TIMEOUT, (void *) &timeout)) { if (ldap_set_option(ldap, LDAP_OPT_TIMEOUT, (void *) &timeout)) {
RETURN_FALSE; RETURN_FALSE;
@ -3141,9 +3158,8 @@ PHP_FUNCTION(ldap_set_option)
case LDAP_OPT_MATCHED_DN: case LDAP_OPT_MATCHED_DN:
#endif #endif
{ {
zend_string *val; zend_string *val = zval_try_get_string(newval);
val = zval_get_string(newval); if (val == NULL) {
if (EG(exception)) {
RETURN_THROWS(); RETURN_THROWS();
} }
if (ldap_set_option(ldap, option, ZSTR_VAL(val))) { if (ldap_set_option(ldap, option, ZSTR_VAL(val))) {
@ -3161,8 +3177,7 @@ PHP_FUNCTION(ldap_set_option)
case LDAP_OPT_X_SASL_NOCANON: case LDAP_OPT_X_SASL_NOCANON:
#endif #endif
{ {
void *val; void *val = zend_is_true(newval) ? LDAP_OPT_ON : LDAP_OPT_OFF;
val = zend_is_true(newval) ? LDAP_OPT_ON : LDAP_OPT_OFF;
if (ldap_set_option(ldap, option, val)) { if (ldap_set_option(ldap, option, val)) {
RETURN_FALSE; RETURN_FALSE;
} }