Avoid potential memory leak in krb5_sendto_set_hostname

If the hostname was already set, a typo in a test meant we were not
freeing it.  While we're at it, handle the unlikely possibility that
the existing pointer is passed as the new value.
This commit is contained in:
Viktor Dukhovni
2017-08-22 21:56:19 +00:00
committed by Nico Williams
parent d73ec2510a
commit 766e6cda8a

View File

@@ -228,13 +228,19 @@ krb5_sendto_set_hostname(krb5_context context,
krb5_sendto_ctx ctx,
const char *hostname)
{
if (ctx->hostname == NULL)
free(ctx->hostname);
ctx->hostname = strdup(hostname);
if (ctx->hostname == NULL) {
char *newname;
/*
* Handle the case where hostname == ctx->hostname by copying it first, and
* disposing of any previous value after.
*/
newname = strdup(hostname);
if (newname == NULL) {
krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
return ENOMEM;
}
free(ctx->hostname);
ctx->hostname = newname;
return 0;
}