From c693dcccda18500956c0ac5f6fae47da71d77609 Mon Sep 17 00:00:00 2001 From: mmacy Date: Mon, 4 Jun 2018 21:17:46 +0000 Subject: [PATCH] libpmc: improve arg checking --- lib/libpmc/libpmc.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/lib/libpmc/libpmc.c b/lib/libpmc/libpmc.c index d71cdfb31131..3b3026cf086b 100644 --- a/lib/libpmc/libpmc.c +++ b/lib/libpmc/libpmc.c @@ -37,12 +37,14 @@ __FBSDID("$FreeBSD$"); #include #include +#include #include #include #include #include #include #include +#include #include #include "libpmcinternal.h" @@ -1035,13 +1037,16 @@ pmc_allocate(const char *ctrspec, enum pmc_mode mode, */ r = spec_copy = strdup(ctrspec); ctrname = strsep(&r, ","); - if (pmc_pmu_pmcallocate(ctrname, &pmc_config) == 0) { - if (PMC_CALL(PMCALLOCATE, &pmc_config) < 0) { + if (pmc_pmu_enabled()) { + if (pmc_pmu_pmcallocate(ctrname, &pmc_config) == 0) { + if (PMC_CALL(PMCALLOCATE, &pmc_config) < 0) { + goto out; + } + retval = 0; + *pmcid = pmc_config.pm_pmcid; goto out; } - retval = 0; - *pmcid = pmc_config.pm_pmcid; - goto out; + errx(EX_USAGE, "ERROR: pmc_pmu_allocate failed, check for ctrname %s\n", ctrname); } else { free(spec_copy); spec_copy = NULL; @@ -1068,7 +1073,7 @@ pmc_allocate(const char *ctrspec, enum pmc_mode mode, ev = NULL; for (n = 0; n < PMC_CLASS_TABLE_SIZE; n++) { pcd = pmc_class_table[n]; - if (pmc_mdep_is_compatible_class(pcd->pm_evc_class) && + if (pcd && pmc_mdep_is_compatible_class(pcd->pm_evc_class) && strncasecmp(ctrname, pcd->pm_evc_name, pcd->pm_evc_name_size) == 0) { if ((ev = pmc_match_event_class(ctrname + @@ -1086,7 +1091,7 @@ pmc_allocate(const char *ctrspec, enum pmc_mode mode, */ for (n = 0; ev == NULL && n < PMC_CLASS_TABLE_SIZE; n++) { pcd = pmc_class_table[n]; - if (pmc_mdep_is_compatible_class(pcd->pm_evc_class)) + if (pcd && pmc_mdep_is_compatible_class(pcd->pm_evc_class)) ev = pmc_match_event_class(ctrname, pcd); }