Hi,
I was looking through the static analysis results of libpfm and there were a 
number of printf statements that are not quite right.  One has too many 
arguments and others are missing arguments. Attached is a patch that addresses 
the following errors in a way that looked reasonable based on what what was 
observed in the code

-Will


For the following extra arg one e->fstr is printed on a later line, so I 
suspect that the line 87 should just be printing the "]".

Error: PRINTF_ARGS (CWE-685): [#def66]
libpfm-4.13.0/lib/pfmlib_intel_x86.c:87: extra_argument: This argument was not 
used by the format string: "e->fstr".
#   85|                 __pfm_vbprintf(" any=%d", reg.sel_anythr);
#   86|   
#   87|->       __pfm_vbprintf("]", e->fstr);
#   88|   
#   89|         for (i = 1 ; i < e->count; i++)


Below are ones with missing arguments.

Suspect for the 'DPRINT("%s: ...' ones that the "s: " should be removed based 
on other DPRINT examples in the code.

Error: PRINTF_ARGS (CWE-685): [#def11]
libpfm-4.13.0/lib/pfmlib_amd64_perf_event.c:78: missing_argument: No argument 
for format specifier "%d".
#   76|   
#   77|         if (e->count > 1) {
#   78|->               DPRINT("%s: unsupported count=%d\n", e->count);
#   79|                 return PFM_ERR_NOTSUPP;
#   80|         }

Following should be printing the name of the pmu, so that should be %s and 
printing out p->name.

Error: PRINTF_ARGS (CWE-685): [#def14]
libpfm-4.13.0/lib/pfmlib_common.c:1151: missing_argument: No argument for 
format specifier "%d".
# 1149|   
# 1150|                 if (pfmlib_is_blacklisted_pmu(p)) {
# 1151|->                       DPRINT("%d PMU blacklisted, skipping 
initialization\n");
# 1152|                         continue;
# 1153|                 }

Looking through the code probably meaning to print out variable str.

Error: PRINTF_ARGS (CWE-685): [#def15]
libpfm-4.13.0/lib/pfmlib_common.c:1367: missing_argument: No argument for 
format specifier "%s".
# 1365|                         ainfo->equiv= NULL;
# 1366|                         if (*endptr) {
# 1367|->                               DPRINT("raw umask (%s) is not a 
number\n");
# 1368|                                 return PFM_ERR_ATTR;
# 1369|         

Following looks like it is printing  state twice.

Error: PRINTF_ARGS (CWE-685): [#def34]
libpfm-4.13.0/lib/pfmlib_intel_skx_unc_cha.c:60: missing_argument: No argument 
for format specifier "%x".
#   58|         f.val = e->codes[1];
#   59|   
#   60|->       __pfm_vbprintf("[UNC_CHA_FILTER0=0x%"PRIx64" thread_id=%d 
source=0x%x state=0x%x"
#   61|                        " state=0x%x]\n",
#   62|                         f.val,

Suspect for the 'DPRINT("%s: ...' ones that the "s: " should be removed based 
on other DPRINT examples in the code.

Error: PRINTF_ARGS (CWE-685): [#def83]
libpfm-4.13.0/lib/pfmlib_intel_x86_perf_event.c:100: missing_argument: No 
argument for format specifier "%d".
#   98|   
#   99|         if (e->count > 2) {
#  100|->               DPRINT("%s: unsupported count=%d\n", e->count);
#  101|                 return PFM_ERR_NOTSUPP;
#  102|         }


-Will
From 6567cd14de51233b9b847576a684b6cace36c3d2 Mon Sep 17 00:00:00 2001
From: William Cohen <wco...@redhat.com>
Date: Fri, 30 Jun 2023 15:06:22 -0400
Subject: [PATCH] Correct the arguments in a number of printf statements

Adjusted the printf statements to fix the following issues flagged by
static analsysis:

 Error: PRINTF_ARGS (CWE-685): [#def66]
 libpfm-4.13.0/lib/pfmlib_intel_x86.c:87: extra_argument: This argument was not used by the format string: "e->fstr".
 #   85|   		__pfm_vbprintf(" any=%d", reg.sel_anythr);
 #   86|
 #   87|-> 	__pfm_vbprintf("]", e->fstr);
 #   88|
 #   89|   	for (i = 1 ; i < e->count; i++)

 Error: PRINTF_ARGS (CWE-685): [#def11]
 libpfm-4.13.0/lib/pfmlib_amd64_perf_event.c:78: missing_argument: No argument for format specifier "%d".
 #   76|
 #   77|   	if (e->count > 1) {
 #   78|-> 		DPRINT("%s: unsupported count=%d\n", e->count);
 #   79|   		return PFM_ERR_NOTSUPP;
 #   80|   	}

 Error: PRINTF_ARGS (CWE-685): [#def14]
 libpfm-4.13.0/lib/pfmlib_common.c:1151: missing_argument: No argument for format specifier "%d".
 # 1149|
 # 1150|   		if (pfmlib_is_blacklisted_pmu(p)) {
 # 1151|-> 			DPRINT("%d PMU blacklisted, skipping initialization\n");
 # 1152|   			continue;
 # 1153|   		}

 Error: PRINTF_ARGS (CWE-685): [#def15]
 libpfm-4.13.0/lib/pfmlib_common.c:1367: missing_argument: No argument for format specifier "%s".
 # 1365|   			ainfo->equiv= NULL;
 # 1366|   			if (*endptr) {
 # 1367|-> 				DPRINT("raw umask (%s) is not a number\n");
 # 1368|   				return PFM_ERR_ATTR;
 # 1369|

 Error: PRINTF_ARGS (CWE-685): [#def34]
 libpfm-4.13.0/lib/pfmlib_intel_skx_unc_cha.c:60: missing_argument: No argument for format specifier "%x".
 #   58|   	f.val = e->codes[1];
 #   59|
 #   60|-> 	__pfm_vbprintf("[UNC_CHA_FILTER0=0x%"PRIx64" thread_id=%d source=0x%x state=0x%x"
 #   61|   		       " state=0x%x]\n",
 #   62|   			f.val,

 Error: PRINTF_ARGS (CWE-685): [#def83]
 libpfm-4.13.0/lib/pfmlib_intel_x86_perf_event.c:100: missing_argument: No argument for format specifier "%d".
 #   98|
 #   99|   	if (e->count > 2) {
 #  100|-> 		DPRINT("%s: unsupported count=%d\n", e->count);
 #  101|   		return PFM_ERR_NOTSUPP;
 #  102|   	}
---
 lib/pfmlib_amd64_perf_event.c     | 2 +-
 lib/pfmlib_common.c               | 4 ++--
 lib/pfmlib_intel_skx_unc_cha.c    | 3 +--
 lib/pfmlib_intel_x86.c            | 2 +-
 lib/pfmlib_intel_x86_perf_event.c | 2 +-
 5 files changed, 6 insertions(+), 7 deletions(-)

diff --git a/lib/pfmlib_amd64_perf_event.c b/lib/pfmlib_amd64_perf_event.c
index 0773408..c995583 100644
--- a/lib/pfmlib_amd64_perf_event.c
+++ b/lib/pfmlib_amd64_perf_event.c
@@ -75,7 +75,7 @@ pfm_amd64_get_perf_encoding(void *this, pfmlib_event_desc_t *e)
 		return ret;
 
 	if (e->count > 1) {
-		DPRINT("%s: unsupported count=%d\n", e->count);
+		DPRINT("unsupported count=%d\n", e->count);
 		return PFM_ERR_NOTSUPP;
 	}
 
diff --git a/lib/pfmlib_common.c b/lib/pfmlib_common.c
index d1b67bc..f28175a 100644
--- a/lib/pfmlib_common.c
+++ b/lib/pfmlib_common.c
@@ -1191,7 +1191,7 @@ pfmlib_init_pmus(void)
 			continue;
 
 		if (pfmlib_is_blacklisted_pmu(p)) {
-			DPRINT("%d PMU blacklisted, skipping initialization\n");
+			DPRINT("%s PMU blacklisted, skipping initialization\n", p->name);
 			continue;
 		}
 		p->flags |= PFMLIB_PMU_FL_INIT;
@@ -1429,7 +1429,7 @@ pfmlib_parse_event_attr(char *str, pfmlib_event_desc_t *d)
 			ainfo->idx  = strtoull(s, &endptr, 0);
 			ainfo->equiv= NULL;
 			if (*endptr) {
-				DPRINT("raw umask (%s) is not a number\n");
+				DPRINT("raw umask (%s) is not a number\n", str);
 				return PFM_ERR_ATTR;
 			}
 
diff --git a/lib/pfmlib_intel_skx_unc_cha.c b/lib/pfmlib_intel_skx_unc_cha.c
index c5f8141..1508008 100644
--- a/lib/pfmlib_intel_skx_unc_cha.c
+++ b/lib/pfmlib_intel_skx_unc_cha.c
@@ -57,8 +57,7 @@ display_cha(void *this, pfmlib_event_desc_t *e, void *val)
 
 	f.val = e->codes[1];
 
-	__pfm_vbprintf("[UNC_CHA_FILTER0=0x%"PRIx64" thread_id=%d source=0x%x state=0x%x"
-		       " state=0x%x]\n",
+	__pfm_vbprintf("[UNC_CHA_FILTER0=0x%"PRIx64" thread_id=%d source=0x%x state=0x%x]\n",
 			f.val,
 			f.skx_cha_filt0.tid,
 			f.skx_cha_filt0.sid,
diff --git a/lib/pfmlib_intel_x86.c b/lib/pfmlib_intel_x86.c
index 1eac435..b876486 100644
--- a/lib/pfmlib_intel_x86.c
+++ b/lib/pfmlib_intel_x86.c
@@ -84,7 +84,7 @@ pfm_intel_x86_display_reg(void *this, pfmlib_event_desc_t *e)
 	if (pe[e->event].modmsk & _INTEL_X86_ATTR_T)
 		__pfm_vbprintf(" any=%d", reg.sel_anythr);
 
-	__pfm_vbprintf("]", e->fstr);
+	__pfm_vbprintf("]");
 
 	for (i = 1 ; i < e->count; i++)
 		__pfm_vbprintf(" [0x%"PRIx64"]", e->codes[i]);
diff --git a/lib/pfmlib_intel_x86_perf_event.c b/lib/pfmlib_intel_x86_perf_event.c
index 2fb62b7..9e923d2 100644
--- a/lib/pfmlib_intel_x86_perf_event.c
+++ b/lib/pfmlib_intel_x86_perf_event.c
@@ -97,7 +97,7 @@ pfm_intel_x86_get_perf_encoding(void *this, pfmlib_event_desc_t *e)
 		return ret;
 
 	if (e->count > 2) {
-		DPRINT("%s: unsupported count=%d\n", e->count);
+		DPRINT("unsupported count=%d\n", e->count);
 		return PFM_ERR_NOTSUPP;
 	}
 	/* default PMU type */
-- 
2.41.0

_______________________________________________
perfmon2-devel mailing list
perfmon2-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/perfmon2-devel

Reply via email to