[libvirt] PATCH: Switch all remaining code to memory alloc APIs

This patch switches all remaining code over to use the memory allocation APIs, with exception of virsh which is going to be slightly more complex It was mostly a straight conversion - there were only a few places which weren't checking for failure corecttly - the most notable being sexpr.c. bridge.c | 18 +++---- conf.c | 51 ++++++++++---------- iptables.c | 60 ++++++++++-------------- lxc_conf.c | 45 ++++++++---------- lxc_container.c | 5 +- lxc_driver.c | 25 ++++------ openvz_conf.c | 49 ++++++++----------- openvz_driver.c | 3 - proxy_internal.c | 8 +-- remote_internal.c | 85 +++++++++++++++------------------- sexpr.c | 31 ++++++++---- storage_backend.c | 32 ++++++------ storage_backend_disk.c | 24 ++++----- storage_backend_fs.c | 56 ++++++++++------------ storage_backend_iscsi.c | 30 +++++------- storage_backend_logical.c | 20 +++----- storage_conf.c | 114 +++++++++++++++++++++++----------------------- storage_driver.c | 5 -- xen_internal.c | 38 +++++++-------- xen_unified.c | 23 ++++----- xend_internal.c | 1 xmlrpc.c | 67 +++++++++++++-------------- 22 files changed, 379 insertions(+), 411 deletions(-) Regards, Daniel. diff -r ff6b92c70738 src/bridge.c --- a/src/bridge.c Fri May 30 10:36:42 2008 -0400 +++ b/src/bridge.c Fri May 30 10:55:44 2008 -0400 @@ -45,6 +45,7 @@ #include <net/if_arp.h> /* ARPHRD_ETHER */ #include "internal.h" +#include "memory.h" #define MAX_BRIDGE_ID 256 @@ -84,8 +85,7 @@ return err; } - *ctlp = malloc(sizeof(**ctlp)); - if (!*ctlp) { + if (VIR_ALLOC(*ctlp) < 0) { close(fd); return ENOMEM; } @@ -110,7 +110,7 @@ close(ctl->fd); ctl->fd = 0; - free(ctl); + VIR_FREE(ctl); } /** @@ -681,7 +681,7 @@ snprintf(delayStr, sizeof(delayStr), "%d", delay); - if (!(argv = calloc(n + 1, sizeof(*argv)))) + if (VIR_ALLOC_N(argv, n + 1) < 0) goto error; n = 0; @@ -706,8 +706,8 @@ if (argv) { n = 0; while (argv[n]) - free(argv[n++]); - free(argv); + VIR_FREE(argv[n++]); + VIR_FREE(argv); } return retval; @@ -738,7 +738,7 @@ 1 + /* brige name */ 1; /* value */ - if (!(argv = calloc(n + 1, sizeof(*argv)))) + if (VIR_ALLOC_N(argv, n + 1) < 0) goto error; n = 0; @@ -763,8 +763,8 @@ if (argv) { n = 0; while (argv[n]) - free(argv[n++]); - free(argv); + VIR_FREE(argv[n++]); + VIR_FREE(argv); } return retval; diff -r ff6b92c70738 src/conf.c --- a/src/conf.c Fri May 30 10:36:42 2008 -0400 +++ b/src/conf.c Fri May 30 10:55:44 2008 -0400 @@ -23,6 +23,7 @@ #include "conf.h" #include "util.h" #include "c-ctype.h" +#include "memory.h" /************************************************************************ * * @@ -138,11 +139,11 @@ return; if (val->type == VIR_CONF_STRING && val->str != NULL) - free(val->str); + VIR_FREE(val->str); if (val->type == VIR_CONF_LIST && val->list != NULL) virConfFreeList(val->list); - free(val); + VIR_FREE(val); } virConfPtr @@ -150,8 +151,7 @@ { virConfPtr ret; - ret = calloc(1, sizeof(*ret)); - if (ret == NULL) { + if (VIR_ALLOC(ret) < 0) { virConfError(NULL, VIR_ERR_NO_MEMORY, _("allocating configuration"), 0); return(NULL); } @@ -199,8 +199,7 @@ if ((comm == NULL) && (name == NULL)) return(NULL); - ret = calloc(1, sizeof(*ret)); - if (ret == NULL) { + if (VIR_ALLOC(ret) < 0) { virConfError(NULL, VIR_ERR_NO_MEMORY, _("allocating configuration"), 0); return(NULL); } @@ -441,7 +440,8 @@ NEXT; SKIP_BLANKS_AND_EOL; if ((ctxt->cur < ctxt->end) && (CUR != ']')) { - lst = virConfParseValue(ctxt); + if ((lst = virConfParseValue(ctxt)) == NULL) + return(NULL); SKIP_BLANKS_AND_EOL; } while ((ctxt->cur < ctxt->end) && (CUR != ']')) { @@ -484,10 +484,10 @@ ctxt->line); return(NULL); } - ret = calloc(1, sizeof(*ret)); - if (ret == NULL) { + if (VIR_ALLOC(ret) < 0) { virConfError(NULL, VIR_ERR_NO_MEMORY, _("allocating configuration"), 0); - free(str); + virConfFreeList(lst); + VIR_FREE(str); return(NULL); } ret->type = type; @@ -618,7 +618,7 @@ SKIP_BLANKS; value = virConfParseValue(ctxt); if (value == NULL) { - free(name); + VIR_FREE(name); return(-1); } SKIP_BLANKS; @@ -630,15 +630,15 @@ if (comm == NULL) { virConfError(NULL, VIR_ERR_NO_MEMORY, _("allocating configuration"), ctxt->line); - free(name); + VIR_FREE(name); virConfFreeValue(value); return(-1); } } if (virConfAddEntry(ctxt->conf, name, value, comm) == NULL) { - free(name); + VIR_FREE(name); virConfFreeValue(value); - free(comm); + VIR_FREE(comm); return(-1); } return(0); @@ -720,7 +720,7 @@ conf = virConfParse(filename, content, len); - free(content); + VIR_FREE(content); return conf; } @@ -769,14 +769,14 @@ tmp = conf->entries; while (tmp) { virConfEntryPtr next; - free(tmp->name); + VIR_FREE(tmp->name); virConfFreeValue(tmp->value); - free(tmp->comment); + VIR_FREE(tmp->comment); next = tmp->next; - free(tmp); + VIR_FREE(tmp); tmp = next; } - free(conf); + VIR_FREE(conf); return(0); } @@ -834,14 +834,14 @@ } if (!cur) { - if (!(cur = malloc(sizeof(*cur)))) { + if (VIR_ALLOC(cur) < 0) { virConfFreeValue(value); return (-1); } cur->comment = NULL; if (!(cur->name = strdup(setting))) { virConfFreeValue(value); - free(cur); + VIR_FREE(cur); return (-1); } cur->value = value; @@ -897,15 +897,16 @@ fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR ); if (fd < 0) { + char *tmp = virBufferContentAndReset(&buf); virConfError(NULL, VIR_ERR_WRITE_FAILED, _("failed to open file"), 0); - free(virBufferContentAndReset(&buf)); + VIR_FREE(tmp); return -1; } use = virBufferUse(&buf); content = virBufferContentAndReset(&buf); ret = safewrite(fd, content, use); - free(content); + VIR_FREE(content); close(fd); if (ret != (int)use) { virConfError(NULL, VIR_ERR_WRITE_FAILED, _("failed to save content"), 0); @@ -955,11 +956,11 @@ if ((int)use >= *len) { *len = (int)use; - free(content); + VIR_FREE(content); return -1; } memcpy(memory, content, use); - free(content); + VIR_FREE(content); *len = use; return use; } diff -r ff6b92c70738 src/iptables.c --- a/src/iptables.c Fri May 30 10:36:42 2008 -0400 +++ b/src/iptables.c Fri May 30 10:55:44 2008 -0400 @@ -45,6 +45,7 @@ #include "internal.h" #include "iptables.h" #include "util.h" +#include "memory.h" #define qemudLog(level, msg...) fprintf(stderr, msg) @@ -156,7 +157,7 @@ snprintf(arg, sizeof(arg), "--custom-rules=ipv4:%s:%s", table, path); if (!stripLine(content, len, arg)) { - free(content); + VIR_FREE(content); return; } @@ -171,7 +172,7 @@ goto write_error; } - free(content); + VIR_FREE(content); return; @@ -181,7 +182,7 @@ strerror(errno)); if (f) fclose(f); - free(content); + VIR_FREE(content); #undef MAX_FILE_LEN } @@ -264,14 +265,14 @@ static void iptRuleFree(iptRule *rule) { - free(rule->rule); + VIR_FREE(rule->rule); rule->rule = NULL; if (rule->argv) { int i = 0; while (rule->argv[i]) - free(rule->argv[i++]); - free(rule->argv); + VIR_FREE(rule->argv[i++]); + VIR_FREE(rule->argv); rule->argv = NULL; } } @@ -282,17 +283,13 @@ char **argv, int command_idx) { - iptRule *r; - - if (!(r = realloc(rules->rules, sizeof(*r) * (rules->nrules+1)))) { + if (VIR_REALLOC_N(rules->rules, rules->nrules+1) < 0) { int i = 0; while (argv[i]) - free(argv[i++]); - free(argv); + VIR_FREE(argv[i++]); + VIR_FREE(argv); return ENOMEM; } - - rules->rules = r; rules->rules[rules->nrules].rule = rule; rules->rules[rules->nrules].argv = argv; @@ -332,23 +329,17 @@ { int i; - if (rules->table) { - free(rules->table); - rules->table = NULL; - } + if (rules->table) + VIR_FREE(rules->table); - if (rules->chain) { - free(rules->chain); - rules->chain = NULL; - } - + if (rules->chain) + VIR_FREE(rules->chain); if (rules->rules) { for (i = 0; i < rules->nrules; i++) iptRuleFree(&rules->rules[i]); - free(rules->rules); - rules->rules = NULL; + VIR_FREE(rules->rules); rules->nrules = 0; } @@ -358,7 +349,7 @@ rules->path[0] = '\0'; #endif /* ENABLE_IPTABLES_LOKKIT */ - free(rules); + VIR_FREE(rules); } static iptRules * @@ -367,7 +358,7 @@ { iptRules *rules; - if (!(rules = calloc(1, sizeof (*rules)))) + if (VIR_ALLOC(rules) < 0) return NULL; if (!(rules->table = strdup(table))) @@ -404,8 +395,9 @@ for (len = 1, i = 0; argv[i]; i++) len += strlen(argv[i]) + 1; - if (!(p = ret = (char *)malloc(len))) + if (VIR_ALLOC_N(ret, len) < 0) return NULL; + p = ret; for (i = 0; argv[i]; i++) { if (i != 0) @@ -441,7 +433,7 @@ va_end(args); - if (!(argv = calloc(n + 1, sizeof(*argv)))) + if (VIR_ALLOC_N(argv, n + 1) < 0) goto error; n = 0; @@ -478,7 +470,7 @@ goto error; if (action == REMOVE) { - free(argv[command_idx]); + VIR_FREE(argv[command_idx]); if (!(argv[command_idx] = strdup("--delete"))) goto error; } @@ -497,13 +489,13 @@ } error: - free(rule); + VIR_FREE(rule); if (argv) { n = 0; while (argv[n]) - free(argv[n++]); - free(argv); + VIR_FREE(argv[n++]); + VIR_FREE(argv); } return retval; @@ -521,7 +513,7 @@ { iptablesContext *ctx; - if (!(ctx = calloc(1, sizeof (*ctx)))) + if (VIR_ALLOC(ctx) < 0) return NULL; if (!(ctx->input_filter = iptRulesNew("filter", "INPUT"))) @@ -555,7 +547,7 @@ iptRulesFree(ctx->forward_filter); if (ctx->nat_postrouting) iptRulesFree(ctx->nat_postrouting); - free(ctx); + VIR_FREE(ctx); } /** diff -r ff6b92c70738 src/lxc_conf.c --- a/src/lxc_conf.c Fri May 30 10:36:42 2008 -0400 +++ b/src/lxc_conf.c Fri May 30 10:55:44 2008 -0400 @@ -42,7 +42,7 @@ #include "util.h" #include "uuid.h" #include "xml.h" - +#include "memory.h" #include "lxc_conf.h" /* debug macros */ @@ -183,10 +183,10 @@ if (virUUIDParse(res, uuid) < 0) { lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR, _("invalid uuid element")); - free(res); + VIR_FREE(res); return(-1); } - free(res); + VIR_FREE(res); } return(0); } @@ -206,15 +206,14 @@ res = virXPathNodeSet("/domain/devices/filesystem", contextPtr, &list); if (res > 0) { for (i = 0; i < res; ++i) { - mountObj = calloc(1, sizeof(lxc_mount_t)); - if (NULL == mountObj) { + if (VIR_ALLOC(mountObj) < 0) { lxcError(conn, NULL, VIR_ERR_NO_MEMORY, "mount"); goto parse_complete; } rc = lxcParseMountXML(conn, list[i], mountObj); if (0 > rc) { - free(mountObj); + VIR_FREE(mountObj); goto parse_complete; } @@ -228,7 +227,7 @@ } prevObj = mountObj; } - free(list); + VIR_FREE(list); } rc = nmounts; @@ -252,7 +251,7 @@ if (strlen(res) >= PATH_MAX - 1) { lxcError(conn, NULL, VIR_ERR_INTERNAL_ERROR, _("init string too long")); - free(res); + VIR_FREE(res); return(-1); } @@ -307,7 +306,7 @@ xmlChar *xmlProp = NULL; lxc_vm_def_t *containerDef; - if (!(containerDef = calloc(1, sizeof(*containerDef)))) { + if (VIR_ALLOC(containerDef) < 0) { lxcError(conn, NULL, VIR_ERR_NO_MEMORY, "containerDef"); return NULL; } @@ -339,8 +338,7 @@ _("invalid domain type")); goto error; } - free(xmlProp); - xmlProp = NULL; + VIR_FREE(xmlProp); if ((xmlProp = xmlGetProp(rootNodePtr, BAD_CAST "id"))) { if (0 > virStrToLong_i((char*)xmlProp, NULL, 10, &(containerDef->id))) { @@ -351,8 +349,7 @@ } else { containerDef->id = -1; } - free(xmlProp); - xmlProp = NULL; + VIR_FREE(xmlProp); if (lxcParseDomainName(conn, &(containerDef->name), contextPtr) < 0) { goto error; @@ -385,7 +382,7 @@ return containerDef; error: - free(xmlProp); + VIR_FREE(xmlProp); xmlXPathFreeContext(contextPtr); lxcFreeVMDef(containerDef); @@ -436,7 +433,7 @@ return vm; } - if (!(vm = calloc(1, sizeof(lxc_vm_t)))) { + if (VIR_ALLOC(vm) < 0) { lxcError(conn, NULL, VIR_ERR_NO_MEMORY, "vm"); return NULL; } @@ -529,7 +526,7 @@ close(fd); } - free(xmlDef); + VIR_FREE(xmlDef); return rc; } @@ -637,7 +634,7 @@ lxcLoadConfig(driver, file, tempPath, xmlData); - free(xmlData); + VIR_FREE(xmlData); load_complete: return rc; @@ -744,14 +741,14 @@ curMount = vmdef->mounts; while (curMount) { nextMount = curMount->next; - free(curMount); + VIR_FREE(curMount); curMount = nextMount; } - free(vmdef->name); - free(vmdef->init); - free(vmdef->tty); - free(vmdef); + VIR_FREE(vmdef->name); + VIR_FREE(vmdef->init); + VIR_FREE(vmdef->tty); + VIR_FREE(vmdef); } void lxcFreeVMs(lxc_vm_t *vms) @@ -769,8 +766,8 @@ void lxcFreeVM(lxc_vm_t *vm) { lxcFreeVMDef(vm->def); - free(vm->containerTty); - free(vm); + VIR_FREE(vm->containerTty); + VIR_FREE(vm); } lxc_vm_t *lxcFindVMByID(const lxc_driver_t *driver, int id) diff -r ff6b92c70738 src/lxc_container.c --- a/src/lxc_container.c Fri May 30 10:36:42 2008 -0400 +++ b/src/lxc_container.c Fri May 30 10:55:44 2008 -0400 @@ -35,6 +35,7 @@ #include "lxc_container.h" #include "lxc_conf.h" #include "util.h" +#include "memory.h" #define DEBUG(fmt,...) VIR_DEBUG(__FILE__, fmt, __VA_ARGS__) #define DEBUG0(msg) VIR_DEBUG(__FILE__, "%s", msg) @@ -54,8 +55,8 @@ char* execString; size_t execStringLen = strlen(vmDef->init) + 1 + 5; - if (NULL == (execString = calloc(execStringLen, sizeof(char)))) { - lxcError(NULL, NULL, VIR_ERR_NO_MEMORY, + if (VIR_ALLOC_N(execString, execStringLen) < 0) { + lxcError(NULL, NULL, VIR_ERR_NO_MEMORY, _("failed to calloc memory for init string: %s"), strerror(errno)); goto error_out; diff -r ff6b92c70738 src/lxc_driver.c --- a/src/lxc_driver.c Fri May 30 10:36:42 2008 -0400 +++ b/src/lxc_driver.c Fri May 30 10:55:44 2008 -0400 @@ -42,6 +42,7 @@ #include "driver.h" #include "internal.h" #include "util.h" +#include "memory.h" /* debug macros */ #define DEBUG(fmt,...) VIR_DEBUG(__FILE__, fmt, __VA_ARGS__) @@ -85,8 +86,7 @@ char *stack; int childStatus; - stack = malloc(getpagesize() * 4); - if(!stack) { + if (VIR_ALLOC_N(stack, getpagesize() * 4) < 0) { DEBUG0("Unable to allocate stack"); rc = -1; goto check_complete; @@ -102,7 +102,7 @@ waitpid(cpid, &childStatus, 0); } - free(stack); + VIR_FREE(stack); check_complete: return rc; @@ -263,7 +263,7 @@ cleanup: for (i = 0 ; i < numDoms ; i++) { - free(names[i]); + VIR_FREE(names[i]); } return -1; @@ -398,16 +398,15 @@ int rc = -1; int flags; int stacksize = getpagesize() * 4; - void *stack, *stacktop; + char *stack, *stacktop; /* allocate a stack for the container */ - stack = malloc(stacksize); - if (!stack) { + if (VIR_ALLOC_N(stack, stacksize) < 0) { lxcError(conn, NULL, VIR_ERR_NO_MEMORY, _("unable to allocate container stack")); goto error_exit; } - stacktop = (char*)stack + stacksize; + stacktop = stack + stacksize; flags = CLONE_NEWPID|CLONE_NEWNS|CLONE_NEWUTS|CLONE_NEWUSER|CLONE_NEWIPC|SIGCHLD; @@ -572,8 +571,7 @@ goto cleanup; } - *ttyName = malloc(sizeof(char) * (strlen(tempTtyName) + 1)); - if (NULL == ttyName) { + if (VIR_ALLOC_N(*ttyName, strlen(tempTtyName) + 1) < 0) { lxcError(conn, NULL, VIR_ERR_NO_MEMORY, _("unable to allocate container name string")); goto cleanup; @@ -1034,8 +1032,7 @@ return -1; } - lxc_driver = calloc(1, sizeof(lxc_driver_t)); - if (NULL == lxc_driver) { + if (VIR_ALLOC(lxc_driver) < 0) { return -1; } @@ -1062,8 +1059,8 @@ static void lxcFreeDriver(lxc_driver_t *driver) { - free(driver->configDir); - free(driver); + VIR_FREE(driver->configDir); + VIR_FREE(driver); } static int lxcShutdown(void) diff -r ff6b92c70738 src/openvz_conf.c --- a/src/openvz_conf.c Fri May 30 10:36:42 2008 -0400 +++ b/src/openvz_conf.c Fri May 30 10:55:44 2008 -0400 @@ -53,6 +53,7 @@ #include "openvz_conf.h" #include "uuid.h" #include "buf.h" +#include "memory.h" #include <string.h> @@ -152,23 +153,22 @@ struct ovz_quota *prev = quota; quota = quota->next; - free(prev); + VIR_FREE(prev); } while (ip) { struct ovz_ip *prev = ip; ip = ip->next; - free(prev); + VIR_FREE(prev); } while (ns) { struct ovz_ns *prev = ns; ns = ns->next; - free(prev); + VIR_FREE(prev); } - free(def); - def = NULL; + VIR_FREE(def); } } @@ -201,8 +201,7 @@ } if (vms) { openvzFreeVMDef(vm->vmdef); - free(vm); - vm = NULL; + VIR_FREE(vm); } } @@ -217,8 +216,7 @@ if (driver->vms) for(next = driver->vms->next; driver->vms; driver->vms = next) openvzFreeVM(driver, driver->vms, 0); - free(driver); - driver = NULL; + VIR_FREE(driver); } struct openvz_vm * @@ -247,7 +245,7 @@ return vm; } - if (!(vm = calloc(1, sizeof(*vm)))) { + if (VIR_ALLOC(vm) < 0) { openvzFreeVMDef(def); error(conn, VIR_ERR_NO_MEMORY, "vm"); return NULL; @@ -299,7 +297,7 @@ struct ovz_ip *ovzIp; struct ovz_ns *ovzNs; - if (!(def = calloc(1, sizeof(*def)))) { + if (VIR_ALLOC(def) < 0) { error(conn, VIR_ERR_NO_MEMORY, "xmlXPathContext"); return NULL; } @@ -328,8 +326,7 @@ error(conn, VIR_ERR_INTERNAL_ERROR, _("invalid domain type attribute")); goto bail_out; } - free(prop); - prop = NULL; + VIR_FREE(prop); /* Extract domain name */ obj = xmlXPathEval(BAD_CAST "string(/domain/name[1])", ctxt); @@ -396,7 +393,7 @@ error(conn, VIR_ERR_INTERNAL_ERROR, errorMessage); goto bail_out; } - if (!(ovzIp = calloc(1, sizeof(*ovzIp)))) { + if (VIR_ALLOC(ovzIp) < 0) { openvzLog(OPENVZ_ERR, _("Failed to Create Memory for 'ovz_ip' structure")); goto bail_out; @@ -478,7 +475,7 @@ error(conn, VIR_ERR_INTERNAL_ERROR, errorMessage); goto bail_out; } - if (!(ovzNs = calloc(1, sizeof(*ovzNs)))) { + if (VIR_ALLOC(ovzNs) < 0) { openvzLog(OPENVZ_ERR, _("Failed to Create Memory for 'ovz_ns' structure")); goto bail_out; @@ -509,7 +506,7 @@ return def; bail_out: - free(prop); + VIR_FREE(prop); xmlXPathFreeObject(obj); xmlXPathFreeContext(ctxt); openvzFreeVMDef(def); @@ -539,8 +536,7 @@ } pnext = &vm; while(!feof(fp)) { - *pnext = calloc(1, sizeof(**pnext)); - if(!*pnext) { + if (VIR_ALLOC(*pnext) < 0) { error(conn, VIR_ERR_INTERNAL_ERROR, _("calloc failed")); goto error; } @@ -568,8 +564,7 @@ (*pnext)->vpsid = -1; } - vmdef = calloc(1, sizeof(*vmdef)); - if(!vmdef) { + if (VIR_ALLOC(vmdef) < 0) { error(conn, VIR_ERR_INTERNAL_ERROR, _("calloc failed")); goto error; } @@ -581,7 +576,7 @@ if(ret == -1) { error(conn, VIR_ERR_INTERNAL_ERROR, _("UUID in config file malformed")); - free(vmdef); + VIR_FREE(vmdef); goto error; } @@ -594,8 +589,8 @@ struct openvz_vm *next; next = vm->next; - free(vm->vmdef); - free(vm); + VIR_FREE(vm->vmdef); + VIR_FREE(vm); vm = next; } return NULL; @@ -656,7 +651,7 @@ if (conf_dir == NULL) return -1; sprintf(conf_file, "%s/%d.conf", conf_dir, vpsid); - free(conf_dir); + VIR_FREE(conf_dir); fd = open(conf_file, O_RDWR); if(fd == -1) @@ -697,7 +692,7 @@ if (conf_dir == NULL) return -1; sprintf(conf_file, "%s/%d.conf", conf_dir, vpsid); - free(conf_dir); + VIR_FREE(conf_dir); if (openvzGetVPSUUID(vpsid, uuidstr)) return -1; @@ -741,7 +736,7 @@ dp = opendir(conf_dir); if(dp == NULL) { - free(conf_dir); + VIR_FREE(conf_dir); return 0; } @@ -753,7 +748,7 @@ openvzSetUUID(vpsid); } closedir(dp); - free(conf_dir); + VIR_FREE(conf_dir); return 0; } diff -r ff6b92c70738 src/openvz_driver.c --- a/src/openvz_driver.c Fri May 30 10:36:42 2008 -0400 +++ b/src/openvz_driver.c Fri May 30 10:55:44 2008 -0400 @@ -49,8 +49,7 @@ #include <stdio.h> #include <sys/wait.h> -#include "libvirt/virterror.h" - +#include "internal.h" #include "openvz_driver.h" #include "event.h" #include "buf.h" diff -r ff6b92c70738 src/proxy_internal.c --- a/src/proxy_internal.c Fri May 30 10:36:42 2008 -0400 +++ b/src/proxy_internal.c Fri May 30 10:55:44 2008 -0400 @@ -28,6 +28,7 @@ #include "proxy_internal.h" #include "util.h" #include "xen_unified.h" +#include "memory.h" #define STANDALONE @@ -992,8 +993,7 @@ } xmllen = ans.len - sizeof (virProxyPacket); - xml = malloc (xmllen+1); - if (!xml) { + if (VIR_ALLOC_N(xml, xmllen+1) < 0) { virProxyError (conn, VIR_ERR_NO_MEMORY, __FUNCTION__); return NULL; } @@ -1044,7 +1044,7 @@ return (NULL); } xmllen = ans.len - sizeof(virProxyPacket); - if (!(xml = malloc(xmllen+1))) { + if (VIR_ALLOC_N(xml, xmllen+1) < 0) { virProxyError(domain->conn, VIR_ERR_NO_MEMORY, __FUNCTION__); return NULL; } @@ -1097,7 +1097,7 @@ return (NULL); } oslen = ans.len - sizeof(virProxyPacket); - if (!(ostype = malloc(oslen+1))) { + if (VIR_ALLOC_N(ostype, oslen+1) < 0) { virProxyError(domain->conn, VIR_ERR_NO_MEMORY, __FUNCTION__); return NULL; } diff -r ff6b92c70738 src/remote_internal.c --- a/src/remote_internal.c Fri May 30 10:36:42 2008 -0400 +++ b/src/remote_internal.c Fri May 30 10:55:44 2008 -0400 @@ -71,6 +71,7 @@ #include "qparams.h" #include "remote_internal.h" #include "remote_protocol.h" +#include "memory.h" #define DEBUG(fmt,...) VIR_DEBUG(__FILE__, fmt,__VA_ARGS__) #define DEBUG0(msg) VIR_DEBUG(__FILE__, "%s", msg) @@ -638,8 +639,7 @@ // Generate the final command argv[] array. // ssh -p $port [-l $username] $hostname $netcat -U $sockname [NULL] - cmd_argv = malloc (nr_args * sizeof (*cmd_argv)); - if (cmd_argv == NULL) { + if (VIR_ALLOC_N(cmd_argv, nr_args) < 0) { error (conn, VIR_ERR_SYSTEM_ERROR, strerror (errno)); goto failed; } @@ -702,8 +702,7 @@ // Run the external process. if (!cmd_argv) { - cmd_argv = malloc (2 * sizeof (*cmd_argv)); - if (cmd_argv == NULL) { + if (VIR_ALLOC_N(cmd_argv, 2) < 0) { error (conn, VIR_ERR_SYSTEM_ERROR, strerror (errno)); goto failed; } @@ -811,8 +810,7 @@ if (inside_daemon) return VIR_DRV_OPEN_DECLINED; - priv = calloc (1, sizeof(*priv)); - if (!priv) { + if (VIR_ALLOC(priv) < 0) { error (conn, VIR_ERR_NO_MEMORY, _("struct private_data")); return VIR_DRV_OPEN_ERROR; } @@ -856,7 +854,7 @@ ret = doRemoteOpen(conn, priv, uri, auth, rflags); if (ret != VIR_DRV_OPEN_SUCCESS) { conn->privateData = NULL; - free(priv); + VIR_FREE(priv); } else { priv->magic = MAGIC; conn->privateData = priv; @@ -2268,9 +2266,7 @@ /* Serialise the scheduler parameters. */ args.params.params_len = nparams; - args.params.params_val = malloc (sizeof (*args.params.params_val) - * nparams); - if (args.params.params_val == NULL) { + if (VIR_ALLOC_N(args.params.params_val, nparams) < 0) { error (domain->conn, VIR_ERR_RPC, _("out of memory allocating array")); return -1; } @@ -2400,9 +2396,9 @@ * use the UNIX transport. This handles Xen driver * which doesn't have its own impl of the network APIs. */ - struct private_data *priv = calloc (1, sizeof(*priv)); + struct private_data *priv; int ret, rflags = 0; - if (!priv) { + if (VIR_ALLOC(priv) < 0) { error (conn, VIR_ERR_NO_MEMORY, _("struct private_data")); return VIR_DRV_OPEN_ERROR; } @@ -2415,7 +2411,7 @@ ret = doRemoteOpen(conn, priv, uri, auth, rflags); if (ret != VIR_DRV_OPEN_SUCCESS) { conn->networkPrivateData = NULL; - free(priv); + VIR_FREE(priv); } else { priv->magic = MAGIC; priv->localUses = 1; @@ -2434,7 +2430,7 @@ priv->localUses--; if (!priv->localUses) { ret = doRemoteClose(conn, priv); - free(priv); + VIR_FREE(priv); conn->networkPrivateData = NULL; } } @@ -2806,9 +2802,9 @@ * use the UNIX transport. This handles Xen driver * which doesn't have its own impl of the network APIs. */ - struct private_data *priv = calloc (1, sizeof(struct private_data)); + struct private_data *priv; int ret, rflags = 0; - if (!priv) { + if (VIR_ALLOC(priv) < 0) { error (NULL, VIR_ERR_NO_MEMORY, _("struct private_data")); return VIR_DRV_OPEN_ERROR; } @@ -2821,7 +2817,7 @@ ret = doRemoteOpen(conn, priv, uri, auth, rflags); if (ret != VIR_DRV_OPEN_SUCCESS) { conn->storagePrivateData = NULL; - free(priv); + VIR_FREE(priv); } else { priv->magic = MAGIC; priv->localUses = 1; @@ -2840,7 +2836,7 @@ priv->localUses--; if (!priv->localUses) { ret = doRemoteClose(conn, priv); - free(priv); + VIR_FREE(priv); conn->storagePrivateData = NULL; } } @@ -3559,7 +3555,7 @@ mech = authtype + 5; if (remoteAuthSASL(conn, priv, in_open, auth, mech) < 0) { - free(ret.types.types_val); + VIR_FREE(ret.types.types_val); return -1; } break; @@ -3569,7 +3565,7 @@ #if HAVE_POLKIT case REMOTE_AUTH_POLKIT: if (remoteAuthPolkit(conn, priv, in_open, auth) < 0) { - free(ret.types.types_val); + VIR_FREE(ret.types.types_val); return -1; } break; @@ -3585,11 +3581,11 @@ NULL, NULL, NULL, 0, 0, _("unsupported authentication type %d"), ret.types.types_val[0]); - free(ret.types.types_val); - return -1; - } - - free(ret.types.types_val); + VIR_FREE(ret.types.types_val); + return -1; + } + + VIR_FREE(ret.types.types_val); return 0; } @@ -3618,8 +3614,7 @@ return NULL; } - addr = malloc(strlen(host) + 1 + strlen(port) + 1); - if (!addr) { + if (VIR_ALLOC_N(addr, strlen(host) + 1 + strlen(port) + 1) < 0) { __virRaiseError (NULL, NULL, NULL, VIR_FROM_REMOTE, VIR_ERR_NO_MEMORY, VIR_ERR_ERROR, NULL, NULL, NULL, 0, 0, @@ -3712,9 +3707,9 @@ */ static sasl_callback_t *remoteAuthMakeCallbacks(int *credtype, int ncredtype) { - sasl_callback_t *cbs = calloc(ncredtype+1, sizeof (*cbs)); + sasl_callback_t *cbs; int i, n; - if (!cbs) { + if (VIR_ALLOC_N(cbs, ncredtype+1) < 0) { return NULL; } @@ -3749,14 +3744,13 @@ for (ninteract = 0 ; interact[ninteract].id != 0 ; ninteract++) ; /* empty */ - *cred = calloc(ninteract, sizeof(*cred)); - if (!*cred) + if (VIR_ALLOC_N(*cred, ninteract) < 0) return -1; for (ninteract = 0 ; interact[ninteract].id != 0 ; ninteract++) { (*cred)[ninteract].type = remoteAuthCredSASL2Vir(interact[ninteract].id); if (!(*cred)[ninteract].type) { - free(*cred); + VIR_FREE(*cred); return -1; } if (interact[ninteract].challenge) @@ -3775,8 +3769,8 @@ { int i; for (i = 0 ; i < ncred ; i++) - free(cred[i].result); - free(cred); + VIR_FREE(cred[i].result); + VIR_FREE(cred); } @@ -3944,7 +3938,7 @@ NULL, NULL, NULL, 0, 0, _("SASL mechanism %s not supported by server"), wantmech); - free(iret.mechlist); + VIR_FREE(iret.mechlist); goto cleanup; } mechlist = wantmech; @@ -3963,7 +3957,7 @@ VIR_ERR_AUTH_FAILED, VIR_ERR_ERROR, NULL, NULL, NULL, 0, 0, _("Failed to start SASL negotiation: %d (%s)"), err, sasl_errdetail(saslconn)); - free(iret.mechlist); + VIR_FREE(iret.mechlist); goto cleanup; } @@ -3980,7 +3974,7 @@ VIR_ERR_AUTH_FAILED, VIR_ERR_ERROR, NULL, NULL, NULL, 0, 0, "%s", _("Failed to make auth credentials")); - free(iret.mechlist); + VIR_FREE(iret.mechlist); goto cleanup; } /* Run the authentication callback */ @@ -3998,7 +3992,7 @@ 0, 0, "%s", msg); goto cleanup; } - free(iret.mechlist); + VIR_FREE(iret.mechlist); if (clientoutlen > REMOTE_AUTH_SASL_DATA_MAX) { __virRaiseError (in_open ? NULL : conn, NULL, NULL, VIR_FROM_REMOTE, @@ -4077,8 +4071,7 @@ } if (serverin) { - free(serverin); - serverin = NULL; + VIR_FREE(serverin); } DEBUG("Client step result %d. Data %d bytes %p", err, clientoutlen, clientout); @@ -4110,7 +4103,7 @@ /* This server call shows complete, and earlier client step was OK */ if (complete && err == SASL_OK) { - free(serverin); + VIR_FREE(serverin); break; } } @@ -4140,11 +4133,11 @@ ret = 0; cleanup: - free(localAddr); - free(remoteAddr); - free(serverin); - - free(saslcb); + VIR_FREE(localAddr); + VIR_FREE(remoteAddr); + VIR_FREE(serverin); + + VIR_FREE(saslcb); remoteAuthFreeCredentials(cred, ncred); if (ret != 0 && saslconn) sasl_dispose(&saslconn); diff -r ff6b92c70738 src/sexpr.c --- a/src/sexpr.c Fri May 30 10:36:42 2008 -0400 +++ b/src/sexpr.c Fri May 30 10:55:44 2008 -0400 @@ -21,6 +21,7 @@ #include "internal.h" #include "sexpr.h" #include "util.h" +#include "memory.h" /** * virSexprError: @@ -55,8 +56,7 @@ { struct sexpr *ret; - ret = (struct sexpr *) malloc(sizeof(*ret)); - if (ret == NULL) { + if (VIR_ALLOC(ret) < 0) { virSexprError(VIR_ERR_NO_MEMORY, _("failed to allocate a node")); return (NULL); } @@ -85,13 +85,13 @@ sexpr_free(sexpr->u.s.cdr); break; case SEXPR_VALUE: - free(sexpr->u.value); + VIR_FREE(sexpr->u.value); break; case SEXPR_NIL: break; } - free(sexpr); + VIR_FREE(sexpr); errno = serrno; } @@ -171,16 +171,23 @@ * * Internal operation appending a value at the end of an existing list */ -static void +static int append(struct sexpr *lst, const struct sexpr *value) { + struct sexpr *nil = sexpr_nil(); + + if (nil == NULL) + return -1; + while (lst->kind != SEXPR_NIL) { lst = lst->u.s.cdr; } lst->kind = SEXPR_CONS; lst->u.s.car = (struct sexpr *) value; - lst->u.s.cdr = sexpr_nil(); + lst->u.s.cdr = nil; + + return 0; } /** @@ -198,7 +205,8 @@ return (NULL); if (value == NULL) return (lst); - append(lst, value); + if (append(lst, value) < 0) + return (NULL); return (lst); } @@ -318,8 +326,11 @@ tmp = _string2sexpr(ptr, &tmp_len); if (tmp == NULL) - return NULL; - append(ret, tmp); + goto error; + if (append(ret, tmp) < 0) { + sexpr_free(tmp); + goto error; + } #if 0 if (0) { char buf[4096]; @@ -351,6 +362,7 @@ if (ret->u.value == NULL) { virSexprError(VIR_ERR_NO_MEMORY, _("failed to copy a string")); + goto error; } if (*ptr == '\'') @@ -367,6 +379,7 @@ if (ret->u.value == NULL) { virSexprError(VIR_ERR_NO_MEMORY, _("failed to copy a string")); + goto error; } } diff -r ff6b92c70738 src/storage_backend.c --- a/src/storage_backend.c Fri May 30 10:36:42 2008 -0400 +++ b/src/storage_backend.c Fri May 30 10:55:44 2008 -0400 @@ -50,6 +50,7 @@ #endif #include "util.h" +#include "memory.h" #include "storage_backend.h" #include "storage_backend_fs.h" @@ -237,8 +238,7 @@ vol->target.perms.uid = sb.st_uid; vol->target.perms.gid = sb.st_gid; - free(vol->target.perms.label); - vol->target.perms.label = NULL; + VIR_FREE(vol->target.perms.label); #if HAVE_SELINUX if (fgetfilecon(fd, &filecon) == -1) { @@ -310,9 +310,8 @@ if (dent->d_name[0] == '.') continue; - stablepath = malloc(strlen(pool->def->target.path) + - 1 + strlen(dent->d_name) + 1); - if (stablepath == NULL) { + if (VIR_ALLOC_N(stablepath, strlen(pool->def->target.path) + + 1 + strlen(dent->d_name) + 1) < 0) { virStorageReportError(conn, VIR_ERR_NO_MEMORY, "%s", _("path")); closedir(dh); return NULL; @@ -327,7 +326,7 @@ return stablepath; } - free(stablepath); + VIR_FREE(stablepath); } closedir(dh); @@ -365,7 +364,7 @@ char **groups; /* Compile all regular expressions */ - if ((reg = calloc(nregex, sizeof(*reg))) == NULL) { + if (VIR_ALLOC_N(reg, nregex) < 0) { virStorageReportError(conn, VIR_ERR_NO_MEMORY, "%s", _("regex")); return -1; } @@ -379,7 +378,7 @@ _("Failed to compile regex %s"), error); for (j = 0 ; j <= i ; j++) regfree(®[j]); - free(reg); + VIR_FREE(reg); return -1; } @@ -390,12 +389,12 @@ } /* Storage for matched variables */ - if ((groups = calloc(totgroups, sizeof(*groups))) == NULL) { + if (VIR_ALLOC_N(groups, totgroups) < 0) { virStorageReportError(conn, VIR_ERR_NO_MEMORY, "%s", _("regex groups")); goto cleanup; } - if ((vars = calloc(maxvars+1, sizeof(*vars))) == NULL) { + if (VIR_ALLOC_N(vars, maxvars+1) < 0) { virStorageReportError(conn, VIR_ERR_NO_MEMORY, "%s", _("regex groups")); goto cleanup; @@ -445,7 +444,7 @@ /* Release matches & restart to matching the first regex */ for (j = 0 ; j < totgroups ; j++) { - free(groups[j]); + VIR_FREE(groups[j]); groups[j] = NULL; } maxReg = 0; @@ -460,15 +459,15 @@ cleanup: if (groups) { for (j = 0 ; j < totgroups ; j++) - free(groups[j]); - free(groups); + VIR_FREE(groups[j]); + VIR_FREE(groups); } - free(vars); + VIR_FREE(vars); for (i = 0 ; i < nregex ; i++) regfree(®[i]); - free(reg); + VIR_FREE(reg); if (list) fclose(list); @@ -534,8 +533,7 @@ if (n_columns == 0) return -1; - if (n_columns > SIZE_MAX / sizeof *v - || (v = malloc (n_columns * sizeof *v)) == NULL) { + if (VIR_ALLOC_N(v, n_columns) < 0) { virStorageReportError(conn, VIR_ERR_NO_MEMORY, "%s", _("n_columns too large")); return -1; diff -r ff6b92c70738 src/storage_backend_disk.c --- a/src/storage_backend_disk.c Fri May 30 10:36:42 2008 -0400 +++ b/src/storage_backend_disk.c Fri May 30 10:55:44 2008 -0400 @@ -26,6 +26,7 @@ #include "internal.h" #include "storage_backend_disk.h" #include "util.h" +#include "memory.h" enum { VIR_STORAGE_POOL_DISK_DOS = 0, @@ -174,7 +175,7 @@ char *tmp, *devpath; if (vol == NULL) { - if ((vol = calloc(1, sizeof(virStorageVolDef))) == NULL) { + if (VIR_ALLOC(vol) < 0) { virStorageReportError(conn, VIR_ERR_NO_MEMORY, _("volume")); return -1; } @@ -211,8 +212,7 @@ return -1; if (devpath != vol->target.path) - free(devpath); - devpath = NULL; + VIR_FREE(devpath); } if (vol->key == NULL) { @@ -224,8 +224,7 @@ } if (vol->source.extents == NULL) { - if ((vol->source.extents = - calloc(1, sizeof(*(vol->source.extents)))) == NULL) { + if (VIR_ALLOC(vol->source.extents) < 0) { virStorageReportError(conn, VIR_ERR_NO_MEMORY, _("volume extents")); return -1; @@ -275,16 +274,15 @@ virStoragePoolObjPtr pool, char **const groups) { - virStoragePoolSourceDeviceExtentPtr tmp; virStoragePoolSourceDevicePtr dev = &pool->def->source.devices[0]; - if ((tmp = realloc(dev->freeExtents, - sizeof(*tmp) * (dev->nfreeExtent+1))) == NULL) + if (VIR_REALLOC_N(dev->freeExtents, + dev->nfreeExtent + 1) < 0) return -1; - dev->freeExtents = tmp; memset(dev->freeExtents + - dev->nfreeExtent, 0, sizeof(*tmp)); + dev->nfreeExtent, 0, + sizeof(dev->freeExtents[0])); if (virStrToLong_ull(groups[3], NULL, 10, &dev->freeExtents[dev->nfreeExtent].start) < 0) @@ -388,9 +386,8 @@ virStorageBackendDiskRefreshPool(virConnectPtr conn, virStoragePoolObjPtr pool) { - free(pool->def->source.devices[0].freeExtents); + VIR_FREE(pool->def->source.devices[0].freeExtents); pool->def->source.devices[0].nfreeExtent = 0; - pool->def->source.devices[0].freeExtents = NULL; return virStorageBackendDiskReadPartitions(conn, pool, NULL); } @@ -476,9 +473,8 @@ return -1; /* Blow away free extent info, as we're about to re-populate it */ - free(pool->def->source.devices[0].freeExtents); + VIR_FREE(pool->def->source.devices[0].freeExtents); pool->def->source.devices[0].nfreeExtent = 0; - pool->def->source.devices[0].freeExtents = NULL; /* Fetch actual extent info */ if (virStorageBackendDiskReadPartitions(conn, pool, vol) < 0) diff -r ff6b92c70738 src/storage_backend_fs.c --- a/src/storage_backend_fs.c Fri May 30 10:36:42 2008 -0400 +++ b/src/storage_backend_fs.c Fri May 30 10:55:44 2008 -0400 @@ -40,8 +40,7 @@ #include "storage_backend_fs.h" #include "storage_conf.h" #include "util.h" -#include "config.h" - +#include "memory.h" enum { VIR_STORAGE_POOL_FS_AUTO = 0, @@ -530,25 +529,27 @@ } if (pool->def->type == VIR_STORAGE_POOL_NETFS) { - src = malloc(strlen(pool->def->source.host.name) + - 1 + strlen(pool->def->source.dir) + 1); + if (VIR_ALLOC_N(src, strlen(pool->def->source.host.name) + + 1 + strlen(pool->def->source.dir) + 1) < 0) { + virStorageReportError(conn, VIR_ERR_NO_MEMORY, "%s", _("source")); + return -1; + } strcpy(src, pool->def->source.host.name); strcat(src, ":"); strcat(src, pool->def->source.dir); } else { - src = strdup(pool->def->source.devices[0].path); - } - if (src == NULL) { - virStorageReportError(conn, VIR_ERR_NO_MEMORY, "%s", _("source")); - return -1; + if ((src = strdup(pool->def->source.devices[0].path)) == NULL) { + virStorageReportError(conn, VIR_ERR_NO_MEMORY, "%s", _("source")); + return -1; + } } mntargv[3] = src; if (virRun(conn, (char**)mntargv, NULL) < 0) { - free(src); + VIR_FREE(src); return -1; } - free(src); + VIR_FREE(src); return 0; } @@ -679,8 +680,7 @@ virStorageVolDefPtr vol; int ret; - vol = calloc(1, sizeof(virStorageVolDef)); - if (vol == NULL) { + if (VIR_ALLOC(vol) < 0) { virStorageReportError(conn, VIR_ERR_NO_MEMORY, "%s", _("volume")); goto cleanup; @@ -688,18 +688,17 @@ vol->name = strdup(ent->d_name); if (vol->name == NULL) { - free(vol); + VIR_FREE(vol); virStorageReportError(conn, VIR_ERR_NO_MEMORY, "%s", _("volume name")); goto cleanup; } vol->target.format = VIR_STORAGE_VOL_RAW; /* Real value is filled in during probe */ - vol->target.path = malloc(strlen(pool->def->target.path) + - 1 + strlen(vol->name) + 1); - if (vol->target.path == NULL) { - free(vol->target.path); - free(vol); + if (VIR_ALLOC_N(vol->target.path, strlen(pool->def->target.path) + + 1 + strlen(vol->name) + 1) < 0) { + VIR_FREE(vol->target.path); + VIR_FREE(vol); virStorageReportError(conn, VIR_ERR_NO_MEMORY, "%s", _("volume name")); goto cleanup; @@ -708,19 +707,19 @@ strcat(vol->target.path, "/"); strcat(vol->target.path, vol->name); if ((vol->key = strdup(vol->target.path)) == NULL) { - free(vol->name); - free(vol->target.path); - free(vol); + VIR_FREE(vol->name); + VIR_FREE(vol->target.path); + VIR_FREE(vol); virStorageReportError(conn, VIR_ERR_NO_MEMORY, "%s", _("volume key")); goto cleanup; } if ((ret = virStorageBackendProbeFile(conn, vol) < 0)) { - free(vol->key); - free(vol->name); - free(vol->target.path); - free(vol); + VIR_FREE(vol->key); + VIR_FREE(vol->name); + VIR_FREE(vol->target.path); + VIR_FREE(vol); if (ret == -1) goto cleanup; else @@ -821,9 +820,8 @@ { int fd; - vol->target.path = malloc(strlen(pool->def->target.path) + - 1 + strlen(vol->name) + 1); - if (vol->target.path == NULL) { + if (VIR_ALLOC_N(vol->target.path, strlen(pool->def->target.path) + + 1 + strlen(vol->name) + 1) < 0) { virStorageReportError(conn, VIR_ERR_NO_MEMORY, "%s", _("target")); return -1; } diff -r ff6b92c70738 src/storage_backend_iscsi.c --- a/src/storage_backend_iscsi.c Fri May 30 10:36:42 2008 -0400 +++ b/src/storage_backend_iscsi.c Fri May 30 10:55:44 2008 -0400 @@ -37,6 +37,7 @@ #include "internal.h" #include "storage_backend_iscsi.h" #include "util.h" +#include "memory.h" static int virStorageBackendISCSITargetIP(virConnectPtr conn, @@ -253,7 +254,7 @@ snprintf(lunid, sizeof(lunid)-1, "lun-%s", groups[3]); - if ((vol = calloc(1, sizeof(virStorageVolDef))) == NULL) { + if (VIR_ALLOC(vol) < 0) { virStorageReportError(conn, VIR_ERR_NO_MEMORY, "%s", _("volume")); goto cleanup; } @@ -263,14 +264,13 @@ goto cleanup; } - if ((devpath = malloc(5 + strlen(dev) + 1)) == NULL) { + if (VIR_ALLOC_N(devpath, 5 + strlen(dev) + 1) < 0) { virStorageReportError(conn, VIR_ERR_NO_MEMORY, "%s", _("devpath")); goto cleanup; } strcpy(devpath, "/dev/"); strcat(devpath, dev); - free(dev); - dev = NULL; + VIR_FREE(dev); /* It can take a little while between logging into the ISCSI * server and udev creating the /dev nodes, so if we get ENOENT * we must retry a few times - they should eventually appear. @@ -303,8 +303,7 @@ goto cleanup; if (devpath != vol->target.path) - free(devpath); - devpath = NULL; + VIR_FREE(devpath); if (virStorageBackendUpdateVolInfoFD(conn, vol, fd, 1) < 0) goto cleanup; @@ -330,9 +329,9 @@ cleanup: if (fd != -1) close(fd); - free(devpath); + VIR_FREE(devpath); virStorageVolDefFree(vol); - free(dev); + VIR_FREE(dev); return -1; } @@ -422,8 +421,7 @@ ipaddr, sizeof(ipaddr)) < 0) return NULL; - portal = malloc(strlen(ipaddr) + 1 + 4 + 2 + 1); - if (portal == NULL) { + if (VIR_ALLOC_N(portal, strlen(ipaddr) + 1 + 4 + 2 + 1) < 0) { virStorageReportError(conn, VIR_ERR_NO_MEMORY, "%s", _("portal")); return NULL; } @@ -457,10 +455,10 @@ if ((portal = virStorageBackendISCSIPortal(conn, pool)) == NULL) return -1; if (virStorageBackendISCSILogin(conn, pool, portal) < 0) { - free(portal); + VIR_FREE(portal); return -1; } - free(portal); + VIR_FREE(portal); return 0; } @@ -478,12 +476,12 @@ goto cleanup; if (virStorageBackendISCSIFindLUNs(conn, pool, session) < 0) goto cleanup; - free(session); + VIR_FREE(session); return 0; cleanup: - free(session); + VIR_FREE(session); return -1; } @@ -498,10 +496,10 @@ return -1; if (virStorageBackendISCSILogout(conn, pool, portal) < 0) { - free(portal); + VIR_FREE(portal); return -1; } - free(portal); + VIR_FREE(portal); return 0; } diff -r ff6b92c70738 src/storage_backend_logical.c --- a/src/storage_backend_logical.c Fri May 30 10:36:42 2008 -0400 +++ b/src/storage_backend_logical.c Fri May 30 10:55:44 2008 -0400 @@ -35,7 +35,7 @@ #include "storage_backend_logical.h" #include "storage_conf.h" #include "util.h" - +#include "memory.h" #define PV_BLANK_SECTOR_SIZE 512 @@ -97,7 +97,6 @@ void *data) { virStorageVolDefPtr vol = NULL; - virStorageVolSourceExtentPtr tmp; unsigned long long offset, size, length; /* See if we're only looking for a specific volume */ @@ -113,7 +112,7 @@ /* Or a completely new volume */ if (vol == NULL) { - if ((vol = calloc(1, sizeof(*vol))) == NULL) { + if (VIR_ALLOC(vol) < 0) { virStorageReportError(conn, VIR_ERR_NO_MEMORY, "%s", _("volume")); return -1; } @@ -129,8 +128,8 @@ } if (vol->target.path == NULL) { - if ((vol->target.path = malloc(strlen(pool->def->target.path) + - 1 + strlen(vol->name) + 1)) == NULL) { + if (VIR_ALLOC_N(vol->target.path, strlen(pool->def->target.path) + + 1 + strlen(vol->name) + 1) < 0) { virStorageReportError(conn, VIR_ERR_NO_MEMORY, "%s", _("volume")); return -1; } @@ -150,12 +149,11 @@ /* Finally fill in extents information */ - if ((tmp = realloc(vol->source.extents, sizeof(*tmp) - * (vol->source.nextent + 1))) == NULL) { + if (VIR_REALLOC_N(vol->source.extents, + vol->source.nextent + 1) < 0) { virStorageReportError(conn, VIR_ERR_NO_MEMORY, "%s", _("extents")); return -1; } - vol->source.extents = tmp; if ((vol->source.extents[vol->source.nextent].path = strdup(groups[2])) == NULL) { @@ -266,7 +264,7 @@ memset(zeros, 0, sizeof(zeros)); /* XXX multiple pvs */ - if ((vgargv = malloc(sizeof(char*) * (1))) == NULL) { + if (VIR_ALLOC_N(vgargv, 1) < 0) { virStorageReportError(conn, VIR_ERR_NO_MEMORY, "%s", _("command line")); return -1; } @@ -318,12 +316,12 @@ if (virRun(conn, (char**)vgargv, NULL) < 0) goto cleanup; - free(vgargv); + VIR_FREE(vgargv); return 0; cleanup: - free(vgargv); + VIR_FREE(vgargv); return -1; } diff -r ff6b92c70738 src/storage_conf.c --- a/src/storage_conf.c Fri May 30 10:36:42 2008 -0400 +++ b/src/storage_conf.c Fri May 30 10:55:44 2008 -0400 @@ -45,6 +45,7 @@ #include "uuid.h" #include "buf.h" #include "util.h" +#include "memory.h" #define virStorageLog(msg...) fprintf(stderr, msg) @@ -70,40 +71,40 @@ void virStorageVolDefFree(virStorageVolDefPtr def) { int i; - free(def->name); - free(def->key); + VIR_FREE(def->name); + VIR_FREE(def->key); for (i = 0 ; i < def->source.nextent ; i++) { - free(def->source.extents[i].path); + VIR_FREE(def->source.extents[i].path); } - free(def->source.extents); + VIR_FREE(def->source.extents); - free(def->target.path); - free(def->target.perms.label); - free(def); + VIR_FREE(def->target.path); + VIR_FREE(def->target.perms.label); + VIR_FREE(def); } void virStoragePoolDefFree(virStoragePoolDefPtr def) { int i; - free(def->name); - free(def->source.host.name); + VIR_FREE(def->name); + VIR_FREE(def->source.host.name); for (i = 0 ; i < def->source.ndevice ; i++) { - free(def->source.devices[i].freeExtents); - free(def->source.devices[i].path); + VIR_FREE(def->source.devices[i].freeExtents); + VIR_FREE(def->source.devices[i].path); } - free(def->source.devices); - free(def->source.dir); + VIR_FREE(def->source.devices); + VIR_FREE(def->source.dir); if (def->source.authType == VIR_STORAGE_POOL_AUTH_CHAP) { - free(def->source.auth.chap.login); - free(def->source.auth.chap.passwd); + VIR_FREE(def->source.auth.chap.login); + VIR_FREE(def->source.auth.chap.passwd); } - free(def->target.path); - free(def->target.perms.label); - free(def); + VIR_FREE(def->target.path); + VIR_FREE(def->target.perms.label); + VIR_FREE(def); } @@ -114,9 +115,9 @@ if (obj->newDef) virStoragePoolDefFree(obj->newDef); - free(obj->configFile); - free(obj->autostartLink); - free(obj); + VIR_FREE(obj->configFile); + VIR_FREE(obj->autostartLink); + VIR_FREE(obj); } void @@ -225,8 +226,11 @@ char *uuid = NULL; char *authType = NULL; - if ((ret = calloc(1, sizeof(virStoragePoolDef))) == NULL) + if (VIR_ALLOC(ret) < 0) { + virStorageReportError(conn, VIR_ERR_NO_MEMORY, + "%s", _("cannot allocate storage pool")); return NULL; + } if (STRNEQ((const char *)root->name, "pool")) { virStorageReportError(conn, VIR_ERR_XML_ERROR, @@ -263,17 +267,16 @@ "%s", _("malformed uuid element")); goto cleanup; } - free(uuid); - uuid = NULL; + VIR_FREE(uuid); } if (options->formatFromString) { char *format = virXPathString("string(/pool/source/format/@type)", ctxt); if ((ret->source.format = (options->formatFromString)(conn, format)) < 0) { - free(format); + VIR_FREE(format); goto cleanup; } - free(format); + VIR_FREE(format); } if (options->flags & VIR_STORAGE_BACKEND_POOL_SOURCE_HOST) { @@ -292,22 +295,22 @@ "%s", _("cannot extract source devices")); goto cleanup; } - if ((ret->source.devices = calloc(nsource, sizeof(*ret->source.devices))) == NULL) { - free(nodeset); + if (VIR_ALLOC_N(ret->source.devices, nsource) < 0) { + VIR_FREE(nodeset); virStorageReportError(conn, VIR_ERR_NO_MEMORY, "%s", _("device")); goto cleanup; } for (i = 0 ; i < nsource ; i++) { xmlChar *path = xmlGetProp(nodeset[i], BAD_CAST "path"); if (path == NULL) { - free(nodeset); + VIR_FREE(nodeset); virStorageReportError(conn, VIR_ERR_XML_ERROR, "%s", _("missing source device path")); goto cleanup; } ret->source.devices[i].path = (char *)path; } - free(nodeset); + VIR_FREE(nodeset); ret->source.ndevice = nsource; } if (options->flags & VIR_STORAGE_BACKEND_POOL_SOURCE_DIR) { @@ -329,12 +332,10 @@ virStorageReportError(conn, VIR_ERR_XML_ERROR, _("unknown auth type '%s'"), (const char *)authType); - free(authType); - authType = NULL; + VIR_FREE(authType); goto cleanup; } - free(authType); - authType = NULL; + VIR_FREE(authType); } if (ret->source.authType == VIR_STORAGE_POOL_AUTH_CHAP) { @@ -354,7 +355,7 @@ return ret; cleanup: - free(uuid); + VIR_FREE(uuid); xmlFree(type); virStoragePoolDefFree(ret); return NULL; @@ -649,8 +650,11 @@ if (options == NULL) return NULL; - if ((ret = calloc(1, sizeof(virStorageVolDef))) == NULL) + if (VIR_ALLOC(ret) < 0) { + virStorageReportError(conn, VIR_ERR_NO_MEMORY, + "%s", _("cannot allocate storage vol")); return NULL; + } if (STRNEQ((const char *)root->name, "volume")) { virStorageReportError(conn, VIR_ERR_XML_ERROR, @@ -677,20 +681,16 @@ } if (virStorageSize(conn, unit, capacity, &ret->capacity) < 0) goto cleanup; - free(capacity); - capacity = NULL; - free(unit); - unit = NULL; + VIR_FREE(capacity); + VIR_FREE(unit); allocation = virXPathString("string(/volume/allocation)", ctxt); if (allocation) { unit = virXPathString("string(/volume/allocation/@unit)", ctxt); if (virStorageSize(conn, unit, allocation, &ret->allocation) < 0) goto cleanup; - free(allocation); - allocation = NULL; - free(unit); - unit = NULL; + VIR_FREE(allocation); + VIR_FREE(unit); } else { ret->allocation = ret->capacity; } @@ -699,10 +699,10 @@ if (options->formatFromString) { char *format = virXPathString("string(/volume/target/format/@type)", ctxt); if ((ret->target.format = (options->formatFromString)(conn, format)) < 0) { - free(format); + VIR_FREE(format); goto cleanup; } - free(format); + VIR_FREE(format); } if (virStorageVolDefParsePerms(conn, ctxt, &ret->target.perms) < 0) @@ -711,9 +711,9 @@ return ret; cleanup: - free(allocation); - free(capacity); - free(unit); + VIR_FREE(allocation); + VIR_FREE(capacity); + VIR_FREE(unit); virStorageVolDefFree(ret); return NULL; } @@ -772,6 +772,7 @@ virStorageVolDefPtr def) { virStorageBackendVolOptionsPtr options; virBuffer buf = VIR_BUFFER_INITIALIZER; + char *tmp; options = virStorageBackendVolOptionsForType(pool->type); if (options == NULL) @@ -849,7 +850,8 @@ no_memory: virStorageReportError(conn, VIR_ERR_NO_MEMORY, "%s", _("xml")); cleanup: - free(virBufferContentAndReset(&buf)); + tmp = virBufferContentAndReset(&buf); + VIR_FREE(tmp); return NULL; } @@ -955,7 +957,7 @@ return pool; } - if (!(pool = calloc(1, sizeof(virStoragePoolObj)))) { + if (VIR_ALLOC(pool) < 0) { virStorageReportError(conn, VIR_ERR_NO_MEMORY, "%s", _("pool")); return NULL; @@ -1063,7 +1065,7 @@ virStoragePoolObjLoad(driver, entry->d_name, path, xml, autostartLink); - free(xml); + VIR_FREE(xml); } closedir(dir); @@ -1108,15 +1110,13 @@ virStorageReportError(conn, VIR_ERR_INTERNAL_ERROR, "%s", _("cannot construct " "autostart link path")); - free(pool->configFile); - pool->configFile = NULL; + VIR_FREE(pool->configFile); return -1; } if (!(pool->autostartLink = strdup(path))) { virStorageReportError(conn, VIR_ERR_NO_MEMORY, "%s", _("config file")); - free(pool->configFile); - pool->configFile = NULL; + VIR_FREE(pool->configFile); return -1; } } @@ -1157,7 +1157,7 @@ if (fd != -1) close(fd); - free(xml); + VIR_FREE(xml); return ret; } diff -r ff6b92c70738 src/storage_driver.c --- a/src/storage_driver.c Fri May 30 10:36:42 2008 -0400 +++ b/src/storage_driver.c Fri May 30 10:55:44 2008 -0400 @@ -37,7 +37,7 @@ #include "util.h" #include "storage_driver.h" #include "storage_conf.h" - +#include "memory.h" #include "storage_backend.h" #define storageLog(msg...) fprintf(stderr, msg) @@ -104,9 +104,8 @@ char *base = NULL; char driverConf[PATH_MAX]; - if (!(driverState = calloc(1, sizeof(virStorageDriverState)))) { + if (VIR_ALLOC(driverState) < 0) return -1; - } if (!uid) { if ((base = strdup (SYSCONF_DIR "/libvirt")) == NULL) diff -r ff6b92c70738 src/xen_internal.c --- a/src/xen_internal.c Fri May 30 10:36:42 2008 -0400 +++ b/src/xen_internal.c Fri May 30 10:55:44 2008 -0400 @@ -48,6 +48,7 @@ #include "buf.h" #include "capabilities.h" +#include "memory.h" #define DEBUG(fmt,...) VIR_DEBUG(__FILE__, fmt, __VA_ARGS__) #define DEBUG0(msg) VIR_DEBUG(__FILE__, "%s", msg) @@ -223,17 +224,17 @@ #define XEN_GETDOMAININFOLIST_ALLOC(domlist, size) \ (hypervisor_version < 2 ? \ - ((domlist.v0 = malloc(sizeof(*domlist.v0)*(size))) != NULL) : \ + (VIR_ALLOC_N(domlist.v0, (size)) == 0) : \ (dom_interface_version < 5 ? \ - ((domlist.v2 = malloc(sizeof(*domlist.v2)*(size))) != NULL) : \ - ((domlist.v2d5 = malloc(sizeof(*domlist.v2d5)*(size))) != NULL))) + (VIR_ALLOC_N(domlist.v2, (size)) == 0) : \ + (VIR_ALLOC_N(domlist.v2d5, (size)) == 0))) -#define XEN_GETDOMAININFOLIST_FREE(domlist) \ - (hypervisor_version < 2 ? \ - free(domlist.v0) : \ - (dom_interface_version < 5 ? \ - free(domlist.v2) : \ - free(domlist.v2d5))) +#define XEN_GETDOMAININFOLIST_FREE(domlist) \ + (hypervisor_version < 2 ? \ + VIR_FREE(domlist.v0) : \ + (dom_interface_version < 5 ? \ + VIR_FREE(domlist.v2) : \ + VIR_FREE(domlist.v2d5))) #define XEN_GETDOMAININFOLIST_CLEAR(domlist, size) \ (hypervisor_version < 2 ? \ @@ -796,8 +797,7 @@ { char *msg_s; - msg_s = malloc (strlen (msg) + 10); - if (msg_s) { + if (VIR_ALLOC_N(msg_s, strlen (msg) + 10) == 0) { strcpy (msg_s, msg); strcat (msg_s, ": %s"); } @@ -1659,8 +1659,7 @@ /* The allocated memory to cpumap must be 'sizeof(uint64_t)' byte * * for Xen, and also nr_cpus must be 'sizeof(uint64_t) * 8' */ if (maplen < 8) { - new = calloc(1, sizeof(uint64_t)); - if (!new) { + if (VIR_ALLOC_N(new, sizeof(uint64_t)) < 0) { virXenErrorFunc(NULL, VIR_ERR_NO_MEMORY, __FUNCTION__, "allocating private data", 0); return (-1); @@ -1683,7 +1682,7 @@ op.u.setvcpumapd5.cpumap.nr_cpus = nr_cpus; } ret = xenHypervisorDoV2Dom(handle, &op); - free(new); + VIR_FREE(new); if (unlock_pages(cpumap, maplen) < 0) { virXenError(NULL, VIR_ERR_XEN_CALL, " release", maplen); @@ -1985,8 +1984,7 @@ */ hypervisor_version = 2; - ipt = malloc(sizeof(*ipt)); - if (ipt == NULL){ + if (VIR_ALLOC(ipt) < 0) { virXenError(NULL, VIR_ERR_NO_MEMORY, __FUNCTION__, 0); return(-1); } @@ -2053,13 +2051,13 @@ virXenError(NULL, VIR_ERR_XEN_CALL, " ioctl ", IOCTL_PRIVCMD_HYPERCALL); close(fd); in_init = 0; - free(ipt); + VIR_FREE(ipt); return(-1); done: close(fd); in_init = 0; - free(ipt); + VIR_FREE(ipt); return(0); } @@ -2647,7 +2645,7 @@ ret = virGetDomain(conn, name, XEN_GETDOMAININFO_UUID(dominfo)); if (ret) ret->id = id; - free(name); + VIR_FREE(name); return ret; } @@ -2714,7 +2712,7 @@ ret = virGetDomain(conn, name, uuid); if (ret) ret->id = id; - free(name); + VIR_FREE(name); return ret; } #endif diff -r ff6b92c70738 src/xen_unified.c --- a/src/xen_unified.c Fri May 30 10:36:42 2008 -0400 +++ b/src/xen_unified.c Fri May 30 10:55:44 2008 -0400 @@ -40,6 +40,7 @@ #include "xm_internal.h" #include "xml.h" #include "util.h" +#include "memory.h" #define DEBUG(fmt,...) VIR_DEBUG(__FILE__, fmt,__VA_ARGS__) #define DEBUG0(msg) VIR_DEBUG(__FILE__, "%s", msg) @@ -172,15 +173,12 @@ if (xenUnifiedNodeGetInfo(dom->conn, &nodeinfo) < 0) return(NULL); - cpulist = calloc(nb_cpu, sizeof(*cpulist)); - if (cpulist == NULL) + if (VIR_ALLOC_N(cpulist, nb_cpu) < 0) goto done; - cpuinfo = malloc(sizeof(*cpuinfo) * nb_vcpu); - if (cpuinfo == NULL) + if (VIR_ALLOC_N(cpuinfo, nb_vcpu) < 0) goto done; cpumaplen = VIR_CPU_MAPLEN(VIR_NODEINFO_MAXCPUS(nodeinfo)); - cpumap = (unsigned char *) calloc(nb_vcpu, cpumaplen); - if (cpumap == NULL) + if (VIR_ALLOC_N(cpumap, nb_vcpu * cpumaplen) < 0) goto done; if ((ncpus = xenUnifiedDomainGetVcpus(dom, cpuinfo, nb_vcpu, @@ -202,9 +200,9 @@ } done: - free(cpulist); - free(cpumap); - free(cpuinfo); + VIR_FREE(cpulist); + VIR_FREE(cpumap); + VIR_FREE(cpuinfo); return(res); } @@ -262,8 +260,7 @@ return VIR_DRV_OPEN_DECLINED; /* Allocate per-connection private data. */ - priv = calloc (1, sizeof *priv); - if (!priv) { + if (VIR_ALLOC(priv) < 0) { xenUnifiedError (NULL, VIR_ERR_NO_MEMORY, "allocating private data"); return VIR_DRV_OPEN_ERROR; } @@ -342,7 +339,7 @@ DEBUG0("Failed to activate a mandatory sub-driver"); for (i = 0 ; i < XEN_UNIFIED_NR_DRIVERS ; i++) if (priv->opened[i]) drivers[i]->close(conn); - free(priv); + VIR_FREE(priv); return VIR_DRV_OPEN_ERROR; } @@ -961,7 +958,7 @@ char *cpus, *res; cpus = xenDomainUsedCpus(dom); res = xenDaemonDomainDumpXML(dom, flags, cpus); - free(cpus); + VIR_FREE(cpus); return(res); } if (priv->opened[XEN_UNIFIED_PROXY_OFFSET]) diff -r ff6b92c70738 src/xend_internal.c --- a/src/xend_internal.c Fri May 30 10:36:42 2008 -0400 +++ b/src/xend_internal.c Fri May 30 10:55:44 2008 -0400 @@ -24,7 +24,6 @@ #include <stdbool.h> #include <math.h> #include <stdarg.h> -#include <malloc.h> #include <netinet/in.h> #include <netinet/tcp.h> #include <arpa/inet.h> diff -r ff6b92c70738 src/xmlrpc.c --- a/src/xmlrpc.c Fri May 30 10:36:42 2008 -0400 +++ b/src/xmlrpc.c Fri May 30 10:55:44 2008 -0400 @@ -12,6 +12,7 @@ #include "xmlrpc.h" #include "internal.h" +#include "memory.h" #include <libxml/nanohttp.h> @@ -47,9 +48,8 @@ static xmlRpcValuePtr xmlRpcValueNew(xmlRpcValueType type) { - xmlRpcValuePtr ret = malloc(sizeof(*ret)); - - if (!ret) + xmlRpcValuePtr ret = NULL; + if (VIR_ALLOC(ret) < 0) xmlRpcError(VIR_ERR_NO_MEMORY, _("allocate value"), sizeof(*ret)); else ret->kind = type; @@ -115,7 +115,7 @@ if (ret && value) ret->value.integer = atoi(value); - free(value); + VIR_FREE(value); return ret; } @@ -130,7 +130,7 @@ ret->value.boolean = true; else ret->value.boolean = false; - free(value); + VIR_FREE(value); return ret; } @@ -141,7 +141,7 @@ if (ret && value) ret->value.real = atof(value); - free(value); + VIR_FREE(value); return ret; } @@ -158,11 +158,10 @@ for (cur = xmlFirstElement(node); cur; cur = xmlNextElement(cur)) n_elements += 1; - elems = malloc(n_elements * sizeof(*elems)); - if (!elems) { + if (VIR_ALLOC_N(elems, n_elements) < 0) { xmlRpcError(VIR_ERR_NO_MEMORY, _("allocate value array"), n_elements * sizeof(*elems)); - free(ret); + VIR_FREE(ret); return NULL; } n_elements = 0; @@ -179,10 +178,10 @@ static xmlRpcValueDictElementPtr xmlRpcValueUnmarshalDictElement(xmlNodePtr node) { - xmlRpcValueDictElementPtr ret = malloc(sizeof(*ret)); + xmlRpcValueDictElementPtr ret; xmlNodePtr cur; - if (!ret) { + if (VIR_ALLOC(ret) < 0) { xmlRpcError(VIR_ERR_NO_MEMORY, _("allocate dict"), sizeof(*ret)); return NULL; } @@ -195,10 +194,10 @@ ret->value = xmlRpcValueUnmarshal(cur); } else { xmlRpcError(VIR_ERR_XML_ERROR, _("unexpected dict node"), 0); - free(ret->name); + VIR_FREE(ret->name); if (ret->value) xmlRpcValueFree(ret->value); - free(ret); + VIR_FREE(ret); return NULL; } } @@ -283,26 +282,26 @@ case XML_RPC_ARRAY: for (i = 0; i < value->value.array.n_elements; i++) xmlRpcValueFree(value->value.array.elements[i]); - free(value->value.array.elements); + VIR_FREE(value->value.array.elements); break; case XML_RPC_STRUCT: next = value->value.dict.root; while (next) { cur = next; next = next->next; - free(cur->name); + VIR_FREE(cur->name); xmlRpcValueFree(cur->value); - free(cur); + VIR_FREE(cur); } break; case XML_RPC_STRING: - free(value->value.string); + VIR_FREE(value->value.string); break; default: break; } - free(value); + VIR_FREE(value); } void xmlRpcValueMarshal(xmlRpcValuePtr value, virBufferPtr buf, int indent) @@ -436,15 +435,14 @@ } len = xmlNanoHTTPContentLength(cxt); - response = malloc(len + 1); - if (response == NULL) { + if (VIR_ALLOC_N(response, len + 1) < 0) { xmlRpcError(VIR_ERR_NO_MEMORY, _("allocate response"), len); goto error; } ret = xmlNanoHTTPRead(cxt, response, len); if (ret != len) { errno = EINVAL; - free(response); + VIR_FREE(response); response = NULL; xmlRpcError(VIR_ERR_POST_FAILED, _("read response"), 0); } @@ -455,7 +453,7 @@ serrno = errno; if (cxt) { xmlNanoHTTPClose(cxt); - free(contentType); + VIR_FREE(contentType); } errno = serrno; @@ -477,7 +475,7 @@ if (value->value.array.elements[i]->kind == XML_RPC_STRING) size += strlen(value->value.array.elements[i]->value.string) + 1; - if (!(ptr = malloc(size))) { + if (VIR_ALLOC_N(ptr, size) < 0) { xmlRpcError(VIR_ERR_NO_MEMORY, _("allocate string array"), size); return NULL; } @@ -507,7 +505,7 @@ int i; *argc = strlen(fmt); - if (!(argv = malloc(sizeof(*argv) * *argc))) { + if (VIR_ALLOC_N(argv, *argc) < 0) { xmlRpcError(VIR_ERR_NO_MEMORY, _("read response"), sizeof(*argv) * *argc); return NULL; } @@ -552,7 +550,7 @@ for (i = 0; i < argc; i++) xmlRpcValueFree(argv[i]); - free(argv); + VIR_FREE(argv); } int xmlRpcCall(xmlRpcContextPtr context, const char *method, @@ -589,7 +587,7 @@ content = virBufferContentAndReset(&buf); ret = xmlRpcCallRaw(context->uri, content); - free(content); + VIR_FREE(content); if (!ret) return -1; @@ -597,7 +595,7 @@ xml = xmlReadDoc((const xmlChar *)ret, "response.xml", NULL, XML_PARSE_NOENT | XML_PARSE_NONET | XML_PARSE_NOERROR | XML_PARSE_NOWARNING); - free(ret); + VIR_FREE(ret); if (xml == NULL) { errno = EINVAL; @@ -659,13 +657,14 @@ xmlRpcContextPtr xmlRpcContextNew(const char *uri) { - xmlRpcContextPtr ret = malloc(sizeof(*ret)); + xmlRpcContextPtr ret = NULL; - if (ret) { + if (VIR_ALLOC(ret) < 0) { + xmlRpcError(VIR_ERR_NO_MEMORY, _("allocate new context"), sizeof(*ret)); + } else { ret->uri = strdup(uri); ret->faultMessage = NULL; - } else - xmlRpcError(VIR_ERR_NO_MEMORY, _("allocate new context"), sizeof(*ret)); + } return ret; } @@ -673,9 +672,9 @@ void xmlRpcContextFree(xmlRpcContextPtr context) { if (context) { - free(context->uri); - free(context->faultMessage); - free(context); + VIR_FREE(context->uri); + VIR_FREE(context->faultMessage); + VIR_FREE(context); } } -- |: 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 :|

On Fri, May 30, 2008 at 04:01:39PM +0100, Daniel P. Berrange wrote:
This patch switches all remaining code over to use the memory allocation APIs, with exception of virsh which is going to be slightly more complex
It was mostly a straight conversion - there were only a few places which weren't checking for failure corecttly - the most notable being sexpr.c. [...] - void *stack, *stacktop; + char *stack, *stacktop;
/* allocate a stack for the container */ - stack = malloc(stacksize); - if (!stack) { + if (VIR_ALLOC_N(stack, stacksize) < 0) {
hum, interesting side effect ... we must type stuff with the new macros. [...]
--- a/src/openvz_driver.c Fri May 30 10:36:42 2008 -0400 +++ b/src/openvz_driver.c Fri May 30 10:55:44 2008 -0400 @@ -49,8 +49,7 @@ #include <stdio.h> #include <sys/wait.h>
-#include "libvirt/virterror.h" - +#include "internal.h"
a bit of cleanup there too. [...]
-static void +static int append(struct sexpr *lst, const struct sexpr *value) {
okay, the sexpr code really needed to be changed, good !
diff -r ff6b92c70738 src/xen_internal.c --- a/src/xen_internal.c Fri May 30 10:36:42 2008 -0400 +++ b/src/xen_internal.c Fri May 30 10:55:44 2008 -0400 ... @@ -1659,8 +1659,7 @@ /* The allocated memory to cpumap must be 'sizeof(uint64_t)' byte * * for Xen, and also nr_cpus must be 'sizeof(uint64_t) * 8' */ if (maplen < 8) { - new = calloc(1, sizeof(uint64_t)); - if (!new) { + if (VIR_ALLOC_N(new, sizeof(uint64_t)) < 0) {
That one worried me, but that works but because we have unsigned char *new
--- a/src/xmlrpc.c Fri May 30 10:36:42 2008 -0400 +++ b/src/xmlrpc.c Fri May 30 10:55:44 2008 -0400 @@ -12,6 +12,7 @@
Hum, i think that's dead code anyway, no ?
#include "xmlrpc.h" #include "internal.h" +#include "memory.h"
#include <libxml/nanohttp.h>
@@ -47,9 +48,8 @@
static xmlRpcValuePtr xmlRpcValueNew(xmlRpcValueType type) { - xmlRpcValuePtr ret = malloc(sizeof(*ret)); - - if (!ret) + xmlRpcValuePtr ret = NULL; + if (VIR_ALLOC(ret) < 0)
I don't think we need to set ret to NULL, do we ? VIR_ALLOC always initialize. Okay I think I really read all the patch and tried to check everything :-) looks good, thanks a lot ! 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/

On Fri, May 30, 2008 at 12:13:42PM -0400, Daniel Veillard wrote:
On Fri, May 30, 2008 at 04:01:39PM +0100, Daniel P. Berrange wrote:
This patch switches all remaining code over to use the memory allocation APIs, with exception of virsh which is going to be slightly more complex
It was mostly a straight conversion - there were only a few places which weren't checking for failure corecttly - the most notable being sexpr.c. [...] - void *stack, *stacktop; + char *stack, *stacktop;
/* allocate a stack for the container */ - stack = malloc(stacksize); - if (!stack) { + if (VIR_ALLOC_N(stack, stacksize) < 0) {
hum, interesting side effect ... we must type stuff with the new macros.
Yes, that is correct - the macros use sizeof() to automatically determine the size of the alloc needed. Although GCC treats sizeof(void) as being the same as sizeof(char), this is not required by the C standard - it is technically 'undefined behaviour'. So its safest to just switchto using a char * for the stack here. An earlier function dealing with stacks in this same file was already using char * too.
@@ -1659,8 +1659,7 @@ /* The allocated memory to cpumap must be 'sizeof(uint64_t)' byte * * for Xen, and also nr_cpus must be 'sizeof(uint64_t) * 8' */ if (maplen < 8) { - new = calloc(1, sizeof(uint64_t)); - if (!new) { + if (VIR_ALLOC_N(new, sizeof(uint64_t)) < 0) {
That one worried me, but that works but because we have unsigned char *new
Yeah, I was undecided whether to use sizeof(uint64_t) here, or just hardcode the value 8 to match the line earlier.
--- a/src/xmlrpc.c Fri May 30 10:36:42 2008 -0400 +++ b/src/xmlrpc.c Fri May 30 10:55:44 2008 -0400 @@ -12,6 +12,7 @@
Hum, i think that's dead code anyway, no ?
Yes, although you might end up using it for VMWare driver if you use the webservices API ?
@@ -47,9 +48,8 @@
static xmlRpcValuePtr xmlRpcValueNew(xmlRpcValueType type) { - xmlRpcValuePtr ret = malloc(sizeof(*ret)); - - if (!ret) + xmlRpcValuePtr ret = NULL; + if (VIR_ALLOC(ret) < 0)
I don't think we need to set ret to NULL, do we ? VIR_ALLOC always initialize.
Yes, that is redundant. Dan. -- |: 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 :|

On Mon, Jun 02, 2008 at 01:49:13PM +0100, Daniel P. Berrange wrote:
On Fri, May 30, 2008 at 12:13:42PM -0400, Daniel Veillard wrote:
--- a/src/xmlrpc.c Fri May 30 10:36:42 2008 -0400 +++ b/src/xmlrpc.c Fri May 30 10:55:44 2008 -0400 @@ -12,6 +12,7 @@
Hum, i think that's dead code anyway, no ?
Yes, although you might end up using it for VMWare driver if you use the webservices API ?
Sigh if only VMWare had used the XML-RPC mechnism, but no they went the bloated SOAP way ... 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/

"Daniel P. Berrange" <berrange@redhat.com> wrote:
This patch switches all remaining code over to use the memory allocation APIs, with exception of virsh which is going to be slightly more complex
It was mostly a straight conversion - there were only a few places which weren't checking for failure corecttly - the most notable being sexpr.c. ... Nice work (but tedious to review!). I went through the whole thing this time. Comments below.
diff -r ff6b92c70738 src/conf.c ... @@ -897,15 +897,16 @@
fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR ); if (fd < 0) { + char *tmp = virBufferContentAndReset(&buf); virConfError(NULL, VIR_ERR_WRITE_FAILED, _("failed to open file"), 0); - free(virBufferContentAndReset(&buf)); + VIR_FREE(tmp); return -1; } ...
diff -r ff6b92c70738 src/remote_internal.c --- a/src/remote_internal.c Fri May 30 10:36:42 2008 -0400 +++ b/src/remote_internal.c Fri May 30 10:55:44 2008 -0400 ... @@ -3749,14 +3744,13 @@ for (ninteract = 0 ; interact[ninteract].id != 0 ; ninteract++) ; /* empty */
- *cred = calloc(ninteract, sizeof(*cred)); - if (!*cred) + if (VIR_ALLOC_N(*cred, ninteract) < 0) return -1;
Cool! You fixed a subtle bug right there. The old code should have read like this: *cred = calloc(ninteract, sizeof(**cred)); Looks like it would have caused heap corruption, since sizeof(*cred) is smaller than sizeof(**cred). ...
diff -r ff6b92c70738 src/storage_backend_logical.c --- a/src/storage_backend_logical.c Fri May 30 10:36:42 2008 -0400 +++ b/src/storage_backend_logical.c Fri May 30 10:55:44 2008 -0400 ... @@ -266,7 +264,7 @@ memset(zeros, 0, sizeof(zeros));
/* XXX multiple pvs */ - if ((vgargv = malloc(sizeof(char*) * (1))) == NULL) { + if (VIR_ALLOC_N(vgargv, 1) < 0) { virStorageReportError(conn, VIR_ERR_NO_MEMORY, "%s", _("command line"));
That can be just if (VIR_ALLOC(vgargv) < 0) { ...
diff -r ff6b92c70738 src/xen_unified.c --- a/src/xen_unified.c Fri May 30 10:36:42 2008 -0400 +++ b/src/xen_unified.c Fri May 30 10:55:44 2008 -0400 @@ -40,6 +40,7 @@ #include "xm_internal.h" #include "xml.h" #include "util.h" +#include "memory.h"
#define DEBUG(fmt,...) VIR_DEBUG(__FILE__, fmt,__VA_ARGS__) #define DEBUG0(msg) VIR_DEBUG(__FILE__, "%s", msg) @@ -172,15 +173,12 @@ if (xenUnifiedNodeGetInfo(dom->conn, &nodeinfo) < 0) return(NULL);
- cpulist = calloc(nb_cpu, sizeof(*cpulist)); - if (cpulist == NULL) + if (VIR_ALLOC_N(cpulist, nb_cpu) < 0) goto done; - cpuinfo = malloc(sizeof(*cpuinfo) * nb_vcpu); - if (cpuinfo == NULL) + if (VIR_ALLOC_N(cpuinfo, nb_vcpu) < 0) goto done; cpumaplen = VIR_CPU_MAPLEN(VIR_NODEINFO_MAXCPUS(nodeinfo)); - cpumap = (unsigned char *) calloc(nb_vcpu, cpumaplen); - if (cpumap == NULL) + if (VIR_ALLOC_N(cpumap, nb_vcpu * cpumaplen) < 0) goto done;
At first I thought it didn't matter that the product wasn't checked for overflow, but then I spent a couple minutes trying to find if/where nb_vcpu was guaranteed to be small enough that we don't have to worry. There may well be code to ensure that, but if so, it's too far from this point of use for my taste, so I think it's best to add an explicit overflow check here, i.e., if (xalloc_oversized(nb_vcpu, cpumaplen) || VIR_ALLOC_N(cpumap, nb_vcpu * cpumaplen) < 0) goto done; ...

On Mon, Jun 02, 2008 at 04:35:47PM +0200, Jim Meyering wrote:
"Daniel P. Berrange" <berrange@redhat.com> wrote:
This patch switches all remaining code over to use the memory allocation APIs, with exception of virsh which is going to be slightly more complex
It was mostly a straight conversion - there were only a few places which weren't checking for failure corecttly - the most notable being sexpr.c.
@@ -266,7 +264,7 @@ memset(zeros, 0, sizeof(zeros));
/* XXX multiple pvs */ - if ((vgargv = malloc(sizeof(char*) * (1))) == NULL) { + if (VIR_ALLOC_N(vgargv, 1) < 0) { virStorageReportError(conn, VIR_ERR_NO_MEMORY, "%s", _("command line"));
That can be just
if (VIR_ALLOC(vgargv) < 0) {
I kept that as ALLOC_N to remind myself that this needs to change in the future to support multiple PVs.
@@ -172,15 +173,12 @@ if (xenUnifiedNodeGetInfo(dom->conn, &nodeinfo) < 0) return(NULL);
- cpulist = calloc(nb_cpu, sizeof(*cpulist)); - if (cpulist == NULL) + if (VIR_ALLOC_N(cpulist, nb_cpu) < 0) goto done; - cpuinfo = malloc(sizeof(*cpuinfo) * nb_vcpu); - if (cpuinfo == NULL) + if (VIR_ALLOC_N(cpuinfo, nb_vcpu) < 0) goto done; cpumaplen = VIR_CPU_MAPLEN(VIR_NODEINFO_MAXCPUS(nodeinfo)); - cpumap = (unsigned char *) calloc(nb_vcpu, cpumaplen); - if (cpumap == NULL) + if (VIR_ALLOC_N(cpumap, nb_vcpu * cpumaplen) < 0) goto done;
At first I thought it didn't matter that the product wasn't checked for overflow, but then I spent a couple minutes trying to find if/where nb_vcpu was guaranteed to be small enough that we don't have to worry. There may well be code to ensure that, but if so, it's too far from this point of use for my taste, so I think it's best to add an explicit overflow check here, i.e.,
if (xalloc_oversized(nb_vcpu, cpumaplen) || VIR_ALLOC_N(cpumap, nb_vcpu * cpumaplen) < 0) goto done;
Yep, this does really need checking Dan. -- |: Red Hat, Engineering, London -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 :|
participants (3)
-
Daniel P. Berrange
-
Daniel Veillard
-
Jim Meyering