From fbfa751a8c2bc621d9e07fb1a5a3d7e05eb16fd4 Mon Sep 17 00:00:00 2001 From: Francesco Chemolli Date: Thu, 23 Jul 2015 14:20:00 +0200 Subject: [PATCH] Rework edir_ldapexxt berEncodeLoginData for improved readability (CID 1294555) --- .../digest_auth/eDirectory/edir_ldapext.cc | 55 ++++++++++++------- 1 file changed, 34 insertions(+), 21 deletions(-) diff --git a/helpers/digest_auth/eDirectory/edir_ldapext.cc b/helpers/digest_auth/eDirectory/edir_ldapext.cc index c303d5b35a..b3d935a1f9 100644 --- a/helpers/digest_auth/eDirectory/edir_ldapext.cc +++ b/helpers/digest_auth/eDirectory/edir_ldapext.cc @@ -155,10 +155,9 @@ static int berEncodeLoginData( size_t putDataLen, void *putData) { - int err = 0; + int presult = 0; BerElement *requestBer = NULL; - unsigned int i; unsigned int elemCnt = methodIDLen / sizeof(unsigned int); char *utf8ObjPtr=NULL; @@ -174,45 +173,59 @@ static int berEncodeLoginData( utf8TagSize = strlen(utf8TagPtr)+1; /* Allocate a BerElement for the request parameters. */ - if ((requestBer = ber_alloc()) == NULL) { - err = LDAP_ENCODING_ERROR; - return err; - } + requestBer = ber_alloc(); + if (requestBer == NULL) + return LDAP_ENCODING_ERROR; /* BER encode the NMAS Version and the objectDN */ - err = (ber_printf(requestBer, "{io", NMAS_LDAP_EXT_VERSION, utf8ObjPtr, utf8ObjSize) < 0) ? LDAP_ENCODING_ERROR : 0; + presult = ber_printf(requestBer, "{io", NMAS_LDAP_EXT_VERSION, utf8ObjPtr, utf8ObjSize); + if (presult < 0) { + ber_free(requestBer, 1); + return LDAP_ENCODING_ERROR; + } /* BER encode the MethodID Length and value */ - if (!err) { - err = (ber_printf(requestBer, "{i{", methodIDLen) < 0) ? LDAP_ENCODING_ERROR : 0; + presult = ber_printf(requestBer, "{i{", methodIDLen); + if (presult < 0) { + ber_free(requestBer, 1); + return LDAP_ENCODING_ERROR; } - for (i = 0; !err && i < elemCnt; ++i) { - err = (ber_printf(requestBer, "i", methodID[i]) < 0) ? LDAP_ENCODING_ERROR : 0; + for (unsigned int i = 0; i < elemCnt; ++i) { + presult = ber_printf(requestBer, "i", methodID[i]); + if (presult < 0) { + ber_free(requestBer, 1); + return LDAP_ENCODING_ERROR; + } } - if (!err) { - err = (ber_printf(requestBer, "}}", 0) < 0) ? LDAP_ENCODING_ERROR : 0; + presult = ber_printf(requestBer, "}}", 0); + if (presult < 0) { + ber_free(requestBer, 1); + return LDAP_ENCODING_ERROR; } if (putData) { /* BER Encode the the tag and data */ - err = (ber_printf(requestBer, "oio}", utf8TagPtr, utf8TagSize, putDataLen, putData, putDataLen) < 0) ? LDAP_ENCODING_ERROR : 0; + presult = ber_printf(requestBer, "oio}", utf8TagPtr, utf8TagSize, putDataLen, putData, putDataLen); } else { /* BER Encode the the tag */ - err = (ber_printf(requestBer, "o}", utf8TagPtr, utf8TagSize) < 0) ? LDAP_ENCODING_ERROR : 0; + presult = ber_printf(requestBer, "o}", utf8TagPtr, utf8TagSize); } - - /* Convert the BER we just built to a berval that we'll send with the extended request. */ - if (!err && (ber_tag_t)ber_flatten(requestBer, requestBV) == LBER_ERROR) { - err = LDAP_ENCODING_ERROR; + if (presult < 0) { + ber_free(requestBer, 1); + return LDAP_ENCODING_ERROR; } - if (requestBer) { + /* Convert the BER we just built to a berval that we'll send with the extended request. */ + if (static_cast(ber_flatten(requestBer, requestBV)) == LBER_ERROR) { ber_free(requestBer, 1); + return LDAP_ENCODING_ERROR; } - return err; + ber_free(requestBer, 1); + + return 0; /* no error */ } /********************************************************************** -- 2.47.2