Skip to content

Commit 2e71e8b

Browse files
jdelvarePeter Zijlstra
authored andcommitted
perf/x86/amd/uncore: Avoid a false positive warning about snprintf truncation in amd_uncore_umc_ctx_init
Fix the following warning: CC [M] arch/x86/events/amd/uncore.o arch/x86/events/amd/uncore.c: In function ‘amd_uncore_umc_ctx_init’: arch/x86/events/amd/uncore.c:951:52: warning: ‘%d’ directive output may be truncated writing between 1 and 10 bytes into a region of size 8 [-Wformat-truncation=] snprintf(pmu->name, sizeof(pmu->name), "amd_umc_%d", index); ^~ arch/x86/events/amd/uncore.c:951:43: note: directive argument in the range [0, 2147483647] snprintf(pmu->name, sizeof(pmu->name), "amd_umc_%d", index); ^~~~~~~~~~~~ arch/x86/events/amd/uncore.c:951:4: note: ‘snprintf’ output between 10 and 19 bytes into a destination of size 16 snprintf(pmu->name, sizeof(pmu->name), "amd_umc_%d", index); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ As far as I can see, there can't be more than UNCORE_GROUP_MAX (256) groups and each group can't have more than 255 PMU, so the number printed by this %d can't exceed 65279, that's only 5 digits and would fit into the buffer. So it's a false positive warning. But we can make the compiler happy by declaring index as a 16-bit number. Signed-off-by: Jean Delvare <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Reviewed-by: Sandipan Das <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 0d5eb14 commit 2e71e8b

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

arch/x86/events/amd/uncore.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -916,7 +916,8 @@ int amd_uncore_umc_ctx_init(struct amd_uncore *uncore, unsigned int cpu)
916916
u8 group_num_pmcs[UNCORE_GROUP_MAX] = { 0 };
917917
union amd_uncore_info info;
918918
struct amd_uncore_pmu *pmu;
919-
int index = 0, gid, i;
919+
int gid, i;
920+
u16 index = 0;
920921

921922
if (pmu_version < 2)
922923
return 0;
@@ -948,7 +949,7 @@ int amd_uncore_umc_ctx_init(struct amd_uncore *uncore, unsigned int cpu)
948949
for_each_set_bit(gid, gmask, UNCORE_GROUP_MAX) {
949950
for (i = 0; i < group_num_pmus[gid]; i++) {
950951
pmu = &uncore->pmus[index];
951-
snprintf(pmu->name, sizeof(pmu->name), "amd_umc_%d", index);
952+
snprintf(pmu->name, sizeof(pmu->name), "amd_umc_%hu", index);
952953
pmu->num_counters = group_num_pmcs[gid] / group_num_pmus[gid];
953954
pmu->msr_base = MSR_F19H_UMC_PERF_CTL + i * pmu->num_counters * 2;
954955
pmu->rdpmc_base = -1;

0 commit comments

Comments
 (0)