From 3ce83f923d61836b13aeb4ba627fa73425765424 Mon Sep 17 00:00:00 2001 From: Zdenek Kabelac Date: Tue, 9 Apr 2024 11:35:23 +0200 Subject: [PATCH] cov: extra pointer validation Add few more internal errors to enusure there is no use of NULL pointers along the code path. --- device_mapper/ioctl/libdm-iface.c | 5 +++++ device_mapper/libdm-report.c | 9 +++++++-- lib/metadata/metadata.c | 5 +++++ libdm/ioctl/libdm-iface.c | 5 +++++ libdm/libdm-report.c | 8 ++++++-- tools/reporter.c | 5 +++++ 6 files changed, 33 insertions(+), 4 deletions(-) diff --git a/device_mapper/ioctl/libdm-iface.c b/device_mapper/ioctl/libdm-iface.c index 5bb4080a0..df91bb5fd 100644 --- a/device_mapper/ioctl/libdm-iface.c +++ b/device_mapper/ioctl/libdm-iface.c @@ -757,6 +757,11 @@ uint32_t dm_task_get_read_ahead(const struct dm_task *dmt, uint32_t *read_ahead) struct dm_deps *dm_task_get_deps(struct dm_task *dmt) { + if (!dmt) { + log_error(INTERNAL_ERROR "Missing dm_task."); + return NULL; + } + return (struct dm_deps *) (((char *) dmt->dmi.v4) + dmt->dmi.v4->data_start); } diff --git a/device_mapper/libdm-report.c b/device_mapper/libdm-report.c index 2a0036035..362c8b8b6 100644 --- a/device_mapper/libdm-report.c +++ b/device_mapper/libdm-report.c @@ -1453,9 +1453,14 @@ static void *_report_get_field_data(struct dm_report *rh, const struct dm_report_field_type *fields = fp->implicit ? _implicit_report_fields : rh->fields; - char *ret = fp->type->data_fn(object); + char *ret; - if (!ret) + if (!object) { + log_error(INTERNAL_ERROR "_report_get_field_data: missing object."); + return NULL; + } + + if (!(ret = fp->type->data_fn(object))) return NULL; return (void *)(ret + fields[fp->field_num].offset); diff --git a/lib/metadata/metadata.c b/lib/metadata/metadata.c index f8a4f6279..c120297e6 100644 --- a/lib/metadata/metadata.c +++ b/lib/metadata/metadata.c @@ -3742,6 +3742,11 @@ uint32_t vg_bad_status_bits(const struct volume_group *vg, uint64_t status) { uint32_t failure = 0; + if (!vg) { + log_error(INTERNAL_ERROR "Missing volume group."); + return FAILED_NOTFOUND; + } + if ((status & CLUSTERED) && !_access_vg_clustered(vg->cmd, vg)) /* Return because other flags are considered undefined. */ return FAILED_CLUSTERED; diff --git a/libdm/ioctl/libdm-iface.c b/libdm/ioctl/libdm-iface.c index b8793e106..9f37d2602 100644 --- a/libdm/ioctl/libdm-iface.c +++ b/libdm/ioctl/libdm-iface.c @@ -765,6 +765,11 @@ uint32_t dm_task_get_read_ahead(const struct dm_task *dmt, uint32_t *read_ahead) struct dm_deps *dm_task_get_deps(struct dm_task *dmt) { + if (!dmt) { + log_error(INTERNAL_ERROR "Missing dm_task."); + return NULL; + } + return (struct dm_deps *) (((char *) dmt->dmi.v4) + dmt->dmi.v4->data_start); } diff --git a/libdm/libdm-report.c b/libdm/libdm-report.c index 863463c84..5e74d9129 100644 --- a/libdm/libdm-report.c +++ b/libdm/libdm-report.c @@ -1451,10 +1451,14 @@ static void *_report_get_field_data(struct dm_report *rh, { const struct dm_report_field_type *fields = fp->implicit ? _implicit_report_fields : rh->fields; + char *ret; - char *ret = fp->type->data_fn(object); + if (!object) { + log_error(INTERNAL_ERROR "_report_get_field_data: missing object."); + return NULL; + } - if (!ret) + if (!(ret = fp->type->data_fn(object))) return NULL; return (void *)(ret + fields[fp->field_num].offset); diff --git a/tools/reporter.c b/tools/reporter.c index de6a37277..3e4b0afa0 100644 --- a/tools/reporter.c +++ b/tools/reporter.c @@ -618,6 +618,11 @@ static int _report_all_in_pv(struct cmd_context *cmd, struct processing_handle * { int r = ECMD_FAILED; + if (!pv) { + log_error(INTERNAL_ERROR "_report_all_in_pv: missing pv."); + return r; + } + switch (type) { case PVS: r = _pvs_single(cmd, pv->vg, pv, handle);