From: Greg Hudson Date: Thu, 5 Feb 2009 18:26:47 +0000 (+0000) Subject: Coverity was nervous that hst_realm.c's domain_heuristic() wasn't X-Git-Tag: krb5-1.8-alpha1~713 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=14f126c33d0259bed41255b9e809a66072733f36;p=thirdparty%2Fkrb5.git Coverity was nervous that hst_realm.c's domain_heuristic() wasn't checking for a NULL return from strchr. The code was safe because a previous call to strchr on the same argments was checked, but make Coverity less nervous by storing the result of that previous call and reusing it. Also make the function conform better to our standards. git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@21891 dc483132-0cff-0310-8789-dd5450dbe970 --- diff --git a/src/lib/krb5/os/hst_realm.c b/src/lib/krb5/os/hst_realm.c index 839df80560..380e5ea449 100644 --- a/src/lib/krb5/os/hst_realm.c +++ b/src/lib/krb5/os/hst_realm.c @@ -518,27 +518,28 @@ domain_heuristic(krb5_context context, const char *domain, krb5_error_code retval = 0, r; struct addrlist alist; krb5_data drealm; - char *cp = NULL; - char *fqdn = NULL; + char *cp = NULL, *fqdn, *dot; *realm = NULL; if (limit < 0) return 0; memset(&drealm, 0, sizeof (drealm)); - if (!(fqdn = strdup(domain))) { + fqdn = strdup(domain); + if (!fqdn) { retval = ENOMEM; goto cleanup; } /* Upper case the domain (for use as a realm) */ - for (cp = fqdn; *cp; cp++) + for (cp = fqdn; *cp; cp++) { if (islower((int)(*cp))) *cp = toupper((int)*cp); + } /* Search up to limit parents, as long as we have multiple labels. */ cp = fqdn; - while (limit-- >= 0 && strchr(cp, '.') != NULL) { + while (limit-- >= 0 && (dot = strchr(cp, '.')) != NULL) { drealm.length = strlen(cp); drealm.data = cp; @@ -547,19 +548,18 @@ domain_heuristic(krb5_context context, const char *domain, r = krb5_locate_kdc(context, &drealm, &alist, 0, SOCK_DGRAM, 0); if (!r) { /* Found a KDC! */ krb5int_free_addrlist(&alist); - if (!(*realm = strdup(cp))) { + *realm = strdup(cp); + if (!*realm) { retval = ENOMEM; goto cleanup; } break; } - cp = strchr(cp, '.'); - cp++; + cp = dot + 1; } cleanup: - if (fqdn) - free(fqdn); + free(fqdn); return retval; }