linux-sunxi.lists.linux.dev archive mirror
 help / color / mirror / Atom feed
* [PATCH] cpufreq: sun50i: Fix build warning around snprint()
@ 2024-04-22  3:31 Viresh Kumar
  2024-04-22  6:27 ` Chen-Yu Tsai
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Viresh Kumar @ 2024-04-22  3:31 UTC (permalink / raw)
  To: Rafael J. Wysocki, Yangtao Li, Viresh Kumar, Chen-Yu Tsai,
	Jernej Skrabec, Samuel Holland
  Cc: linux-pm, Vincent Guittot, kernel test robot, linux-arm-kernel,
	linux-sunxi, linux-kernel

The Sun50i driver generates a warning with W=1:

warning: '%d' directive output may be truncated writing between 1 and 10 bytes into a region of size 2 [-Wformat-truncation=]

Fix it by allocating a big enough array to print an integer.

Reported-by: kernel test robot <lkp@intel.com>
Closes: https://lore.kernel.org/oe-kbuild-all/202404191715.LDwMm2gP-lkp@intel.com/
Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
---
 drivers/cpufreq/sun50i-cpufreq-nvmem.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/drivers/cpufreq/sun50i-cpufreq-nvmem.c b/drivers/cpufreq/sun50i-cpufreq-nvmem.c
index 30e5c337611c..cd50cea16a87 100644
--- a/drivers/cpufreq/sun50i-cpufreq-nvmem.c
+++ b/drivers/cpufreq/sun50i-cpufreq-nvmem.c
@@ -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) {
-- 
2.31.1.272.g89b43f80a514


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] cpufreq: sun50i: Fix build warning around snprint()
  2024-04-22  3:31 [PATCH] cpufreq: sun50i: Fix build warning around snprint() Viresh Kumar
@ 2024-04-22  6:27 ` Chen-Yu Tsai
  2024-04-22 22:39 ` Andre Przywara
  2024-04-23  1:38 ` Julian Calaby
  2 siblings, 0 replies; 6+ messages in thread
From: Chen-Yu Tsai @ 2024-04-22  6:27 UTC (permalink / raw)
  To: Viresh Kumar
  Cc: Rafael J. Wysocki, Yangtao Li, Jernej Skrabec, Samuel Holland,
	linux-pm, Vincent Guittot, kernel test robot, linux-arm-kernel,
	linux-sunxi, linux-kernel

On Sun, Apr 21, 2024 at 8:31 PM Viresh Kumar <viresh.kumar@linaro.org> wrote:
>
> The Sun50i driver generates a warning with W=1:
>
> warning: '%d' directive output may be truncated writing between 1 and 10 bytes into a region of size 2 [-Wformat-truncation=]
>
> Fix it by allocating a big enough array to print an integer.
>
> Reported-by: kernel test robot <lkp@intel.com>
> Closes: https://lore.kernel.org/oe-kbuild-all/202404191715.LDwMm2gP-lkp@intel.com/
> Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>

Acked-by: Chen-Yu Tsai <wens@csie.org>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] cpufreq: sun50i: Fix build warning around snprint()
  2024-04-22  3:31 [PATCH] cpufreq: sun50i: Fix build warning around snprint() Viresh Kumar
  2024-04-22  6:27 ` Chen-Yu Tsai
@ 2024-04-22 22:39 ` Andre Przywara
  2024-04-23  1:38 ` Julian Calaby
  2 siblings, 0 replies; 6+ messages in thread
From: Andre Przywara @ 2024-04-22 22:39 UTC (permalink / raw)
  To: Viresh Kumar
  Cc: Rafael J. Wysocki, Yangtao Li, Chen-Yu Tsai, Jernej Skrabec,
	Samuel Holland, linux-pm, Vincent Guittot, kernel test robot,
	linux-arm-kernel, linux-sunxi, linux-kernel

On Mon, 22 Apr 2024 09:01:09 +0530
Viresh Kumar <viresh.kumar@linaro.org> wrote:

Hi,

> The Sun50i driver generates a warning with W=1:
> 
> warning: '%d' directive output may be truncated writing between 1 and 10 bytes into a region of size 2 [-Wformat-truncation=]
> 
> Fix it by allocating a big enough array to print an integer.
> 
> Reported-by: kernel test robot <lkp@intel.com>
> Closes: https://lore.kernel.org/oe-kbuild-all/202404191715.LDwMm2gP-lkp@intel.com/
> Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>

For the records: as it stands right now, "speed" will always be smaller
than 10 at the moment, but it's indeed better to play safe here.
So the fix makes sense to me and fixes the issue:

Reviewed-by: Andre Przywara <andre.przywara@arm.com>
Tested-by: Andre Przywara <andre.przywara@arm.com>

Cheers,
Andre

> ---
>  drivers/cpufreq/sun50i-cpufreq-nvmem.c | 6 ++----
>  1 file changed, 2 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/cpufreq/sun50i-cpufreq-nvmem.c b/drivers/cpufreq/sun50i-cpufreq-nvmem.c
> index 30e5c337611c..cd50cea16a87 100644
> --- a/drivers/cpufreq/sun50i-cpufreq-nvmem.c
> +++ b/drivers/cpufreq/sun50i-cpufreq-nvmem.c
> @@ -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) {


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] cpufreq: sun50i: Fix build warning around snprint()
  2024-04-22  3:31 [PATCH] cpufreq: sun50i: Fix build warning around snprint() Viresh Kumar
  2024-04-22  6:27 ` Chen-Yu Tsai
  2024-04-22 22:39 ` Andre Przywara
@ 2024-04-23  1:38 ` Julian Calaby
  2024-04-23  6:12   ` Viresh Kumar
  2 siblings, 1 reply; 6+ messages in thread
From: Julian Calaby @ 2024-04-23  1:38 UTC (permalink / raw)
  To: Viresh Kumar
  Cc: Rafael J. Wysocki, Yangtao Li, Chen-Yu Tsai, Jernej Skrabec,
	Samuel Holland, linux-pm, Vincent Guittot, kernel test robot,
	linux-arm-kernel, linux-sunxi, linux-kernel

Hi Viresh,

On Mon, Apr 22, 2024 at 1:31 PM Viresh Kumar <viresh.kumar@linaro.org> wrote:
>
> The Sun50i driver generates a warning with W=1:
>
> warning: '%d' directive output may be truncated writing between 1 and 10 bytes into a region of size 2 [-Wformat-truncation=]
>
> Fix it by allocating a big enough array to print an integer.
>
> Reported-by: kernel test robot <lkp@intel.com>
> Closes: https://lore.kernel.org/oe-kbuild-all/202404191715.LDwMm2gP-lkp@intel.com/
> Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>

This'll absolutely fix the issue here, so:

Reviewed-by: Julian Calaby <julian.calaby@gmail.com>

However:

> ---
>  drivers/cpufreq/sun50i-cpufreq-nvmem.c | 6 ++----
>  1 file changed, 2 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/cpufreq/sun50i-cpufreq-nvmem.c b/drivers/cpufreq/sun50i-cpufreq-nvmem.c
> index 30e5c337611c..cd50cea16a87 100644
> --- a/drivers/cpufreq/sun50i-cpufreq-nvmem.c
> +++ b/drivers/cpufreq/sun50i-cpufreq-nvmem.c
> @@ -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 */

Would it make sense to just set a static length for the string here,
say 17-20 characters and add a comment explaining the number, say: /*
"speed" + 11 chars for the int */

The string constant, while it'll probably be optimised away, seems
weird and wasteful.

Thanks,

-- 
Julian Calaby

Email: julian.calaby@gmail.com
Profile: http://www.google.com/profiles/julian.calaby/

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] cpufreq: sun50i: Fix build warning around snprint()
  2024-04-23  1:38 ` Julian Calaby
@ 2024-04-23  6:12   ` Viresh Kumar
  2024-04-23  6:28     ` Julian Calaby
  0 siblings, 1 reply; 6+ messages in thread
From: Viresh Kumar @ 2024-04-23  6:12 UTC (permalink / raw)
  To: Julian Calaby
  Cc: Rafael J. Wysocki, Yangtao Li, Chen-Yu Tsai, Jernej Skrabec,
	Samuel Holland, linux-pm, Vincent Guittot, kernel test robot,
	linux-arm-kernel, linux-sunxi, linux-kernel

On 23-04-24, 11:38, Julian Calaby wrote:
> On Mon, Apr 22, 2024 at 1:31 PM Viresh Kumar <viresh.kumar@linaro.org> wrote:
> > diff --git a/drivers/cpufreq/sun50i-cpufreq-nvmem.c b/drivers/cpufreq/sun50i-cpufreq-nvmem.c
> > index 30e5c337611c..cd50cea16a87 100644
> > --- a/drivers/cpufreq/sun50i-cpufreq-nvmem.c
> > +++ b/drivers/cpufreq/sun50i-cpufreq-nvmem.c
> > @@ -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 */
> 
> Would it make sense to just set a static length for the string here,
> say 17-20 characters and add a comment explaining the number, say: /*
> "speed" + 11 chars for the int */
> 
> The string constant, while it'll probably be optimised away, seems
> weird and wasteful.

The counting goes wrong (I have done it in the past) sometimes and so
I like to explicitly reserve space like this, it also makes it look
cleaner, i.e. how the eventual string will be named.

-- 
viresh

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] cpufreq: sun50i: Fix build warning around snprint()
  2024-04-23  6:12   ` Viresh Kumar
@ 2024-04-23  6:28     ` Julian Calaby
  0 siblings, 0 replies; 6+ messages in thread
From: Julian Calaby @ 2024-04-23  6:28 UTC (permalink / raw)
  To: Viresh Kumar
  Cc: Rafael J. Wysocki, Yangtao Li, Chen-Yu Tsai, Jernej Skrabec,
	Samuel Holland, linux-pm, Vincent Guittot, kernel test robot,
	linux-arm-kernel, linux-sunxi, linux-kernel

Hi Viresh,

On Tue, Apr 23, 2024 at 4:12 PM Viresh Kumar <viresh.kumar@linaro.org> wrote:
>
> On 23-04-24, 11:38, Julian Calaby wrote:
> > On Mon, Apr 22, 2024 at 1:31 PM Viresh Kumar <viresh.kumar@linaro.org> wrote:
> > > diff --git a/drivers/cpufreq/sun50i-cpufreq-nvmem.c b/drivers/cpufreq/sun50i-cpufreq-nvmem.c
> > > index 30e5c337611c..cd50cea16a87 100644
> > > --- a/drivers/cpufreq/sun50i-cpufreq-nvmem.c
> > > +++ b/drivers/cpufreq/sun50i-cpufreq-nvmem.c
> > > @@ -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 */
> >
> > Would it make sense to just set a static length for the string here,
> > say 17-20 characters and add a comment explaining the number, say: /*
> > "speed" + 11 chars for the int */
> >
> > The string constant, while it'll probably be optimised away, seems
> > weird and wasteful.
>
> The counting goes wrong (I have done it in the past) sometimes and so
> I like to explicitly reserve space like this, it also makes it look
> cleaner, i.e. how the eventual string will be named.

I completely agree - ultimately it's whatever works and either way
works equally well.

Thanks,

-- 
Julian Calaby

Email: julian.calaby@gmail.com
Profile: http://www.google.com/profiles/julian.calaby/

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2024-04-23  6:28 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-22  3:31 [PATCH] cpufreq: sun50i: Fix build warning around snprint() Viresh Kumar
2024-04-22  6:27 ` Chen-Yu Tsai
2024-04-22 22:39 ` Andre Przywara
2024-04-23  1:38 ` Julian Calaby
2024-04-23  6:12   ` Viresh Kumar
2024-04-23  6:28     ` Julian Calaby

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).