From 7eca4a5b32b815f178ded2daeff682d63edc2d96 Mon Sep 17 00:00:00 2001 From: Jiri Denemark Date: Wed, 26 Mar 2014 15:59:26 +0100 Subject: [PATCH] cpu: Properly check input parameters Most of the APIs in CPU driver do not expect to get NULL for input parameters. Let's mark them with ATTRIBUTE_NONNULL and also check for some members of virCPUDef when the APIs expect them have some specific values. Signed-off-by: Jiri Denemark --- src/cpu/cpu.c | 48 +++++++++++++++++++++++++++++++++++++----------- src/cpu/cpu.h | 36 ++++++++++++++++++++++++------------ 2 files changed, 61 insertions(+), 23 deletions(-) diff --git a/src/cpu/cpu.c b/src/cpu/cpu.c index 9cd2300f4e..528e1a7e8d 100644 --- a/src/cpu/cpu.c +++ b/src/cpu/cpu.c @@ -108,12 +108,6 @@ cpuCompareXML(virCPUDefPtr host, if (cpu == NULL) goto cleanup; - if (!cpu->model) { - virReportError(VIR_ERR_OPERATION_INVALID, - "%s", _("no CPU model specified")); - goto cleanup; - } - ret = cpuCompare(host, cpu); cleanup: @@ -146,6 +140,12 @@ cpuCompare(virCPUDefPtr host, VIR_DEBUG("host=%p, cpu=%p", host, cpu); + if (!cpu->model) { + virReportError(VIR_ERR_INVALID_ARG, "%s", + _("no guest CPU model specified")); + return VIR_CPU_COMPARE_ERROR; + } + if ((driver = cpuGetSubDriver(host->arch)) == NULL) return VIR_CPU_COMPARE_ERROR; @@ -203,14 +203,15 @@ cpuDecode(virCPUDefPtr cpu, } if (models == NULL && nmodels != 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, - "%s", _("nonzero nmodels doesn't match with NULL models")); + virReportError(VIR_ERR_INVALID_ARG, "%s", + _("nonzero nmodels doesn't match with NULL models")); return -1; } - if (cpu == NULL) { - virReportError(VIR_ERR_INTERNAL_ERROR, - "%s", _("invalid CPU definition")); + if (cpu->type > VIR_CPU_TYPE_GUEST || + cpu->mode != VIR_CPU_MODE_CUSTOM) { + virReportError(VIR_ERR_INVALID_ARG, "%s", + _("invalid CPU definition stub")); return -1; } @@ -264,6 +265,12 @@ cpuEncode(virArch arch, virArchToString(arch), cpu, forced, required, optional, disabled, forbidden, vendor); + if (!cpu->model) { + virReportError(VIR_ERR_INVALID_ARG, "%s", + _("no guest CPU model specified")); + return -1; + } + if ((driver = cpuGetSubDriver(arch)) == NULL) return -1; @@ -367,6 +374,12 @@ cpuGuestData(virCPUDefPtr host, VIR_DEBUG("host=%p, guest=%p, data=%p, msg=%p", host, guest, data, msg); + if (!guest->model) { + virReportError(VIR_ERR_INVALID_ARG, "%s", + _("no guest CPU model specified")); + return VIR_CPU_COMPARE_ERROR; + } + if ((driver = cpuGetSubDriver(host->arch)) == NULL) return VIR_CPU_COMPARE_ERROR; @@ -529,6 +542,19 @@ cpuBaseline(virCPUDefPtr *cpus, return NULL; } + for (i = 0; i < ncpus; i++) { + if (!cpus[i]) { + virReportError(VIR_ERR_INVALID_ARG, + _("invalid CPU definition at index %zu"), i); + return NULL; + } + if (!cpus[i]->model) { + virReportError(VIR_ERR_INVALID_ARG, + _("no CPU model specified at index %zu"), i); + return NULL; + } + } + if (models == NULL && nmodels != 0) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("nonzero nmodels doesn't match with NULL models")); diff --git a/src/cpu/cpu.h b/src/cpu/cpu.h index 27169fe3c4..e9f271330b 100644 --- a/src/cpu/cpu.h +++ b/src/cpu/cpu.h @@ -119,18 +119,21 @@ struct cpuArchDriver { extern virCPUCompareResult cpuCompareXML(virCPUDefPtr host, - const char *xml); + const char *xml) + ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2); extern virCPUCompareResult cpuCompare (virCPUDefPtr host, - virCPUDefPtr cpu); + virCPUDefPtr cpu) + ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2); extern int cpuDecode (virCPUDefPtr cpu, const virCPUData *data, const char **models, unsigned int nmodels, - const char *preferred); + const char *preferred) + ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2); extern int cpuEncode (virArch arch, @@ -140,7 +143,8 @@ cpuEncode (virArch arch, virCPUDataPtr *optional, virCPUDataPtr *disabled, virCPUDataPtr *forbidden, - virCPUDataPtr *vendor); + virCPUDataPtr *vendor) + ATTRIBUTE_NONNULL(2); extern void cpuDataFree (virCPUDataPtr data); @@ -152,7 +156,8 @@ extern virCPUCompareResult cpuGuestData(virCPUDefPtr host, virCPUDefPtr guest, virCPUDataPtr *data, - char **msg); + char **msg) + ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2); extern char * cpuBaselineXML(const char **xmlCPUs, @@ -166,30 +171,37 @@ cpuBaseline (virCPUDefPtr *cpus, unsigned int ncpus, const char **models, unsigned int nmodels, - unsigned int flags); + unsigned int flags) + ATTRIBUTE_NONNULL(1); extern int cpuUpdate (virCPUDefPtr guest, - const virCPUDef *host); + const virCPUDef *host) + ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2); extern int cpuHasFeature(const virCPUData *data, - const char *feature); + const char *feature) + ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2); bool cpuModelIsAllowed(const char *model, const char **models, - unsigned int nmodels); + unsigned int nmodels) + ATTRIBUTE_NONNULL(1); extern int -cpuGetModels(const char *arch, char ***models); +cpuGetModels(const char *arch, char ***models) + ATTRIBUTE_NONNULL(1); /* cpuDataFormat and cpuDataParse are implemented for unit tests only and * have no real-life usage */ -char *cpuDataFormat(const virCPUData *data); +char *cpuDataFormat(const virCPUData *data) + ATTRIBUTE_NONNULL(1); virCPUDataPtr cpuDataParse(virArch arch, - const char *xmlStr); + const char *xmlStr) + ATTRIBUTE_NONNULL(2); #endif /* __VIR_CPU_H__ */