From: Amos Jeffries Date: Sun, 18 Nov 2012 04:13:21 +0000 (-0700) Subject: ext_kerberos_ldap_group_acl: more memory leaks and NULL check cleanups X-Git-Tag: SQUID_3_4_0_1~490 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=b656d2120c29d906d783666c37c9ecb5ed741354;p=thirdparty%2Fsquid.git ext_kerberos_ldap_group_acl: more memory leaks and NULL check cleanups * remove some more useless NULL checks before xfree() * add 'up' temporary allocation to the create_gd() cleanup * document why gpbuf was added and is used for. Detected by Coverity Scan. Issues 740420, 740448, 740449 --- diff --git a/helpers/external_acl/kerberos_ldap_group/support_group.cc b/helpers/external_acl/kerberos_ldap_group/support_group.cc index c3e30f3f0f..af2cd8bcb7 100644 --- a/helpers/external_acl/kerberos_ldap_group/support_group.cc +++ b/helpers/external_acl/kerberos_ldap_group/support_group.cc @@ -148,15 +148,13 @@ hex_utf_char(struct main_args *margs, int flag) ichar = (ival - 48) * 16; else { debug((char *) "%s| %s: WARNING: Invalid Hex value %c\n", LogTime(), PROGRAM, ival); - if (ul) - xfree(ul); + xfree(ul); return NULL; } if (n == a - 1) { debug((char *) "%s| %s: WARNING: Invalid Hex UTF-8 string %s\n", LogTime(), PROGRAM, up); - if (ul) - xfree(ul); + xfree(ul); return NULL; } ++n; @@ -169,8 +167,7 @@ hex_utf_char(struct main_args *margs, int flag) ichar = ichar + ival - 48; else { debug((char *) "%s| %s: WARNING: Invalid Hex value %c\n", LogTime(), PROGRAM, ival); - if (ul) - xfree(ul); + xfree(ul); return NULL; } @@ -190,8 +187,7 @@ hex_utf_char(struct main_args *margs, int flag) ul[nl] = ichar; ul[nl + 1] = '\0'; debug((char *) "%s| %s: WARNING: Invalid UTF-8 sequence for Unicode %s\n", LogTime(), PROGRAM, ul); - if (ul) - xfree(ul); + xfree(ul); return NULL; } } else if (iUTF3) { @@ -220,8 +216,7 @@ hex_utf_char(struct main_args *margs, int flag) ul[nl] = ichar; ul[nl + 1] = '\0'; debug((char *) "%s| %s: WARNING: Invalid UTF-8 sequence for Unicode %s\n", LogTime(), PROGRAM, ul); - if (ul) - xfree(ul); + xfree(ul); return NULL; } } else if (iUTF4) { @@ -249,8 +244,7 @@ hex_utf_char(struct main_args *margs, int flag) ul[nl] = ichar; ul[nl + 1] = '\0'; debug((char *) "%s| %s: WARNING: Invalid UTF-8 sequence for Unicode %s\n", LogTime(), PROGRAM, ul); - if (ul) - xfree(ul); + xfree(ul); return NULL; } } else if (ichar < 0x80) { @@ -276,8 +270,7 @@ hex_utf_char(struct main_args *margs, int flag) ul[nl] = ichar; ul[nl + 1] = '\0'; debug((char *) "%s| %s: WARNING: Invalid UTF-8 sequence for Unicode %s\n", LogTime(), PROGRAM, ul); - if (ul) - xfree(ul); + xfree(ul); return NULL; } ++n; @@ -323,6 +316,10 @@ create_gd(struct main_args *margs) char *hp1 = hex_utf_char(margs, 0); char *hp2 = hex_utf_char(margs, 1); char *up = utf8dup(margs); + + // NP: will point to the start of a temporary assembly buffer used by 'p' and 'gp' + // for catenation of the hp1, hp2, and up buffer contents from above. + // necessary for xfree() because both p and gp move over the assembly area char *gpbuf = NULL; // release the allocated UTF decoding buffers @@ -330,6 +327,7 @@ create_gd(struct main_args *margs) xfree(gpbuf); \ xfree(hp1); \ xfree(hp2); \ + xfree(up); \ free_gd(gdsp); \ } diff --git a/helpers/external_acl/kerberos_ldap_group/support_ldap.cc b/helpers/external_acl/kerberos_ldap_group/support_ldap.cc index 08ac11ba7e..fe78a3c409 100644 --- a/helpers/external_acl/kerberos_ldap_group/support_ldap.cc +++ b/helpers/external_acl/kerberos_ldap_group/support_ldap.cc @@ -672,22 +672,16 @@ tool_ldap_open(struct main_args * margs, char *host, int port, char *ssl) rc = ldap_url_parse(ldapuri, &url); if (rc != LDAP_SUCCESS) { error((char *) "%s| %s: ERROR: Error while parsing url: %s\n", LogTime(), PROGRAM, ldap_err2string(rc)); - if (ldapuri) - xfree(ldapuri); - if (url) - xfree(url); + xfree(ldapuri); + xfree(url); return NULL; } #else #error "No URL parsing function" #endif - if (url) { - xfree(url); - url = NULL; - } + safe_free(url); rc = ldap_initialize(&ld, ldapuri); - if (ldapuri) - xfree(ldapuri); + xfree(ldapuri); if (rc != LDAP_SUCCESS) { error((char *) "%s| %s: ERROR: Error while initialising connection to ldap server: %s\n", LogTime(), PROGRAM, ldap_err2string(rc)); ldap_unbind(ld);