From e41382501912d4c8eb9e7fc0a681f5417b7b8213 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sat, 15 Aug 2009 00:52:31 +0000 Subject: [PATCH] - Improved param. parsing for ldap_read(), ldap_search() and ldap_list() --- ext/ldap/ldap.c | 130 +++++++++----------------- ext/ldap/tests/ldap_search_error.phpt | 4 +- 2 files changed, 48 insertions(+), 86 deletions(-) diff --git a/ext/ldap/ldap.c b/ext/ldap/ldap.c index 3d34151b17..84be3ada26 100644 --- a/ext/ldap/ldap.c +++ b/ext/ldap/ldap.c @@ -597,60 +597,36 @@ static void php_set_opts(LDAP *ldap, int sizelimit, int timelimit, int deref, in */ static void php_ldap_do_search(INTERNAL_FUNCTION_PARAMETERS, int scope) { - zval **link, **base_dn, **filter, **attrs, **attr, **attrsonly, **sizelimit, **timelimit, **deref; - char *ldap_base_dn = NULL; - char *ldap_filter = NULL; - char **ldap_attrs = NULL; + zval *link, *base_dn, **filter, *attrs, **attr; + long attrsonly, sizelimit, timelimit, deref; + char *ldap_base_dn = NULL, *ldap_filter = NULL, **ldap_attrs = NULL; ldap_linkdata *ld = NULL; LDAPMessage *ldap_res; - int ldap_attrsonly = 0; - int ldap_sizelimit = -1; - int ldap_timelimit = -1; - int ldap_deref = -1; - int old_ldap_sizelimit = -1; - int old_ldap_timelimit = -1; - int old_ldap_deref = -1; - int num_attribs = 0; - int i, errno; - int myargcount = ZEND_NUM_ARGS(); - int free_base_dn = 0; - int ret = 1; - - if (zend_parse_parameters(myargcount TSRMLS_CC, "ZZZ|ZZZZZ", &link, &base_dn, &filter, &attrs, &attrsonly, + int ldap_attrsonly = 0, ldap_sizelimit = -1, ldap_timelimit = -1, ldap_deref = -1; + int old_ldap_sizelimit = -1, old_ldap_timelimit = -1, old_ldap_deref = -1; + int num_attribs = 0, ret = 1, i, errno, argcount = ZEND_NUM_ARGS(); + + if (zend_parse_parameters(argcount TSRMLS_CC, "zzZ|allll", &link, &base_dn, &filter, &attrs, &attrsonly, &sizelimit, &timelimit, &deref) == FAILURE) { return; } /* Reverse -> fall through */ - switch (myargcount) { - case 8 : - convert_to_long_ex(deref); - ldap_deref = Z_LVAL_PP(deref); - - case 7 : - convert_to_long_ex(timelimit); - ldap_timelimit = Z_LVAL_PP(timelimit); - - case 6 : - convert_to_long_ex(sizelimit); - ldap_sizelimit = Z_LVAL_PP(sizelimit); - - case 5 : - convert_to_long_ex(attrsonly); - ldap_attrsonly = Z_LVAL_PP(attrsonly); - - case 4 : - if (Z_TYPE_PP(attrs) != IS_ARRAY) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "Expected Array as last element"); - ret = 0; - goto cleanup; - } - - num_attribs = zend_hash_num_elements(Z_ARRVAL_PP(attrs)); + switch (argcount) { + case 8: + ldap_deref = deref; + case 7: + ldap_timelimit = timelimit; + case 6: + ldap_sizelimit = sizelimit; + case 5: + ldap_attrsonly = attrsonly; + case 4: + num_attribs = zend_hash_num_elements(Z_ARRVAL_P(attrs)); ldap_attrs = safe_emalloc((num_attribs+1), sizeof(char *), 0); for (i = 0; ilink, ldap_base_dn, scope, ldap_filter, ldap_attrs, ldap_attrsonly); lds[i] = ld; - zend_hash_move_forward(Z_ARRVAL_PP(link)); + zend_hash_move_forward(Z_ARRVAL_P(link)); } array_init(return_value); @@ -787,16 +752,13 @@ cleanup_parallel: ldap_filter = Z_STRVAL_PP(filter); /* If anything else than string is passed, ldap_base_dn = NULL */ - if (Z_TYPE_PP(base_dn) == IS_STRING) { - ldap_base_dn = Z_STRVAL_PP(base_dn); - } else if (Z_TYPE_PP(base_dn) == IS_UNICODE) { - char *tmp = zend_unicode_to_ascii(Z_USTRVAL_PP(base_dn), Z_USTRLEN_PP(base_dn) TSRMLS_CC); - ldap_base_dn = estrdup(tmp); - efree(tmp); - free_base_dn = 1; + if (Z_TYPE_P(base_dn) == IS_STRING) { + ldap_base_dn = Z_STRVAL_P(base_dn); + } else if (Z_TYPE_P(base_dn) == IS_UNICODE) { + ldap_base_dn = zend_unicode_to_ascii(Z_USTRVAL_P(base_dn), Z_USTRLEN_P(base_dn) TSRMLS_CC); } - ld = (ldap_linkdata *) zend_fetch_resource(link TSRMLS_CC, -1, "ldap link", NULL, 1, le_link); + ld = (ldap_linkdata *) zend_fetch_resource(&link TSRMLS_CC, -1, "ldap link", NULL, 1, le_link); if (ld == NULL) { ret = 0; goto cleanup; @@ -840,7 +802,7 @@ cleanup: if (ldap_attrs != NULL) { efree(ldap_attrs); } - if (free_base_dn) { + if (Z_TYPE_P(base_dn) == IS_UNICODE) { efree(ldap_base_dn); } if (!ret) { @@ -849,7 +811,7 @@ cleanup: } /* }}} */ -/* {{{ proto resource ldap_read(resource link, string base_dn, string filter [, array attrs [, int attrsonly [, int sizelimit [, int timelimit [, int deref]]]]]) U +/* {{{ proto resource ldap_read(resource|array link, string base_dn, string filter [, array attrs [, int attrsonly [, int sizelimit [, int timelimit [, int deref]]]]]) U Read an entry */ PHP_FUNCTION(ldap_read) { @@ -857,7 +819,7 @@ PHP_FUNCTION(ldap_read) } /* }}} */ -/* {{{ proto resource ldap_list(resource link, string base_dn, string filter [, array attrs [, int attrsonly [, int sizelimit [, int timelimit [, int deref]]]]]) U +/* {{{ proto resource ldap_list(resource|array link, string base_dn, string filter [, array attrs [, int attrsonly [, int sizelimit [, int timelimit [, int deref]]]]]) U Single-level search */ PHP_FUNCTION(ldap_list) { @@ -865,7 +827,7 @@ PHP_FUNCTION(ldap_list) } /* }}} */ -/* {{{ proto resource ldap_search(resource link, string base_dn, string filter [, array attrs [, int attrsonly [, int sizelimit [, int timelimit [, int deref]]]]]) U +/* {{{ proto resource ldap_search(resource|array link, string base_dn, string filter [, array attrs [, int attrsonly [, int sizelimit [, int timelimit [, int deref]]]]]) U Search LDAP tree under base_dn */ PHP_FUNCTION(ldap_search) { diff --git a/ext/ldap/tests/ldap_search_error.phpt b/ext/ldap/tests/ldap_search_error.phpt index 8b57864b3c..c03bd2ca8f 100644 --- a/ext/ldap/tests/ldap_search_error.phpt +++ b/ext/ldap/tests/ldap_search_error.phpt @@ -45,8 +45,8 @@ NULL Warning: ldap_search(): Search: No such object in %s on line %d bool(false) -Warning: ldap_search(): Expected Array as last element in %s on line %d -bool(false) +Warning: ldap_search() expects parameter 4 to be array, null given in %s on line %d +NULL Warning: ldap_search(): Array initialization wrong in %s on line %d bool(false) -- 2.40.0