Skip to content

Commit fbf35a1

Browse files
committed
ext/ldap: Stop assigning values in if statement
1 parent 35db6da commit fbf35a1

1 file changed

Lines changed: 50 additions & 42 deletions

File tree

ext/ldap/ldap.c

Lines changed: 50 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,8 @@ static int php_ldap_control_from_array(LDAP *ld, LDAPControl** ctrl, const HashT
451451
bool control_value_alloc = false;
452452
int rc = LDAP_SUCCESS;
453453

454-
if ((val = zend_hash_find(control_ht, ZSTR_KNOWN(ZEND_STR_VALUE))) != NULL) {
454+
val = zend_hash_find(control_ht, ZSTR_KNOWN(ZEND_STR_VALUE));
455+
if (val != NULL) {
455456
if (Z_TYPE_P(val) != IS_ARRAY) {
456457
tmpstring = zval_try_get_string(val);
457458
if (!tmpstring) {
@@ -461,13 +462,14 @@ static int php_ldap_control_from_array(LDAP *ld, LDAPControl** ctrl, const HashT
461462
control_value.bv_val = ZSTR_VAL(tmpstring);
462463
control_value.bv_len = ZSTR_LEN(tmpstring);
463464
} else if (zend_string_equals_literal(control_oid, LDAP_CONTROL_PAGEDRESULTS)) {
464-
zval* tmp;
465+
zval* tmp = zend_hash_str_find(Z_ARRVAL_P(val), "size", sizeof("size") - 1);
465466
int pagesize = 1;
466467
struct berval cookie = { 0L, NULL };
467-
if ((tmp = zend_hash_str_find(Z_ARRVAL_P(val), "size", sizeof("size") - 1)) != NULL) {
468+
if (tmp != NULL) {
468469
pagesize = zval_get_long(tmp);
469470
}
470-
if ((tmp = zend_hash_str_find(Z_ARRVAL_P(val), "cookie", sizeof("cookie") - 1)) != NULL) {
471+
tmp = zend_hash_str_find(Z_ARRVAL_P(val), "cookie", sizeof("cookie") - 1);
472+
if (tmp != NULL) {
471473
tmpstring = zval_try_get_string(tmp);
472474
if (!tmpstring) {
473475
rc = -1;
@@ -483,8 +485,8 @@ static int php_ldap_control_from_array(LDAP *ld, LDAPControl** ctrl, const HashT
483485
php_error_docref(NULL, E_WARNING, "Failed to create paged result control value: %s (%d)", ldap_err2string(rc), rc);
484486
}
485487
} else if (zend_string_equals_literal(control_oid, LDAP_CONTROL_ASSERT)) {
486-
zval* tmp;
487-
if ((tmp = zend_hash_str_find(Z_ARRVAL_P(val), "filter", sizeof("filter") - 1)) == NULL) {
488+
zval *tmp = zend_hash_str_find(Z_ARRVAL_P(val), "filter", sizeof("filter") - 1);
489+
if (tmp == NULL) {
488490
rc = -1;
489491
zend_value_error("%s(): Control must have a \"filter\" key", get_active_function_name());
490492
} else {
@@ -506,8 +508,8 @@ static int php_ldap_control_from_array(LDAP *ld, LDAPControl** ctrl, const HashT
506508
zend_string_release(assert);
507509
}
508510
} else if (zend_string_equals_literal(control_oid, LDAP_CONTROL_VALUESRETURNFILTER)) {
509-
zval* tmp;
510-
if ((tmp = zend_hash_str_find(Z_ARRVAL_P(val), "filter", sizeof("filter") - 1)) == NULL) {
511+
zval *tmp = zend_hash_str_find(Z_ARRVAL_P(val), "filter", sizeof("filter") - 1);
512+
if (tmp == NULL) {
511513
rc = -1;
512514
zend_value_error("%s(): Control must have a \"filter\" key", get_active_function_name());
513515
} else {
@@ -530,8 +532,8 @@ static int php_ldap_control_from_array(LDAP *ld, LDAPControl** ctrl, const HashT
530532
}
531533
}
532534
} else if (zend_string_equals_literal(control_oid, LDAP_CONTROL_PRE_READ) || zend_string_equals_literal(control_oid, LDAP_CONTROL_POST_READ)) {
533-
zval* tmp;
534-
if ((tmp = zend_hash_str_find(Z_ARRVAL_P(val), "attrs", sizeof("attrs") - 1)) == NULL) {
535+
zval *tmp = zend_hash_str_find(Z_ARRVAL_P(val), "attrs", sizeof("attrs") - 1);
536+
if (tmp == NULL) {
535537
rc = -1;
536538
zend_value_error("%s(): Control must have an \"attrs\" key", get_active_function_name());
537539
} else {
@@ -541,15 +543,14 @@ static int php_ldap_control_from_array(LDAP *ld, LDAPControl** ctrl, const HashT
541543
rc = -1;
542544
php_error_docref(NULL, E_WARNING, "Failed to allocate control value");
543545
} else {
544-
zval* attr;
545-
546546
uint32_t num_attribs = zend_hash_num_elements(Z_ARRVAL_P(tmp));
547547
ldap_attrs = safe_emalloc((num_attribs+1), sizeof(char *), 0);
548548
tmpstrings1 = safe_emalloc(num_attribs, sizeof(zend_string*), 0);
549549
num_tmpstrings1 = 0;
550550

551551
for (uint32_t i = 0; i < num_attribs; i++) {
552-
if ((attr = zend_hash_index_find(Z_ARRVAL_P(tmp), i)) == NULL) {
552+
zval* attr = zend_hash_index_find(Z_ARRVAL_P(tmp), i);
553+
if (attr == NULL) {
553554
rc = -1;
554555
php_error_docref(NULL, E_WARNING, "Failed to encode attribute list");
555556
goto failure;
@@ -581,8 +582,6 @@ static int php_ldap_control_from_array(LDAP *ld, LDAPControl** ctrl, const HashT
581582
}
582583
}
583584
} else if (zend_string_equals_literal(control_oid, LDAP_CONTROL_SORTREQUEST)) {
584-
zval *sortkey, *tmp;
585-
586585
uint32_t num_keys = zend_hash_num_elements(Z_ARRVAL_P(val));
587586
sort_keys = safe_emalloc((num_keys+1), sizeof(LDAPSortKey*), 0);
588587
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
591590
num_tmpstrings2 = 0;
592591

593592
for (uint32_t i = 0; i < num_keys; i++) {
594-
if ((sortkey = zend_hash_index_find(Z_ARRVAL_P(val), i)) == NULL) {
593+
zval *sortkey = zend_hash_index_find(Z_ARRVAL_P(val), i);
594+
if (sortkey == NULL) {
595595
rc = -1;
596596
php_error_docref(NULL, E_WARNING, "Failed to encode sort keys list");
597597
goto failure;
598598
}
599599

600-
if ((tmp = zend_hash_str_find(Z_ARRVAL_P(sortkey), "attr", sizeof("attr") - 1)) == NULL) {
600+
zval *tmp = zend_hash_str_find(Z_ARRVAL_P(sortkey), "attr", sizeof("attr") - 1);
601+
if (tmp == NULL) {
601602
rc = -1;
602603
zend_value_error("%s(): Sort key list must have an \"attr\" key", get_active_function_name());
603604
goto failure;
@@ -611,7 +612,8 @@ static int php_ldap_control_from_array(LDAP *ld, LDAPControl** ctrl, const HashT
611612
sort_keys[i]->attributeType = ZSTR_VAL(tmpstrings1[num_tmpstrings1]);
612613
++num_tmpstrings1;
613614

614-
if ((tmp = zend_hash_str_find(Z_ARRVAL_P(sortkey), "oid", sizeof("oid") - 1)) != NULL) {
615+
tmp = zend_hash_str_find(Z_ARRVAL_P(sortkey), "oid", sizeof("oid") - 1);
616+
if (tmp == NULL) {
615617
tmpstrings2[num_tmpstrings2] = zval_try_get_string(tmp);
616618
if (!tmpstrings2[num_tmpstrings2]) {
617619
rc = -1;
@@ -623,7 +625,8 @@ static int php_ldap_control_from_array(LDAP *ld, LDAPControl** ctrl, const HashT
623625
sort_keys[i]->orderingRule = NULL;
624626
}
625627

626-
if ((tmp = zend_hash_str_find(Z_ARRVAL_P(sortkey), "reverse", sizeof("reverse") - 1)) != NULL) {
628+
tmp = zend_hash_str_find(Z_ARRVAL_P(sortkey), "reverse", sizeof("reverse") - 1);
629+
if (tmp == NULL) {
627630
sort_keys[i]->reverseOrder = zend_is_true(tmp);
628631
} else {
629632
sort_keys[i]->reverseOrder = 0;
@@ -637,28 +640,30 @@ static int php_ldap_control_from_array(LDAP *ld, LDAPControl** ctrl, const HashT
637640
php_error_docref(NULL, E_WARNING, "Failed to create sort control value: %s (%d)", ldap_err2string(rc), rc);
638641
}
639642
} else if (zend_string_equals_literal(control_oid, LDAP_CONTROL_VLVREQUEST)) {
640-
zval* tmp;
641643
LDAPVLVInfo vlvInfo;
642644
struct berval attrValue;
643645
struct berval context;
644646

645-
if ((tmp = zend_hash_str_find(Z_ARRVAL_P(val), "before", sizeof("before") - 1)) != NULL) {
647+
zval *tmp = zend_hash_str_find(Z_ARRVAL_P(val), "before", sizeof("before") - 1);
648+
if (tmp != NULL) {
646649
vlvInfo.ldvlv_before_count = zval_get_long(tmp);
647650
} else {
648651
rc = -1;
649652
zend_value_error("%s(): Array value for VLV control must have a \"before\" key", get_active_function_name());
650653
goto failure;
651654
}
652655

653-
if ((tmp = zend_hash_str_find(Z_ARRVAL_P(val), "after", sizeof("after") - 1)) != NULL) {
656+
tmp = zend_hash_str_find(Z_ARRVAL_P(val), "after", sizeof("after") - 1);
657+
if (tmp != NULL) {
654658
vlvInfo.ldvlv_after_count = zval_get_long(tmp);
655659
} else {
656660
rc = -1;
657661
zend_value_error("%s(): Array value for VLV control must have an \"after\" key", get_active_function_name());
658662
goto failure;
659663
}
660664

661-
if ((tmp = zend_hash_str_find(Z_ARRVAL_P(val), "attrvalue", sizeof("attrvalue") - 1)) != NULL) {
665+
tmp = zend_hash_str_find(Z_ARRVAL_P(val), "attrvalue", sizeof("attrvalue") - 1);
666+
if (tmp != NULL) {
662667
tmpstring = zval_try_get_string(tmp);
663668
if (!tmpstring) {
664669
rc = -1;
@@ -671,8 +676,9 @@ static int php_ldap_control_from_array(LDAP *ld, LDAPControl** ctrl, const HashT
671676
vlvInfo.ldvlv_attrvalue = NULL;
672677
vlvInfo.ldvlv_offset = zval_get_long(tmp);
673678
/* Find "count" key */
674-
if ((tmp = zend_hash_find(Z_ARRVAL_P(val), ZSTR_KNOWN(ZEND_STR_COUNT))) != NULL) {
675-
vlvInfo.ldvlv_count = zval_get_long(tmp);
679+
zval *count_key = zend_hash_find(Z_ARRVAL_P(val), ZSTR_KNOWN(ZEND_STR_COUNT));
680+
if (count_key != NULL) {
681+
vlvInfo.ldvlv_count = zval_get_long(count_key);
676682
} else {
677683
rc = -1;
678684
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
685691
}
686692

687693
zend_string *context_str = NULL;
688-
if ((tmp = zend_hash_str_find(Z_ARRVAL_P(val), "context", sizeof("context") - 1)) != NULL) {
694+
tmp = zend_hash_str_find(Z_ARRVAL_P(val), "context", sizeof("context") - 1);
695+
if (tmp != NULL) {
689696
context_str = zval_try_get_string(tmp);
690697
if (!context_str) {
691698
rc = -1;
@@ -1861,7 +1868,6 @@ PHP_FUNCTION(ldap_first_entry)
18611868
zval *link, *result;
18621869
ldap_linkdata *ld;
18631870
ldap_resultdata *ldap_result;
1864-
LDAPMessage *entry;
18651871

18661872
if (zend_parse_parameters(ZEND_NUM_ARGS(), "OO", &link, ldap_link_ce, &result, ldap_result_ce) != SUCCESS) {
18671873
RETURN_THROWS();
@@ -1873,7 +1879,8 @@ PHP_FUNCTION(ldap_first_entry)
18731879
ldap_result = Z_LDAP_RESULT_P(result);
18741880
VERIFY_LDAP_RESULT_OPEN(ldap_result);
18751881

1876-
if ((entry = ldap_first_entry(ld->link, ldap_result->result)) == NULL) {
1882+
LDAPMessage *entry = ldap_first_entry(ld->link, ldap_result->result);
1883+
if (entry == NULL) {
18771884
RETVAL_FALSE;
18781885
} else {
18791886
object_init_ex(return_value, ldap_result_entry_ce);
@@ -1891,7 +1898,6 @@ PHP_FUNCTION(ldap_next_entry)
18911898
zval *link, *result_entry;
18921899
ldap_linkdata *ld;
18931900
ldap_result_entry *resultentry;
1894-
LDAPMessage *entry_next;
18951901

18961902
if (zend_parse_parameters(ZEND_NUM_ARGS(), "OO", &link, ldap_link_ce, &result_entry, ldap_result_entry_ce) != SUCCESS) {
18971903
RETURN_THROWS();
@@ -1902,7 +1908,8 @@ PHP_FUNCTION(ldap_next_entry)
19021908

19031909
resultentry = Z_LDAP_RESULT_ENTRY_P(result_entry);
19041910

1905-
if ((entry_next = ldap_next_entry(ld->link, resultentry->data)) == NULL) {
1911+
LDAPMessage *entry_next = ldap_next_entry(ld->link, resultentry->data);
1912+
if (entry_next == NULL) {
19061913
RETVAL_FALSE;
19071914
} else {
19081915
object_init_ex(return_value, ldap_result_entry_ce);
@@ -2019,7 +2026,6 @@ PHP_FUNCTION(ldap_first_attribute)
20192026
zval *link, *result_entry;
20202027
ldap_linkdata *ld;
20212028
ldap_result_entry *resultentry;
2022-
char *attribute;
20232029

20242030
if (zend_parse_parameters(ZEND_NUM_ARGS(), "OO", &link, ldap_link_ce, &result_entry, ldap_result_entry_ce) != SUCCESS) {
20252031
RETURN_THROWS();
@@ -2030,7 +2036,8 @@ PHP_FUNCTION(ldap_first_attribute)
20302036

20312037
resultentry = Z_LDAP_RESULT_ENTRY_P(result_entry);
20322038

2033-
if ((attribute = ldap_first_attribute(ld->link, resultentry->data, &resultentry->ber)) == NULL) {
2039+
char *attribute = ldap_first_attribute(ld->link, resultentry->data, &resultentry->ber);
2040+
if (attribute == NULL) {
20342041
RETURN_FALSE;
20352042
} else {
20362043
RETVAL_STRING(attribute);
@@ -2047,7 +2054,6 @@ PHP_FUNCTION(ldap_next_attribute)
20472054
zval *link, *result_entry;
20482055
ldap_linkdata *ld;
20492056
ldap_result_entry *resultentry;
2050-
char *attribute;
20512057

20522058
if (zend_parse_parameters(ZEND_NUM_ARGS(), "OO", &link, ldap_link_ce, &result_entry, ldap_result_entry_ce) != SUCCESS) {
20532059
RETURN_THROWS();
@@ -2063,7 +2069,8 @@ PHP_FUNCTION(ldap_next_attribute)
20632069
RETURN_FALSE;
20642070
}
20652071

2066-
if ((attribute = ldap_next_attribute(ld->link, resultentry->data, resultentry->ber)) == NULL) {
2072+
char *attribute = ldap_next_attribute(ld->link, resultentry->data, resultentry->ber);
2073+
if (attribute == NULL) {
20672074
#if (LDAP_API_VERSION > 2000) || defined(HAVE_ORALDAP)
20682075
if (resultentry->ber != NULL) {
20692076
ber_free(resultentry->ber, 0);
@@ -2141,7 +2148,6 @@ PHP_FUNCTION(ldap_get_values_len)
21412148
ldap_linkdata *ld;
21422149
ldap_result_entry *resultentry;
21432150
char *attr;
2144-
struct berval **ldap_value_len;
21452151
int num_values;
21462152
size_t attr_len;
21472153

@@ -2154,7 +2160,8 @@ PHP_FUNCTION(ldap_get_values_len)
21542160

21552161
resultentry = Z_LDAP_RESULT_ENTRY_P(result_entry);
21562162

2157-
if ((ldap_value_len = ldap_get_values_len(ld->link, resultentry->data, attr)) == NULL) {
2163+
struct berval **ldap_value_len = ldap_get_values_len(ld->link, resultentry->data, attr);
2164+
if (ldap_value_len == NULL) {
21582165
php_error_docref(NULL, E_WARNING, "Cannot get the value(s) of attribute %s", ldap_err2string(_get_lderrno(ld->link)));
21592166
RETURN_FALSE;
21602167
}
@@ -2207,14 +2214,15 @@ PHP_FUNCTION(ldap_get_dn)
22072214
PHP_FUNCTION(ldap_explode_dn)
22082215
{
22092216
zend_long with_attrib;
2210-
char *dn, **ldap_value;
2217+
char *dn;
22112218
size_t dn_len;
22122219

22132220
if (zend_parse_parameters(ZEND_NUM_ARGS(), "pl", &dn, &dn_len, &with_attrib) != SUCCESS) {
22142221
RETURN_THROWS();
22152222
}
22162223

2217-
if (!(ldap_value = ldap_explode_dn(dn, with_attrib))) {
2224+
char **ldap_value = ldap_explode_dn(dn, with_attrib);
2225+
if (ldap_value == NULL) {
22182226
/* Invalid parameters were passed to ldap_explode_dn */
22192227
RETURN_FALSE;
22202228
}
@@ -3522,7 +3530,6 @@ PHP_FUNCTION(ldap_first_reference)
35223530
zval *link, *result;
35233531
ldap_linkdata *ld;
35243532
ldap_resultdata *ldap_result;
3525-
LDAPMessage *entry;
35263533

35273534
if (zend_parse_parameters(ZEND_NUM_ARGS(), "OO", &link, ldap_link_ce, &result, ldap_result_ce) != SUCCESS) {
35283535
RETURN_THROWS();
@@ -3534,7 +3541,8 @@ PHP_FUNCTION(ldap_first_reference)
35343541
ldap_result = Z_LDAP_RESULT_P(result);
35353542
VERIFY_LDAP_RESULT_OPEN(ldap_result);
35363543

3537-
if ((entry = ldap_first_reference(ld->link, ldap_result->result)) == NULL) {
3544+
LDAPMessage *entry = ldap_first_reference(ld->link, ldap_result->result);
3545+
if (entry == NULL) {
35383546
RETVAL_FALSE;
35393547
} else {
35403548
object_init_ex(return_value, ldap_result_entry_ce);
@@ -3552,7 +3560,6 @@ PHP_FUNCTION(ldap_next_reference)
35523560
zval *link, *result_entry;
35533561
ldap_linkdata *ld;
35543562
ldap_result_entry *resultentry;
3555-
LDAPMessage *entry_next;
35563563

35573564
if (zend_parse_parameters(ZEND_NUM_ARGS(), "OO", &link, ldap_link_ce, &result_entry, ldap_result_entry_ce) != SUCCESS) {
35583565
RETURN_THROWS();
@@ -3563,7 +3570,8 @@ PHP_FUNCTION(ldap_next_reference)
35633570

35643571
resultentry = Z_LDAP_RESULT_ENTRY_P(result_entry);
35653572

3566-
if ((entry_next = ldap_next_reference(ld->link, resultentry->data)) == NULL) {
3573+
LDAPMessage *entry_next = ldap_first_reference(ld->link, resultentry->data);
3574+
if (entry_next == NULL) {
35673575
RETVAL_FALSE;
35683576
} else {
35693577
object_init_ex(return_value, ldap_result_entry_ce);

0 commit comments

Comments
 (0)