diff --git a/ChangeLog b/ChangeLog index 5979ae954f..e205611ee8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,15 @@ +Fri Jun 6 12:01:00 BST 2008 Daniel P. Berrange + + * src/bridge.c, src/conf.c, src/iptables.c, src/lxc_conf.c, + src/lxc_container.c, src/lxc_driver.c, src/openvz_conf.c, + src/openvz_driver.c, src/proxy_internal.c, src/remote_internal.c, + src/sexpr.c, src/storage_backend.c, src/storage_backend_disk.c, + src/storage_backend_fs.c, src/storage_backend_iscsi.c, + src/storage_backend_logical.c, src/storage_conf.c, + src/storage_driver.c, src/xen_internal.c, src/xen_unified.c, + src/xend_internal.c, src/xmlrpc.c: Switch over to use the + internal memory allocation APIs. + Fri Jun 6 11:41:00 BST 2008 Daniel P. Berrange * qemud/event.c, qemud/mdns.c, qemud/qemud.c, qemud/remote.c: diff --git a/src/bridge.c b/src/bridge.c index b5af09c768..15917b3254 100644 --- a/src/bridge.c +++ b/src/bridge.c @@ -45,6 +45,7 @@ #include /* ARPHRD_ETHER */ #include "internal.h" +#include "memory.h" #define MAX_BRIDGE_ID 256 @@ -84,8 +85,7 @@ brInit(brControl **ctlp) return err; } - *ctlp = malloc(sizeof(**ctlp)); - if (!*ctlp) { + if (VIR_ALLOC(*ctlp) < 0) { close(fd); return ENOMEM; } @@ -110,7 +110,7 @@ brShutdown(brControl *ctl) close(ctl->fd); ctl->fd = 0; - free(ctl); + VIR_FREE(ctl); } /** @@ -681,7 +681,7 @@ brSetForwardDelay(brControl *ctl ATTRIBUTE_UNUSED, 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 @@ brSetForwardDelay(brControl *ctl ATTRIBUTE_UNUSED, 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 @@ brSetEnableSTP(brControl *ctl ATTRIBUTE_UNUSED, 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 @@ brSetEnableSTP(brControl *ctl ATTRIBUTE_UNUSED, if (argv) { n = 0; while (argv[n]) - free(argv[n++]); - free(argv); + VIR_FREE(argv[n++]); + VIR_FREE(argv); } return retval; diff --git a/src/conf.c b/src/conf.c index f927262311..233357fb17 100644 --- a/src/conf.c +++ b/src/conf.c @@ -23,6 +23,7 @@ #include "conf.h" #include "util.h" #include "c-ctype.h" +#include "memory.h" /************************************************************************ * * @@ -138,11 +139,11 @@ __virConfFreeValue(virConfValuePtr val) 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 @@ __virConfNew(void) { 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 @@ virConfAddEntry(virConfPtr conf, char *name, virConfValuePtr value, char *comm) 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 @@ virConfParseValue(virConfParserCtxtPtr ctxt) 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 @@ virConfParseValue(virConfParserCtxtPtr ctxt) 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 @@ virConfParseStatement(virConfParserCtxtPtr ctxt) SKIP_BLANKS; value = virConfParseValue(ctxt); if (value == NULL) { - free(name); + VIR_FREE(name); return(-1); } SKIP_BLANKS; @@ -630,15 +630,15 @@ virConfParseStatement(virConfParserCtxtPtr ctxt) 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 @@ __virConfReadFile(const char *filename) conf = virConfParse(filename, content, len); - free(content); + VIR_FREE(content); return conf; } @@ -769,14 +769,14 @@ __virConfFree(virConfPtr conf) 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 @@ __virConfSetValue (virConfPtr conf, } 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 @@ __virConfWriteFile(const char *filename, virConfPtr conf) 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 @@ __virConfWriteMem(char *memory, int *len, virConfPtr conf) 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 --git a/src/iptables.c b/src/iptables.c index 7cba53e3e0..e7613e2bed 100644 --- a/src/iptables.c +++ b/src/iptables.c @@ -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 @@ notifyRulesRemoved(const char *table, 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 @@ notifyRulesRemoved(const char *table, goto write_error; } - free(content); + VIR_FREE(content); return; @@ -181,7 +182,7 @@ notifyRulesRemoved(const char *table, strerror(errno)); if (f) fclose(f); - free(content); + VIR_FREE(content); #undef MAX_FILE_LEN } @@ -264,14 +265,14 @@ iptRulesSave(iptRules *rules) 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,18 +283,14 @@ iptRulesAppend(iptRules *rules, 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; rules->rules[rules->nrules].command_idx = command_idx; @@ -332,23 +329,17 @@ iptRulesFree(iptRules *rules) { int i; - if (rules->table) { - free(rules->table); - rules->table = NULL; - } - - if (rules->chain) { - free(rules->chain); - rules->chain = NULL; - } + if (rules->table) + VIR_FREE(rules->table); + 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 @@ iptRulesFree(iptRules *rules) rules->path[0] = '\0'; #endif /* ENABLE_IPTABLES_LOKKIT */ - free(rules); + VIR_FREE(rules); } static iptRules * @@ -367,7 +358,7 @@ iptRulesNew(const char *table, { iptRules *rules; - if (!(rules = calloc(1, sizeof (*rules)))) + if (VIR_ALLOC(rules) < 0) return NULL; if (!(rules->table = strdup(table))) @@ -404,8 +395,9 @@ argvToString(char **argv) 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 @@ iptablesAddRemoveRule(iptRules *rules, int action, const char *arg, ...) 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 @@ iptablesAddRemoveRule(iptRules *rules, int action, const char *arg, ...) 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 @@ iptablesAddRemoveRule(iptRules *rules, int action, const char *arg, ...) } 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 @@ iptablesContextNew(void) { 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 @@ iptablesContextFree(iptablesContext *ctx) iptRulesFree(ctx->forward_filter); if (ctx->nat_postrouting) iptRulesFree(ctx->nat_postrouting); - free(ctx); + VIR_FREE(ctx); } /** diff --git a/src/lxc_conf.c b/src/lxc_conf.c index e1138f20a0..d6ed936c5c 100644 --- a/src/lxc_conf.c +++ b/src/lxc_conf.c @@ -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 @@ static int lxcParseDomainUUID(virConnectPtr conn, unsigned char *uuid, 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 @@ static int lxcParseDomainMounts(virConnectPtr conn, 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 @@ static int lxcParseDomainMounts(virConnectPtr conn, } prevObj = mountObj; } - free(list); + VIR_FREE(list); } rc = nmounts; @@ -252,7 +251,7 @@ static int lxcParseDomainInit(virConnectPtr conn, char** init, 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 @@ static lxc_vm_def_t * lxcParseXML(virConnectPtr conn, xmlDocPtr docPtr) 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 @@ static lxc_vm_def_t * lxcParseXML(virConnectPtr conn, xmlDocPtr docPtr) _("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))) { @@ -357,8 +355,7 @@ static lxc_vm_def_t * lxcParseXML(virConnectPtr conn, xmlDocPtr docPtr) } else { containerDef->id = -1; } - free(xmlProp); - xmlProp = NULL; + VIR_FREE(xmlProp); if (lxcParseDomainName(conn, &(containerDef->name), contextPtr) < 0) { goto error; @@ -391,7 +388,7 @@ static lxc_vm_def_t * lxcParseXML(virConnectPtr conn, xmlDocPtr docPtr) return containerDef; error: - free(xmlProp); + VIR_FREE(xmlProp); xmlXPathFreeContext(contextPtr); lxcFreeVMDef(containerDef); @@ -442,7 +439,7 @@ lxc_vm_t * lxcAssignVMDef(virConnectPtr conn, 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; } @@ -575,7 +572,7 @@ int lxcSaveConfig(virConnectPtr conn, close(fd); } - free(xmlDef); + VIR_FREE(xmlDef); return rc; } @@ -689,7 +686,7 @@ int lxcLoadContainerConfigFile(lxc_driver_t *driver, lxcLoadConfig(driver, file, tempPath, xmlData); - free(xmlData); + VIR_FREE(xmlData); load_complete: return rc; @@ -796,14 +793,14 @@ void lxcFreeVMDef(lxc_vm_def_t *vmdef) 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) @@ -821,8 +818,8 @@ void lxcFreeVMs(lxc_vm_t *vms) 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 --git a/src/lxc_container.c b/src/lxc_container.c index 2c7cbebd62..e2c74f363d 100644 --- a/src/lxc_container.c +++ b/src/lxc_container.c @@ -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 @@ static int lxcExecContainerInit(const lxc_vm_def_t *vmDef) 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 --git a/src/lxc_driver.c b/src/lxc_driver.c index ba747327a0..e765afa679 100644 --- a/src/lxc_driver.c +++ b/src/lxc_driver.c @@ -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 @@ static int lxcCheckContainerSupport( void ) 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 @@ static int lxcCheckContainerSupport( void ) waitpid(cpid, &childStatus, 0); } - free(stack); + VIR_FREE(stack); check_complete: return rc; @@ -263,7 +263,7 @@ static int lxcListDefinedDomains(virConnectPtr conn, cleanup: for (i = 0 ; i < numDoms ; i++) { - free(names[i]); + VIR_FREE(names[i]); } return -1; @@ -400,16 +400,15 @@ static int lxcStartContainer(virConnectPtr conn, 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; @@ -574,8 +573,7 @@ static int lxcSetupContainerTty(virConnectPtr conn, 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; @@ -1046,8 +1044,7 @@ static int lxcStartup(void) return -1; } - lxc_driver = calloc(1, sizeof(lxc_driver_t)); - if (NULL == lxc_driver) { + if (VIR_ALLOC(lxc_driver) < 0) { return -1; } @@ -1074,9 +1071,9 @@ static int lxcStartup(void) static void lxcFreeDriver(lxc_driver_t *driver) { - free(driver->configDir); - free(driver->stateDir); - free(driver); + VIR_FREE(driver->configDir); + VIR_FREE(driver->stateDir); + VIR_FREE(driver); } static int lxcShutdown(void) diff --git a/src/memory.c b/src/memory.c index 4394ba9e96..d35ada3b71 100644 --- a/src/memory.c +++ b/src/memory.c @@ -77,22 +77,6 @@ static int virAllocTestFail(void) #endif -/* Return 1 if an array of N objects, each of size S, cannot exist due - to size arithmetic overflow. S must be positive and N must be - nonnegative. This is a macro, not an inline function, so that it - works correctly even when SIZE_MAX < N. - - By gnulib convention, SIZE_MAX represents overflow in size - calculations, so the conservative dividend to use here is - SIZE_MAX - 1, since SIZE_MAX might represent an overflowed value. - However, malloc (SIZE_MAX) fails on all known hosts where - sizeof (ptrdiff_t) <= sizeof (size_t), so do not bother to test for - exactly-SIZE_MAX allocations on such hosts; this avoids a test and - branch when S is known to be 1. */ -# define xalloc_oversized(n, s) \ - ((size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / (s) < (n)) - - /** * virAlloc: * @ptrptr: pointer to pointer for address of allocated memory diff --git a/src/memory.h b/src/memory.h index 43864ff1ba..d40edfb039 100644 --- a/src/memory.h +++ b/src/memory.h @@ -25,6 +25,25 @@ #include "internal.h" +/* Return 1 if an array of N objects, each of size S, cannot exist due + to size arithmetic overflow. S must be positive and N must be + nonnegative. This is a macro, not an inline function, so that it + works correctly even when SIZE_MAX < N. + + By gnulib convention, SIZE_MAX represents overflow in size + calculations, so the conservative dividend to use here is + SIZE_MAX - 1, since SIZE_MAX might represent an overflowed value. + However, malloc (SIZE_MAX) fails on all known hosts where + sizeof (ptrdiff_t) <= sizeof (size_t), so do not bother to test for + exactly-SIZE_MAX allocations on such hosts; this avoids a test and + branch when S is known to be 1. */ +#ifndef xalloc_oversized +# define xalloc_oversized(n, s) \ + ((size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / (s) < (n)) +#endif + + + /* Don't call these directly - use the macros below */ int __virAlloc(void *ptrptr, size_t size) ATTRIBUTE_RETURN_CHECK; int __virAllocN(void *ptrptr, size_t size, size_t count) ATTRIBUTE_RETURN_CHECK; diff --git a/src/openvz_conf.c b/src/openvz_conf.c index 7a00bbc977..090c0a4fd5 100644 --- a/src/openvz_conf.c +++ b/src/openvz_conf.c @@ -53,6 +53,7 @@ #include "openvz_conf.h" #include "uuid.h" #include "buf.h" +#include "memory.h" #include @@ -152,23 +153,22 @@ openvzFreeVMDef(struct openvz_vm_def *def) 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 @@ openvzFreeVM(struct openvz_driver *driver, struct openvz_vm *vm, } if (vms) { openvzFreeVMDef(vm->vmdef); - free(vm); - vm = NULL; + VIR_FREE(vm); } } @@ -217,8 +216,7 @@ openvzFreeDriver(struct openvz_driver *driver) 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 @@ openvzAssignVMDef(virConnectPtr conn, 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 @@ static struct openvz_vm_def 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 @@ static struct openvz_vm_def 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 @@ static struct openvz_vm_def 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 @@ static struct openvz_vm_def 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 @@ static struct openvz_vm_def return def; bail_out: - free(prop); + VIR_FREE(prop); xmlXPathFreeObject(obj); xmlXPathFreeContext(ctxt); openvzFreeVMDef(def); @@ -539,8 +536,7 @@ openvzGetVPSInfo(virConnectPtr conn) { } 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 @@ openvzGetVPSInfo(virConnectPtr conn) { (*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 @@ openvzGetVPSInfo(virConnectPtr conn) { 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 @@ error: 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 @@ openvzGetVPSUUID(int vpsid, char *uuidstr) 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 @@ openvzSetUUID(int vpsid) 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 @@ int openvzAssignUUIDs(void) dp = opendir(conf_dir); if(dp == NULL) { - free(conf_dir); + VIR_FREE(conf_dir); return 0; } @@ -753,7 +748,7 @@ int openvzAssignUUIDs(void) openvzSetUUID(vpsid); } closedir(dp); - free(conf_dir); + VIR_FREE(conf_dir); return 0; } diff --git a/src/openvz_driver.c b/src/openvz_driver.c index 22c898aea3..a20438f091 100644 --- a/src/openvz_driver.c +++ b/src/openvz_driver.c @@ -49,8 +49,7 @@ #include #include -#include "libvirt/virterror.h" - +#include "internal.h" #include "openvz_driver.h" #include "event.h" #include "buf.h" diff --git a/src/proxy_internal.c b/src/proxy_internal.c index b16d1be01d..02c620ce1a 100644 --- a/src/proxy_internal.c +++ b/src/proxy_internal.c @@ -28,6 +28,7 @@ #include "proxy_internal.h" #include "util.h" #include "xen_unified.h" +#include "memory.h" #define STANDALONE @@ -992,8 +993,7 @@ xenProxyGetCapabilities (virConnectPtr conn) } 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 @@ xenProxyDomainDumpXML(virDomainPtr domain, int flags ATTRIBUTE_UNUSED) 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 @@ xenProxyDomainGetOSType(virDomainPtr domain) 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 --git a/src/remote_internal.c b/src/remote_internal.c index 478c93dc85..cc34ae6e35 100644 --- a/src/remote_internal.c +++ b/src/remote_internal.c @@ -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 @@ doRemoteOpen (virConnectPtr conn, // 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 @@ doRemoteOpen (virConnectPtr conn, // 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 @@ remoteOpen (virConnectPtr conn, 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 @@ remoteOpen (virConnectPtr conn, 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 @@ remoteDomainSetSchedulerParameters (virDomainPtr domain, /* 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; } @@ -2446,9 +2442,9 @@ remoteNetworkOpen (virConnectPtr conn, * 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; } @@ -2461,7 +2457,7 @@ remoteNetworkOpen (virConnectPtr conn, 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; @@ -2480,7 +2476,7 @@ remoteNetworkClose (virConnectPtr conn) priv->localUses--; if (!priv->localUses) { ret = doRemoteClose(conn, priv); - free(priv); + VIR_FREE(priv); conn->networkPrivateData = NULL; } } @@ -2852,9 +2848,9 @@ remoteStorageOpen (virConnectPtr conn, * 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; } @@ -2867,7 +2863,7 @@ remoteStorageOpen (virConnectPtr conn, 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; @@ -2886,7 +2882,7 @@ remoteStorageClose (virConnectPtr conn) priv->localUses--; if (!priv->localUses) { ret = doRemoteClose(conn, priv); - free(priv); + VIR_FREE(priv); conn->storagePrivateData = NULL; } } @@ -3605,7 +3601,7 @@ remoteAuthenticate (virConnectPtr conn, struct private_data *priv, int in_open, 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; @@ -3615,7 +3611,7 @@ remoteAuthenticate (virConnectPtr conn, struct private_data *priv, int in_open, #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; @@ -3631,11 +3627,11 @@ remoteAuthenticate (virConnectPtr conn, struct private_data *priv, int in_open, NULL, NULL, NULL, 0, 0, _("unsupported authentication type %d"), ret.types.types_val[0]); - free(ret.types.types_val); + VIR_FREE(ret.types.types_val); return -1; } - free(ret.types.types_val); + VIR_FREE(ret.types.types_val); return 0; } @@ -3664,8 +3660,7 @@ static char *addrToString(struct sockaddr_storage *sa, socklen_t salen) 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, @@ -3758,9 +3753,9 @@ static int remoteAuthCredSASL2Vir(int vircred) */ 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; } @@ -3795,14 +3790,13 @@ static int remoteAuthMakeCredentials(sasl_interact_t *interact, 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) @@ -3821,8 +3815,8 @@ static void remoteAuthFreeCredentials(virConnectCredentialPtr cred, { int i; for (i = 0 ; i < ncred ; i++) - free(cred[i].result); - free(cred); + VIR_FREE(cred[i].result); + VIR_FREE(cred); } @@ -3990,7 +3984,7 @@ remoteAuthSASL (virConnectPtr conn, struct private_data *priv, int in_open, NULL, NULL, NULL, 0, 0, _("SASL mechanism %s not supported by server"), wantmech); - free(iret.mechlist); + VIR_FREE(iret.mechlist); goto cleanup; } mechlist = wantmech; @@ -4009,7 +4003,7 @@ remoteAuthSASL (virConnectPtr conn, struct private_data *priv, int in_open, 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; } @@ -4026,7 +4020,7 @@ remoteAuthSASL (virConnectPtr conn, struct private_data *priv, int in_open, 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 */ @@ -4044,7 +4038,7 @@ remoteAuthSASL (virConnectPtr conn, struct private_data *priv, int in_open, 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, @@ -4123,8 +4117,7 @@ remoteAuthSASL (virConnectPtr conn, struct private_data *priv, int in_open, } if (serverin) { - free(serverin); - serverin = NULL; + VIR_FREE(serverin); } DEBUG("Client step result %d. Data %d bytes %p", err, clientoutlen, clientout); @@ -4156,7 +4149,7 @@ remoteAuthSASL (virConnectPtr conn, struct private_data *priv, int in_open, /* This server call shows complete, and earlier client step was OK */ if (complete && err == SASL_OK) { - free(serverin); + VIR_FREE(serverin); break; } } @@ -4186,11 +4179,11 @@ remoteAuthSASL (virConnectPtr conn, struct private_data *priv, int in_open, ret = 0; cleanup: - free(localAddr); - free(remoteAddr); - free(serverin); + VIR_FREE(localAddr); + VIR_FREE(remoteAddr); + VIR_FREE(serverin); - free(saslcb); + VIR_FREE(saslcb); remoteAuthFreeCredentials(cred, ncred); if (ret != 0 && saslconn) sasl_dispose(&saslconn); diff --git a/src/sexpr.c b/src/sexpr.c index 44d9c74c50..8498c8b793 100644 --- a/src/sexpr.c +++ b/src/sexpr.c @@ -21,6 +21,7 @@ #include "internal.h" #include "sexpr.h" #include "util.h" +#include "memory.h" /** * virSexprError: @@ -55,8 +56,7 @@ sexpr_new(void) { 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(struct sexpr *sexpr) 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 @@ sexpr_cons(const struct sexpr *car, const struct sexpr *cdr) * * 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 @@ sexpr_append(struct sexpr *lst, const struct sexpr *value) return (NULL); if (value == NULL) return (lst); - append(lst, value); + if (append(lst, value) < 0) + return (NULL); return (lst); } @@ -318,8 +326,11 @@ _string2sexpr(const char *buffer, size_t * end) 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 @@ _string2sexpr(const char *buffer, size_t * end) if (ret->u.value == NULL) { virSexprError(VIR_ERR_NO_MEMORY, _("failed to copy a string")); + goto error; } if (*ptr == '\'') @@ -367,6 +379,7 @@ _string2sexpr(const char *buffer, size_t * end) if (ret->u.value == NULL) { virSexprError(VIR_ERR_NO_MEMORY, _("failed to copy a string")); + goto error; } } diff --git a/src/storage_backend.c b/src/storage_backend.c index eb51b8297a..9052a0b0cd 100644 --- a/src/storage_backend.c +++ b/src/storage_backend.c @@ -50,6 +50,7 @@ #endif #include "util.h" +#include "memory.h" #include "storage_backend.h" #include "storage_backend_fs.h" @@ -237,8 +238,7 @@ virStorageBackendUpdateVolInfoFD(virConnectPtr conn, 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 @@ virStorageBackendStablePath(virConnectPtr conn, 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 @@ virStorageBackendStablePath(virConnectPtr conn, return stablepath; } - free(stablepath); + VIR_FREE(stablepath); } closedir(dh); @@ -365,7 +364,7 @@ virStorageBackendRunProgRegex(virConnectPtr conn, 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 @@ virStorageBackendRunProgRegex(virConnectPtr conn, _("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 @@ virStorageBackendRunProgRegex(virConnectPtr conn, } /* 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 @@ virStorageBackendRunProgRegex(virConnectPtr conn, /* 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 @@ virStorageBackendRunProgRegex(virConnectPtr conn, 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 @@ virStorageBackendRunProgNul(virConnectPtr conn, 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 --git a/src/storage_backend_disk.c b/src/storage_backend_disk.c index a8637a4ccc..d6548eb343 100644 --- a/src/storage_backend_disk.c +++ b/src/storage_backend_disk.c @@ -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 @@ virStorageBackendDiskMakeDataVol(virConnectPtr conn, 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 @@ virStorageBackendDiskMakeDataVol(virConnectPtr conn, return -1; if (devpath != vol->target.path) - free(devpath); - devpath = NULL; + VIR_FREE(devpath); } if (vol->key == NULL) { @@ -224,8 +224,7 @@ virStorageBackendDiskMakeDataVol(virConnectPtr conn, } 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 @@ virStorageBackendDiskMakeFreeExtent(virConnectPtr conn ATTRIBUTE_UNUSED, 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 @@ static int 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 @@ virStorageBackendDiskCreateVol(virConnectPtr conn, 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 --git a/src/storage_backend_fs.c b/src/storage_backend_fs.c index 023fed9afc..992649309f 100644 --- a/src/storage_backend_fs.c +++ b/src/storage_backend_fs.c @@ -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 @@ virStorageBackendFileSystemMount(virConnectPtr conn, } 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 @@ virStorageBackendFileSystemRefresh(virConnectPtr conn, 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 @@ virStorageBackendFileSystemRefresh(virConnectPtr conn, 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 @@ virStorageBackendFileSystemRefresh(virConnectPtr conn, 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 @@ virStorageBackendFileSystemVolCreate(virConnectPtr conn, { 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 --git a/src/storage_backend_iscsi.c b/src/storage_backend_iscsi.c index a32d4878f4..e168adcb86 100644 --- a/src/storage_backend_iscsi.c +++ b/src/storage_backend_iscsi.c @@ -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 @@ virStorageBackendISCSIMakeLUN(virConnectPtr conn, 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 @@ virStorageBackendISCSIMakeLUN(virConnectPtr conn, 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 @@ virStorageBackendISCSIMakeLUN(virConnectPtr conn, 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 @@ virStorageBackendISCSIMakeLUN(virConnectPtr conn, cleanup: if (fd != -1) close(fd); - free(devpath); + VIR_FREE(devpath); virStorageVolDefFree(vol); - free(dev); + VIR_FREE(dev); return -1; } @@ -422,8 +421,7 @@ virStorageBackendISCSIPortal(virConnectPtr conn, 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 @@ virStorageBackendISCSIStartPool(virConnectPtr conn, 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 @@ virStorageBackendISCSIRefreshPool(virConnectPtr conn, 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 @@ virStorageBackendISCSIStopPool(virConnectPtr conn, return -1; if (virStorageBackendISCSILogout(conn, pool, portal) < 0) { - free(portal); + VIR_FREE(portal); return -1; } - free(portal); + VIR_FREE(portal); return 0; } diff --git a/src/storage_backend_logical.c b/src/storage_backend_logical.c index c27b86e5cd..916c23c7ab 100644 --- a/src/storage_backend_logical.c +++ b/src/storage_backend_logical.c @@ -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 @@ virStorageBackendLogicalMakeVol(virConnectPtr conn, 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 @@ virStorageBackendLogicalMakeVol(virConnectPtr conn, /* 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 @@ virStorageBackendLogicalMakeVol(virConnectPtr conn, } 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 @@ virStorageBackendLogicalMakeVol(virConnectPtr conn, /* 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 @@ virStorageBackendLogicalBuildPool(virConnectPtr conn, 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 @@ virStorageBackendLogicalBuildPool(virConnectPtr conn, 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 --git a/src/storage_conf.c b/src/storage_conf.c index 685344484b..e6278ab76e 100644 --- a/src/storage_conf.c +++ b/src/storage_conf.c @@ -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 @@ virStorageReportError(virConnectPtr conn, int code, const char *fmt, ...) { 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 @@ virStoragePoolObjFree(virStoragePoolObjPtr obj) { 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 @@ virStoragePoolDefParseDoc(virConnectPtr conn, 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 @@ virStoragePoolDefParseDoc(virConnectPtr conn, "%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 @@ virStoragePoolDefParseDoc(virConnectPtr conn, "%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 @@ virStoragePoolDefParseDoc(virConnectPtr conn, 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 @@ virStoragePoolDefParseDoc(virConnectPtr conn, return ret; cleanup: - free(uuid); + VIR_FREE(uuid); xmlFree(type); virStoragePoolDefFree(ret); return NULL; @@ -649,8 +650,11 @@ virStorageVolDefParseDoc(virConnectPtr conn, 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 @@ virStorageVolDefParseDoc(virConnectPtr conn, } 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 @@ virStorageVolDefParseDoc(virConnectPtr conn, 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 @@ virStorageVolDefParseDoc(virConnectPtr conn, 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 @@ virStorageVolDefFormat(virConnectPtr conn, virStorageVolDefPtr def) { virStorageBackendVolOptionsPtr options; virBuffer buf = VIR_BUFFER_INITIALIZER; + char *tmp; options = virStorageBackendVolOptionsForType(pool->type); if (options == NULL) @@ -849,7 +850,8 @@ virStorageVolDefFormat(virConnectPtr conn, 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 @@ virStoragePoolObjAssignDef(virConnectPtr conn, 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 @@ virStoragePoolObjScanConfigs(virStorageDriverStatePtr driver) { virStoragePoolObjLoad(driver, entry->d_name, path, xml, autostartLink); - free(xml); + VIR_FREE(xml); } closedir(dir); @@ -1108,15 +1110,13 @@ virStoragePoolObjSaveDef(virConnectPtr conn, 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 @@ virStoragePoolObjSaveDef(virConnectPtr conn, if (fd != -1) close(fd); - free(xml); + VIR_FREE(xml); return ret; } diff --git a/src/storage_driver.c b/src/storage_driver.c index e9222fd276..45f2635fbf 100644 --- a/src/storage_driver.c +++ b/src/storage_driver.c @@ -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 @@ storageDriverStartup(void) { 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 --git a/src/xen_internal.c b/src/xen_internal.c index ca7a0c547a..6a5fd81b51 100644 --- a/src/xen_internal.c +++ b/src/xen_internal.c @@ -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 @@ typedef struct xen_v2s4_availheap xen_v2s4_availheap; #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 @@ virXenPerror (virConnectPtr conn, const char *msg) { 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 @@ virXen_setvcpumap(int handle, int id, unsigned int vcpu, /* 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 @@ virXen_setvcpumap(int handle, int id, unsigned int vcpu, 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 @@ xenHypervisorInit(void) */ 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 @@ xenHypervisorInit(void) 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 @@ xenHypervisorLookupDomainByID(virConnectPtr conn, ret = virGetDomain(conn, name, XEN_GETDOMAININFO_UUID(dominfo)); if (ret) ret->id = id; - free(name); + VIR_FREE(name); return ret; } @@ -2714,7 +2712,7 @@ xenHypervisorLookupDomainByUUID(virConnectPtr conn, ret = virGetDomain(conn, name, uuid); if (ret) ret->id = id; - free(name); + VIR_FREE(name); return ret; } #endif diff --git a/src/xen_unified.c b/src/xen_unified.c index c74e3a9465..73c335eaf2 100644 --- a/src/xen_unified.c +++ b/src/xen_unified.c @@ -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,13 @@ xenDomainUsedCpus(virDomainPtr dom) 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 (xalloc_oversized(nb_vcpu, cpumaplen) || + VIR_ALLOC_N(cpumap, nb_vcpu * cpumaplen) < 0) goto done; if ((ncpus = xenUnifiedDomainGetVcpus(dom, cpuinfo, nb_vcpu, @@ -202,9 +201,9 @@ xenDomainUsedCpus(virDomainPtr dom) } done: - free(cpulist); - free(cpumap); - free(cpuinfo); + VIR_FREE(cpulist); + VIR_FREE(cpumap); + VIR_FREE(cpuinfo); return(res); } @@ -262,8 +261,7 @@ xenUnifiedOpen (virConnectPtr conn, xmlURIPtr uri, virConnectAuthPtr auth, int f 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 +340,7 @@ xenUnifiedOpen (virConnectPtr conn, xmlURIPtr uri, virConnectAuthPtr auth, int f 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 +959,7 @@ xenUnifiedDomainDumpXML (virDomainPtr dom, int flags) 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 --git a/src/xend_internal.c b/src/xend_internal.c index b30a878542..35d5ffe3eb 100644 --- a/src/xend_internal.c +++ b/src/xend_internal.c @@ -26,7 +26,6 @@ #include #include #include -#include #include #include #include diff --git a/src/xmlrpc.c b/src/xmlrpc.c index 8e83b7c189..d62760776a 100644 --- a/src/xmlrpc.c +++ b/src/xmlrpc.c @@ -12,6 +12,7 @@ #include "xmlrpc.h" #include "internal.h" +#include "memory.h" #include @@ -47,9 +48,8 @@ static void xmlRpcError(virErrorNumber error, const char *info, int value) 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 @@ static xmlRpcValuePtr xmlRpcValueUnmarshalInteger(xmlNodePtr node) if (ret && value) ret->value.integer = atoi(value); - free(value); + VIR_FREE(value); return ret; } @@ -130,7 +130,7 @@ static xmlRpcValuePtr xmlRpcValueUnmarshalBoolean(xmlNodePtr node) ret->value.boolean = true; else ret->value.boolean = false; - free(value); + VIR_FREE(value); return ret; } @@ -141,7 +141,7 @@ static xmlRpcValuePtr xmlRpcValueUnmarshalDouble(xmlNodePtr node) if (ret && value) ret->value.real = atof(value); - free(value); + VIR_FREE(value); return ret; } @@ -158,11 +158,10 @@ static xmlRpcValuePtr xmlRpcValueUnmarshalArray(xmlNodePtr node) 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 xmlRpcValuePtr xmlRpcValueUnmarshalArray(xmlNodePtr node) 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 @@ static xmlRpcValueDictElementPtr xmlRpcValueUnmarshalDictElement(xmlNodePtr node 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 @@ void xmlRpcValueFree(xmlRpcValuePtr value) 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 @@ static char *xmlRpcCallRaw(const char *url, const char *request) } 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 @@ static char *xmlRpcCallRaw(const char *url, const char *request) serrno = errno; if (cxt) { xmlNanoHTTPClose(cxt); - free(contentType); + VIR_FREE(contentType); } errno = serrno; @@ -477,7 +475,7 @@ static char **xmlRpcStringArray(xmlRpcValuePtr value) 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 @@ xmlRpcArgvNew(const char *fmt, va_list ap, int *argc) 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 @@ xmlRpcArgvFree(int argc, xmlRpcValuePtr *argv) 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 @@ int xmlRpcCall(xmlRpcContextPtr context, const char *method, content = virBufferContentAndReset(&buf); ret = xmlRpcCallRaw(context->uri, content); - free(content); + VIR_FREE(content); if (!ret) return -1; @@ -597,7 +595,7 @@ int xmlRpcCall(xmlRpcContextPtr context, const char *method, 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 @@ int xmlRpcCall(xmlRpcContextPtr context, const char *method, xmlRpcContextPtr xmlRpcContextNew(const char *uri) { - xmlRpcContextPtr ret = malloc(sizeof(*ret)); + xmlRpcContextPtr ret; - 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 @@ xmlRpcContextPtr xmlRpcContextNew(const char *uri) 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); } }