Skip to content

Commit 34e9d9e

Browse files
committed
ext/ldap: Zero out arrays and traverse them as NULL terminated list
1 parent 304a514 commit 34e9d9e

File tree

1 file changed

+16
-22
lines changed

1 file changed

+16
-22
lines changed

ext/ldap/ldap.c

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2201,9 +2201,8 @@ static void php_ldap_do_modify(INTERNAL_FUNCTION_PARAMETERS, int oper, int ext)
22012201
LDAPControl **lserverctrls = NULL;
22022202
ldap_resultdata *result;
22032203
LDAPMessage *ldap_res;
2204-
int i, j, num_attribs, msgid;
2204+
int i, num_attribs, msgid;
22052205
size_t dn_len;
2206-
int *num_berval;
22072206
zend_string *attribute;
22082207
zend_ulong index;
22092208
int is_full_add=0; /* flag for full add operation so ldap_mod_add can be put back into oper, gerrit THomson */
@@ -2222,7 +2221,8 @@ static void php_ldap_do_modify(INTERNAL_FUNCTION_PARAMETERS, int oper, int ext)
22222221
}
22232222

22242223
ldap_mods = safe_emalloc((num_attribs+1), sizeof(LDAPMod *), 0);
2225-
num_berval = safe_emalloc(num_attribs, sizeof(int), 0);
2224+
/* Zero out the list */
2225+
memset(ldap_mods, 0, sizeof(LDAPMod *) * (num_attribs+1));
22262226
zend_hash_internal_pointer_reset(Z_ARRVAL_P(entry));
22272227

22282228
/* added by gerrit thomson to fix ldap_add using ldap_mod_add */
@@ -2242,8 +2242,6 @@ static void php_ldap_do_modify(INTERNAL_FUNCTION_PARAMETERS, int oper, int ext)
22422242
} else {
22432243
php_error_docref(NULL, E_WARNING, "Unknown attribute in the data");
22442244
RETVAL_FALSE;
2245-
num_berval[i] = 0;
2246-
num_attribs = i + 1;
22472245
ldap_mods[i]->mod_bvalues = NULL;
22482246
goto cleanup;
22492247
}
@@ -2257,12 +2255,9 @@ static void php_ldap_do_modify(INTERNAL_FUNCTION_PARAMETERS, int oper, int ext)
22572255
convert_to_string(value);
22582256
if (EG(exception)) {
22592257
RETVAL_FALSE;
2260-
num_berval[i] = 0;
2261-
num_attribs = i + 1;
22622258
ldap_mods[i]->mod_bvalues = NULL;
22632259
goto cleanup;
22642260
}
2265-
num_berval[i] = 1;
22662261
ldap_mods[i]->mod_bvalues = safe_emalloc(2, sizeof(struct berval *), 0);
22672262
ldap_mods[i]->mod_bvalues[0] = (struct berval *) emalloc (sizeof(struct berval));
22682263
ldap_mods[i]->mod_bvalues[0]->bv_val = Z_STRVAL_P(value);
@@ -2274,30 +2269,25 @@ static void php_ldap_do_modify(INTERNAL_FUNCTION_PARAMETERS, int oper, int ext)
22742269
if (num_values == 0) {
22752270
zend_argument_value_error(3, "list of attribute values must not be empty");
22762271
RETVAL_FALSE;
2277-
num_berval[i] = 0;
2278-
num_attribs = i + 1;
22792272
ldap_mods[i]->mod_bvalues = NULL;
22802273
goto cleanup;
22812274
}
22822275
if (!zend_array_is_list(Z_ARRVAL_P(value))) {
22832276
zend_argument_value_error(3, "must be a list of attribute values");
22842277
RETVAL_FALSE;
2285-
num_berval[i] = 0;
2286-
num_attribs = i + 1;
22872278
ldap_mods[i]->mod_bvalues = NULL;
22882279
goto cleanup;
22892280
}
22902281

2291-
num_berval[i] = num_values;
22922282
ldap_mods[i]->mod_bvalues = safe_emalloc((num_values + 1), sizeof(struct berval *), 0);
2283+
/* Zero out the list */
2284+
memset(ldap_mods[i]->mod_bvalues, 0, sizeof(struct berval *) * (num_values+1));
22932285

22942286
zend_ulong attribute_value_index = 0;
22952287
zval *attribute_value = NULL;
22962288
ZEND_HASH_FOREACH_NUM_KEY_VAL(Z_ARRVAL_P(value), attribute_value_index, attribute_value) {
22972289
convert_to_string(attribute_value);
22982290
if (EG(exception)) {
2299-
num_berval[i] = (int)attribute_value_index;
2300-
num_attribs = i + 1;
23012291
RETVAL_FALSE;
23022292
goto cleanup;
23032293
}
@@ -2368,15 +2358,19 @@ static void php_ldap_do_modify(INTERNAL_FUNCTION_PARAMETERS, int oper, int ext)
23682358
}
23692359

23702360
cleanup:
2371-
for (i = 0; i < num_attribs; i++) {
2372-
efree(ldap_mods[i]->mod_type);
2373-
for (j = 0; j < num_berval[i]; j++) {
2374-
efree(ldap_mods[i]->mod_bvalues[j]);
2361+
for (LDAPMod **ptr = ldap_mods; *ptr != NULL; ptr++) {
2362+
LDAPMod *mod = *ptr;
2363+
if (mod->mod_type) {
2364+
efree(mod->mod_type);
2365+
}
2366+
if (mod->mod_bvalues != NULL) {
2367+
for (struct berval **bval_ptr = mod->mod_bvalues; *bval_ptr != NULL; bval_ptr++) {
2368+
efree(*bval_ptr);
2369+
}
2370+
efree(mod->mod_bvalues);
23752371
}
2376-
efree(ldap_mods[i]->mod_bvalues);
2377-
efree(ldap_mods[i]);
2372+
efree(mod);
23782373
}
2379-
efree(num_berval);
23802374
efree(ldap_mods);
23812375

23822376
if (lserverctrls) {

0 commit comments

Comments
 (0)