Skip to content

Commit da498c4

Browse files
committed
ext/ldap: Stop assigning values in if statement
1 parent fef767d commit da498c4

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;
@@ -1852,7 +1859,6 @@ PHP_FUNCTION(ldap_first_entry)
18521859
zval *link, *result;
18531860
ldap_linkdata *ld;
18541861
ldap_resultdata *ldap_result;
1855-
LDAPMessage *entry;
18561862

18571863
if (zend_parse_parameters(ZEND_NUM_ARGS(), "OO", &link, ldap_link_ce, &result, ldap_result_ce) != SUCCESS) {
18581864
RETURN_THROWS();
@@ -1864,7 +1870,8 @@ PHP_FUNCTION(ldap_first_entry)
18641870
ldap_result = Z_LDAP_RESULT_P(result);
18651871
VERIFY_LDAP_RESULT_OPEN(ldap_result);
18661872

1867-
if ((entry = ldap_first_entry(ld->link, ldap_result->result)) == NULL) {
1873+
LDAPMessage *entry = ldap_first_entry(ld->link, ldap_result->result);
1874+
if (entry == NULL) {
18681875
RETVAL_FALSE;
18691876
} else {
18701877
object_init_ex(return_value, ldap_result_entry_ce);
@@ -1882,7 +1889,6 @@ PHP_FUNCTION(ldap_next_entry)
18821889
zval *link, *result_entry;
18831890
ldap_linkdata *ld;
18841891
ldap_result_entry *resultentry;
1885-
LDAPMessage *entry_next;
18861892

18871893
if (zend_parse_parameters(ZEND_NUM_ARGS(), "OO", &link, ldap_link_ce, &result_entry, ldap_result_entry_ce) != SUCCESS) {
18881894
RETURN_THROWS();
@@ -1893,7 +1899,8 @@ PHP_FUNCTION(ldap_next_entry)
18931899

18941900
resultentry = Z_LDAP_RESULT_ENTRY_P(result_entry);
18951901

1896-
if ((entry_next = ldap_next_entry(ld->link, resultentry->data)) == NULL) {
1902+
LDAPMessage *entry_next = ldap_next_entry(ld->link, resultentry->data);
1903+
if (entry_next == NULL) {
18971904
RETVAL_FALSE;
18981905
} else {
18991906
object_init_ex(return_value, ldap_result_entry_ce);
@@ -2010,7 +2017,6 @@ PHP_FUNCTION(ldap_first_attribute)
20102017
zval *link, *result_entry;
20112018
ldap_linkdata *ld;
20122019
ldap_result_entry *resultentry;
2013-
char *attribute;
20142020

20152021
if (zend_parse_parameters(ZEND_NUM_ARGS(), "OO", &link, ldap_link_ce, &result_entry, ldap_result_entry_ce) != SUCCESS) {
20162022
RETURN_THROWS();
@@ -2021,7 +2027,8 @@ PHP_FUNCTION(ldap_first_attribute)
20212027

20222028
resultentry = Z_LDAP_RESULT_ENTRY_P(result_entry);
20232029

2024-
if ((attribute = ldap_first_attribute(ld->link, resultentry->data, &resultentry->ber)) == NULL) {
2030+
char *attribute = ldap_first_attribute(ld->link, resultentry->data, &resultentry->ber);
2031+
if (attribute == NULL) {
20252032
RETURN_FALSE;
20262033
} else {
20272034
RETVAL_STRING(attribute);
@@ -2038,7 +2045,6 @@ PHP_FUNCTION(ldap_next_attribute)
20382045
zval *link, *result_entry;
20392046
ldap_linkdata *ld;
20402047
ldap_result_entry *resultentry;
2041-
char *attribute;
20422048

20432049
if (zend_parse_parameters(ZEND_NUM_ARGS(), "OO", &link, ldap_link_ce, &result_entry, ldap_result_entry_ce) != SUCCESS) {
20442050
RETURN_THROWS();
@@ -2054,7 +2060,8 @@ PHP_FUNCTION(ldap_next_attribute)
20542060
RETURN_FALSE;
20552061
}
20562062

2057-
if ((attribute = ldap_next_attribute(ld->link, resultentry->data, resultentry->ber)) == NULL) {
2063+
char *attribute = ldap_next_attribute(ld->link, resultentry->data, resultentry->ber);
2064+
if (attribute == NULL) {
20582065
#if (LDAP_API_VERSION > 2000) || defined(HAVE_ORALDAP)
20592066
if (resultentry->ber != NULL) {
20602067
ber_free(resultentry->ber, 0);
@@ -2132,7 +2139,6 @@ PHP_FUNCTION(ldap_get_values_len)
21322139
ldap_linkdata *ld;
21332140
ldap_result_entry *resultentry;
21342141
char *attr;
2135-
struct berval **ldap_value_len;
21362142
int num_values;
21372143
size_t attr_len;
21382144

@@ -2145,7 +2151,8 @@ PHP_FUNCTION(ldap_get_values_len)
21452151

21462152
resultentry = Z_LDAP_RESULT_ENTRY_P(result_entry);
21472153

2148-
if ((ldap_value_len = ldap_get_values_len(ld->link, resultentry->data, attr)) == NULL) {
2154+
struct berval **ldap_value_len = ldap_get_values_len(ld->link, resultentry->data, attr);
2155+
if (ldap_value_len == NULL) {
21492156
php_error_docref(NULL, E_WARNING, "Cannot get the value(s) of attribute %s", ldap_err2string(_get_lderrno(ld->link)));
21502157
RETURN_FALSE;
21512158
}
@@ -2198,14 +2205,15 @@ PHP_FUNCTION(ldap_get_dn)
21982205
PHP_FUNCTION(ldap_explode_dn)
21992206
{
22002207
zend_long with_attrib;
2201-
char *dn, **ldap_value;
2208+
char *dn;
22022209
size_t dn_len;
22032210

22042211
if (zend_parse_parameters(ZEND_NUM_ARGS(), "pl", &dn, &dn_len, &with_attrib) != SUCCESS) {
22052212
RETURN_THROWS();
22062213
}
22072214

2208-
if (!(ldap_value = ldap_explode_dn(dn, with_attrib))) {
2215+
char **ldap_value = ldap_explode_dn(dn, with_attrib);
2216+
if (ldap_value == NULL) {
22092217
/* Invalid parameters were passed to ldap_explode_dn */
22102218
RETURN_FALSE;
22112219
}
@@ -3500,7 +3508,6 @@ PHP_FUNCTION(ldap_first_reference)
35003508
zval *link, *result;
35013509
ldap_linkdata *ld;
35023510
ldap_resultdata *ldap_result;
3503-
LDAPMessage *entry;
35043511

35053512
if (zend_parse_parameters(ZEND_NUM_ARGS(), "OO", &link, ldap_link_ce, &result, ldap_result_ce) != SUCCESS) {
35063513
RETURN_THROWS();
@@ -3512,7 +3519,8 @@ PHP_FUNCTION(ldap_first_reference)
35123519
ldap_result = Z_LDAP_RESULT_P(result);
35133520
VERIFY_LDAP_RESULT_OPEN(ldap_result);
35143521

3515-
if ((entry = ldap_first_reference(ld->link, ldap_result->result)) == NULL) {
3522+
LDAPMessage *entry = ldap_first_reference(ld->link, ldap_result->result);
3523+
if (entry == NULL) {
35163524
RETVAL_FALSE;
35173525
} else {
35183526
object_init_ex(return_value, ldap_result_entry_ce);
@@ -3530,7 +3538,6 @@ PHP_FUNCTION(ldap_next_reference)
35303538
zval *link, *result_entry;
35313539
ldap_linkdata *ld;
35323540
ldap_result_entry *resultentry;
3533-
LDAPMessage *entry_next;
35343541

35353542
if (zend_parse_parameters(ZEND_NUM_ARGS(), "OO", &link, ldap_link_ce, &result_entry, ldap_result_entry_ce) != SUCCESS) {
35363543
RETURN_THROWS();
@@ -3541,7 +3548,8 @@ PHP_FUNCTION(ldap_next_reference)
35413548

35423549
resultentry = Z_LDAP_RESULT_ENTRY_P(result_entry);
35433550

3544-
if ((entry_next = ldap_next_reference(ld->link, resultentry->data)) == NULL) {
3551+
LDAPMessage *entry_next = ldap_first_reference(ld->link, resultentry->data);
3552+
if (entry_next == NULL) {
35453553
RETVAL_FALSE;
35463554
} else {
35473555
object_init_ex(return_value, ldap_result_entry_ce);

0 commit comments

Comments
 (0)