mirror of
https://github.com/libvirt/libvirt.git
synced 2025-02-25 18:55:26 -06:00
qemuDomainExtractTLSSubject: Refactor memory handling
Switch to automatic memory cleaning, use g_new0 for allocation and get rid of the 'error' label. Signed-off-by: Peter Krempa <pkrempa@redhat.com> Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
parent
518be41aaa
commit
a09cf33adf
@ -133,9 +133,9 @@ qemuMigrationCookieFree(qemuMigrationCookiePtr mig)
|
|||||||
static char *
|
static char *
|
||||||
qemuDomainExtractTLSSubject(const char *certdir)
|
qemuDomainExtractTLSSubject(const char *certdir)
|
||||||
{
|
{
|
||||||
char *certfile = NULL;
|
g_autofree char *certfile = NULL;
|
||||||
char *subject = NULL;
|
char *subject = NULL;
|
||||||
char *pemdata = NULL;
|
g_autofree char *pemdata = NULL;
|
||||||
gnutls_datum_t pemdatum;
|
gnutls_datum_t pemdatum;
|
||||||
gnutls_x509_crt_t cert;
|
gnutls_x509_crt_t cert;
|
||||||
int ret;
|
int ret;
|
||||||
@ -146,7 +146,7 @@ qemuDomainExtractTLSSubject(const char *certdir)
|
|||||||
if (virFileReadAll(certfile, 8192, &pemdata) < 0) {
|
if (virFileReadAll(certfile, 8192, &pemdata) < 0) {
|
||||||
virReportError(VIR_ERR_INTERNAL_ERROR,
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
||||||
_("unable to read server cert %s"), certfile);
|
_("unable to read server cert %s"), certfile);
|
||||||
goto error;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = gnutls_x509_crt_init(&cert);
|
ret = gnutls_x509_crt_init(&cert);
|
||||||
@ -154,7 +154,7 @@ qemuDomainExtractTLSSubject(const char *certdir)
|
|||||||
virReportError(VIR_ERR_INTERNAL_ERROR,
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
||||||
_("cannot initialize cert object: %s"),
|
_("cannot initialize cert object: %s"),
|
||||||
gnutls_strerror(ret));
|
gnutls_strerror(ret));
|
||||||
goto error;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
pemdatum.data = (unsigned char *)pemdata;
|
pemdatum.data = (unsigned char *)pemdata;
|
||||||
@ -165,25 +165,16 @@ qemuDomainExtractTLSSubject(const char *certdir)
|
|||||||
virReportError(VIR_ERR_INTERNAL_ERROR,
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
||||||
_("cannot load cert data from %s: %s"),
|
_("cannot load cert data from %s: %s"),
|
||||||
certfile, gnutls_strerror(ret));
|
certfile, gnutls_strerror(ret));
|
||||||
goto error;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
subjectlen = 1024;
|
subjectlen = 1024;
|
||||||
if (VIR_ALLOC_N(subject, subjectlen+1) < 0)
|
subject = g_new0(char, subjectlen + 1);
|
||||||
goto error;
|
|
||||||
|
|
||||||
gnutls_x509_crt_get_dn(cert, subject, &subjectlen);
|
gnutls_x509_crt_get_dn(cert, subject, &subjectlen);
|
||||||
subject[subjectlen] = '\0';
|
subject[subjectlen] = '\0';
|
||||||
|
|
||||||
VIR_FREE(certfile);
|
|
||||||
VIR_FREE(pemdata);
|
|
||||||
|
|
||||||
return subject;
|
return subject;
|
||||||
|
|
||||||
error:
|
|
||||||
VIR_FREE(certfile);
|
|
||||||
VIR_FREE(pemdata);
|
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user