*val = NULL;
 
        ber_init2( ber, NULL, LBER_USE_DER );
+       if ( ber == NULL ) {
+               return -1;
+       }
 
        (void)ber_printf( ber, "{is" /*}*/ , resultCode, diagnosticMessage ? diagnosticMessage : "" );
 
                if ( rc == -1 ) goto done;
        }
 
-       ber_printf( ber, /*{*/ "}" );
+       rc = ber_printf( ber, /*{*/ "}" );
+       if ( rc == -1 ) goto done;
 
        rc = ber_flatten2( ber, &bv, 0 );
-
-       *val = ber_bvdup( &bv );
+       if ( rc == 0 ) {
+               *val = ber_bvdup( &bv );
+       }
 
 done:;
        ber_free_buf( ber );
 
                        goto done;
                }
 
-               ber_scanf(ber, "{im" /*"}"*/, &i, &diagmsg_bv);
+               rc = LDAP_DECODING_ERROR;
+
+               if (ber_scanf(ber, "{im" /*"}"*/, &i, &diagmsg_bv) == LBER_ERROR) {
+                       goto ber_done;
+               }
                if ( diagmsg != NULL ) {
                        *diagmsg = LDAP_MALLOC( diagmsg_bv.bv_len + 1 );
                        AC_MEMCPY( *diagmsg, diagmsg_bv.bv_val, diagmsg_bv.bv_len );
 
                tag = ber_peek_tag(ber, &len);
                if (tag == LDAP_TAG_EXOP_VERIFY_CREDENTIALS_COOKIE) {
-                       ber_scanf(ber, "O", cookie);
-                   tag = ber_peek_tag(ber, &len);
+                       if (ber_scanf(ber, "O", cookie) == LBER_ERROR)
+                               goto ber_done;
+                       tag = ber_peek_tag(ber, &len);
                }
 
                if (tag == LDAP_TAG_EXOP_VERIFY_CREDENTIALS_SCREDS) {
-                       ber_scanf(ber, "O", screds);
-                   tag = ber_peek_tag(ber, &len);
+                       if (ber_scanf(ber, "O", screds) == LBER_ERROR)
+                               goto ber_done;
+                       tag = ber_peek_tag(ber, &len);
                }
 
                if (tag == LDAP_TAG_EXOP_VERIFY_CREDENTIALS_CONTROLS) {
 
                        if (!*ctrls) {
                                rc = LDAP_NO_MEMORY;
-                               goto done;
+                               goto ber_done;
                        }
 
                        *ctrls[nctrls] = NULL;
                                        ldap_controls_free(*ctrls);
                                        *ctrls = NULL;
                                    rc = LDAP_NO_MEMORY;
-                                   goto done;
+                                   goto ber_done;
                                }
 
                                tctrls[nctrls++] = tctrl;
                                if (tag == LBER_ERROR) {
                                        *ctrls = NULL;
                                        ldap_controls_free(tctrls);
-                                       rc = LDAP_DECODING_ERROR;
-                                       goto done;
+                                       goto ber_done;
                                }
 
                                tag = ber_peek_tag(ber, &len);
                        }
            }
 
+               rc = LDAP_SUCCESS;
+
+       ber_done:
            ber_free(ber, 1);
     }
 
     return ld->ld_errno;
 }
 #endif
-