From 218f866f866ff91346b8d1483c04a16c1823ae82 Mon Sep 17 00:00:00 2001 From: Jim Meyering Date: Wed, 30 Jan 2008 19:52:16 +0000 Subject: [PATCH] Avoid segfault upon malloc failure, and plug a leak. * src/test.c (testDomainSave): Detect testDomainDumpXML failure. Free "xml". --- ChangeLog | 4 ++++ src/test.c | 9 ++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index fad8577f11..8b26a4b8da 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,9 @@ Wed Jan 30 20:49:34 CET 2008 Jim Meyering + Avoid segfault upon malloc failure, and plug a leak. + * src/test.c (testDomainSave): Detect testDomainDumpXML failure. + Free "xml". + Plug test-related leaks. * src/test.c (testLoadNetwork): Free forwardDev. (testLoadDomain): Free ctxt. diff --git a/src/test.c b/src/test.c index 35e41a3dce..85170d9961 100644 --- a/src/test.c +++ b/src/test.c @@ -1,7 +1,7 @@ /* * test.c: A "mock" hypervisor for use by application unit tests * - * Copyright (C) 2006-2007 Red Hat, Inc. + * Copyright (C) 2006-2008 Red Hat, Inc. * Copyright (C) 2006 Daniel P. Berrange * * This library is free software; you can redistribute it and/or @@ -1281,6 +1281,11 @@ static int testDomainSave(virDomainPtr domain, GET_DOMAIN(domain, -1); xml = testDomainDumpXML(domain, 0); + if (xml == NULL) { + testError(domain->conn, domain, NULL, VIR_ERR_INTERNAL_ERROR, + "cannot allocate space for metadata"); + return (-1); + } if ((fd = open(path, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR)) < 0) { testError(domain->conn, domain, NULL, VIR_ERR_INTERNAL_ERROR, @@ -1303,9 +1308,11 @@ static int testDomainSave(virDomainPtr domain, if (write(fd, xml, len) != len) { testError(domain->conn, domain, NULL, VIR_ERR_INTERNAL_ERROR, "cannot write metadata"); + free(xml); close(fd); return (-1); } + free(xml); if (close(fd) < 0) { testError(domain->conn, domain, NULL, VIR_ERR_INTERNAL_ERROR, "cannot save domain data"); -- GitLab