Skip to contentSkip to conversation

drivers/cpufreq/sun50i-cpufreq-nvmem.c:128:44: warning: '%d' directive output may be truncated writing between 1 and 10 bytes into a region of size 2

PATCH kernel test robot 3 participants 5 messages Apr 19, 2024 – Apr 22, 2024

Subsystems (3)
lore
Share & tools
lore

kernel test robot

author build bot
#0 lore raw
tree:   https://git.kernel.org/pub/scm/linux/kernel/git/vireshk/pm.git cpufreq/arm/linux-next
head:   09d0aaa0ae9c80ff9569393b206226c1008801b1
commit: 6cc4bcceff9af0e6be9738096d95e4ba75e75123 [6/10] cpufreq: sun50i: Refactor speed bin decoding
config: arm64-defconfig (https://download.01.org/0day-ci/archive/20240419/[email protected]/config)
compiler: aarch64-linux-gcc (GCC) 13.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240419/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

drivers/cpufreq/sun50i-cpufreq-nvmem.c: In function 'sun50i_cpufreq_nvmem_probe':

>> drivers/cpufreq/sun50i-cpufreq-nvmem.c:128:44: warning: '%d' directive output may be truncated writing between 1 and 10 bytes into a region of size 2 [-Wformat-truncation=]
     128 |         snprintf(name, MAX_NAME_LEN, "speed%d", speed);
         |                                            ^~
   drivers/cpufreq/sun50i-cpufreq-nvmem.c:128:38: note: directive argument in the range [0, 2147483647]
     128 |         snprintf(name, MAX_NAME_LEN, "speed%d", speed);
         |                                      ^~~~~~~~~
   drivers/cpufreq/sun50i-cpufreq-nvmem.c:128:9: note: 'snprintf' output between 7 and 16 bytes into a destination of size 7
     128 |         snprintf(name, MAX_NAME_LEN, "speed%d", speed);
         |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

vim +128 drivers/cpufreq/sun50i-cpufreq-nvmem.c

f328584f7bff86 Yangtao Li        2019-06-12  108  
f328584f7bff86 Yangtao Li        2019-06-12  109  static int sun50i_cpufreq_nvmem_probe(struct platform_device *pdev)
f328584f7bff86 Yangtao Li        2019-06-12  110  {
298098e55a6fcc Viresh Kumar      2022-05-26  111  	int *opp_tokens;
f328584f7bff86 Yangtao Li        2019-06-12  112  	char name[MAX_NAME_LEN];
f328584f7bff86 Yangtao Li        2019-06-12  113  	unsigned int cpu;
6cc4bcceff9af0 Brandon Cheo Fusi 2024-04-18  114  	int speed;
f328584f7bff86 Yangtao Li        2019-06-12  115  	int ret;
f328584f7bff86 Yangtao Li        2019-06-12  116  
298098e55a6fcc Viresh Kumar      2022-05-26  117  	opp_tokens = kcalloc(num_possible_cpus(), sizeof(*opp_tokens),
f328584f7bff86 Yangtao Li        2019-06-12  118  			     GFP_KERNEL);
298098e55a6fcc Viresh Kumar      2022-05-26  119  	if (!opp_tokens)
f328584f7bff86 Yangtao Li        2019-06-12  120  		return -ENOMEM;
f328584f7bff86 Yangtao Li        2019-06-12  121  
6cc4bcceff9af0 Brandon Cheo Fusi 2024-04-18  122  	speed = sun50i_cpufreq_get_efuse();
6cc4bcceff9af0 Brandon Cheo Fusi 2024-04-18  123  	if (speed < 0) {
298098e55a6fcc Viresh Kumar      2022-05-26  124  		kfree(opp_tokens);
6cc4bcceff9af0 Brandon Cheo Fusi 2024-04-18  125  		return speed;
1aa24a8f3b5133 Xiaobing Luo      2022-04-23  126  	}
f328584f7bff86 Yangtao Li        2019-06-12  127  
f328584f7bff86 Yangtao Li        2019-06-12 @128  	snprintf(name, MAX_NAME_LEN, "speed%d", speed);
f328584f7bff86 Yangtao Li        2019-06-12  129  
f328584f7bff86 Yangtao Li        2019-06-12  130  	for_each_possible_cpu(cpu) {
f328584f7bff86 Yangtao Li        2019-06-12  131  		struct device *cpu_dev = get_cpu_device(cpu);
f328584f7bff86 Yangtao Li        2019-06-12  132  
f328584f7bff86 Yangtao Li        2019-06-12  133  		if (!cpu_dev) {
f328584f7bff86 Yangtao Li        2019-06-12  134  			ret = -ENODEV;
f328584f7bff86 Yangtao Li        2019-06-12  135  			goto free_opp;
f328584f7bff86 Yangtao Li        2019-06-12  136  		}
f328584f7bff86 Yangtao Li        2019-06-12  137  
298098e55a6fcc Viresh Kumar      2022-05-26  138  		opp_tokens[cpu] = dev_pm_opp_set_prop_name(cpu_dev, name);
298098e55a6fcc Viresh Kumar      2022-05-26  139  		if (opp_tokens[cpu] < 0) {
298098e55a6fcc Viresh Kumar      2022-05-26  140  			ret = opp_tokens[cpu];
f328584f7bff86 Yangtao Li        2019-06-12  141  			pr_err("Failed to set prop name\n");
f328584f7bff86 Yangtao Li        2019-06-12  142  			goto free_opp;
f328584f7bff86 Yangtao Li        2019-06-12  143  		}
f328584f7bff86 Yangtao Li        2019-06-12  144  	}
f328584f7bff86 Yangtao Li        2019-06-12  145  
f328584f7bff86 Yangtao Li        2019-06-12  146  	cpufreq_dt_pdev = platform_device_register_simple("cpufreq-dt", -1,
f328584f7bff86 Yangtao Li        2019-06-12  147  							  NULL, 0);
f328584f7bff86 Yangtao Li        2019-06-12  148  	if (!IS_ERR(cpufreq_dt_pdev)) {
298098e55a6fcc Viresh Kumar      2022-05-26  149  		platform_set_drvdata(pdev, opp_tokens);
f328584f7bff86 Yangtao Li        2019-06-12  150  		return 0;
f328584f7bff86 Yangtao Li        2019-06-12  151  	}
f328584f7bff86 Yangtao Li        2019-06-12  152  
f328584f7bff86 Yangtao Li        2019-06-12  153  	ret = PTR_ERR(cpufreq_dt_pdev);
f328584f7bff86 Yangtao Li        2019-06-12  154  	pr_err("Failed to register platform device\n");
f328584f7bff86 Yangtao Li        2019-06-12  155  
f328584f7bff86 Yangtao Li        2019-06-12  156  free_opp:
298098e55a6fcc Viresh Kumar      2022-05-26  157  	for_each_possible_cpu(cpu)
298098e55a6fcc Viresh Kumar      2022-05-26  158  		dev_pm_opp_put_prop_name(opp_tokens[cpu]);
298098e55a6fcc Viresh Kumar      2022-05-26  159  	kfree(opp_tokens);
f328584f7bff86 Yangtao Li        2019-06-12  160  
f328584f7bff86 Yangtao Li        2019-06-12  161  	return ret;
f328584f7bff86 Yangtao Li        2019-06-12  162  }
f328584f7bff86 Yangtao Li        2019-06-12  163  

:::::: The code at line 128 was first introduced by commit
:::::: f328584f7bff86858249bb358f1adcecde48388a cpufreq: Add sun50i nvmem based CPU scaling driver

:::::: TO: Yangtao Li <[email protected]>
:::::: CC: Viresh Kumar <[email protected]>

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
#1 lore raw

On Fri, 19 Apr 2024 17:36:21 +0800
kernel test robot <[email protected]> wrote:

Hi,

Quoted 16 lines
tree:   https://git.kernel.org/pub/scm/linux/kernel/git/vireshk/pm.git cpufreq/arm/linux-next
head:   09d0aaa0ae9c80ff9569393b206226c1008801b1
commit: 6cc4bcceff9af0e6be9738096d95e4ba75e75123 [6/10] cpufreq: sun50i: Refactor speed bin decoding
config: arm64-defconfig (https://download.01.org/0day-ci/archive/20240419/[email protected]/config)
compiler: aarch64-linux-gcc (GCC) 13.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240419/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

drivers/cpufreq/sun50i-cpufreq-nvmem.c: In function 'sun50i_cpufreq_nvmem_probe':

Quoted 1 line
Quoted 1 line

drivers/cpufreq/sun50i-cpufreq-nvmem.c:128:44: warning: '%d' directive output may be truncated writing between 1 and 10 bytes into a region of size 2 [-Wformat-truncation=]

Quoted 8 lines
     128 |         snprintf(name, MAX_NAME_LEN, "speed%d", speed);
         |                                            ^~
   drivers/cpufreq/sun50i-cpufreq-nvmem.c:128:38: note: directive argument in the range [0, 2147483647]
     128 |         snprintf(name, MAX_NAME_LEN, "speed%d", speed);
         |                                      ^~~~~~~~~
   drivers/cpufreq/sun50i-cpufreq-nvmem.c:128:9: note: 'snprintf' output between 7 and 16 bytes into a destination of size 7
     128 |         snprintf(name, MAX_NAME_LEN, "speed%d", speed);
         |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

So I see the problem: "speed" must fit into one decimal digit. At the moment it's fine, because "speed" is returned by one of the *_efuse_xlate functions. For the H6, the output is masked by 0x7, so can never be larger than that. For the H616, the return value is a constant selected in a switch/case, which is at most "4" at the moment, so it's safe as well.

So is there some established way to communicate this, to appease gcc here? Would explicitly checking for the range and bailing out solve this? Or is the some kind of assert to make gcc happy?
Or do we just increase MAX_NAME_LEN?

Cheers,
Andre

Quoted 67 lines

vim +128 drivers/cpufreq/sun50i-cpufreq-nvmem.c

f328584f7bff86 Yangtao Li        2019-06-12  108  
f328584f7bff86 Yangtao Li        2019-06-12  109  static int sun50i_cpufreq_nvmem_probe(struct platform_device *pdev)
f328584f7bff86 Yangtao Li        2019-06-12  110  {
298098e55a6fcc Viresh Kumar      2022-05-26  111  	int *opp_tokens;
f328584f7bff86 Yangtao Li        2019-06-12  112  	char name[MAX_NAME_LEN];
f328584f7bff86 Yangtao Li        2019-06-12  113  	unsigned int cpu;
6cc4bcceff9af0 Brandon Cheo Fusi 2024-04-18  114  	int speed;
f328584f7bff86 Yangtao Li        2019-06-12  115  	int ret;
f328584f7bff86 Yangtao Li        2019-06-12  116  
298098e55a6fcc Viresh Kumar      2022-05-26  117  	opp_tokens = kcalloc(num_possible_cpus(), sizeof(*opp_tokens),
f328584f7bff86 Yangtao Li        2019-06-12  118  			     GFP_KERNEL);
298098e55a6fcc Viresh Kumar      2022-05-26  119  	if (!opp_tokens)
f328584f7bff86 Yangtao Li        2019-06-12  120  		return -ENOMEM;
f328584f7bff86 Yangtao Li        2019-06-12  121  
6cc4bcceff9af0 Brandon Cheo Fusi 2024-04-18  122  	speed = sun50i_cpufreq_get_efuse();
6cc4bcceff9af0 Brandon Cheo Fusi 2024-04-18  123  	if (speed < 0) {
298098e55a6fcc Viresh Kumar      2022-05-26  124  		kfree(opp_tokens);
6cc4bcceff9af0 Brandon Cheo Fusi 2024-04-18  125  		return speed;
1aa24a8f3b5133 Xiaobing Luo      2022-04-23  126  	}
f328584f7bff86 Yangtao Li        2019-06-12  127  
f328584f7bff86 Yangtao Li        2019-06-12 @128  	snprintf(name, MAX_NAME_LEN, "speed%d", speed);
f328584f7bff86 Yangtao Li        2019-06-12  129  
f328584f7bff86 Yangtao Li        2019-06-12  130  	for_each_possible_cpu(cpu) {
f328584f7bff86 Yangtao Li        2019-06-12  131  		struct device *cpu_dev = get_cpu_device(cpu);
f328584f7bff86 Yangtao Li        2019-06-12  132  
f328584f7bff86 Yangtao Li        2019-06-12  133  		if (!cpu_dev) {
f328584f7bff86 Yangtao Li        2019-06-12  134  			ret = -ENODEV;
f328584f7bff86 Yangtao Li        2019-06-12  135  			goto free_opp;
f328584f7bff86 Yangtao Li        2019-06-12  136  		}
f328584f7bff86 Yangtao Li        2019-06-12  137  
298098e55a6fcc Viresh Kumar      2022-05-26  138  		opp_tokens[cpu] = dev_pm_opp_set_prop_name(cpu_dev, name);
298098e55a6fcc Viresh Kumar      2022-05-26  139  		if (opp_tokens[cpu] < 0) {
298098e55a6fcc Viresh Kumar      2022-05-26  140  			ret = opp_tokens[cpu];
f328584f7bff86 Yangtao Li        2019-06-12  141  			pr_err("Failed to set prop name\n");
f328584f7bff86 Yangtao Li        2019-06-12  142  			goto free_opp;
f328584f7bff86 Yangtao Li        2019-06-12  143  		}
f328584f7bff86 Yangtao Li        2019-06-12  144  	}
f328584f7bff86 Yangtao Li        2019-06-12  145  
f328584f7bff86 Yangtao Li        2019-06-12  146  	cpufreq_dt_pdev = platform_device_register_simple("cpufreq-dt", -1,
f328584f7bff86 Yangtao Li        2019-06-12  147  							  NULL, 0);
f328584f7bff86 Yangtao Li        2019-06-12  148  	if (!IS_ERR(cpufreq_dt_pdev)) {
298098e55a6fcc Viresh Kumar      2022-05-26  149  		platform_set_drvdata(pdev, opp_tokens);
f328584f7bff86 Yangtao Li        2019-06-12  150  		return 0;
f328584f7bff86 Yangtao Li        2019-06-12  151  	}
f328584f7bff86 Yangtao Li        2019-06-12  152  
f328584f7bff86 Yangtao Li        2019-06-12  153  	ret = PTR_ERR(cpufreq_dt_pdev);
f328584f7bff86 Yangtao Li        2019-06-12  154  	pr_err("Failed to register platform device\n");
f328584f7bff86 Yangtao Li        2019-06-12  155  
f328584f7bff86 Yangtao Li        2019-06-12  156  free_opp:
298098e55a6fcc Viresh Kumar      2022-05-26  157  	for_each_possible_cpu(cpu)
298098e55a6fcc Viresh Kumar      2022-05-26  158  		dev_pm_opp_put_prop_name(opp_tokens[cpu]);
298098e55a6fcc Viresh Kumar      2022-05-26  159  	kfree(opp_tokens);
f328584f7bff86 Yangtao Li        2019-06-12  160  
f328584f7bff86 Yangtao Li        2019-06-12  161  	return ret;
f328584f7bff86 Yangtao Li        2019-06-12  162  }
f328584f7bff86 Yangtao Li        2019-06-12  163  

:::::: The code at line 128 was first introduced by commit
:::::: f328584f7bff86858249bb358f1adcecde48388a cpufreq: Add sun50i nvmem based CPU scaling driver

:::::: TO: Yangtao Li <[email protected]>
:::::: CC: Viresh Kumar <[email protected]>

maintainer
#2 lore raw
Quoted 10 lines · Andre Przywara

So I see the problem: "speed" must fit into one decimal digit. At the moment it's fine, because "speed" is returned by one of the *_efuse_xlate functions. For the H6, the output is masked by 0x7, so can never be larger than that. For the H616, the return value is a constant selected in a switch/case, which is at most "4" at the moment, so it's safe as well.

So is there some established way to communicate this, to appease gcc here? Would explicitly checking for the range and bailing out solve this? Or is the some kind of assert to make gcc happy?
Or do we just increase MAX_NAME_LEN?

I fixed it like this in the OPP core earlier:

drivers/cpufreq/sun50i-cpufreq-nvmem.c +2 -4
index 30e5c337611c..cd50cea16a87 100644
@@ -19,8 +19,6 @@
 #include <linux/pm_opp.h>
 #include <linux/slab.h>
 
-#define MAX_NAME_LEN   7
-
 #define NVMEM_MASK     0x7
 #define NVMEM_SHIFT    5
 
@@ -208,7 +206,7 @@ static int sun50i_cpufreq_get_efuse(void)
 static int sun50i_cpufreq_nvmem_probe(struct platform_device *pdev)
 {
        int *opp_tokens;
-       char name[MAX_NAME_LEN];
+       char name[] = "speedXXXXXXXXXXX"; /* Integers can take 11 chars max */
        unsigned int cpu, supported_hw;
        struct dev_pm_opp_config config = {};
        int speed;
@@ -235,7 +233,7 @@ static int sun50i_cpufreq_nvmem_probe(struct platform_device *pdev)
                config.supported_hw_count = 1;
        }
 
-       snprintf(name, MAX_NAME_LEN, "speed%d", speed);
+       snprintf(name, sizeof(name), "speed%d", speed);
        config.prop_name = name;
 
        for_each_possible_cpu(cpu) {
Signature · 1 line
viresh
#3 lore raw

On Fri, 19 Apr 2024 16:26:28 +0530
Viresh Kumar <[email protected]> wrote:

Hi Viresh,

Quoted 7 lines · Andre Przywara
Quoted 6 lines

So I see the problem: "speed" must fit into one decimal digit. At the moment it's fine, because "speed" is returned by one of the *_efuse_xlate functions. For the H6, the output is masked by 0x7, so can never be larger than that. For the H616, the return value is a constant selected in a switch/case, which is at most "4" at the moment, so it's safe as well.

Quoted 6 lines
Quoted 4 lines

So is there some established way to communicate this, to appease gcc here? Would explicitly checking for the range and bailing out solve this? Or is the some kind of assert to make gcc happy?
Or do we just increase MAX_NAME_LEN?

I fixed it like this in the OPP core earlier:

Ah, yes, thanks, that looks good to me, and indeed fixes the warning.

How are we going to handle this? Do you gonna squash this into the respective patch?

Cheers,
Andre

Quoted 33 lines
drivers/cpufreq/sun50i-cpufreq-nvmem.c +2 -4
index 30e5c337611c..cd50cea16a87 100644
@@ -19,8 +19,6 @@
 #include <linux/pm_opp.h>
 #include <linux/slab.h>
 
-#define MAX_NAME_LEN   7
-
 #define NVMEM_MASK     0x7
 #define NVMEM_SHIFT    5
 
@@ -208,7 +206,7 @@ static int sun50i_cpufreq_get_efuse(void)
 static int sun50i_cpufreq_nvmem_probe(struct platform_device *pdev)
 {
        int *opp_tokens;
-       char name[MAX_NAME_LEN];
+       char name[] = "speedXXXXXXXXXXX"; /* Integers can take 11 chars max */
        unsigned int cpu, supported_hw;
        struct dev_pm_opp_config config = {};
        int speed;
@@ -235,7 +233,7 @@ static int sun50i_cpufreq_nvmem_probe(struct platform_device *pdev)
                config.supported_hw_count = 1;
        }
 
-       snprintf(name, MAX_NAME_LEN, "speed%d", speed);
+       snprintf(name, sizeof(name), "speed%d", speed);
        config.prop_name = name;
 
        for_each_possible_cpu(cpu) {
maintainer
#4 lore raw
Quoted 4 lines · Andre Przywara

Ah, yes, thanks, that looks good to me, and indeed fixes the warning.

How are we going to handle this? Do you gonna squash this into the respective patch?

This was an existing issue I think and wasn't introduced with your patches. And so I have applied a separate patch now.

Signature · 1 line
viresh