1
0
mirror of https://gitlab.com/libvirt/libvirt.git synced 2025-03-07 17:28:15 +00:00

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 <jdenemar@redhat.com>
This commit is contained in:
Jiri Denemark 2014-03-26 15:59:26 +01:00
parent a1b1fcdcfe
commit 7eca4a5b32
2 changed files with 61 additions and 23 deletions

View File

@ -108,12 +108,6 @@ cpuCompareXML(virCPUDefPtr host,
if (cpu == NULL) if (cpu == NULL)
goto cleanup; goto cleanup;
if (!cpu->model) {
virReportError(VIR_ERR_OPERATION_INVALID,
"%s", _("no CPU model specified"));
goto cleanup;
}
ret = cpuCompare(host, cpu); ret = cpuCompare(host, cpu);
cleanup: cleanup:
@ -146,6 +140,12 @@ cpuCompare(virCPUDefPtr host,
VIR_DEBUG("host=%p, cpu=%p", host, cpu); 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) if ((driver = cpuGetSubDriver(host->arch)) == NULL)
return VIR_CPU_COMPARE_ERROR; return VIR_CPU_COMPARE_ERROR;
@ -203,14 +203,15 @@ cpuDecode(virCPUDefPtr cpu,
} }
if (models == NULL && nmodels != 0) { if (models == NULL && nmodels != 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INVALID_ARG, "%s",
"%s", _("nonzero nmodels doesn't match with NULL models")); _("nonzero nmodels doesn't match with NULL models"));
return -1; return -1;
} }
if (cpu == NULL) { if (cpu->type > VIR_CPU_TYPE_GUEST ||
virReportError(VIR_ERR_INTERNAL_ERROR, cpu->mode != VIR_CPU_MODE_CUSTOM) {
"%s", _("invalid CPU definition")); virReportError(VIR_ERR_INVALID_ARG, "%s",
_("invalid CPU definition stub"));
return -1; return -1;
} }
@ -264,6 +265,12 @@ cpuEncode(virArch arch,
virArchToString(arch), cpu, forced, required, virArchToString(arch), cpu, forced, required,
optional, disabled, forbidden, vendor); 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) if ((driver = cpuGetSubDriver(arch)) == NULL)
return -1; return -1;
@ -367,6 +374,12 @@ cpuGuestData(virCPUDefPtr host,
VIR_DEBUG("host=%p, guest=%p, data=%p, msg=%p", host, guest, data, msg); 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) if ((driver = cpuGetSubDriver(host->arch)) == NULL)
return VIR_CPU_COMPARE_ERROR; return VIR_CPU_COMPARE_ERROR;
@ -529,6 +542,19 @@ cpuBaseline(virCPUDefPtr *cpus,
return NULL; 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) { if (models == NULL && nmodels != 0) {
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("nonzero nmodels doesn't match with NULL models")); "%s", _("nonzero nmodels doesn't match with NULL models"));

View File

@ -119,18 +119,21 @@ struct cpuArchDriver {
extern virCPUCompareResult extern virCPUCompareResult
cpuCompareXML(virCPUDefPtr host, cpuCompareXML(virCPUDefPtr host,
const char *xml); const char *xml)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
extern virCPUCompareResult extern virCPUCompareResult
cpuCompare (virCPUDefPtr host, cpuCompare (virCPUDefPtr host,
virCPUDefPtr cpu); virCPUDefPtr cpu)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
extern int extern int
cpuDecode (virCPUDefPtr cpu, cpuDecode (virCPUDefPtr cpu,
const virCPUData *data, const virCPUData *data,
const char **models, const char **models,
unsigned int nmodels, unsigned int nmodels,
const char *preferred); const char *preferred)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
extern int extern int
cpuEncode (virArch arch, cpuEncode (virArch arch,
@ -140,7 +143,8 @@ cpuEncode (virArch arch,
virCPUDataPtr *optional, virCPUDataPtr *optional,
virCPUDataPtr *disabled, virCPUDataPtr *disabled,
virCPUDataPtr *forbidden, virCPUDataPtr *forbidden,
virCPUDataPtr *vendor); virCPUDataPtr *vendor)
ATTRIBUTE_NONNULL(2);
extern void extern void
cpuDataFree (virCPUDataPtr data); cpuDataFree (virCPUDataPtr data);
@ -152,7 +156,8 @@ extern virCPUCompareResult
cpuGuestData(virCPUDefPtr host, cpuGuestData(virCPUDefPtr host,
virCPUDefPtr guest, virCPUDefPtr guest,
virCPUDataPtr *data, virCPUDataPtr *data,
char **msg); char **msg)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
extern char * extern char *
cpuBaselineXML(const char **xmlCPUs, cpuBaselineXML(const char **xmlCPUs,
@ -166,30 +171,37 @@ cpuBaseline (virCPUDefPtr *cpus,
unsigned int ncpus, unsigned int ncpus,
const char **models, const char **models,
unsigned int nmodels, unsigned int nmodels,
unsigned int flags); unsigned int flags)
ATTRIBUTE_NONNULL(1);
extern int extern int
cpuUpdate (virCPUDefPtr guest, cpuUpdate (virCPUDefPtr guest,
const virCPUDef *host); const virCPUDef *host)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
extern int extern int
cpuHasFeature(const virCPUData *data, cpuHasFeature(const virCPUData *data,
const char *feature); const char *feature)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
bool bool
cpuModelIsAllowed(const char *model, cpuModelIsAllowed(const char *model,
const char **models, const char **models,
unsigned int nmodels); unsigned int nmodels)
ATTRIBUTE_NONNULL(1);
extern int 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 /* cpuDataFormat and cpuDataParse are implemented for unit tests only and
* have no real-life usage * have no real-life usage
*/ */
char *cpuDataFormat(const virCPUData *data); char *cpuDataFormat(const virCPUData *data)
ATTRIBUTE_NONNULL(1);
virCPUDataPtr cpuDataParse(virArch arch, virCPUDataPtr cpuDataParse(virArch arch,
const char *xmlStr); const char *xmlStr)
ATTRIBUTE_NONNULL(2);
#endif /* __VIR_CPU_H__ */ #endif /* __VIR_CPU_H__ */