[libvirt] PATCH: Convert lots of code to new memory APIs

This patch converts a large amount of the XML parsing/generating code in the Xen and QEMU drivers over to using the new memory APIs, so we can get OOM checking of it. In the process I've discovered and fixed a number of double-free's, and leaks src/conf.c | 5 - src/conf.h | 18 ++-- src/qemu_conf.c | 134 +++++++++++++++--------------- src/qemu_driver.c | 114 ++++++++++++------------- src/test.c | 94 ++++++++++----------- src/util.c | 11 -- src/xend_internal.c | 127 ++++++++++++++-------------- src/xm_internal.c | 229 +++++++++++++++++++++++++++------------------------- src/xml.c | 100 +++++++++++----------- tests/virshtest.c | 23 ++--- 10 files changed, 434 insertions(+), 421 deletions(-) Dan. diff -r 2069e1bf9132 src/conf.c --- a/src/conf.c Thu May 22 14:22:39 2008 -0400 +++ b/src/conf.c Thu May 22 17:39:03 2008 -0400 @@ -105,7 +105,6 @@ * Structures allocations and deallocations * * * ************************************************************************/ -static void virConfFreeValue(virConfValuePtr val); /** * virConfFreeList: @@ -132,8 +131,8 @@ * * Free a value */ -static void -virConfFreeValue(virConfValuePtr val) +void +__virConfFreeValue(virConfValuePtr val) { if (val == NULL) return; diff -r 2069e1bf9132 src/conf.h --- a/src/conf.h Thu May 22 14:22:39 2008 -0400 +++ b/src/conf.h Thu May 22 17:39:03 2008 -0400 @@ -70,6 +70,7 @@ virConfPtr __virConfReadMem (const char *memory, int len); int __virConfFree (virConfPtr conf); +void __virConfFreeValue (virConfValuePtr val); virConfValuePtr __virConfGetValue (virConfPtr conf, const char *setting); @@ -82,14 +83,15 @@ int *len, virConfPtr conf); -#define virConfNew() (__virConfNew()) -#define virConfReadFile(f) (__virConfReadFile((f))) -#define virConfReadMem(m,l) (__virConfReadMem((m),(l))) -#define virConfFree(c) (__virConfFree((c))) -#define virConfGetValue(c,s) (__virConfGetValue((c),(s))) -#define virConfSetValue(c,s,v) (__virConfSetValue((c),(s),(v))) -#define virConfWriteFile(f,c) (__virConfWriteFile((f),(c))) -#define virConfWriteMem(m,l,c) (__virConfWriteMem((m),(l),(c))) +#define virConfNew() __virConfNew() +#define virConfReadFile(f) __virConfReadFile((f)) +#define virConfReadMem(m,l) __virConfReadMem((m),(l)) +#define virConfFree(c) __virConfFree((c)) +#define virConfFreeValue(v) __virConfFreeValue((v)) +#define virConfGetValue(c,s) __virConfGetValue((c),(s)) +#define virConfSetValue(c,s,v) __virConfSetValue((c),(s),(v)) +#define virConfWriteFile(f,c) __virConfWriteFile((f),(c)) +#define virConfWriteMem(m,l,c) __virConfWriteMem((m),(l),(c)) #ifdef __cplusplus } diff -r 2069e1bf9132 src/qemu_conf.c --- a/src/qemu_conf.c Thu May 22 14:22:39 2008 -0400 +++ b/src/qemu_conf.c Thu May 22 17:39:03 2008 -0400 @@ -122,7 +122,7 @@ p = virConfGetValue (conf, "vnc_tls_x509_cert_dir"); CHECK_TYPE ("vnc_tls_x509_cert_dir", VIR_CONF_STRING); if (p && p->str) { - free(driver->vncTLSx509certdir); + VIR_FREE(driver->vncTLSx509certdir); if (!(driver->vncTLSx509certdir = strdup(p->str))) { qemudReportError(NULL, NULL, NULL, VIR_ERR_NO_MEMORY, "%s", _("failed to allocate vncTLSx509certdir")); @@ -220,42 +220,42 @@ while (disk) { struct qemud_vm_disk_def *prev = disk; disk = disk->next; - free(prev); + VIR_FREE(prev); } while (net) { struct qemud_vm_net_def *prev = net; net = net->next; - free(prev); + VIR_FREE(prev); } while (input) { struct qemud_vm_input_def *prev = input; input = input->next; - free(prev); + VIR_FREE(prev); } while (serial) { struct qemud_vm_chr_def *prev = serial; serial = serial->next; - free(prev); + VIR_FREE(prev); } while (parallel) { struct qemud_vm_chr_def *prev = parallel; parallel = parallel->next; - free(prev); + VIR_FREE(prev); } while (sound) { struct qemud_vm_sound_def *prev = sound; sound = sound->next; - free(prev); + VIR_FREE(prev); } xmlFree(def->keymap); - free(def); + VIR_FREE(def); } void qemudFreeVM(struct qemud_vm *vm) { qemudFreeVMDef(vm->def); if (vm->newDef) qemudFreeVMDef(vm->newDef); - free(vm); + VIR_FREE(vm); } @@ -1418,8 +1418,8 @@ } for (i = 0; i < obj->nodesetval->nodeNr; i++) { - struct qemud_vm_chr_def *chr = calloc(1, sizeof(*chr)); - if (!chr) { + struct qemud_vm_chr_def *chr; + if (VIR_ALLOC(chr) < 0) { qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, "%s", _("failed to allocate space for char device")); @@ -1427,7 +1427,7 @@ } if (qemudParseCharXML(conn, chr, i, obj->nodesetval->nodeTab[i]) < 0) { - free(chr); + VIR_FREE(chr); goto cleanup; } if (ndevs) @@ -1620,7 +1620,7 @@ int i; struct qemud_vm_def *def; - if (!(def = calloc(1, sizeof(*def)))) { + if (VIR_ALLOC(def) < 0) { qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, "%s", _("failed to allocate space for xmlXPathContext")); return NULL; @@ -1660,8 +1660,7 @@ "%s", _("invalid domain type attribute")); goto error; } - free(prop); - prop = NULL; + VIR_FREE(prop); /* Extract domain name */ @@ -2033,14 +2032,14 @@ if ((obj != NULL) && (obj->type == XPATH_NODESET) && (obj->nodesetval != NULL) && (obj->nodesetval->nodeNr >= 0)) { for (i = 0; i < obj->nodesetval->nodeNr; i++) { - struct qemud_vm_disk_def *disk = calloc(1, sizeof(*disk)); - if (!disk) { + struct qemud_vm_disk_def *disk; + if (VIR_ALLOC(disk) < 0) { qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, "%s", _("failed to allocate space for disk string")); goto error; } if (qemudParseDiskXML(conn, disk, obj->nodesetval->nodeTab[i]) < 0) { - free(disk); + VIR_FREE(disk); goto error; } def->ndisks++; @@ -2083,8 +2082,8 @@ obj = xmlXPathEval(BAD_CAST "/domain/devices/console", ctxt); if ((obj != NULL) && (obj->type == XPATH_NODESET) && (obj->nodesetval != NULL) && (obj->nodesetval->nodeNr == 1)) { - struct qemud_vm_chr_def *chr = calloc(1, sizeof(*chr)); - if (!chr) { + struct qemud_vm_chr_def *chr; + if (VIR_ALLOC(chr) < 0) { qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, "%s", _("failed to allocate space for char device")); @@ -2092,7 +2091,7 @@ } if (qemudParseCharXML(conn, chr, 0, obj->nodesetval->nodeTab[0]) < 0) { - free(chr); + VIR_FREE(chr); goto error; } def->nserials = 1; @@ -2108,14 +2107,14 @@ (obj->nodesetval != NULL) && (obj->nodesetval->nodeNr >= 0)) { struct qemud_vm_net_def *prev = NULL; for (i = 0; i < obj->nodesetval->nodeNr; i++) { - struct qemud_vm_net_def *net = calloc(1, sizeof(*net)); - if (!net) { + struct qemud_vm_net_def *net; + if (VIR_ALLOC(net) < 0) { qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, "%s", _("failed to allocate space for net string")); goto error; } if (qemudParseInterfaceXML(conn, net, obj->nodesetval->nodeTab[i]) < 0) { - free(net); + VIR_FREE(net); goto error; } def->nnets++; @@ -2136,20 +2135,20 @@ (obj->nodesetval != NULL) && (obj->nodesetval->nodeNr >= 0)) { struct qemud_vm_input_def *prev = NULL; for (i = 0; i < obj->nodesetval->nodeNr; i++) { - struct qemud_vm_input_def *input = calloc(1, sizeof(*input)); - if (!input) { + struct qemud_vm_input_def *input; + if (VIR_ALLOC(input) < 0) { qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, "%s", _("failed to allocate space for input string")); goto error; } if (qemudParseInputXML(conn, def, input, obj->nodesetval->nodeTab[i]) < 0) { - free(input); + VIR_FREE(input); goto error; } /* Mouse + PS/2 is implicit with graphics, so don't store it */ if (input->bus == QEMU_INPUT_BUS_PS2 && input->type == QEMU_INPUT_TYPE_MOUSE) { - free(input); + VIR_FREE(input); continue; } def->ninputs++; @@ -2171,17 +2170,17 @@ struct qemud_vm_sound_def *prev = NULL; for (i = 0; i < obj->nodesetval->nodeNr; i++) { - struct qemud_vm_sound_def *sound = calloc(1, sizeof(*sound)); + struct qemud_vm_sound_def *sound; struct qemud_vm_sound_def *check = def->sounds; int collision = 0; - if (!sound) { + if (VIR_ALLOC(sound) < 0) { qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, "%s", _("failed to allocate space for sound dev")); goto error; } if (qemudParseSoundXML(conn, sound, obj->nodesetval->nodeTab[i]) < 0) { - free(sound); + VIR_FREE(sound); goto error; } @@ -2194,7 +2193,7 @@ check = check->next; } if (collision) { - free(sound); + VIR_FREE(sound); continue; } @@ -2223,8 +2222,7 @@ } if (!hasPS2mouse) { - input = calloc(1, sizeof(*input)); - if (!input) { + if (VIR_ALLOC(input) < 0) { qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, "%s", _("failed to allocate space for input string")); goto error; @@ -2242,7 +2240,7 @@ return def; error: - free(prop); + VIR_FREE(prop); xmlXPathFreeObject(obj); xmlXPathFreeContext(ctxt); qemudFreeVMDef(def); @@ -2264,7 +2262,6 @@ char *retval = NULL; int err; int tapfd = -1; - int *tapfds; if (net->type == QEMUD_NET_NETWORK) { if (!(network = qemudFindNetworkByName(driver, net->dst.network.name))) { @@ -2321,10 +2318,9 @@ if (!(retval = strdup(tapfdstr))) goto no_memory; - if (!(tapfds = realloc(vm->tapfds, sizeof(*tapfds) * (vm->ntapfds+2)))) - goto no_memory; - - vm->tapfds = tapfds; + if (VIR_ALLOC_N(vm->tapfds, vm->ntapfds+2) < 0) + goto no_memory; + vm->tapfds[vm->ntapfds++] = tapfd; vm->tapfds[vm->ntapfds] = -1; @@ -2334,7 +2330,7 @@ qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, "%s", _("failed to allocate space for tapfds string")); error: - free(retval); + VIR_FREE(retval); if (tapfd != -1) close(tapfd); return NULL; @@ -2831,14 +2827,14 @@ /* Add sound hardware */ if (sound) { int size = 100; - char *modstr = calloc(1, size+1); - if (!modstr) + char *modstr; + if (VIR_ALLOC_N(modstr, size+1) < 0) goto no_memory; while(sound && size > 0) { const char *model = qemudSoundModelToString(sound->model); if (!model) { - free(modstr); + VIR_FREE(modstr); qemudReportError(conn, NULL, NULL, VIR_ERR_INTERNAL_ERROR, "%s", _("invalid sound model")); goto error; @@ -2870,14 +2866,14 @@ if (vm->tapfds) { for (i = 0; vm->tapfds[i] != -1; i++) close(vm->tapfds[i]); - free(vm->tapfds); + VIR_FREE(vm->tapfds); vm->tapfds = NULL; vm->ntapfds = 0; } if (qargv) { for (i = 0 ; i < qargc ; i++) - free((qargv)[i]); - free(qargv); + VIR_FREE((qargv)[i]); + VIR_FREE(qargv); } return -1; } @@ -2925,7 +2921,7 @@ if (fd != -1) close(fd); - free(xml); + VIR_FREE(xml); return ret; } @@ -2937,13 +2933,18 @@ { xmlDocPtr xml; xmlNodePtr node; - struct qemud_vm_device_def *dev = calloc(1, sizeof(*dev)); + struct qemud_vm_device_def *dev; + + if (VIR_ALLOC(dev) < 0) { + qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, NULL); + return NULL; + } if (!(xml = xmlReadDoc(BAD_CAST xmlStr, "device.xml", NULL, XML_PARSE_NOENT | XML_PARSE_NONET | XML_PARSE_NOERROR | XML_PARSE_NOWARNING))) { qemudReportError(conn, NULL, NULL, VIR_ERR_XML_ERROR, NULL); - return NULL; + goto error; } node = xmlDocGetRootElement(xml); @@ -2976,7 +2977,7 @@ error: if (xml) xmlFreeDoc(xml); - free(dev); + VIR_FREE(dev); return NULL; } @@ -3024,7 +3025,7 @@ return vm; } - if (!(vm = calloc(1, sizeof(*vm)))) { + if (VIR_ALLOC(vm) < 0) { qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, "%s", _("failed to allocate space for vm string")); return NULL; @@ -3152,7 +3153,7 @@ cleanup: - free(xml); + VIR_FREE(xml); return ret; } @@ -3161,17 +3162,17 @@ struct qemud_dhcp_range_def *range = def->ranges; while (range) { struct qemud_dhcp_range_def *next = range->next; - free(range); + VIR_FREE(range); range = next; } - free(def); + VIR_FREE(def); } void qemudFreeNetwork(struct qemud_network *network) { qemudFreeNetworkDef(network->def); if (network->newDef) qemudFreeNetworkDef(network->newDef); - free(network); + VIR_FREE(network); } static int qemudParseBridgeXML(struct qemud_driver *driver ATTRIBUTE_UNUSED, @@ -3224,7 +3225,7 @@ continue; } - if (!(range = calloc(1, sizeof(*range)))) { + if (VIR_ALLOC(range) < 0) { qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, "%s", _("failed to allocate space for range string")); return 0; @@ -3244,7 +3245,7 @@ def->ranges = range; def->nranges++; } else { - free(range); + VIR_FREE(range); } xmlFree(start); @@ -3315,7 +3316,7 @@ xmlXPathObjectPtr obj = NULL, tmp = NULL; struct qemud_network_def *def; - if (!(def = calloc(1, sizeof(*def)))) { + if (VIR_ALLOC(def) < 0) { qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, "%s", _("failed to allocate space for network_def string")); return NULL; @@ -3489,7 +3490,7 @@ return network; } - if (!(network = calloc(1, sizeof(*network)))) { + if (VIR_ALLOC(network) < 0) { qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, "%s", _("failed to allocate space for network string")); return NULL; @@ -3700,7 +3701,7 @@ else qemudLoadNetworkConfig(driver, entry->d_name, path, xml, autostartLink); - free(xml); + VIR_FREE(xml); } closedir(dir); @@ -3836,7 +3837,7 @@ const struct qemud_vm_input_def *input; const struct qemud_vm_sound_def *sound; const struct qemud_vm_chr_def *chr; - const char *type = NULL; + const char *type = NULL, *tmp; int n, allones = 1; if (!(type = qemudVirtTypeToString(def->virtType))) { @@ -3873,7 +3874,7 @@ goto cleanup; } virBufferVSprintf(&buf, " <vcpu cpuset='%s'>%d</vcpu>\n", cpumask, def->vcpus); - free(cpumask); + VIR_FREE(cpumask); } if (def->os.bootloader[0]) @@ -4113,7 +4114,8 @@ qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, "%s", _("failed to generate XML: out of memory")); cleanup: - free(virBufferContentAndReset(&buf)); + tmp = virBufferContentAndReset(&buf); + VIR_FREE(tmp); return NULL; } @@ -4124,6 +4126,7 @@ struct qemud_network_def *def) { virBuffer buf = VIR_BUFFER_INITIALIZER; unsigned char *uuid; + char *tmp; char uuidstr[VIR_UUID_STRING_BUFLEN]; virBufferAddLit(&buf, "<network>\n"); @@ -4188,7 +4191,8 @@ no_memory: qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, "%s", _("failed to generate XML: out of memory")); - free(virBufferContentAndReset(&buf)); + tmp = virBufferContentAndReset(&buf); + VIR_FREE(tmp); return NULL; } diff -r 2069e1bf9132 src/qemu_driver.c --- a/src/qemu_driver.c Thu May 22 14:22:39 2008 -0400 +++ b/src/qemu_driver.c Thu May 22 17:39:03 2008 -0400 @@ -179,9 +179,8 @@ char *base = NULL; char driverConf[PATH_MAX]; - if (!(qemu_driver = calloc(1, sizeof(*qemu_driver)))) { + if (VIR_ALLOC(qemu_driver) < 0) return -1; - } /* Don't have a dom0 so start from 1 */ qemu_driver->nextvmid = 1; @@ -229,8 +228,7 @@ base) == -1) goto out_of_memory; - free(base); - base = NULL; + VIR_FREE(base); if ((qemu_driver->caps = qemudCapsInit()) == NULL) goto out_of_memory; @@ -256,9 +254,8 @@ out_of_memory: qemudLog (QEMUD_ERR, "%s", _("qemudStartup: out of memory")); - free (base); - free(qemu_driver); - qemu_driver = NULL; + VIR_FREE(base); + VIR_FREE(qemu_driver); return -1; } @@ -361,19 +358,18 @@ qemu_driver->nactivenetworks = 0; qemu_driver->ninactivenetworks = 0; - free(qemu_driver->configDir); - free(qemu_driver->autostartDir); - free(qemu_driver->networkConfigDir); - free(qemu_driver->networkAutostartDir); - free(qemu_driver->vncTLSx509certdir); + VIR_FREE(qemu_driver->configDir); + VIR_FREE(qemu_driver->autostartDir); + VIR_FREE(qemu_driver->networkConfigDir); + VIR_FREE(qemu_driver->networkAutostartDir); + VIR_FREE(qemu_driver->vncTLSx509certdir); if (qemu_driver->brctl) brShutdown(qemu_driver->brctl); if (qemu_driver->iptables) iptablesContextFree(qemu_driver->iptables); - free(qemu_driver); - qemu_driver = NULL; + VIR_FREE(qemu_driver); return 0; } @@ -702,13 +698,13 @@ if (lastVcpu != (vm->def->vcpus - 1)) goto error; - free(qemucpus); + VIR_FREE(qemucpus); return 0; error: VIR_FREE(vm->vcpupids); - vm->vcpupids = 0; - free(qemucpus); + vm->nvcpupids = 0; + VIR_FREE(qemucpus); /* Explicitly return success, not error. Older KVM does not have vCPU -> Thread mapping info and we don't @@ -757,7 +753,7 @@ "%s", _("resume operation failed")); return -1; } - free(info); + VIR_FREE(info); return 0; } @@ -906,16 +902,15 @@ } for (i = 0 ; argv[i] ; i++) - free(argv[i]); - free(argv); + VIR_FREE(argv[i]); + VIR_FREE(argv); if (vm->tapfds) { for (i = 0; vm->tapfds[i] != -1; i++) { close(vm->tapfds[i]); vm->tapfds[i] = -1; } - free(vm->tapfds); - vm->tapfds = NULL; + VIR_FREE(vm->tapfds); vm->ntapfds = 0; } @@ -1006,8 +1001,7 @@ vm->pid = -1; vm->id = -1; vm->state = VIR_DOMAIN_SHUTOFF; - free(vm->vcpupids); - vm->vcpupids = NULL; + VIR_FREE(vm->vcpupids); vm->nvcpupids = 0; if (vm->newDef) { @@ -1059,7 +1053,7 @@ (2 * network->def->nranges) + /* --dhcp-range 10.0.0.2,10.0.0.254 */ 1; /* NULL */ - if (!(*argv = calloc(len, sizeof(**argv)))) + if (VIR_ALLOC_N(*argv, len) < 0) goto no_memory; #define APPEND_ARG(v, n, s) do { \ @@ -1127,8 +1121,8 @@ no_memory: if (argv) { for (i = 0; (*argv)[i]; i++) - free((*argv)[i]); - free(*argv); + VIR_FREE((*argv)[i]); + VIR_FREE(*argv); } qemudReportError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, "%s", _("failed to allocate space for dnsmasq argv")); @@ -1156,8 +1150,8 @@ ret = virExecNonBlock(conn, argv, &network->dnsmasqPid, -1, NULL, NULL); for (i = 0; argv[i]; i++) - free(argv[i]); - free(argv); + VIR_FREE(argv[i]); + VIR_FREE(argv); return ret; } @@ -1626,7 +1620,6 @@ for (;;) { char data[1024]; int got = read(vm->monitor, data, sizeof(data)); - char *b; if (got == 0) goto error; @@ -1637,10 +1630,9 @@ break; goto error; } - if (!(b = realloc(buf, size+got+1))) + if (VIR_REALLOC_N(buf, size+got+1) < 0) goto error; - buf = b; memmove(buf+size, data, got); buf[size+got] = '\0'; size += got; @@ -1674,7 +1666,7 @@ if (safewrite(vm->logfile, buf, strlen(buf)) < 0) qemudLog(QEMUD_WARN, _("Unable to log VM console data: %s"), strerror(errno)); - free(buf); + VIR_FREE(buf); } return -1; } @@ -2018,7 +2010,7 @@ } vm->state = VIR_DOMAIN_PAUSED; qemudDebug("Reply %s", info); - free(info); + VIR_FREE(info); return 0; } @@ -2046,7 +2038,7 @@ } vm->state = VIR_DOMAIN_RUNNING; qemudDebug("Reply %s", info); - free(info); + VIR_FREE(info); return 0; } @@ -2230,7 +2222,7 @@ } } - if ((out = (char *)malloc(len + 1)) == NULL) + if (VIR_ALLOC_N(out, len + 1) < 0) return NULL; for (i = j = 0; in[i] != '\0'; i++) { @@ -2339,7 +2331,7 @@ if ((fd = open(path, O_CREAT|O_TRUNC|O_WRONLY, S_IRUSR|S_IWUSR)) < 0) { qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED, _("failed to create '%s'"), path); - free(xml); + VIR_FREE(xml); return -1; } @@ -2347,7 +2339,7 @@ qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED, "%s", _("failed to write save header")); close(fd); - free(xml); + VIR_FREE(xml); return -1; } @@ -2355,12 +2347,12 @@ qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED, "%s", _("failed to write xml")); close(fd); - free(xml); + VIR_FREE(xml); return -1; } close(fd); - free(xml); + VIR_FREE(xml); /* Migrate to file */ safe_path = qemudEscapeShellArg(path); @@ -2374,7 +2366,7 @@ "\"", safe_path) == -1) { qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED, "%s", _("out of memory")); - free(safe_path); + VIR_FREE(safe_path); return -1; } free(safe_path); @@ -2382,12 +2374,12 @@ if (qemudMonitorCommand(driver, vm, command, &info) < 0) { qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED, "%s", _("migrate operation failed")); - free(command); + VIR_FREE(command); return -1; } - free(info); - free(command); + VIR_FREE(info); + VIR_FREE(command); /* Shut it down */ qemudShutdownVMDaemon(dom->conn, driver, vm); @@ -2622,7 +2614,7 @@ return -1; } - if ((xml = (char *)malloc(header.xml_len)) == NULL) { + if (VIR_ALLOC_N(xml, header.xml_len) < 0) { qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED, "%s", _("out of memory")); close(fd); @@ -2633,7 +2625,7 @@ qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED, "%s", _("failed to read XML")); close(fd); - free(xml); + VIR_FREE(xml); return -1; } @@ -2642,10 +2634,10 @@ qemudReportError(conn, NULL, NULL, VIR_ERR_OPERATION_FAILED, "%s", _("failed to parse XML")); close(fd); - free(xml); + VIR_FREE(xml); return -1; } - free(xml); + VIR_FREE(xml); /* Ensure the name and UUID don't already exist in an active VM */ vm = qemudFindVMByUUID(driver, def->uuid); @@ -2688,7 +2680,7 @@ "%s", _("failed to resume domain")); return -1; } - free(info); + VIR_FREE(info); vm->state = VIR_DOMAIN_RUNNING; } @@ -2730,7 +2722,7 @@ cleanup: for (i = 0 ; i < got ; i++) - free(names[i]); + VIR_FREE(names[i]); return -1; } @@ -2830,10 +2822,10 @@ safe_path) == -1) { qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED, "%s", _("out of memory")); - free(safe_path); + VIR_FREE(safe_path); return -1; } - free(safe_path); + VIR_FREE(safe_path); } else if (asprintf(&cmd, "eject cdrom") == -1) { qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED, @@ -2844,11 +2836,11 @@ if (qemudMonitorCommand(driver, vm, cmd, &reply) < 0) { qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED, "%s", _("cannot change cdrom media")); - free(cmd); + VIR_FREE(cmd); return -1; } - free(reply); - free(cmd); + VIR_FREE(reply); + VIR_FREE(cmd); strcpy(olddisk->src, newdisk->src); olddisk->type = newdisk->type; return 0; @@ -2881,7 +2873,7 @@ if (dev->type != QEMUD_DEVICE_DISK || dev->data.disk.device != QEMUD_DISK_CDROM) { qemudReportError(dom->conn, dom, NULL, VIR_ERR_NO_SUPPORT, "%s", _("only CDROM disk devices can be attached")); - free(dev); + VIR_FREE(dev); return -1; } @@ -2896,16 +2888,16 @@ if (!disk) { qemudReportError(dom->conn, dom, NULL, VIR_ERR_NO_SUPPORT, "%s", _("CDROM not attached, cannot change media")); - free(dev); + VIR_FREE(dev); return -1; } if (qemudDomainChangeCDROM(dom, vm, disk, &dev->data.disk) < 0) { - free(dev); + VIR_FREE(dev); return -1; } - free(dev); + VIR_FREE(dev); return 0; } @@ -3243,7 +3235,7 @@ cleanup: for (i = 0 ; i < got ; i++) - free(names[i]); + VIR_FREE(names[i]); return -1; } @@ -3271,7 +3263,7 @@ cleanup: for (i = 0 ; i < got ; i++) - free(names[i]); + VIR_FREE(names[i]); return -1; } diff -r 2069e1bf9132 src/test.c --- a/src/test.c Thu May 22 14:22:39 2008 -0400 +++ b/src/test.c Thu May 22 17:39:03 2008 -0400 @@ -45,6 +45,7 @@ #include "util.h" #include "uuid.h" #include "capabilities.h" +#include "memory.h" /* Flags that determine the action to take on a shutdown or crash of a domain */ @@ -275,7 +276,7 @@ testError(conn, NULL, NULL, VIR_ERR_XML_ERROR, _("domain uuid")); goto error; } - free(str); + VIR_FREE(str); ret = virXPathLong("string(/domain/memory[1])", ctxt, &l); @@ -309,30 +310,30 @@ if (str != NULL) { if (!(onReboot = testRestartStringToFlag(str))) { testError(conn, NULL, NULL, VIR_ERR_XML_ERROR, _("domain reboot behaviour")); - free(str); + VIR_FREE(str); goto error; } - free(str); + VIR_FREE(str); } str = virXPathString("string(/domain/on_poweroff[1])", ctxt); if (str != NULL) { if (!(onPoweroff = testRestartStringToFlag(str))) { testError(conn, NULL, NULL, VIR_ERR_XML_ERROR, _("domain poweroff behaviour")); - free(str); + VIR_FREE(str); goto error; } - free(str); + VIR_FREE(str); } str = virXPathString("string(/domain/on_crash[1])", ctxt); if (str != NULL) { if (!(onCrash = testRestartStringToFlag(str))) { testError(conn, NULL, NULL, VIR_ERR_XML_ERROR, _("domain crash behaviour")); - free(str); + VIR_FREE(str); goto error; } - free(str); + VIR_FREE(str); } for (i = 0 ; i < MAX_DOMAINS ; i++) { @@ -348,7 +349,7 @@ privconn->domains[handle].id = domid; strncpy(privconn->domains[handle].name, name, sizeof(privconn->domains[handle].name)-1); privconn->domains[handle].name[sizeof(privconn->domains[handle].name)-1] = '\0'; - free(name); + VIR_FREE(name); name = NULL; if (memory > maxMem) @@ -371,7 +372,7 @@ error: xmlXPathFreeContext(ctxt); - free(name); + VIR_FREE(name); return (-1); } @@ -464,7 +465,7 @@ testError(conn, NULL, NULL, VIR_ERR_XML_ERROR, _("network uuid")); goto error; } - free(str); + VIR_FREE(str); forward = virXPathBoolean("count(/network/forward) != 0", ctxt); @@ -512,10 +513,10 @@ privconn->networks[handle].name[sizeof(privconn->networks[handle].name)-1] = '\0'; strncpy(privconn->networks[handle].bridge, bridge ? bridge : name, sizeof(privconn->networks[handle].bridge)-1); privconn->networks[handle].bridge[sizeof(privconn->networks[handle].bridge)-1] = '\0'; - free(name); + VIR_FREE(name); name = NULL; if (bridge) { - free(bridge); + VIR_FREE(bridge); bridge = NULL; } @@ -524,32 +525,32 @@ if (forwardDev) { strncpy(privconn->networks[handle].forwardDev, forwardDev, sizeof(privconn->networks[handle].forwardDev)-1); privconn->networks[handle].forwardDev[sizeof(privconn->networks[handle].forwardDev)-1] = '\0'; - free(forwardDev); + VIR_FREE(forwardDev); } strncpy(privconn->networks[handle].ipAddress, ipaddress, sizeof(privconn->networks[handle].ipAddress)-1); privconn->networks[handle].ipAddress[sizeof(privconn->networks[handle].ipAddress)-1] = '\0'; - free(ipaddress); + VIR_FREE(ipaddress); strncpy(privconn->networks[handle].ipNetmask, ipnetmask, sizeof(privconn->networks[handle].ipNetmask)-1); privconn->networks[handle].ipNetmask[sizeof(privconn->networks[handle].ipNetmask)-1] = '\0'; - free(ipnetmask); + VIR_FREE(ipnetmask); strncpy(privconn->networks[handle].dhcpStart, dhcpstart, sizeof(privconn->networks[handle].dhcpStart)-1); privconn->networks[handle].dhcpStart[sizeof(privconn->networks[handle].dhcpStart)-1] = '\0'; - free(dhcpstart); + VIR_FREE(dhcpstart); strncpy(privconn->networks[handle].dhcpEnd, dhcpend, sizeof(privconn->networks[handle].dhcpEnd)-1); privconn->networks[handle].dhcpEnd[sizeof(privconn->networks[handle].dhcpEnd)-1] = '\0'; - free(dhcpend); + VIR_FREE(dhcpend); xmlXPathFreeContext(ctxt); return (handle); error: xmlXPathFreeContext(ctxt); - free (forwardDev); - free(ipaddress); - free(ipnetmask); - free(dhcpstart); - free(dhcpend); - free(name); + VIR_FREE (forwardDev); + VIR_FREE(ipaddress); + VIR_FREE(ipnetmask); + VIR_FREE(dhcpstart); + VIR_FREE(dhcpend); + VIR_FREE(name); return (-1); } @@ -601,8 +602,8 @@ static int testOpenDefault(virConnectPtr conn) { int u; struct timeval tv; - testConnPtr privconn = malloc(sizeof(*privconn)); - if (!privconn) { + testConnPtr privconn; + if (VIR_ALLOC(privconn) < 0) { testError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, "testConn"); return VIR_DRV_OPEN_ERROR; } @@ -677,7 +678,9 @@ offset = strrchr(relativeTo, '/'); if ((baseLen = (offset-relativeTo+1))) { - char *absFile = malloc(baseLen + strlen(filename) + 1); + char *absFile; + if (VIR_ALLOC_N(absFile, baseLen + strlen(filename) + 1) < 0) + return NULL; strncpy(absFile, relativeTo, baseLen); absFile[baseLen] = '\0'; strcat(absFile, filename); @@ -697,8 +700,8 @@ xmlNodePtr *domains, *networks = NULL; xmlXPathContextPtr ctxt = NULL; virNodeInfoPtr nodeInfo; - testConnPtr privconn = calloc(1, sizeof(*privconn)); - if (!privconn) { + testConnPtr privconn; + if (VIR_ALLOC(privconn) < 0) { testError(NULL, NULL, NULL, VIR_ERR_NO_MEMORY, "testConn"); return VIR_DRV_OPEN_ERROR; } @@ -794,7 +797,7 @@ if (str != NULL) { strncpy(nodeInfo->model, str, sizeof(nodeInfo->model)-1); nodeInfo->model[sizeof(nodeInfo->model)-1] = '\0'; - free(str); + VIR_FREE(str); } ret = virXPathLong("string(/node/memory[1])", ctxt, &l); @@ -815,21 +818,21 @@ xmlChar *domFile = xmlGetProp(domains[i], BAD_CAST "file"); char *absFile = testBuildFilename(file, (const char *)domFile); int domid = privconn->nextDomID++, handle; - free(domFile); + VIR_FREE(domFile); if (!absFile) { testError(NULL, NULL, NULL, VIR_ERR_INTERNAL_ERROR, _("resolving domain filename")); goto error; } if ((handle = testLoadDomainFromFile(conn, domid, absFile)) < 0) { - free(absFile); + VIR_FREE(absFile); goto error; } privconn->domains[handle].config = 1; - free(absFile); + VIR_FREE(absFile); privconn->numDomains++; } if (domains != NULL) { - free(domains); + VIR_FREE(domains); domains = NULL; } @@ -840,21 +843,21 @@ xmlChar *netFile = xmlGetProp(networks[i], BAD_CAST "file"); char *absFile = testBuildFilename(file, (const char *)netFile); int handle; - free(netFile); + VIR_FREE(netFile); if (!absFile) { testError(NULL, NULL, NULL, VIR_ERR_INTERNAL_ERROR, _("resolving network filename")); goto error; } if ((handle = testLoadNetworkFromFile(conn, absFile)) < 0) { - free(absFile); + VIR_FREE(absFile); goto error; } privconn->networks[handle].config = 1; - free(absFile); + VIR_FREE(absFile); privconn->numNetworks++; } if (networks != NULL) { - free(networks); + VIR_FREE(networks); networks = NULL; } } @@ -866,13 +869,13 @@ error: xmlXPathFreeContext(ctxt); - free(domains); - free(networks); + VIR_FREE(domains); + VIR_FREE(networks); if (xml) xmlFreeDoc(xml); if (fd != -1) close(fd); - free(privconn); + VIR_FREE(privconn); conn->privateData = NULL; return VIR_DRV_OPEN_ERROR; } @@ -945,7 +948,7 @@ static int testClose(virConnectPtr conn) { GET_CONNECTION(conn, -1); - free (privconn); + VIR_FREE (privconn); conn->privateData = conn; return 0; } @@ -1361,11 +1364,11 @@ if (safewrite(fd, xml, len) < 0) { testError(domain->conn, domain, NULL, VIR_ERR_INTERNAL_ERROR, _("cannot write metadata")); - free(xml); + VIR_FREE(xml); close(fd); return (-1); } - free(xml); + VIR_FREE(xml); if (close(fd) < 0) { testError(domain->conn, domain, NULL, VIR_ERR_INTERNAL_ERROR, _("cannot save domain data")); @@ -1419,8 +1422,7 @@ close(fd); return (-1); } - xml = malloc(len+1); - if (!xml) { + if (VIR_ALLOC_N(xml, len+1) < 0) { testError(conn, NULL, NULL, VIR_ERR_NO_MEMORY, "xml"); close(fd); return (-1); @@ -1435,7 +1437,7 @@ close(fd); domid = privconn->nextDomID++; ret = testLoadDomainFromDoc(conn, domid, xml); - free(xml); + VIR_FREE(xml); return ret < 0 ? -1 : 0; } diff -r 2069e1bf9132 src/util.c --- a/src/util.c Thu May 22 14:22:39 2008 -0400 +++ b/src/util.c Thu May 22 17:39:03 2008 -0400 @@ -48,7 +48,7 @@ #include "event.h" #include "buf.h" #include "util.h" - +#include "memory.h" #include "util-lib.c" #ifndef MIN @@ -303,19 +303,14 @@ size_t requested; if (size + BUFSIZ + 1 > alloc) { - char *new_buf; - alloc += alloc / 2; if (alloc < size + BUFSIZ + 1) alloc = size + BUFSIZ + 1; - new_buf = realloc (buf, alloc); - if (!new_buf) { + if (VIR_ALLOC_N(buf, alloc) < 0) { save_errno = errno; break; } - - buf = new_buf; } /* Ensure that (size + requested <= max_len); */ @@ -359,7 +354,7 @@ } if (len > maxlen || (int)len != len) { - free(s); + VIR_FREE(s); virLog("File '%s' is too large %d, max %d", path, (int)len, maxlen); goto error; diff -r 2069e1bf9132 src/xend_internal.c --- a/src/xend_internal.c Thu May 22 14:22:39 2008 -0400 +++ b/src/xend_internal.c Thu May 22 17:39:03 2008 -0400 @@ -44,6 +44,7 @@ #include "xend_internal.h" #include "xen_internal.h" /* for DOM0_INTERFACE_VERSION */ #include "xs_internal.h" /* To extract VNC port & Serial console TTY */ +#include "memory.h" /* required for cpumap_t */ #include <xen/dom0_ops.h> @@ -630,7 +631,7 @@ content = virBufferContentAndReset(&buf); ret = http2unix(xend, xend_post(xend, path, content, error, n_error)); - free(content); + VIR_FREE(content); return ret; } @@ -825,14 +826,15 @@ urlencode(const char *string) { size_t len = strlen(string); - char *buffer = malloc(len * 3 + 1); - char *ptr = buffer; + char *buffer; + char *ptr; size_t i; - if (buffer == NULL) { + if (VIR_ALLOC_N(buffer, len * 3 + 1) < 0) { virXendError(NULL, VIR_ERR_NO_MEMORY, _("allocate new buffer")); return (NULL); } + ptr = buffer; for (i = 0; i < len; i++) { switch (string[i]) { case ' ': @@ -909,6 +911,7 @@ char *sound_string_to_xml(const char *sound) { virBuffer buf = VIR_BUFFER_INITIALIZER; + char *tmp; while (sound) { int modelsize, valid, collision = 0; @@ -925,15 +928,16 @@ if (STREQ(model, "all")) { int i; if (virBufferError(&buf)) { - free(model); - goto error; - } - free(virBufferContentAndReset(&buf)); + VIR_FREE(model); + goto error; + } + tmp = virBufferContentAndReset(&buf); + VIR_FREE(tmp); for (i=0; i < sizeof(sound_models)/sizeof(*sound_models); ++i) virBufferVSprintf(&buf, " <sound model='%s'/>\n", sound_models[i]); - free(model); + VIR_FREE(model); break; } } @@ -944,7 +948,7 @@ virBufferVSprintf(&buf, " <sound model='%s'/>\n", model); sound = (model_end ? ++model_end : NULL); - free(model); + VIR_FREE(model); } if (virBufferError(&buf)) @@ -952,7 +956,8 @@ return virBufferContentAndReset(&buf); error: - free(virBufferContentAndReset(&buf)); + tmp = virBufferContentAndReset(&buf); + VIR_FREE(tmp); return NULL; } @@ -1092,8 +1097,7 @@ count++; } - ptr = malloc((count + 1) * sizeof(char *) + extra); - if (ptr == NULL) + if (VIR_ALLOC_N(ptr, count + 1 + extra) < 0) goto error; ret = (char **) ptr; @@ -1149,7 +1153,7 @@ ret = xend_op(xend, "", "op", "create", "config", ptr, NULL); serrno = errno; - free(ptr); + VIR_FREE(ptr); errno = serrno; return ret; @@ -1250,10 +1254,8 @@ error: sexpr_free(root); - if (domname && *domname) { - free(*domname); - *domname = NULL; - } + if (domname) + VIR_FREE(*domname); return (-1); } @@ -1694,11 +1696,11 @@ error: - free(path); - free(bindHost); - free(bindPort); - free(connectHost); - free(connectPort); + VIR_FREE(path); + VIR_FREE(bindHost); + VIR_FREE(bindPort); + VIR_FREE(connectHost); + VIR_FREE(connectPort); return ret; } @@ -1723,7 +1725,7 @@ { struct sexpr *cur, *node; const char *tmp; - char *tty; + char *tty, *val; virBuffer buf = VIR_BUFFER_INITIALIZER; int hvm = 0, bootloader = 0, vfb = 0; int domid = -1; @@ -1906,8 +1908,7 @@ goto bad_parse; } - drvName = malloc((offset-src)+1); - if (!drvName) { + if (VIR_ALLOC_N(drvName, (offset-src)+1) < 0) { virXendError(conn, VIR_ERR_NO_MEMORY, _("allocate new buffer")); goto bad_parse; @@ -1925,8 +1926,7 @@ goto bad_parse; } - drvType = malloc((offset-src)+1); - if (!drvType) { + if (VIR_ALLOC_N(drvType, (offset-src)+1)< 0) { virXendError(conn, VIR_ERR_NO_MEMORY, _("allocate new buffer")); goto bad_parse; @@ -2004,8 +2004,8 @@ virBufferAddLit(&buf, " </disk>\n"); bad_parse: - free(drvName); - free(drvType); + VIR_FREE(drvName); + VIR_FREE(drvType); } else if (sexpr_lookup(node, "device/vif")) { const char *tmp2, *model; tmp2 = sexpr_node(node, "device/vif/script"); @@ -2191,7 +2191,7 @@ virBufferAddLit(&buf, " <target port='0'/>\n"); virBufferAddLit(&buf, " </console>\n"); } - free(tty); + VIR_FREE(tty); if (hvm) { if (sexpr_node(root, "domain/image/hvm/soundhw")) { @@ -2200,7 +2200,7 @@ if (tmp && *tmp) { if ((soundxml = sound_string_to_xml(tmp))) { virBufferVSprintf(&buf, "%s", soundxml); - free(soundxml); + VIR_FREE(soundxml); } else { virXendError(conn, VIR_ERR_INTERNAL_ERROR, _("parsing soundhw string failed.")); @@ -2221,7 +2221,8 @@ return virBufferContentAndReset(&buf); error: - free(virBufferContentAndReset(&buf)); + val = virBufferContentAndReset(&buf); + VIR_FREE(val); return (NULL); } @@ -2379,11 +2380,9 @@ numCpus = sexpr_int(root, "node/nr_cpus"); - cpuset = malloc(numCpus * sizeof(*cpuset)); - if (cpuset == NULL) + if (VIR_ALLOC_N(cpuset, numCpus) < 0) goto memory_error; - cpuNums = malloc(numCpus * sizeof(*cpuNums)); - if (cpuNums == NULL) + if (VIR_ALLOC_N(cpuNums, numCpus) < 0) goto memory_error; cur = nodeToCpu; @@ -2423,21 +2422,21 @@ cpuNums) < 0) goto memory_error; } - free(cpuNums); - free(cpuset); + VIR_FREE(cpuNums); + VIR_FREE(cpuset); return (0); parse_error: virXendError(conn, VIR_ERR_XEN_CALL, _("topology syntax error")); error: - free(cpuNums); - free(cpuset); + VIR_FREE(cpuNums); + VIR_FREE(cpuset); return (-1); memory_error: - free(cpuNums); - free(cpuset); + VIR_FREE(cpuNums); + VIR_FREE(cpuset); virXendError(conn, VIR_ERR_NO_MEMORY, _("allocate buffer")); return (-1); } @@ -3269,7 +3268,7 @@ } error: - sexpr_free(root); + sexpr_free(root); return(ret); } @@ -3302,7 +3301,7 @@ } error: - sexpr_free(root); + sexpr_free(root); return(ret); } #endif /* ! PROXY */ @@ -3331,11 +3330,11 @@ if (ret == NULL) return NULL; ret->id = id; - free(name); + VIR_FREE(name); return (ret); error: - free(name); + VIR_FREE(name); return (NULL); } @@ -3543,7 +3542,7 @@ } tmp++; } - free(names); + VIR_FREE(names); } else { /* New approach for xen >= 3.0.4 */ char *domname = NULL; char uuidstr[VIR_UUID_STRING_BUFLEN]; @@ -3569,7 +3568,7 @@ if (ret == NULL) return NULL; ret->id = id; - free(name); + VIR_FREE(name); return (ret); } @@ -3610,14 +3609,14 @@ if ((sexpr == NULL) || (name == NULL)) { virXendError(conn, VIR_ERR_XML_ERROR, _("failed to parse domain description")); - free(sexpr); - free(name); + VIR_FREE(sexpr); + VIR_FREE(name); return (NULL); } ret = xenDaemonDomainCreateLinux(conn, sexpr); - free(sexpr); + VIR_FREE(sexpr); if (ret != 0) { goto error; } @@ -3633,7 +3632,7 @@ if ((ret = xenDaemonDomainResume(dom)) < 0) goto error; - free(name); + VIR_FREE(name); return (dom); @@ -3643,7 +3642,7 @@ xenDaemonDomainDestroy(dom); virUnrefDomain(dom); } - free(name); + VIR_FREE(name); return (NULL); } @@ -3683,7 +3682,7 @@ str = virDomainGetOSType(domain); if (STREQ(str, "hvm")) hvm = 1; - free(str); + VIR_FREE(str); sexpr = virParseXMLDevice(domain->conn, xml, hvm, priv->xendConfigVersion); if (sexpr == NULL) return (-1); @@ -3702,7 +3701,7 @@ ret = xend_op(domain->conn, domain->name, "op", "device_configure", "config", conf, "dev", ref, NULL); } - free(sexpr); + VIR_FREE(sexpr); return ret; } @@ -3810,7 +3809,7 @@ // Change the autostart value in place, then define the new sexpr autonode = sexpr_lookup(root, "domain/on_xend_start"); - free(autonode->u.s.car->u.value); + VIR_FREE(autonode->u.s.car->u.value); autonode->u.s.car->u.value = (autostart ? strdup("start") : strdup("ignore")); if (!(autonode->u.s.car->u.value)) { @@ -3996,7 +3995,7 @@ "port", port, "resource", "0", /* required, xend ignores it */ NULL); - free (hostname); + VIR_FREE (hostname); DEBUG0("migration done"); @@ -4028,14 +4027,14 @@ if ((sexpr == NULL) || (name == NULL)) { virXendError(conn, VIR_ERR_XML_ERROR, _("failed to parse domain description")); - free(sexpr); - free(name); + VIR_FREE(sexpr); + VIR_FREE(name); return (NULL); } ret = xend_op(conn, "", "op", "new", "config", sexpr, NULL); - free(sexpr); + VIR_FREE(sexpr); if (ret != 0) { fprintf(stderr, _("Failed to create inactive domain %s\n"), name); goto error; @@ -4048,7 +4047,7 @@ return (dom); error: - free(name); + VIR_FREE(name); return (NULL); } int xenDaemonDomainCreate(virDomainPtr domain) @@ -4318,7 +4317,7 @@ error: sexpr_free(root); - free(sched_type); + VIR_FREE(sched_type); return (ret); } @@ -4430,7 +4429,7 @@ error: sexpr_free(root); - free(sched_type); + VIR_FREE(sched_type); return (ret); } diff -r 2069e1bf9132 src/xm_internal.c --- a/src/xm_internal.c Thu May 22 14:22:39 2008 -0400 +++ b/src/xm_internal.c Thu May 22 17:39:03 2008 -0400 @@ -54,6 +54,7 @@ #include "buf.h" #include "uuid.h" #include "util.h" +#include "memory.h" static int xenXMConfigSetString(virConfPtr conf, const char *setting, const char *str); @@ -241,16 +242,14 @@ /* Had better have a name...*/ if (xenXMConfigGetString(conf, "name", &name) < 0) { virConfValuePtr value; - value = malloc(sizeof(*value)); - if (!value) { + if (VIR_ALLOC(value) < 0) return (-1); - } /* Set name based on filename */ value->type = VIR_CONF_STRING; value->str = strdup(filename); if (!value->str) { - free(value); + VIR_FREE(value); return (-1); } if (virConfSetValue(conf, "name", value) < 0) @@ -262,10 +261,8 @@ virConfValuePtr value; char uuidstr[VIR_UUID_STRING_BUFLEN]; - value = malloc(sizeof(*value)); - if (!value) { + if (VIR_ALLOC(value) < 0) return (-1); - } /* ... then generate one */ virUUIDGenerate(uuid); @@ -274,7 +271,7 @@ value->type = VIR_CONF_STRING; value->str = strdup(uuidstr); if (!value->str) { - free(value); + VIR_FREE(value); return (-1); } @@ -289,7 +286,7 @@ static void xenXMConfigFree(void *payload, const char *key ATTRIBUTE_UNUSED) { xenXMConfCachePtr entry = (xenXMConfCachePtr)payload; virConfFree(entry->conf); - free(entry); + VIR_FREE(entry); } @@ -411,7 +408,7 @@ entry->conf = NULL; } else { /* Completely new entry */ newborn = 1; - if (!(entry = malloc(sizeof(*entry)))) { + if (VIR_ALLOC(entry) < 0) { xenXMError (conn, VIR_ERR_NO_MEMORY, strerror (errno)); goto cleanup; } @@ -424,7 +421,7 @@ if (!newborn) { virHashRemoveEntry(configCache, path, NULL); } - free(entry); + VIR_FREE(entry); continue; } @@ -433,7 +430,7 @@ if (!newborn) { virHashRemoveEntry(configCache, path, NULL); } - free(entry); + VIR_FREE(entry); xenXMError (conn, VIR_ERR_INTERNAL_ERROR, _("xenXMConfigCacheRefresh: name")); goto cleanup; @@ -444,7 +441,7 @@ if (newborn) { if (virHashAddEntry(configCache, entry->filename, entry) < 0) { virConfFree(entry->conf); - free(entry); + VIR_FREE(entry); xenXMError (conn, VIR_ERR_INTERNAL_ERROR, _("xenXMConfigCacheRefresh: virHashAddEntry")); goto cleanup; @@ -458,7 +455,7 @@ if (virHashAddEntry(nameConfigMap, domname, entry->filename) < 0) { virHashRemoveEntry(configCache, ent->d_name, NULL); virConfFree(entry->conf); - free(entry); + VIR_FREE(entry); } } } @@ -674,7 +671,7 @@ ranges = virConvertCpuSet(conn, str, 0); if (ranges != NULL) { virBufferVSprintf(&buf, " cpuset='%s'", ranges); - free(ranges); + VIR_FREE(ranges); } else virBufferVSprintf(&buf, " cpuset='%s'", str); } @@ -1064,7 +1061,7 @@ char *soundxml; if ((soundxml = sound_string_to_xml(str))) { virBufferVSprintf(&buf, "%s", soundxml); - free(soundxml); + VIR_FREE(soundxml); } else { xenXMError(conn, VIR_ERR_INTERNAL_ERROR, _("parsing soundhw string failed.")); @@ -1085,7 +1082,8 @@ return virBufferContentAndReset(&buf); error: - free(virBufferContentAndReset(&buf)); + str = virBufferContentAndReset(&buf); + VIR_FREE(str); return NULL; } @@ -1140,7 +1138,7 @@ if (!(entry = virHashLookup(configCache, filename))) return (-1); - if (!(value = malloc(sizeof(*value)))) + if (VIR_ALLOC(value) < 0) return (-1); value->type = VIR_CONF_LONG; @@ -1182,7 +1180,7 @@ if (!(entry = virHashLookup(configCache, filename))) return (-1); - if (!(value = malloc(sizeof(*value)))) + if (VIR_ALLOC(value) < 0) return (-1); value->type = VIR_CONF_LONG; @@ -1255,7 +1253,7 @@ if (!(entry = virHashLookup(configCache, filename))) return (-1); - if (!(value = malloc(sizeof(*value)))) + if (VIR_ALLOC(value) < 0) return (-1); value->type = VIR_CONF_LONG; @@ -1359,8 +1357,8 @@ ret = 0; cleanup: - free(mapstr); - free(ranges); + VIR_FREE(mapstr); + VIR_FREE(ranges); return (ret); } @@ -1494,13 +1492,13 @@ priv = (xenUnifiedPrivatePtr) domain->conn->privateData; if (!(sexpr = virDomainParseXMLDesc(domain->conn, xml, NULL, priv->xendConfigVersion))) { - free(xml); + VIR_FREE(xml); return (-1); } - free(xml); + VIR_FREE(xml); ret = xenDaemonDomainCreateLinux(domain->conn, sexpr); - free(sexpr); + VIR_FREE(sexpr); if (ret != 0) { return (-1); } @@ -1531,7 +1529,7 @@ int xenXMConfigSetInt(virConfPtr conf, const char *setting, long l) { virConfValuePtr value = NULL; - if (!(value = malloc(sizeof(*value)))) + if (VIR_ALLOC(value) < 0) return -1; value->type = VIR_CONF_LONG; @@ -1546,13 +1544,13 @@ int xenXMConfigSetString(virConfPtr conf, const char *setting, const char *str) { virConfValuePtr value = NULL; - if (!(value = malloc(sizeof(*value)))) + if (VIR_ALLOC(value) < 0) return -1; value->type = VIR_CONF_STRING; value->next = NULL; if (!(value->str = strdup(str))) { - free(value); + VIR_FREE(value); return -1; } @@ -1635,7 +1633,8 @@ ret = 0; error: - xmlXPathFreeObject(obj); + if (obj) + xmlXPathFreeObject(obj); return ret; } @@ -1755,7 +1754,7 @@ buflen += 2; /* mode */ - if (!(buf = malloc(buflen))) + if (VIR_ALLOC_N(buf, buflen) < 0) goto cleanup; if(source) { @@ -1884,7 +1883,7 @@ if (ip) buflen += 4 + strlen((const char*)ip); - if (!(buf = malloc(buflen))) + if (VIR_ALLOC_N(buf, buflen) < 0) goto cleanup; strcpy(buf, "mac="); @@ -1918,7 +1917,7 @@ } cleanup: - free(bridge); + VIR_FREE(bridge); xmlFree(mac); xmlFree(source); xmlFree(script); @@ -2007,18 +2006,18 @@ ranges = virConvertCpuSet(conn, cpus, 0); if (ranges != NULL) { - free(cpus); + VIR_FREE(cpus); if (xenXMConfigSetString(conf, "cpus", ranges) < 0) { - free(ranges); + VIR_FREE(ranges); goto error; } - free(ranges); + VIR_FREE(ranges); } else { if (xenXMConfigSetString(conf, "cpus", cpus) < 0) { - free(cpus); + VIR_FREE(cpus); goto error; } - free(cpus); + VIR_FREE(cpus); } } @@ -2027,6 +2026,7 @@ (obj->stringval != NULL) && STREQ((char*)obj->stringval, "hvm")) hvm = 1; xmlXPathFreeObject(obj); + obj = NULL; priv = (xenUnifiedPrivatePtr) conn->privateData; @@ -2051,6 +2051,7 @@ boot = "d"; } xmlXPathFreeObject(obj); + obj = NULL; if (xenXMConfigSetString(conf, "boot", boot) < 0) goto error; @@ -2073,6 +2074,7 @@ clockLocal = 1; } xmlXPathFreeObject(obj); + obj = NULL; if (xenXMConfigSetInt(conf, "localtime", clockLocal) < 0) goto error; @@ -2160,7 +2162,7 @@ obj = xmlXPathEval(BAD_CAST "/domain/devices/graphics", ctxt); if ((obj != NULL) && (obj->type == XPATH_NODESET) && (obj->nodesetval != NULL) && (obj->nodesetval->nodeNr >= 0)) { - if (!(vfb = malloc(sizeof(*vfb)))) { + if (VIR_ALLOC(vfb) < 0) { xenXMError(conn, VIR_ERR_NO_MEMORY, _("config")); goto error; } @@ -2192,40 +2194,49 @@ len += 11 + strlen((const char*)vncpasswd); if (keymap) len += 8 + strlen((const char*)keymap); - if ((val = malloc(len)) != NULL) { - strcpy(val, "type=vnc"); - if (vncunused) { - strcat(val, ",vncunused=1"); - } else { - char portstr[50]; - int port = atoi((const char*)vncport); - snprintf(portstr, sizeof(portstr), "%d", port-5900); - strcat(val, ",vncdisplay="); - strcat(val, portstr); - } + if (VIR_ALLOC_N(val, len) < 0) { + xmlFree(type); xmlFree(vncport); - if (vnclisten) { - strcat(val, ",vnclisten="); - strcat(val, (const char*)vnclisten); - xmlFree(vnclisten); - } - if (vncpasswd) { - strcat(val, ",vncpasswd="); - strcat(val, (const char*)vncpasswd); - xmlFree(vncpasswd); - } - if (keymap) { - strcat(val, ",keymap="); - strcat(val, (const char*)keymap); - xmlFree(keymap); - } + xmlFree(vnclisten); + xmlFree(vncpasswd); + xmlFree(keymap); + VIR_FREE(vfb); + xenXMError (conn, VIR_ERR_NO_MEMORY, strerror (errno)); + goto error; + } + strcpy(val, "type=vnc"); + if (vncunused) { + strcat(val, ",vncunused=1"); + } else { + char portstr[50]; + int port = atoi((const char*)vncport); + snprintf(portstr, sizeof(portstr), "%d", port-5900); + strcat(val, ",vncdisplay="); + strcat(val, portstr); + } + xmlFree(vncport); + if (vnclisten) { + strcat(val, ",vnclisten="); + strcat(val, (const char*)vnclisten); + xmlFree(vnclisten); + } + if (vncpasswd) { + strcat(val, ",vncpasswd="); + strcat(val, (const char*)vncpasswd); + xmlFree(vncpasswd); + } + if (keymap) { + strcat(val, ",keymap="); + strcat(val, (const char*)keymap); + xmlFree(keymap); } } xmlFree(type); if (val) { virConfValuePtr disp; - if (!(disp = malloc(sizeof(*disp)))) { - free(val); + if (VIR_ALLOC(disp) < 0) { + VIR_FREE(val); + VIR_FREE(vfb); xenXMError(conn, VIR_ERR_NO_MEMORY, _("config")); goto error; } @@ -2246,7 +2257,7 @@ if ((obj != NULL) && (obj->type == XPATH_NODESET) && (obj->nodesetval != NULL) && (obj->nodesetval->nodeNr >= 0)) { virConfValuePtr disks; - if (!(disks = malloc(sizeof(*disks)))) { + if (VIR_ALLOC(disks) < 0) { xenXMError(conn, VIR_ERR_NO_MEMORY, _("config")); goto error; } @@ -2255,11 +2266,14 @@ for (i = obj->nodesetval->nodeNr -1 ; i >= 0 ; i--) { virConfValuePtr thisDisk; char *disk = NULL; - if (xenXMParseXMLDisk(obj->nodesetval->nodeTab[i], hvm, priv->xendConfigVersion, &disk) < 0) + if (xenXMParseXMLDisk(obj->nodesetval->nodeTab[i], hvm, priv->xendConfigVersion, &disk) < 0) { + virConfFreeValue(disks); goto error; + } if (disk) { - if (!(thisDisk = malloc(sizeof(*thisDisk)))) { - free(disk); + if (VIR_ALLOC(thisDisk) < 0) { + VIR_FREE(disk); + virConfFreeValue(disks); xenXMError(conn, VIR_ERR_NO_MEMORY, _("config")); goto error; } @@ -2278,7 +2292,7 @@ if ((obj != NULL) && (obj->type == XPATH_NODESET) && (obj->nodesetval != NULL) && (obj->nodesetval->nodeNr >= 0)) { virConfValuePtr vifs; - if (!(vifs = malloc(sizeof(*vifs)))) { + if (VIR_ALLOC(vifs) < 0) { xenXMError(conn, VIR_ERR_NO_MEMORY, _("config")); goto error; } @@ -2287,10 +2301,13 @@ for (i = obj->nodesetval->nodeNr - 1; i >= 0; i--) { virConfValuePtr thisVif; char *vif = xenXMParseXMLVif(conn, obj->nodesetval->nodeTab[i], hvm); - if (!vif) + if (!vif) { + virConfFreeValue(vifs); goto error; - if (!(thisVif = malloc(sizeof(*thisVif)))) { - free(vif); + } + if (VIR_ALLOC(thisVif) < 0) { + VIR_FREE(vif); + virConfFreeValue(vifs); xenXMError(conn, VIR_ERR_NO_MEMORY, _("config")); goto error; } @@ -2344,10 +2361,10 @@ if (!(soundstr = virBuildSoundStringFromXML(conn, ctxt))) goto error; if (xenXMConfigSetString(conf, "soundhw", soundstr) < 0) { - free(soundstr); + VIR_FREE(soundstr); goto error; } - free(soundstr); + VIR_FREE(soundstr); } } @@ -2463,7 +2480,7 @@ goto error; } - if (!(entry = calloc(1, sizeof(*entry)))) { + if (VIR_ALLOC(entry) < 0) { xenXMError(conn, VIR_ERR_NO_MEMORY, _("config")); goto error; } @@ -2505,7 +2522,7 @@ return (ret); error: - free(entry); + VIR_FREE(entry); if (conf) virConfFree(conf); return (NULL); @@ -2712,7 +2729,7 @@ ret = 0; cleanup: - free(domxml); + VIR_FREE(domxml); xmlXPathFreeObject(obj); xmlXPathFreeContext(ctxt); if (doc) @@ -2803,11 +2820,11 @@ list_val = list_val->next; } } else if (!list_item) { - if (!(list_item = calloc(1, sizeof(virConfValue)))) + if (VIR_ALLOC(list_item) < 0) goto cleanup; list_item->type = VIR_CONF_LIST; if(virConfSetValue(entry->conf, "disk", list_item)) { - free(list_item); + VIR_FREE(list_item); goto cleanup; } list_val = NULL; @@ -2817,7 +2834,7 @@ if (!list_val) { /* insert */ - if (!(list_val = malloc(sizeof(virConfValue)))) + if (VIR_ALLOC(list_val) < 0) goto cleanup; list_val->type = VIR_CONF_STRING; list_val->next = NULL; @@ -2828,7 +2845,7 @@ prev->next = list_val; } else { /* configure */ - free(list_val->str); + VIR_FREE(list_val->str); list_val->str = dev; } @@ -2836,9 +2853,9 @@ goto cleanup; cleanup: - free(type); - free(source); - free(target); + VIR_FREE(type); + VIR_FREE(source); + VIR_FREE(target); return (ret); } @@ -2912,7 +2929,7 @@ if (virMacAddrCompare (dommac, (const char *) mac) == 0) { if (autoassign) { - free(mac); + VIR_FREE(mac); mac = NULL; if (!(mac = (xmlChar *)xenXMAutoAssignMac())) goto cleanup; @@ -2929,11 +2946,11 @@ list_val = list_val->next; } } else if (!list_item) { - if (!(list_item = calloc(1, sizeof(virConfValue)))) + if (VIR_ALLOC(list_item) < 0) goto cleanup; list_item->type = VIR_CONF_LIST; if(virConfSetValue(entry->conf, "vif", list_item)) { - free(list_item); + VIR_FREE(list_item); goto cleanup; } list_val = NULL; @@ -2948,28 +2965,28 @@ while (node_cur->next) node_cur = node_cur->next; - if (!(node_tmp = calloc(1, sizeof(xmlNode)))) + if (VIR_ALLOC(node_tmp) < 0) goto node_cleanup; node_tmp->type = XML_ELEMENT_NODE; - if (!(node_tmp->name = malloc(4))) + if (VIR_ALLOC_N(node_tmp->name, 4) < 0) goto node_cleanup; strcpy((char *)node_tmp->name, "mac"); node_tmp->children = NULL; - if (!(attr_node = calloc(1, sizeof(xmlAttr)))) + if (VIR_ALLOC(attr_node) < 0) goto node_cleanup; attr_node->type = XML_ATTRIBUTE_NODE; attr_node->ns = NULL; - if (!(attr_node->name = malloc(8))) + if (VIR_ALLOC_N(attr_node->name, 8) < 0) goto node_cleanup; strcpy((char *) attr_node->name, "address"); node_tmp->properties = attr_node; - if (!(text_node = calloc(1, sizeof(xmlNode)))) + if (VIR_ALLOC(text_node) < 0) goto node_cleanup; text_node->type = XML_TEXT_NODE; text_node->_private = NULL; - if (!(text_node->name = malloc(8))) + if (VIR_ALLOC_N(text_node->name, 5) < 0) goto node_cleanup; strcpy((char *) text_node->name, "text"); text_node->children = NULL; @@ -2989,7 +3006,7 @@ if (!list_val) { /* insert */ - if (!(list_val = malloc(sizeof(virConfValue)))) + if (VIR_ALLOC(list_val) < 0) goto cleanup; list_val->type = VIR_CONF_STRING; list_val->next = NULL; @@ -3000,7 +3017,7 @@ prev->next = list_val; } else { /* configure */ - free(list_val->str); + VIR_FREE(list_val->str); list_val->str = dev; } @@ -3012,9 +3029,9 @@ xmlFree(attr_node); xmlFree(text_node); cleanup: - free(type); - free(source); - free(mac); + VIR_FREE(type); + VIR_FREE(source); + VIR_FREE(mac); return (ret); } @@ -3031,7 +3048,7 @@ xenXMAutoAssignMac() { char *buf; - if (!(buf = malloc(18))) + if (VIR_ALLOC_N(buf, 18) < 0) return 0; srand((unsigned)time(NULL)); sprintf(buf, "00:16:3e:%02x:%02x:%02x" @@ -3198,13 +3215,13 @@ else { if (!prev) { virConfValuePtr value; - if (!(value = calloc(1, sizeof(virConfValue)))) + if (VIR_ALLOC(value) < 0) goto cleanup; value->type = VIR_CONF_LIST; value->list = list_val->next; list_val->next = NULL; if (virConfSetValue(entry->conf, device, value)) { - free(value); + VIR_FREE(value); goto cleanup; } } else @@ -3223,9 +3240,9 @@ xmlXPathFreeContext(ctxt); if (doc) xmlFreeDoc(doc); - free(domdevice); - free(key); - free(list_val); + VIR_FREE(domdevice); + VIR_FREE(key); + VIR_FREE(list_val); return (ret); } diff -r 2069e1bf9132 src/xml.c --- a/src/xml.c Thu May 22 14:22:39 2008 -0400 +++ b/src/xml.c Thu May 22 17:39:03 2008 -0400 @@ -28,6 +28,7 @@ #include "xml.h" #include "buf.h" #include "util.h" +#include "memory.h" #include "xs_internal.h" /* for xenStoreDomainGetNetworkID */ #include "xen_unified.h" #include "xend_internal.h" /* for is_sound_* functions */ @@ -276,19 +277,18 @@ if (maxcpu <= 0) maxcpu = 4096; - cpuset = calloc(maxcpu, sizeof(*cpuset)); - if (cpuset == NULL) { + if (VIR_ALLOC_N(cpuset, maxcpu) < 0) { virXMLError(conn, VIR_ERR_NO_MEMORY, _("allocate buffer"), 0); return(NULL); } ret = virParseCpuSet(conn, &cur, 0, cpuset, maxcpu); if (ret < 0) { - free(cpuset); + VIR_FREE(cpuset); return(NULL); } res = virSaveCpuSet(conn, cpuset, maxcpu); - free(cpuset); + VIR_FREE(cpuset); return (res); } @@ -311,7 +311,7 @@ char *sound; xmlNodePtr *nodes = NULL; - if (!(sound = calloc(1, size+1))) { + if (VIR_ALLOC_N(sound, size + 1) < 0) { virXMLError(conn, VIR_ERR_NO_MEMORY, _("failed to allocate sound string"), 0); return NULL; @@ -334,7 +334,7 @@ if (!is_sound_model_valid(model)) { virXMLError(conn, VIR_ERR_XML_ERROR, _("unknown sound model type"), 0); - free(model); + VIR_FREE(model); goto error; } @@ -347,21 +347,21 @@ if (*sound && (size >= (strlen(model) + 1))) { strncat(sound, ",", size--); } else if (*sound || size < strlen(model)) { - free(model); + VIR_FREE(model); continue; } strncat(sound, model, size); size -= strlen(model); } - free(model); + VIR_FREE(model); } } - free(nodes); + VIR_FREE(nodes); return sound; error: - free(nodes); + VIR_FREE(nodes); return NULL; } #endif /* !PROXY */ @@ -592,11 +592,11 @@ ret = obj->nodesetval->nodeNr; if (list != NULL) { - *list = malloc(ret * sizeof(**list)); - if (*list == NULL) { + if (VIR_ALLOC_N(*list, ret) < 0) { virXMLError(NULL, VIR_ERR_NO_MEMORY, _("allocate string array"), ret * sizeof(**list)); + ret = -1; } else { memcpy(*list, obj->nodesetval->nodeTab, ret * sizeof(xmlNodePtr)); @@ -1028,7 +1028,7 @@ fdfile = xmlGetProp(cur, BAD_CAST "file"); if (fdfile != NULL) { virBufferVSprintf(buf, "(fda '%s')", fdfile); - free(fdfile); + VIR_FREE(fdfile); } } @@ -1042,7 +1042,7 @@ fdfile = xmlGetProp(cur, BAD_CAST "file"); if (fdfile != NULL) { virBufferVSprintf(buf, "(fdb '%s')", fdfile); - free(fdfile); + VIR_FREE(fdfile); } } @@ -1120,7 +1120,7 @@ } xmlFree(bus); } - free(nodes); + VIR_FREE(nodes); nodes = NULL; } @@ -1159,19 +1159,19 @@ if (!(soundstr = virBuildSoundStringFromXML(conn, ctxt))) goto error; virBufferVSprintf(buf, "(soundhw '%s')", soundstr); - free(soundstr); + VIR_FREE(soundstr); } str = virXPathString("string(/domain/clock/@offset)", ctxt); if (str != NULL && STREQ(str, "localtime")) { virBufferAddLit(buf, "(localtime 1)"); } - free(str); + VIR_FREE(str); return (0); error: - free(nodes); + VIR_FREE(nodes); return (-1); } @@ -1533,7 +1533,7 @@ } virNetworkFree(network); virBufferVSprintf(buf, "(bridge '%s')", bridge); - free(bridge); + VIR_FREE(bridge); } } if (script != NULL) @@ -1580,7 +1580,7 @@ { xmlDocPtr xml = NULL; xmlNodePtr node; - char *nam = NULL; + char *nam = NULL, *tmp; virBuffer buf = VIR_BUFFER_INITIALIZER; xmlChar *prop; xmlParserCtxtPtr pctxt; @@ -1677,30 +1677,29 @@ * it in a range format guaranteed to be understood by Xen. */ if (maxcpu > 0) { - cpuset = malloc(maxcpu * sizeof(*cpuset)); - if (cpuset != NULL) { - res = virParseCpuSet(conn, &cur, 0, cpuset, maxcpu); - if (res > 0) { - ranges = virSaveCpuSet(conn, cpuset, maxcpu); - if (ranges != NULL) { - virBufferVSprintf(&buf, "(cpus '%s')", ranges); - free(ranges); - } + if (VIR_ALLOC_N(cpuset, maxcpu) < 0) { + virXMLError(conn, VIR_ERR_NO_MEMORY, xmldesc, 0); + goto error; + } + res = virParseCpuSet(conn, &cur, 0, cpuset, maxcpu); + if (res > 0) { + ranges = virSaveCpuSet(conn, cpuset, maxcpu); + if (ranges != NULL) { + virBufferVSprintf(&buf, "(cpus '%s')", ranges); + VIR_FREE(ranges); } - free(cpuset); - if (res < 0) - goto error; - } else { - virXMLError(conn, VIR_ERR_NO_MEMORY, xmldesc, 0); } + VIR_FREE(cpuset); + if (res < 0) + goto error; } - free(str); + VIR_FREE(str); } str = virXPathString("string(/domain/uuid[1])", ctxt); if (str != NULL) { virBufferVSprintf(&buf, "(uuid '%s')", str); - free(str); + VIR_FREE(str); } str = virXPathString("string(/domain/bootloader[1])", ctxt); @@ -1711,7 +1710,7 @@ * significant and should be discarded */ bootloader = 1; - free(str); + VIR_FREE(str); } else if (virXPathNumber("count(/domain/bootloader)", ctxt, &f) == 0 && (f > 0)) { virBufferAddLit(&buf, "(bootloader)"); @@ -1728,25 +1727,25 @@ * ignore the bootloader_args value unless a bootloader was specified */ virBufferVSprintf(&buf, "(bootloader_args '%s')", str); - free(str); + VIR_FREE(str); } str = virXPathString("string(/domain/on_poweroff[1])", ctxt); if (str != NULL) { virBufferVSprintf(&buf, "(on_poweroff '%s')", str); - free(str); + VIR_FREE(str); } str = virXPathString("string(/domain/on_reboot[1])", ctxt); if (str != NULL) { virBufferVSprintf(&buf, "(on_reboot '%s')", str); - free(str); + VIR_FREE(str); } str = virXPathString("string(/domain/on_crash[1])", ctxt); if (str != NULL) { virBufferVSprintf(&buf, "(on_crash '%s')", str); - free(str); + VIR_FREE(str); } if (!bootloader) { @@ -1812,11 +1811,11 @@ res = virDomainParseXMLDiskDesc(conn, nodes[i], &buf, hvm, xendConfigVersion); if (res != 0) { - free(nodes); + VIR_FREE(nodes); goto error; } } - free(nodes); + VIR_FREE(nodes); } nb_nodes = virXPathNodeSet("/domain/devices/interface", ctxt, &nodes); @@ -1827,12 +1826,12 @@ virDomainParseXMLIfDesc(conn, nodes[i], &buf, hvm, xendConfigVersion); if (res != 0) { - free(nodes); + VIR_FREE(nodes); goto error; } virBufferAddLit(&buf, ")"); } - free(nodes); + VIR_FREE(nodes); } /* New style PV graphics config xen >= 3.0.4, @@ -1844,11 +1843,11 @@ for (i = 0; i < nb_nodes; i++) { res = virDomainParseXMLGraphicsDescVFB(conn, nodes[i], &buf); if (res != 0) { - free(nodes); + VIR_FREE(nodes); goto error; } } - free(nodes); + VIR_FREE(nodes); } } @@ -1862,7 +1861,7 @@ if (name != NULL) *name = nam; else - free(nam); + VIR_FREE(nam); if (virBufferError(&buf)) { virXMLError(conn, VIR_ERR_NO_MEMORY, _("allocate buffer"), 0); @@ -1872,7 +1871,7 @@ return virBufferContentAndReset(&buf); error: - free(nam); + VIR_FREE(nam); if (name != NULL) *name = NULL; xmlXPathFreeContext(ctxt); @@ -1880,7 +1879,8 @@ xmlFreeDoc(xml); if (pctxt != NULL) xmlFreeParserCtxt(pctxt); - free(virBufferContentAndReset(&buf)); + tmp = virBufferContentAndReset(&buf); + VIR_FREE(tmp); return (NULL); } diff -r 2069e1bf9132 tests/virshtest.c --- a/tests/virshtest.c Thu May 22 14:22:39 2008 -0400 +++ b/tests/virshtest.c Thu May 22 17:39:04 2008 -0400 @@ -9,7 +9,7 @@ #include "testutils.h" static char *progname; -static char *abs_top_srcdir; +static char *abs_srcdir; #define MAX_FILE 4096 static int testFilterLine(char *buffer, @@ -36,7 +36,7 @@ char *actualPtr = &(actualData[0]); char expect[PATH_MAX]; - snprintf(expect, sizeof expect - 1, "%s/tests/%s", abs_top_srcdir, expect_rel); + snprintf(expect, sizeof expect - 1, "%s/%s", abs_srcdir, expect_rel); if (virtTestLoadFile(expect, &expectPtr, MAX_FILE) < 0) return -1; @@ -271,24 +271,25 @@ -int -main(int argc, char **argv) +static int +mymain(int argc, char **argv) { int ret = 0; char buffer[PATH_MAX]; + char cwd[PATH_MAX]; - abs_top_srcdir = getenv("abs_top_srcdir"); - if (!abs_top_srcdir) - return 1; + abs_srcdir = getenv("abs_srcdir"); + if (!abs_srcdir) + abs_srcdir = getcwd(cwd, sizeof(cwd)); - snprintf(buffer, PATH_MAX-1, "test://%s/docs/testnode.xml", abs_top_srcdir); + snprintf(buffer, PATH_MAX-1, "test://%s/../docs/testnode.xml", abs_srcdir); buffer[PATH_MAX-1] = '\0'; progname = argv[0]; custom_uri = buffer; if (argc > 1) { fprintf(stderr, "Usage: %s\n", progname); - exit(EXIT_FAILURE); + return(EXIT_FAILURE); } if (virtTestRun("virsh list (default)", @@ -355,5 +356,7 @@ 1, testCompareDomstateByName, NULL) != 0) ret = -1; - exit(ret==0 ? EXIT_SUCCESS : EXIT_FAILURE); + return(ret==0 ? EXIT_SUCCESS : EXIT_FAILURE); } + +VIRT_TEST_MAIN(mymain) -- |: Red Hat, Engineering, Boston -o- http://people.redhat.com/berrange/ :| |: http://libvirt.org -o- http://virt-manager.org -o- http://ovirt.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: GnuPG: 7D3B9505 -o- F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|

"Daniel P. Berrange" <berrange@redhat.com> wrote:
This patch converts a large amount of the XML parsing/generating code in the Xen and QEMU drivers over to using the new memory APIs, so we can get OOM checking of it. In the process I've discovered and fixed a number of double-free's, and leaks
Nice work! I've looked through the first half, and think that's enough to ACK the whole thing. It would have been good to point out double frees and leaks, (or even put them in a separate patch) but probably not worth the cost of doing it after the fact.

On Thu, May 22, 2008 at 10:42:03PM +0100, Daniel P. Berrange wrote:
This patch converts a large amount of the XML parsing/generating code in the Xen and QEMU drivers over to using the new memory APIs, so we can get OOM checking of it. In the process I've discovered and fixed a number of double-free's, and leaks
Excellent, I think most allocations are done in those section of code (not taking into account libxml2 own tree constructions at parse time) +1 Daniel -- Red Hat Virtualization group http://redhat.com/virtualization/ Daniel Veillard | virtualization library http://libvirt.org/ veillard@redhat.com | libxml GNOME XML XSLT toolkit http://xmlsoft.org/ http://veillard.com/ | Rpmfind RPM search engine http://rpmfind.net/
participants (3)
-
Daniel P. Berrange
-
Daniel Veillard
-
Jim Meyering