Skip to content

Commit 74c3702

Browse files
committed
ext/ldap: Stop assigning values in if statement
1 parent b55ffeb commit 74c3702

File tree

1 file changed

+50
-42
lines changed

1 file changed

+50
-42
lines changed

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_get_string(val);
457458
if (EG(exception)) {
@@ -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_get_string(tmp);
472474
if (EG(exception)) {
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_get_string(tmp);
616618
if (EG(exception)) {
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_get_string(tmp);
663668
if (EG(exception)) {
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());
@@ -684,7 +690,8 @@ static int php_ldap_control_from_array(LDAP *ld, LDAPControl** ctrl, const HashT
684690
goto failure;
685691
}
686692

687-
if ((tmp = zend_hash_str_find(Z_ARRVAL_P(val), "context", sizeof("context") - 1)) != NULL) {
693+
tmp = zend_hash_str_find(Z_ARRVAL_P(val), "context", sizeof("context") - 1);
694+
if (tmp != NULL) {
688695
tmpstring = zval_get_string(tmp);
689696
if (EG(exception)) {
690697
rc = -1;
@@ -1857,7 +1864,6 @@ PHP_FUNCTION(ldap_first_entry)
18571864
zval *link, *result;
18581865
ldap_linkdata *ld;
18591866
ldap_resultdata *ldap_result;
1860-
LDAPMessage *entry;
18611867

18621868
if (zend_parse_parameters(ZEND_NUM_ARGS(), "OO", &link, ldap_link_ce, &result, ldap_result_ce) != SUCCESS) {
18631869
RETURN_THROWS();
@@ -1869,7 +1875,8 @@ PHP_FUNCTION(ldap_first_entry)
18691875
ldap_result = Z_LDAP_RESULT_P(result);
18701876
VERIFY_LDAP_RESULT_OPEN(ldap_result);
18711877

1872-
if ((entry = ldap_first_entry(ld->link, ldap_result->result)) == NULL) {
1878+
LDAPMessage *entry = ldap_first_entry(ld->link, ldap_result->result);
1879+
if (entry == NULL) {
18731880
RETVAL_FALSE;
18741881
} else {
18751882
object_init_ex(return_value, ldap_result_entry_ce);
@@ -1887,7 +1894,6 @@ PHP_FUNCTION(ldap_next_entry)
18871894
zval *link, *result_entry;
18881895
ldap_linkdata *ld;
18891896
ldap_result_entry *resultentry;
1890-
LDAPMessage *entry_next;
18911897

18921898
if (zend_parse_parameters(ZEND_NUM_ARGS(), "OO", &link, ldap_link_ce, &result_entry, ldap_result_entry_ce) != SUCCESS) {
18931899
RETURN_THROWS();
@@ -1898,7 +1904,8 @@ PHP_FUNCTION(ldap_next_entry)
18981904

18991905
resultentry = Z_LDAP_RESULT_ENTRY_P(result_entry);
19001906

1901-
if ((entry_next = ldap_next_entry(ld->link, resultentry->data)) == NULL) {
1907+
LDAPMessage *entry_next = ldap_next_entry(ld->link, resultentry->data);
1908+
if (entry_next == NULL) {
19021909
RETVAL_FALSE;
19031910
} else {
19041911
object_init_ex(return_value, ldap_result_entry_ce);
@@ -2015,7 +2022,6 @@ PHP_FUNCTION(ldap_first_attribute)
20152022
zval *link, *result_entry;
20162023
ldap_linkdata *ld;
20172024
ldap_result_entry *resultentry;
2018-
char *attribute;
20192025

20202026
if (zend_parse_parameters(ZEND_NUM_ARGS(), "OO", &link, ldap_link_ce, &result_entry, ldap_result_entry_ce) != SUCCESS) {
20212027
RETURN_THROWS();
@@ -2026,7 +2032,8 @@ PHP_FUNCTION(ldap_first_attribute)
20262032

20272033
resultentry = Z_LDAP_RESULT_ENTRY_P(result_entry);
20282034

2029-
if ((attribute = ldap_first_attribute(ld->link, resultentry->data, &resultentry->ber)) == NULL) {
2035+
char *attribute = ldap_first_attribute(ld->link, resultentry->data, &resultentry->ber);
2036+
if (attribute == NULL) {
20302037
RETURN_FALSE;
20312038
} else {
20322039
RETVAL_STRING(attribute);
@@ -2043,7 +2050,6 @@ PHP_FUNCTION(ldap_next_attribute)
20432050
zval *link, *result_entry;
20442051
ldap_linkdata *ld;
20452052
ldap_result_entry *resultentry;
2046-
char *attribute;
20472053

20482054
if (zend_parse_parameters(ZEND_NUM_ARGS(), "OO", &link, ldap_link_ce, &result_entry, ldap_result_entry_ce) != SUCCESS) {
20492055
RETURN_THROWS();
@@ -2059,7 +2065,8 @@ PHP_FUNCTION(ldap_next_attribute)
20592065
RETURN_FALSE;
20602066
}
20612067

2062-
if ((attribute = ldap_next_attribute(ld->link, resultentry->data, resultentry->ber)) == NULL) {
2068+
char *attribute = ldap_next_attribute(ld->link, resultentry->data, resultentry->ber);
2069+
if (attribute == NULL) {
20632070
#if (LDAP_API_VERSION > 2000) || defined(HAVE_ORALDAP)
20642071
if (resultentry->ber != NULL) {
20652072
ber_free(resultentry->ber, 0);
@@ -2137,7 +2144,6 @@ PHP_FUNCTION(ldap_get_values_len)
21372144
ldap_linkdata *ld;
21382145
ldap_result_entry *resultentry;
21392146
char *attr;
2140-
struct berval **ldap_value_len;
21412147
int num_values;
21422148
size_t attr_len;
21432149

@@ -2150,7 +2156,8 @@ PHP_FUNCTION(ldap_get_values_len)
21502156

21512157
resultentry = Z_LDAP_RESULT_ENTRY_P(result_entry);
21522158

2153-
if ((ldap_value_len = ldap_get_values_len(ld->link, resultentry->data, attr)) == NULL) {
2159+
struct berval **ldap_value_len = ldap_get_values_len(ld->link, resultentry->data, attr);
2160+
if (ldap_value_len == NULL) {
21542161
php_error_docref(NULL, E_WARNING, "Cannot get the value(s) of attribute %s", ldap_err2string(_get_lderrno(ld->link)));
21552162
RETURN_FALSE;
21562163
}
@@ -2203,14 +2210,15 @@ PHP_FUNCTION(ldap_get_dn)
22032210
PHP_FUNCTION(ldap_explode_dn)
22042211
{
22052212
zend_long with_attrib;
2206-
char *dn, **ldap_value;
2213+
char *dn;
22072214
size_t dn_len;
22082215

22092216
if (zend_parse_parameters(ZEND_NUM_ARGS(), "pl", &dn, &dn_len, &with_attrib) != SUCCESS) {
22102217
RETURN_THROWS();
22112218
}
22122219

2213-
if (!(ldap_value = ldap_explode_dn(dn, with_attrib))) {
2220+
char **ldap_value = ldap_explode_dn(dn, with_attrib);
2221+
if (ldap_value == NULL) {
22142222
/* Invalid parameters were passed to ldap_explode_dn */
22152223
RETURN_FALSE;
22162224
}
@@ -3511,7 +3519,6 @@ PHP_FUNCTION(ldap_first_reference)
35113519
zval *link, *result;
35123520
ldap_linkdata *ld;
35133521
ldap_resultdata *ldap_result;
3514-
LDAPMessage *entry;
35153522

35163523
if (zend_parse_parameters(ZEND_NUM_ARGS(), "OO", &link, ldap_link_ce, &result, ldap_result_ce) != SUCCESS) {
35173524
RETURN_THROWS();
@@ -3523,7 +3530,8 @@ PHP_FUNCTION(ldap_first_reference)
35233530
ldap_result = Z_LDAP_RESULT_P(result);
35243531
VERIFY_LDAP_RESULT_OPEN(ldap_result);
35253532

3526-
if ((entry = ldap_first_reference(ld->link, ldap_result->result)) == NULL) {
3533+
LDAPMessage *entry = ldap_first_reference(ld->link, ldap_result->result);
3534+
if (entry == NULL) {
35273535
RETVAL_FALSE;
35283536
} else {
35293537
object_init_ex(return_value, ldap_result_entry_ce);
@@ -3541,7 +3549,6 @@ PHP_FUNCTION(ldap_next_reference)
35413549
zval *link, *result_entry;
35423550
ldap_linkdata *ld;
35433551
ldap_result_entry *resultentry;
3544-
LDAPMessage *entry_next;
35453552

35463553
if (zend_parse_parameters(ZEND_NUM_ARGS(), "OO", &link, ldap_link_ce, &result_entry, ldap_result_entry_ce) != SUCCESS) {
35473554
RETURN_THROWS();
@@ -3552,7 +3559,8 @@ PHP_FUNCTION(ldap_next_reference)
35523559

35533560
resultentry = Z_LDAP_RESULT_ENTRY_P(result_entry);
35543561

3555-
if ((entry_next = ldap_next_reference(ld->link, resultentry->data)) == NULL) {
3562+
LDAPMessage *entry_next = ldap_first_reference(ld->link, resultentry->data);
3563+
if (entry_next == NULL) {
35563564
RETVAL_FALSE;
35573565
} else {
35583566
object_init_ex(return_value, ldap_result_entry_ce);

0 commit comments

Comments
 (0)