From c0628698013735cdf9f19b312430d80dfc447107 Mon Sep 17 00:00:00 2001 From: Gina Peter Banyard Date: Sat, 26 Apr 2025 02:24:27 +0100 Subject: [PATCH 1/4] ext/ldap: ldap_free_result() always returns true --- ext/ldap/ldap.stub.php | 2 +- ext/ldap/ldap_arginfo.h | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/ext/ldap/ldap.stub.php b/ext/ldap/ldap.stub.php index 84befe3f43f8e..52cf3828dba3d 100644 --- a/ext/ldap/ldap.stub.php +++ b/ext/ldap/ldap.stub.php @@ -659,7 +659,7 @@ function ldap_list($ldap, array|string $base, array|string $filter, array $attri /** @param LDAP\Connection|array $ldap */ function ldap_search($ldap, array|string $base, array|string $filter, array $attributes = [], int $attributes_only = 0, int $sizelimit = -1, int $timelimit = -1, int $deref = LDAP_DEREF_NEVER, ?array $controls = null): LDAP\Result|array|false {} - function ldap_free_result(LDAP\Result $result): bool {} + function ldap_free_result(LDAP\Result $result): true {} function ldap_count_entries(LDAP\Connection $ldap, LDAP\Result $result): int {} diff --git a/ext/ldap/ldap_arginfo.h b/ext/ldap/ldap_arginfo.h index aa371b7a8d49c..8f5e7e34ba327 100644 --- a/ext/ldap/ldap_arginfo.h +++ b/ext/ldap/ldap_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit ldap.stub.php instead. - * Stub hash: 9e47a0d85336f0e149abbdf56468513c5d31780f */ + * Stub hash: 0dde8fd813f43640dee842c03365d7431858a56d */ #if defined(HAVE_ORALDAP) ZEND_BEGIN_ARG_WITH_RETURN_OBJ_TYPE_MASK_EX(arginfo_ldap_connect, 0, 0, LDAP\\Connection, MAY_BE_FALSE) @@ -75,7 +75,7 @@ ZEND_END_ARG_INFO() #define arginfo_ldap_search arginfo_ldap_read -ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_ldap_free_result, 0, 1, _IS_BOOL, 0) +ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_ldap_free_result, 0, 1, IS_TRUE, 0) ZEND_ARG_OBJ_INFO(0, result, LDAP\\Result, 0) ZEND_END_ARG_INFO() From 35db6daa3de7a2f57a262b98ada8673659d6d197 Mon Sep 17 00:00:00 2001 From: Gina Peter Banyard Date: Sat, 26 Apr 2025 02:42:38 +0100 Subject: [PATCH 2/4] ext/ldap: CS nit --- ext/ldap/ldap.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ext/ldap/ldap.c b/ext/ldap/ldap.c index 5cc462d624ac5..a13b25e1efdec 100644 --- a/ext/ldap/ldap.c +++ b/ext/ldap/ldap.c @@ -2434,7 +2434,9 @@ static void php_ldap_do_modify(INTERNAL_FUNCTION_PARAMETERS, int oper, bool ext) object_init_ex(return_value, ldap_result_ce); result = Z_LDAP_RESULT_P(return_value); result->result = ldap_res; - } else RETVAL_TRUE; + } else { + RETVAL_TRUE; + } } cleanup: From fbf35a1b666d6baaaf2b39650f4104a0ecd4e4ae Mon Sep 17 00:00:00 2001 From: Gina Peter Banyard Date: Sat, 26 Apr 2025 01:56:07 +0100 Subject: [PATCH 3/4] ext/ldap: Stop assigning values in if statement --- ext/ldap/ldap.c | 92 +++++++++++++++++++++++++++---------------------- 1 file changed, 50 insertions(+), 42 deletions(-) diff --git a/ext/ldap/ldap.c b/ext/ldap/ldap.c index a13b25e1efdec..72df6d3d8292f 100644 --- a/ext/ldap/ldap.c +++ b/ext/ldap/ldap.c @@ -451,7 +451,8 @@ static int php_ldap_control_from_array(LDAP *ld, LDAPControl** ctrl, const HashT bool control_value_alloc = false; int rc = LDAP_SUCCESS; - if ((val = zend_hash_find(control_ht, ZSTR_KNOWN(ZEND_STR_VALUE))) != NULL) { + val = zend_hash_find(control_ht, ZSTR_KNOWN(ZEND_STR_VALUE)); + if (val != NULL) { if (Z_TYPE_P(val) != IS_ARRAY) { tmpstring = zval_try_get_string(val); if (!tmpstring) { @@ -461,13 +462,14 @@ static int php_ldap_control_from_array(LDAP *ld, LDAPControl** ctrl, const HashT control_value.bv_val = ZSTR_VAL(tmpstring); control_value.bv_len = ZSTR_LEN(tmpstring); } else if (zend_string_equals_literal(control_oid, LDAP_CONTROL_PAGEDRESULTS)) { - zval* tmp; + zval* tmp = zend_hash_str_find(Z_ARRVAL_P(val), "size", sizeof("size") - 1); int pagesize = 1; struct berval cookie = { 0L, NULL }; - if ((tmp = zend_hash_str_find(Z_ARRVAL_P(val), "size", sizeof("size") - 1)) != NULL) { + if (tmp != NULL) { pagesize = zval_get_long(tmp); } - if ((tmp = zend_hash_str_find(Z_ARRVAL_P(val), "cookie", sizeof("cookie") - 1)) != NULL) { + tmp = zend_hash_str_find(Z_ARRVAL_P(val), "cookie", sizeof("cookie") - 1); + if (tmp != NULL) { tmpstring = zval_try_get_string(tmp); if (!tmpstring) { rc = -1; @@ -483,8 +485,8 @@ static int php_ldap_control_from_array(LDAP *ld, LDAPControl** ctrl, const HashT php_error_docref(NULL, E_WARNING, "Failed to create paged result control value: %s (%d)", ldap_err2string(rc), rc); } } else if (zend_string_equals_literal(control_oid, LDAP_CONTROL_ASSERT)) { - zval* tmp; - if ((tmp = zend_hash_str_find(Z_ARRVAL_P(val), "filter", sizeof("filter") - 1)) == NULL) { + zval *tmp = zend_hash_str_find(Z_ARRVAL_P(val), "filter", sizeof("filter") - 1); + if (tmp == NULL) { rc = -1; zend_value_error("%s(): Control must have a \"filter\" key", get_active_function_name()); } else { @@ -506,8 +508,8 @@ static int php_ldap_control_from_array(LDAP *ld, LDAPControl** ctrl, const HashT zend_string_release(assert); } } else if (zend_string_equals_literal(control_oid, LDAP_CONTROL_VALUESRETURNFILTER)) { - zval* tmp; - if ((tmp = zend_hash_str_find(Z_ARRVAL_P(val), "filter", sizeof("filter") - 1)) == NULL) { + zval *tmp = zend_hash_str_find(Z_ARRVAL_P(val), "filter", sizeof("filter") - 1); + if (tmp == NULL) { rc = -1; zend_value_error("%s(): Control must have a \"filter\" key", get_active_function_name()); } else { @@ -530,8 +532,8 @@ static int php_ldap_control_from_array(LDAP *ld, LDAPControl** ctrl, const HashT } } } else if (zend_string_equals_literal(control_oid, LDAP_CONTROL_PRE_READ) || zend_string_equals_literal(control_oid, LDAP_CONTROL_POST_READ)) { - zval* tmp; - if ((tmp = zend_hash_str_find(Z_ARRVAL_P(val), "attrs", sizeof("attrs") - 1)) == NULL) { + zval *tmp = zend_hash_str_find(Z_ARRVAL_P(val), "attrs", sizeof("attrs") - 1); + if (tmp == NULL) { rc = -1; zend_value_error("%s(): Control must have an \"attrs\" key", get_active_function_name()); } else { @@ -541,15 +543,14 @@ static int php_ldap_control_from_array(LDAP *ld, LDAPControl** ctrl, const HashT rc = -1; php_error_docref(NULL, E_WARNING, "Failed to allocate control value"); } else { - zval* attr; - uint32_t num_attribs = zend_hash_num_elements(Z_ARRVAL_P(tmp)); ldap_attrs = safe_emalloc((num_attribs+1), sizeof(char *), 0); tmpstrings1 = safe_emalloc(num_attribs, sizeof(zend_string*), 0); num_tmpstrings1 = 0; for (uint32_t i = 0; i < num_attribs; i++) { - if ((attr = zend_hash_index_find(Z_ARRVAL_P(tmp), i)) == NULL) { + zval* attr = zend_hash_index_find(Z_ARRVAL_P(tmp), i); + if (attr == NULL) { rc = -1; php_error_docref(NULL, E_WARNING, "Failed to encode attribute list"); goto failure; @@ -581,8 +582,6 @@ static int php_ldap_control_from_array(LDAP *ld, LDAPControl** ctrl, const HashT } } } else if (zend_string_equals_literal(control_oid, LDAP_CONTROL_SORTREQUEST)) { - zval *sortkey, *tmp; - uint32_t num_keys = zend_hash_num_elements(Z_ARRVAL_P(val)); sort_keys = safe_emalloc((num_keys+1), sizeof(LDAPSortKey*), 0); tmpstrings1 = safe_emalloc(num_keys, sizeof(zend_string*), 0); @@ -591,13 +590,15 @@ static int php_ldap_control_from_array(LDAP *ld, LDAPControl** ctrl, const HashT num_tmpstrings2 = 0; for (uint32_t i = 0; i < num_keys; i++) { - if ((sortkey = zend_hash_index_find(Z_ARRVAL_P(val), i)) == NULL) { + zval *sortkey = zend_hash_index_find(Z_ARRVAL_P(val), i); + if (sortkey == NULL) { rc = -1; php_error_docref(NULL, E_WARNING, "Failed to encode sort keys list"); goto failure; } - if ((tmp = zend_hash_str_find(Z_ARRVAL_P(sortkey), "attr", sizeof("attr") - 1)) == NULL) { + zval *tmp = zend_hash_str_find(Z_ARRVAL_P(sortkey), "attr", sizeof("attr") - 1); + if (tmp == NULL) { rc = -1; zend_value_error("%s(): Sort key list must have an \"attr\" key", get_active_function_name()); goto failure; @@ -611,7 +612,8 @@ static int php_ldap_control_from_array(LDAP *ld, LDAPControl** ctrl, const HashT sort_keys[i]->attributeType = ZSTR_VAL(tmpstrings1[num_tmpstrings1]); ++num_tmpstrings1; - if ((tmp = zend_hash_str_find(Z_ARRVAL_P(sortkey), "oid", sizeof("oid") - 1)) != NULL) { + tmp = zend_hash_str_find(Z_ARRVAL_P(sortkey), "oid", sizeof("oid") - 1); + if (tmp == NULL) { tmpstrings2[num_tmpstrings2] = zval_try_get_string(tmp); if (!tmpstrings2[num_tmpstrings2]) { rc = -1; @@ -623,7 +625,8 @@ static int php_ldap_control_from_array(LDAP *ld, LDAPControl** ctrl, const HashT sort_keys[i]->orderingRule = NULL; } - if ((tmp = zend_hash_str_find(Z_ARRVAL_P(sortkey), "reverse", sizeof("reverse") - 1)) != NULL) { + tmp = zend_hash_str_find(Z_ARRVAL_P(sortkey), "reverse", sizeof("reverse") - 1); + if (tmp == NULL) { sort_keys[i]->reverseOrder = zend_is_true(tmp); } else { sort_keys[i]->reverseOrder = 0; @@ -637,12 +640,12 @@ static int php_ldap_control_from_array(LDAP *ld, LDAPControl** ctrl, const HashT php_error_docref(NULL, E_WARNING, "Failed to create sort control value: %s (%d)", ldap_err2string(rc), rc); } } else if (zend_string_equals_literal(control_oid, LDAP_CONTROL_VLVREQUEST)) { - zval* tmp; LDAPVLVInfo vlvInfo; struct berval attrValue; struct berval context; - if ((tmp = zend_hash_str_find(Z_ARRVAL_P(val), "before", sizeof("before") - 1)) != NULL) { + zval *tmp = zend_hash_str_find(Z_ARRVAL_P(val), "before", sizeof("before") - 1); + if (tmp != NULL) { vlvInfo.ldvlv_before_count = zval_get_long(tmp); } else { rc = -1; @@ -650,7 +653,8 @@ static int php_ldap_control_from_array(LDAP *ld, LDAPControl** ctrl, const HashT goto failure; } - if ((tmp = zend_hash_str_find(Z_ARRVAL_P(val), "after", sizeof("after") - 1)) != NULL) { + tmp = zend_hash_str_find(Z_ARRVAL_P(val), "after", sizeof("after") - 1); + if (tmp != NULL) { vlvInfo.ldvlv_after_count = zval_get_long(tmp); } else { rc = -1; @@ -658,7 +662,8 @@ static int php_ldap_control_from_array(LDAP *ld, LDAPControl** ctrl, const HashT goto failure; } - if ((tmp = zend_hash_str_find(Z_ARRVAL_P(val), "attrvalue", sizeof("attrvalue") - 1)) != NULL) { + tmp = zend_hash_str_find(Z_ARRVAL_P(val), "attrvalue", sizeof("attrvalue") - 1); + if (tmp != NULL) { tmpstring = zval_try_get_string(tmp); if (!tmpstring) { rc = -1; @@ -671,8 +676,9 @@ static int php_ldap_control_from_array(LDAP *ld, LDAPControl** ctrl, const HashT vlvInfo.ldvlv_attrvalue = NULL; vlvInfo.ldvlv_offset = zval_get_long(tmp); /* Find "count" key */ - if ((tmp = zend_hash_find(Z_ARRVAL_P(val), ZSTR_KNOWN(ZEND_STR_COUNT))) != NULL) { - vlvInfo.ldvlv_count = zval_get_long(tmp); + zval *count_key = zend_hash_find(Z_ARRVAL_P(val), ZSTR_KNOWN(ZEND_STR_COUNT)); + if (count_key != NULL) { + vlvInfo.ldvlv_count = zval_get_long(count_key); } else { rc = -1; zend_value_error("%s(): Array value for VLV control must have a \"count\" key", get_active_function_name()); @@ -685,7 +691,8 @@ static int php_ldap_control_from_array(LDAP *ld, LDAPControl** ctrl, const HashT } zend_string *context_str = NULL; - if ((tmp = zend_hash_str_find(Z_ARRVAL_P(val), "context", sizeof("context") - 1)) != NULL) { + tmp = zend_hash_str_find(Z_ARRVAL_P(val), "context", sizeof("context") - 1); + if (tmp != NULL) { context_str = zval_try_get_string(tmp); if (!context_str) { rc = -1; @@ -1861,7 +1868,6 @@ PHP_FUNCTION(ldap_first_entry) zval *link, *result; ldap_linkdata *ld; ldap_resultdata *ldap_result; - LDAPMessage *entry; if (zend_parse_parameters(ZEND_NUM_ARGS(), "OO", &link, ldap_link_ce, &result, ldap_result_ce) != SUCCESS) { RETURN_THROWS(); @@ -1873,7 +1879,8 @@ PHP_FUNCTION(ldap_first_entry) ldap_result = Z_LDAP_RESULT_P(result); VERIFY_LDAP_RESULT_OPEN(ldap_result); - if ((entry = ldap_first_entry(ld->link, ldap_result->result)) == NULL) { + LDAPMessage *entry = ldap_first_entry(ld->link, ldap_result->result); + if (entry == NULL) { RETVAL_FALSE; } else { object_init_ex(return_value, ldap_result_entry_ce); @@ -1891,7 +1898,6 @@ PHP_FUNCTION(ldap_next_entry) zval *link, *result_entry; ldap_linkdata *ld; ldap_result_entry *resultentry; - LDAPMessage *entry_next; if (zend_parse_parameters(ZEND_NUM_ARGS(), "OO", &link, ldap_link_ce, &result_entry, ldap_result_entry_ce) != SUCCESS) { RETURN_THROWS(); @@ -1902,7 +1908,8 @@ PHP_FUNCTION(ldap_next_entry) resultentry = Z_LDAP_RESULT_ENTRY_P(result_entry); - if ((entry_next = ldap_next_entry(ld->link, resultentry->data)) == NULL) { + LDAPMessage *entry_next = ldap_next_entry(ld->link, resultentry->data); + if (entry_next == NULL) { RETVAL_FALSE; } else { object_init_ex(return_value, ldap_result_entry_ce); @@ -2019,7 +2026,6 @@ PHP_FUNCTION(ldap_first_attribute) zval *link, *result_entry; ldap_linkdata *ld; ldap_result_entry *resultentry; - char *attribute; if (zend_parse_parameters(ZEND_NUM_ARGS(), "OO", &link, ldap_link_ce, &result_entry, ldap_result_entry_ce) != SUCCESS) { RETURN_THROWS(); @@ -2030,7 +2036,8 @@ PHP_FUNCTION(ldap_first_attribute) resultentry = Z_LDAP_RESULT_ENTRY_P(result_entry); - if ((attribute = ldap_first_attribute(ld->link, resultentry->data, &resultentry->ber)) == NULL) { + char *attribute = ldap_first_attribute(ld->link, resultentry->data, &resultentry->ber); + if (attribute == NULL) { RETURN_FALSE; } else { RETVAL_STRING(attribute); @@ -2047,7 +2054,6 @@ PHP_FUNCTION(ldap_next_attribute) zval *link, *result_entry; ldap_linkdata *ld; ldap_result_entry *resultentry; - char *attribute; if (zend_parse_parameters(ZEND_NUM_ARGS(), "OO", &link, ldap_link_ce, &result_entry, ldap_result_entry_ce) != SUCCESS) { RETURN_THROWS(); @@ -2063,7 +2069,8 @@ PHP_FUNCTION(ldap_next_attribute) RETURN_FALSE; } - if ((attribute = ldap_next_attribute(ld->link, resultentry->data, resultentry->ber)) == NULL) { + char *attribute = ldap_next_attribute(ld->link, resultentry->data, resultentry->ber); + if (attribute == NULL) { #if (LDAP_API_VERSION > 2000) || defined(HAVE_ORALDAP) if (resultentry->ber != NULL) { ber_free(resultentry->ber, 0); @@ -2141,7 +2148,6 @@ PHP_FUNCTION(ldap_get_values_len) ldap_linkdata *ld; ldap_result_entry *resultentry; char *attr; - struct berval **ldap_value_len; int num_values; size_t attr_len; @@ -2154,7 +2160,8 @@ PHP_FUNCTION(ldap_get_values_len) resultentry = Z_LDAP_RESULT_ENTRY_P(result_entry); - if ((ldap_value_len = ldap_get_values_len(ld->link, resultentry->data, attr)) == NULL) { + struct berval **ldap_value_len = ldap_get_values_len(ld->link, resultentry->data, attr); + if (ldap_value_len == NULL) { php_error_docref(NULL, E_WARNING, "Cannot get the value(s) of attribute %s", ldap_err2string(_get_lderrno(ld->link))); RETURN_FALSE; } @@ -2207,14 +2214,15 @@ PHP_FUNCTION(ldap_get_dn) PHP_FUNCTION(ldap_explode_dn) { zend_long with_attrib; - char *dn, **ldap_value; + char *dn; size_t dn_len; if (zend_parse_parameters(ZEND_NUM_ARGS(), "pl", &dn, &dn_len, &with_attrib) != SUCCESS) { RETURN_THROWS(); } - if (!(ldap_value = ldap_explode_dn(dn, with_attrib))) { + char **ldap_value = ldap_explode_dn(dn, with_attrib); + if (ldap_value == NULL) { /* Invalid parameters were passed to ldap_explode_dn */ RETURN_FALSE; } @@ -3522,7 +3530,6 @@ PHP_FUNCTION(ldap_first_reference) zval *link, *result; ldap_linkdata *ld; ldap_resultdata *ldap_result; - LDAPMessage *entry; if (zend_parse_parameters(ZEND_NUM_ARGS(), "OO", &link, ldap_link_ce, &result, ldap_result_ce) != SUCCESS) { RETURN_THROWS(); @@ -3534,7 +3541,8 @@ PHP_FUNCTION(ldap_first_reference) ldap_result = Z_LDAP_RESULT_P(result); VERIFY_LDAP_RESULT_OPEN(ldap_result); - if ((entry = ldap_first_reference(ld->link, ldap_result->result)) == NULL) { + LDAPMessage *entry = ldap_first_reference(ld->link, ldap_result->result); + if (entry == NULL) { RETVAL_FALSE; } else { object_init_ex(return_value, ldap_result_entry_ce); @@ -3552,7 +3560,6 @@ PHP_FUNCTION(ldap_next_reference) zval *link, *result_entry; ldap_linkdata *ld; ldap_result_entry *resultentry; - LDAPMessage *entry_next; if (zend_parse_parameters(ZEND_NUM_ARGS(), "OO", &link, ldap_link_ce, &result_entry, ldap_result_entry_ce) != SUCCESS) { RETURN_THROWS(); @@ -3563,7 +3570,8 @@ PHP_FUNCTION(ldap_next_reference) resultentry = Z_LDAP_RESULT_ENTRY_P(result_entry); - if ((entry_next = ldap_next_reference(ld->link, resultentry->data)) == NULL) { + LDAPMessage *entry_next = ldap_first_reference(ld->link, resultentry->data); + if (entry_next == NULL) { RETVAL_FALSE; } else { object_init_ex(return_value, ldap_result_entry_ce); From d7fe070f3a472296808cc95c82ba629ae7fbbb67 Mon Sep 17 00:00:00 2001 From: Gina Peter Banyard Date: Thu, 26 Mar 2026 17:25:04 +0000 Subject: [PATCH 4/4] Revert "ext/ldap: Stop assigning values in if statement" This reverts commit fbf35a1b666d6baaaf2b39650f4104a0ecd4e4ae. --- ext/ldap/ldap.c | 92 ++++++++++++++++++++++--------------------------- 1 file changed, 42 insertions(+), 50 deletions(-) diff --git a/ext/ldap/ldap.c b/ext/ldap/ldap.c index 72df6d3d8292f..a13b25e1efdec 100644 --- a/ext/ldap/ldap.c +++ b/ext/ldap/ldap.c @@ -451,8 +451,7 @@ static int php_ldap_control_from_array(LDAP *ld, LDAPControl** ctrl, const HashT bool control_value_alloc = false; int rc = LDAP_SUCCESS; - val = zend_hash_find(control_ht, ZSTR_KNOWN(ZEND_STR_VALUE)); - if (val != NULL) { + if ((val = zend_hash_find(control_ht, ZSTR_KNOWN(ZEND_STR_VALUE))) != NULL) { if (Z_TYPE_P(val) != IS_ARRAY) { tmpstring = zval_try_get_string(val); if (!tmpstring) { @@ -462,14 +461,13 @@ static int php_ldap_control_from_array(LDAP *ld, LDAPControl** ctrl, const HashT control_value.bv_val = ZSTR_VAL(tmpstring); control_value.bv_len = ZSTR_LEN(tmpstring); } else if (zend_string_equals_literal(control_oid, LDAP_CONTROL_PAGEDRESULTS)) { - zval* tmp = zend_hash_str_find(Z_ARRVAL_P(val), "size", sizeof("size") - 1); + zval* tmp; int pagesize = 1; struct berval cookie = { 0L, NULL }; - if (tmp != NULL) { + if ((tmp = zend_hash_str_find(Z_ARRVAL_P(val), "size", sizeof("size") - 1)) != NULL) { pagesize = zval_get_long(tmp); } - tmp = zend_hash_str_find(Z_ARRVAL_P(val), "cookie", sizeof("cookie") - 1); - if (tmp != NULL) { + if ((tmp = zend_hash_str_find(Z_ARRVAL_P(val), "cookie", sizeof("cookie") - 1)) != NULL) { tmpstring = zval_try_get_string(tmp); if (!tmpstring) { rc = -1; @@ -485,8 +483,8 @@ static int php_ldap_control_from_array(LDAP *ld, LDAPControl** ctrl, const HashT php_error_docref(NULL, E_WARNING, "Failed to create paged result control value: %s (%d)", ldap_err2string(rc), rc); } } else if (zend_string_equals_literal(control_oid, LDAP_CONTROL_ASSERT)) { - zval *tmp = zend_hash_str_find(Z_ARRVAL_P(val), "filter", sizeof("filter") - 1); - if (tmp == NULL) { + zval* tmp; + if ((tmp = zend_hash_str_find(Z_ARRVAL_P(val), "filter", sizeof("filter") - 1)) == NULL) { rc = -1; zend_value_error("%s(): Control must have a \"filter\" key", get_active_function_name()); } else { @@ -508,8 +506,8 @@ static int php_ldap_control_from_array(LDAP *ld, LDAPControl** ctrl, const HashT zend_string_release(assert); } } else if (zend_string_equals_literal(control_oid, LDAP_CONTROL_VALUESRETURNFILTER)) { - zval *tmp = zend_hash_str_find(Z_ARRVAL_P(val), "filter", sizeof("filter") - 1); - if (tmp == NULL) { + zval* tmp; + if ((tmp = zend_hash_str_find(Z_ARRVAL_P(val), "filter", sizeof("filter") - 1)) == NULL) { rc = -1; zend_value_error("%s(): Control must have a \"filter\" key", get_active_function_name()); } else { @@ -532,8 +530,8 @@ static int php_ldap_control_from_array(LDAP *ld, LDAPControl** ctrl, const HashT } } } else if (zend_string_equals_literal(control_oid, LDAP_CONTROL_PRE_READ) || zend_string_equals_literal(control_oid, LDAP_CONTROL_POST_READ)) { - zval *tmp = zend_hash_str_find(Z_ARRVAL_P(val), "attrs", sizeof("attrs") - 1); - if (tmp == NULL) { + zval* tmp; + if ((tmp = zend_hash_str_find(Z_ARRVAL_P(val), "attrs", sizeof("attrs") - 1)) == NULL) { rc = -1; zend_value_error("%s(): Control must have an \"attrs\" key", get_active_function_name()); } else { @@ -543,14 +541,15 @@ static int php_ldap_control_from_array(LDAP *ld, LDAPControl** ctrl, const HashT rc = -1; php_error_docref(NULL, E_WARNING, "Failed to allocate control value"); } else { + zval* attr; + uint32_t num_attribs = zend_hash_num_elements(Z_ARRVAL_P(tmp)); ldap_attrs = safe_emalloc((num_attribs+1), sizeof(char *), 0); tmpstrings1 = safe_emalloc(num_attribs, sizeof(zend_string*), 0); num_tmpstrings1 = 0; for (uint32_t i = 0; i < num_attribs; i++) { - zval* attr = zend_hash_index_find(Z_ARRVAL_P(tmp), i); - if (attr == NULL) { + if ((attr = zend_hash_index_find(Z_ARRVAL_P(tmp), i)) == NULL) { rc = -1; php_error_docref(NULL, E_WARNING, "Failed to encode attribute list"); goto failure; @@ -582,6 +581,8 @@ static int php_ldap_control_from_array(LDAP *ld, LDAPControl** ctrl, const HashT } } } else if (zend_string_equals_literal(control_oid, LDAP_CONTROL_SORTREQUEST)) { + zval *sortkey, *tmp; + uint32_t num_keys = zend_hash_num_elements(Z_ARRVAL_P(val)); sort_keys = safe_emalloc((num_keys+1), sizeof(LDAPSortKey*), 0); tmpstrings1 = safe_emalloc(num_keys, sizeof(zend_string*), 0); @@ -590,15 +591,13 @@ static int php_ldap_control_from_array(LDAP *ld, LDAPControl** ctrl, const HashT num_tmpstrings2 = 0; for (uint32_t i = 0; i < num_keys; i++) { - zval *sortkey = zend_hash_index_find(Z_ARRVAL_P(val), i); - if (sortkey == NULL) { + if ((sortkey = zend_hash_index_find(Z_ARRVAL_P(val), i)) == NULL) { rc = -1; php_error_docref(NULL, E_WARNING, "Failed to encode sort keys list"); goto failure; } - zval *tmp = zend_hash_str_find(Z_ARRVAL_P(sortkey), "attr", sizeof("attr") - 1); - if (tmp == NULL) { + if ((tmp = zend_hash_str_find(Z_ARRVAL_P(sortkey), "attr", sizeof("attr") - 1)) == NULL) { rc = -1; zend_value_error("%s(): Sort key list must have an \"attr\" key", get_active_function_name()); goto failure; @@ -612,8 +611,7 @@ static int php_ldap_control_from_array(LDAP *ld, LDAPControl** ctrl, const HashT sort_keys[i]->attributeType = ZSTR_VAL(tmpstrings1[num_tmpstrings1]); ++num_tmpstrings1; - tmp = zend_hash_str_find(Z_ARRVAL_P(sortkey), "oid", sizeof("oid") - 1); - if (tmp == NULL) { + if ((tmp = zend_hash_str_find(Z_ARRVAL_P(sortkey), "oid", sizeof("oid") - 1)) != NULL) { tmpstrings2[num_tmpstrings2] = zval_try_get_string(tmp); if (!tmpstrings2[num_tmpstrings2]) { rc = -1; @@ -625,8 +623,7 @@ static int php_ldap_control_from_array(LDAP *ld, LDAPControl** ctrl, const HashT sort_keys[i]->orderingRule = NULL; } - tmp = zend_hash_str_find(Z_ARRVAL_P(sortkey), "reverse", sizeof("reverse") - 1); - if (tmp == NULL) { + if ((tmp = zend_hash_str_find(Z_ARRVAL_P(sortkey), "reverse", sizeof("reverse") - 1)) != NULL) { sort_keys[i]->reverseOrder = zend_is_true(tmp); } else { sort_keys[i]->reverseOrder = 0; @@ -640,12 +637,12 @@ static int php_ldap_control_from_array(LDAP *ld, LDAPControl** ctrl, const HashT php_error_docref(NULL, E_WARNING, "Failed to create sort control value: %s (%d)", ldap_err2string(rc), rc); } } else if (zend_string_equals_literal(control_oid, LDAP_CONTROL_VLVREQUEST)) { + zval* tmp; LDAPVLVInfo vlvInfo; struct berval attrValue; struct berval context; - zval *tmp = zend_hash_str_find(Z_ARRVAL_P(val), "before", sizeof("before") - 1); - if (tmp != NULL) { + if ((tmp = zend_hash_str_find(Z_ARRVAL_P(val), "before", sizeof("before") - 1)) != NULL) { vlvInfo.ldvlv_before_count = zval_get_long(tmp); } else { rc = -1; @@ -653,8 +650,7 @@ static int php_ldap_control_from_array(LDAP *ld, LDAPControl** ctrl, const HashT goto failure; } - tmp = zend_hash_str_find(Z_ARRVAL_P(val), "after", sizeof("after") - 1); - if (tmp != NULL) { + if ((tmp = zend_hash_str_find(Z_ARRVAL_P(val), "after", sizeof("after") - 1)) != NULL) { vlvInfo.ldvlv_after_count = zval_get_long(tmp); } else { rc = -1; @@ -662,8 +658,7 @@ static int php_ldap_control_from_array(LDAP *ld, LDAPControl** ctrl, const HashT goto failure; } - tmp = zend_hash_str_find(Z_ARRVAL_P(val), "attrvalue", sizeof("attrvalue") - 1); - if (tmp != NULL) { + if ((tmp = zend_hash_str_find(Z_ARRVAL_P(val), "attrvalue", sizeof("attrvalue") - 1)) != NULL) { tmpstring = zval_try_get_string(tmp); if (!tmpstring) { rc = -1; @@ -676,9 +671,8 @@ static int php_ldap_control_from_array(LDAP *ld, LDAPControl** ctrl, const HashT vlvInfo.ldvlv_attrvalue = NULL; vlvInfo.ldvlv_offset = zval_get_long(tmp); /* Find "count" key */ - zval *count_key = zend_hash_find(Z_ARRVAL_P(val), ZSTR_KNOWN(ZEND_STR_COUNT)); - if (count_key != NULL) { - vlvInfo.ldvlv_count = zval_get_long(count_key); + if ((tmp = zend_hash_find(Z_ARRVAL_P(val), ZSTR_KNOWN(ZEND_STR_COUNT))) != NULL) { + vlvInfo.ldvlv_count = zval_get_long(tmp); } else { rc = -1; zend_value_error("%s(): Array value for VLV control must have a \"count\" key", get_active_function_name()); @@ -691,8 +685,7 @@ static int php_ldap_control_from_array(LDAP *ld, LDAPControl** ctrl, const HashT } zend_string *context_str = NULL; - tmp = zend_hash_str_find(Z_ARRVAL_P(val), "context", sizeof("context") - 1); - if (tmp != NULL) { + if ((tmp = zend_hash_str_find(Z_ARRVAL_P(val), "context", sizeof("context") - 1)) != NULL) { context_str = zval_try_get_string(tmp); if (!context_str) { rc = -1; @@ -1868,6 +1861,7 @@ PHP_FUNCTION(ldap_first_entry) zval *link, *result; ldap_linkdata *ld; ldap_resultdata *ldap_result; + LDAPMessage *entry; if (zend_parse_parameters(ZEND_NUM_ARGS(), "OO", &link, ldap_link_ce, &result, ldap_result_ce) != SUCCESS) { RETURN_THROWS(); @@ -1879,8 +1873,7 @@ PHP_FUNCTION(ldap_first_entry) ldap_result = Z_LDAP_RESULT_P(result); VERIFY_LDAP_RESULT_OPEN(ldap_result); - LDAPMessage *entry = ldap_first_entry(ld->link, ldap_result->result); - if (entry == NULL) { + if ((entry = ldap_first_entry(ld->link, ldap_result->result)) == NULL) { RETVAL_FALSE; } else { object_init_ex(return_value, ldap_result_entry_ce); @@ -1898,6 +1891,7 @@ PHP_FUNCTION(ldap_next_entry) zval *link, *result_entry; ldap_linkdata *ld; ldap_result_entry *resultentry; + LDAPMessage *entry_next; if (zend_parse_parameters(ZEND_NUM_ARGS(), "OO", &link, ldap_link_ce, &result_entry, ldap_result_entry_ce) != SUCCESS) { RETURN_THROWS(); @@ -1908,8 +1902,7 @@ PHP_FUNCTION(ldap_next_entry) resultentry = Z_LDAP_RESULT_ENTRY_P(result_entry); - LDAPMessage *entry_next = ldap_next_entry(ld->link, resultentry->data); - if (entry_next == NULL) { + if ((entry_next = ldap_next_entry(ld->link, resultentry->data)) == NULL) { RETVAL_FALSE; } else { object_init_ex(return_value, ldap_result_entry_ce); @@ -2026,6 +2019,7 @@ PHP_FUNCTION(ldap_first_attribute) zval *link, *result_entry; ldap_linkdata *ld; ldap_result_entry *resultentry; + char *attribute; if (zend_parse_parameters(ZEND_NUM_ARGS(), "OO", &link, ldap_link_ce, &result_entry, ldap_result_entry_ce) != SUCCESS) { RETURN_THROWS(); @@ -2036,8 +2030,7 @@ PHP_FUNCTION(ldap_first_attribute) resultentry = Z_LDAP_RESULT_ENTRY_P(result_entry); - char *attribute = ldap_first_attribute(ld->link, resultentry->data, &resultentry->ber); - if (attribute == NULL) { + if ((attribute = ldap_first_attribute(ld->link, resultentry->data, &resultentry->ber)) == NULL) { RETURN_FALSE; } else { RETVAL_STRING(attribute); @@ -2054,6 +2047,7 @@ PHP_FUNCTION(ldap_next_attribute) zval *link, *result_entry; ldap_linkdata *ld; ldap_result_entry *resultentry; + char *attribute; if (zend_parse_parameters(ZEND_NUM_ARGS(), "OO", &link, ldap_link_ce, &result_entry, ldap_result_entry_ce) != SUCCESS) { RETURN_THROWS(); @@ -2069,8 +2063,7 @@ PHP_FUNCTION(ldap_next_attribute) RETURN_FALSE; } - char *attribute = ldap_next_attribute(ld->link, resultentry->data, resultentry->ber); - if (attribute == NULL) { + if ((attribute = ldap_next_attribute(ld->link, resultentry->data, resultentry->ber)) == NULL) { #if (LDAP_API_VERSION > 2000) || defined(HAVE_ORALDAP) if (resultentry->ber != NULL) { ber_free(resultentry->ber, 0); @@ -2148,6 +2141,7 @@ PHP_FUNCTION(ldap_get_values_len) ldap_linkdata *ld; ldap_result_entry *resultentry; char *attr; + struct berval **ldap_value_len; int num_values; size_t attr_len; @@ -2160,8 +2154,7 @@ PHP_FUNCTION(ldap_get_values_len) resultentry = Z_LDAP_RESULT_ENTRY_P(result_entry); - struct berval **ldap_value_len = ldap_get_values_len(ld->link, resultentry->data, attr); - if (ldap_value_len == NULL) { + if ((ldap_value_len = ldap_get_values_len(ld->link, resultentry->data, attr)) == NULL) { php_error_docref(NULL, E_WARNING, "Cannot get the value(s) of attribute %s", ldap_err2string(_get_lderrno(ld->link))); RETURN_FALSE; } @@ -2214,15 +2207,14 @@ PHP_FUNCTION(ldap_get_dn) PHP_FUNCTION(ldap_explode_dn) { zend_long with_attrib; - char *dn; + char *dn, **ldap_value; size_t dn_len; if (zend_parse_parameters(ZEND_NUM_ARGS(), "pl", &dn, &dn_len, &with_attrib) != SUCCESS) { RETURN_THROWS(); } - char **ldap_value = ldap_explode_dn(dn, with_attrib); - if (ldap_value == NULL) { + if (!(ldap_value = ldap_explode_dn(dn, with_attrib))) { /* Invalid parameters were passed to ldap_explode_dn */ RETURN_FALSE; } @@ -3530,6 +3522,7 @@ PHP_FUNCTION(ldap_first_reference) zval *link, *result; ldap_linkdata *ld; ldap_resultdata *ldap_result; + LDAPMessage *entry; if (zend_parse_parameters(ZEND_NUM_ARGS(), "OO", &link, ldap_link_ce, &result, ldap_result_ce) != SUCCESS) { RETURN_THROWS(); @@ -3541,8 +3534,7 @@ PHP_FUNCTION(ldap_first_reference) ldap_result = Z_LDAP_RESULT_P(result); VERIFY_LDAP_RESULT_OPEN(ldap_result); - LDAPMessage *entry = ldap_first_reference(ld->link, ldap_result->result); - if (entry == NULL) { + if ((entry = ldap_first_reference(ld->link, ldap_result->result)) == NULL) { RETVAL_FALSE; } else { object_init_ex(return_value, ldap_result_entry_ce); @@ -3560,6 +3552,7 @@ PHP_FUNCTION(ldap_next_reference) zval *link, *result_entry; ldap_linkdata *ld; ldap_result_entry *resultentry; + LDAPMessage *entry_next; if (zend_parse_parameters(ZEND_NUM_ARGS(), "OO", &link, ldap_link_ce, &result_entry, ldap_result_entry_ce) != SUCCESS) { RETURN_THROWS(); @@ -3570,8 +3563,7 @@ PHP_FUNCTION(ldap_next_reference) resultentry = Z_LDAP_RESULT_ENTRY_P(result_entry); - LDAPMessage *entry_next = ldap_first_reference(ld->link, resultentry->data); - if (entry_next == NULL) { + if ((entry_next = ldap_next_reference(ld->link, resultentry->data)) == NULL) { RETVAL_FALSE; } else { object_init_ex(return_value, ldap_result_entry_ce);