Michael B Allen wrote:
Hey,

What is the proper method for deinitializing an LDAP * context if the binding 
fails?

Consider the following code:

  ret = ldap_initialize(&ld, buf);
if (ret) { ERR("ldap_initialize: %s: %s", buf, ldap_err2string(ret));
  }
  ret = ldap_set_option(ld, LDAP_OPT_REFERRALS, LDAP_OPT_OFF);
if (ret) { ERR("ldap_set_option: %s", ldap_err2string(ret));
  } else {
      ret = ldap_set_option(ld, LDAP_OPT_PROTOCOL_VERSION, &v3);
if (ret) { ERR("ldap_set_option: %s", ldap_err2string(ret));
      } else {
if (my_ldap_bind_gssapi(ld, flags) == 0) { return 0;
          }
} }

  ldap_unbind_ext(lx->ld, NULL, NULL);

If the bind fails, the ldap_unbind_ext function asserts:

  unbind.c:49: ldap_unbind_ext: Assertion `( (ld)->ld_options.ldo_valid == 0x2 
)' failed.
  Aborted

What am I doing wrong?

You're confusing your variables, for one thing. You set up ld and then try to unbind lx->ld which is obviously something different.

Also you should not progress any further if ldap_initialize fails. Your sample there just prints an error message and then keeps on going, using an ld variable that is certainly unmodified by ldap_initialize, most likely completely uninitialized by you. You should be returning after printing that error message...

Writing code requires the utmost attention to detail, in any language, in any API. Your problems are basic mistakes caused by not paying attention, nothing particular to the LDAP API.
--
  -- Howard Chu
  Chief Architect, Symas Corp.  http://www.symas.com
  Director, Highland Sun        http://highlandsun.com/hyc
  OpenLDAP Core Team            http://www.openldap.org/project/

Reply via email to