2024-02-14 05:44:28

by kernel test robot

[permalink] [raw]
Subject: drivers/opp/debugfs.c:48:54: warning: '%d' directive output may be truncated writing between 1 and 11 bytes into a region of size 8

Hi Viresh,

FYI, the error/warning still remains.

tree: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git master
head: 7e90b5c295ec1e47c8ad865429f046970c549a66
commit: 46f48aca2e5aef3f430e95d1a5fb68227ec8ec85 OPP: Fix missing debugfs supply directory for OPPs
date: 5 years ago
config: x86_64-buildonly-randconfig-001-20231012 (https://download.01.org/0day-ci/archive/20240214/[email protected]/config)
compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240214/[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/opp/debugfs.c: In function 'opp_debug_create_one':
>> drivers/opp/debugfs.c:48:54: warning: '%d' directive output may be truncated writing between 1 and 11 bytes into a region of size 8 [-Wformat-truncation=]
48 | snprintf(name, sizeof(name), "supply-%d", i);
| ^~
In function 'opp_debug_create_supplies',
inlined from 'opp_debug_create_one' at drivers/opp/debugfs.c:119:7:
drivers/opp/debugfs.c:48:46: note: directive argument in the range [-2147483644, 2147483646]
48 | snprintf(name, sizeof(name), "supply-%d", i);
| ^~~~~~~~~~~
drivers/opp/debugfs.c:48:17: note: 'snprintf' output between 9 and 19 bytes into a destination of size 15
48 | snprintf(name, sizeof(name), "supply-%d", i);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


vim +48 drivers/opp/debugfs.c

deaa51465105a7 drivers/base/power/opp/debugfs.c Viresh Kumar 2015-11-11 37
dfbe4678d709e2 drivers/base/power/opp/debugfs.c Viresh Kumar 2016-12-01 38 static bool opp_debug_create_supplies(struct dev_pm_opp *opp,
dfbe4678d709e2 drivers/base/power/opp/debugfs.c Viresh Kumar 2016-12-01 39 struct opp_table *opp_table,
dfbe4678d709e2 drivers/base/power/opp/debugfs.c Viresh Kumar 2016-12-01 40 struct dentry *pdentry)
dfbe4678d709e2 drivers/base/power/opp/debugfs.c Viresh Kumar 2016-12-01 41 {
dfbe4678d709e2 drivers/base/power/opp/debugfs.c Viresh Kumar 2016-12-01 42 struct dentry *d;
1fae788ed640e2 drivers/base/power/opp/debugfs.c Viresh Kumar 2017-05-23 43 int i;
dfbe4678d709e2 drivers/base/power/opp/debugfs.c Viresh Kumar 2016-12-01 44
1fae788ed640e2 drivers/base/power/opp/debugfs.c Viresh Kumar 2017-05-23 45 for (i = 0; i < opp_table->regulator_count; i++) {
d741029a239040 drivers/opp/debugfs.c Arvind Yadav 2017-09-21 46 char name[15];
d741029a239040 drivers/opp/debugfs.c Arvind Yadav 2017-09-21 47
d741029a239040 drivers/opp/debugfs.c Arvind Yadav 2017-09-21 @48 snprintf(name, sizeof(name), "supply-%d", i);
dfbe4678d709e2 drivers/base/power/opp/debugfs.c Viresh Kumar 2016-12-01 49
dfbe4678d709e2 drivers/base/power/opp/debugfs.c Viresh Kumar 2016-12-01 50 /* Create per-opp directory */
dfbe4678d709e2 drivers/base/power/opp/debugfs.c Viresh Kumar 2016-12-01 51 d = debugfs_create_dir(name, pdentry);
dfbe4678d709e2 drivers/base/power/opp/debugfs.c Viresh Kumar 2016-12-01 52
dfbe4678d709e2 drivers/base/power/opp/debugfs.c Viresh Kumar 2016-12-01 53 if (!d)
dfbe4678d709e2 drivers/base/power/opp/debugfs.c Viresh Kumar 2016-12-01 54 return false;
dfbe4678d709e2 drivers/base/power/opp/debugfs.c Viresh Kumar 2016-12-01 55
dfbe4678d709e2 drivers/base/power/opp/debugfs.c Viresh Kumar 2016-12-01 56 if (!debugfs_create_ulong("u_volt_target", S_IRUGO, d,
dfbe4678d709e2 drivers/base/power/opp/debugfs.c Viresh Kumar 2016-12-01 57 &opp->supplies[i].u_volt))
dfbe4678d709e2 drivers/base/power/opp/debugfs.c Viresh Kumar 2016-12-01 58 return false;
dfbe4678d709e2 drivers/base/power/opp/debugfs.c Viresh Kumar 2016-12-01 59
dfbe4678d709e2 drivers/base/power/opp/debugfs.c Viresh Kumar 2016-12-01 60 if (!debugfs_create_ulong("u_volt_min", S_IRUGO, d,
dfbe4678d709e2 drivers/base/power/opp/debugfs.c Viresh Kumar 2016-12-01 61 &opp->supplies[i].u_volt_min))
dfbe4678d709e2 drivers/base/power/opp/debugfs.c Viresh Kumar 2016-12-01 62 return false;
dfbe4678d709e2 drivers/base/power/opp/debugfs.c Viresh Kumar 2016-12-01 63
dfbe4678d709e2 drivers/base/power/opp/debugfs.c Viresh Kumar 2016-12-01 64 if (!debugfs_create_ulong("u_volt_max", S_IRUGO, d,
dfbe4678d709e2 drivers/base/power/opp/debugfs.c Viresh Kumar 2016-12-01 65 &opp->supplies[i].u_volt_max))
dfbe4678d709e2 drivers/base/power/opp/debugfs.c Viresh Kumar 2016-12-01 66 return false;
dfbe4678d709e2 drivers/base/power/opp/debugfs.c Viresh Kumar 2016-12-01 67
dfbe4678d709e2 drivers/base/power/opp/debugfs.c Viresh Kumar 2016-12-01 68 if (!debugfs_create_ulong("u_amp", S_IRUGO, d,
dfbe4678d709e2 drivers/base/power/opp/debugfs.c Viresh Kumar 2016-12-01 69 &opp->supplies[i].u_amp))
dfbe4678d709e2 drivers/base/power/opp/debugfs.c Viresh Kumar 2016-12-01 70 return false;
1fae788ed640e2 drivers/base/power/opp/debugfs.c Viresh Kumar 2017-05-23 71 }
dfbe4678d709e2 drivers/base/power/opp/debugfs.c Viresh Kumar 2016-12-01 72
dfbe4678d709e2 drivers/base/power/opp/debugfs.c Viresh Kumar 2016-12-01 73 return true;
dfbe4678d709e2 drivers/base/power/opp/debugfs.c Viresh Kumar 2016-12-01 74 }
dfbe4678d709e2 drivers/base/power/opp/debugfs.c Viresh Kumar 2016-12-01 75

:::::: The code at line 48 was first introduced by commit
:::::: d741029a2390406d4d94279ae5b346831a9e61e6 PM / OPP: Use snprintf() to avoid kasprintf() and kfree()

:::::: TO: Arvind Yadav <[email protected]>
:::::: CC: Rafael J. Wysocki <[email protected]>

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki


2024-02-14 05:58:48

by Viresh Kumar

[permalink] [raw]
Subject: Re: drivers/opp/debugfs.c:48:54: warning: '%d' directive output may be truncated writing between 1 and 11 bytes into a region of size 8

On 14-02-24, 13:43, kernel test robot wrote:
> Hi Viresh,
>
> FYI, the error/warning still remains.
>
> tree: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git master
> head: 7e90b5c295ec1e47c8ad865429f046970c549a66
> commit: 46f48aca2e5aef3f430e95d1a5fb68227ec8ec85 OPP: Fix missing debugfs supply directory for OPPs
> date: 5 years ago
> config: x86_64-buildonly-randconfig-001-20231012 (https://download.01.org/0day-ci/archive/20240214/[email protected]/config)
> compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
> reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240214/[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]/

I have informed this earlier too, this report is incorrect and applies
to a very old commit.

--
viresh

2024-02-21 06:02:28

by Yujie Liu

[permalink] [raw]
Subject: Re: drivers/opp/debugfs.c:48:54: warning: '%d' directive output may be truncated writing between 1 and 11 bytes into a region of size 8

Hi Viresh,

On Wed, 2024-02-14 at 11:28 +0530, Viresh Kumar wrote:
> On 14-02-24, 13:43, kernel test robot wrote:
> > Hi Viresh,
> >
> > FYI, the error/warning still remains.
> >
> > tree:   https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git master
> > head:   7e90b5c295ec1e47c8ad865429f046970c549a66
> > commit: 46f48aca2e5aef3f430e95d1a5fb68227ec8ec85 OPP: Fix missing debugfs supply directory for OPPs
> > date:   5 years ago
> > config: x86_64-buildonly-randconfig-001-20231012 (https://download.01.org/0day-ci/archive/20240214/[email protected]/config)
> > compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
> > reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240214/[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]/
>
> I have informed this earlier too, this report is incorrect and applies
> to a very old commit.

Really sorry for not responding timely to the questions in the
following reports:

[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/

We see that this still happens on the latest mainline rc regardless of
x86_64 or arm64 arch. Please note that this is a "W=1" warning. In our
tests, it can be reproduced with gcc-12 and gcc-13, but not with other
versions of gcc.

$ git checkout v6.8-rc5
HEAD is now at b401b621758e4 Linux 6.8-rc5

$ wget https://download.01.org/0day-ci/archive/20240214/[email protected]/config -O .config

$ make CC=gcc-12 W=1 -j72 ARCH=x86_64 olddefconfig
$ make CC=gcc-12 W=1 -j72 ARCH=x86_64 drivers/opp/debugfs.o
...
drivers/opp/debugfs.c: In function 'opp_debug_create_one':
drivers/opp/debugfs.c:105:54: error: '%d' directive output may be truncated writing between 1 and 11 bytes into a region of size 8 [-Werror=format-truncation=]
105 | snprintf(name, sizeof(name), "supply-%d", i);
| ^~
In function 'opp_debug_create_supplies',
inlined from 'opp_debug_create_one' at drivers/opp/debugfs.c:163:2:
drivers/opp/debugfs.c:105:46: note: directive argument in the range [-2147483644, 2147483646]
105 | snprintf(name, sizeof(name), "supply-%d", i);
| ^~~~~~~~~~~
drivers/opp/debugfs.c:105:17: note: 'snprintf' output between 9 and 19 bytes into a destination of size 15
105 | snprintf(name, sizeof(name), "supply-%d", i);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

$ make CROSS_COMPILE=aarch64-linux-gnu- CC=aarch64-linux-gnu-gcc-12 W=1 -j72 ARCH=arm64 olddefconfig
$ make CROSS_COMPILE=aarch64-linux-gnu- CC=aarch64-linux-gnu-gcc-12 W=1 -j72 ARCH=arm64 drivers/opp/debugfs.o
...
drivers/opp/debugfs.c: In function 'opp_debug_create_one':
drivers/opp/debugfs.c:105:54: error: '%d' directive output may be truncated writing between 1 and 11 bytes into a region of size 8 [-Werror=format-truncation=]
105 | snprintf(name, sizeof(name), "supply-%d", i);
| ^~
In function 'opp_debug_create_supplies',
inlined from 'opp_debug_create_one' at drivers/opp/debugfs.c:163:2:
drivers/opp/debugfs.c:105:46: note: directive argument in the range [-2147483644, 2147483646]
105 | snprintf(name, sizeof(name), "supply-%d", i);
| ^~~~~~~~~~~
drivers/opp/debugfs.c:105:17: note: 'snprintf' output between 9 and 19 bytes into a destination of size 15
105 | snprintf(name, sizeof(name), "supply-%d", i);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

We are not sure if this is a real problem or a compiler shortcoming
that gcc calculates an incorrect size. Might need gcc experts to take a
look.

Regardless, we will configure the bot to ignore this issue to avoid
further bothering you with duplicate reports.

Best Regards,
Yujie

2024-03-04 06:54:23

by Viresh Kumar

[permalink] [raw]
Subject: Re: drivers/opp/debugfs.c:48:54: warning: '%d' directive output may be truncated writing between 1 and 11 bytes into a region of size 8

On 21-02-24, 06:01, Liu, Yujie wrote:
> Hi Viresh,
>
> On Wed, 2024-02-14 at 11:28 +0530, Viresh Kumar wrote:
> > On 14-02-24, 13:43, kernel test robot wrote:
> > > Hi Viresh,
> > >
> > > FYI, the error/warning still remains.
> > >
> > > tree:?? https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git?master
> > > head:?? 7e90b5c295ec1e47c8ad865429f046970c549a66
> > > commit: 46f48aca2e5aef3f430e95d1a5fb68227ec8ec85 OPP: Fix missing debugfs supply directory for OPPs
> > > date:?? 5 years ago
> > > config: x86_64-buildonly-randconfig-001-20231012 (https://download.01.org/0day-ci/archive/20240214/[email protected]/config)
> > > compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
> > > reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240214/[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]/
> >
> > I have informed this earlier too, this report is incorrect and applies
> > to a very old commit.
>
> Really sorry for not responding timely to the questions in the
> following reports:
>
> [1] https://lore.kernel.org/all/[email protected]/
> [2] https://lore.kernel.org/all/[email protected]/
>
> We see that this still happens on the latest mainline rc regardless of
> x86_64 or arm64 arch. Please note that this is a "W=1" warning. In our
> tests, it can be reproduced with gcc-12 and gcc-13, but not with other
> versions of gcc.

Okay, I have a different version of gcc. Can you see if this change fixes it or
not ?

diff --git a/drivers/opp/debugfs.c b/drivers/opp/debugfs.c
index ec030b19164a..986c08e18d05 100644
--- a/drivers/opp/debugfs.c
+++ b/drivers/opp/debugfs.c
@@ -100,7 +100,7 @@ static void opp_debug_create_supplies(struct dev_pm_opp *opp,
int i;

for (i = 0; i < opp_table->regulator_count; i++) {
- char name[15];
+ char name[19];

snprintf(name, sizeof(name), "supply-%d", i);

--
viresh

2024-03-04 08:37:39

by Yujie Liu

[permalink] [raw]
Subject: Re: drivers/opp/debugfs.c:48:54: warning: '%d' directive output may be truncated writing between 1 and 11 bytes into a region of size 8

On Mon, Mar 04, 2024 at 12:24:11PM +0530, Viresh Kumar wrote:
> On 21-02-24, 06:01, Liu, Yujie wrote:
> > Hi Viresh,
> >
> > On Wed, 2024-02-14 at 11:28 +0530, Viresh Kumar wrote:
> > > On 14-02-24, 13:43, kernel test robot wrote:
> > > > Hi Viresh,
> > > >
> > > > FYI, the error/warning still remains.
> > > >
> > > > tree:?? https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git?master
> > > > head:?? 7e90b5c295ec1e47c8ad865429f046970c549a66
> > > > commit: 46f48aca2e5aef3f430e95d1a5fb68227ec8ec85 OPP: Fix missing debugfs supply directory for OPPs
> > > > date:?? 5 years ago
> > > > config: x86_64-buildonly-randconfig-001-20231012 (https://download.01.org/0day-ci/archive/20240214/[email protected]/config)
> > > > compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
> > > > reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240214/[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]/
> > >
> > > I have informed this earlier too, this report is incorrect and applies
> > > to a very old commit.
> >
> > Really sorry for not responding timely to the questions in the
> > following reports:
> >
> > [1] https://lore.kernel.org/all/[email protected]/
> > [2] https://lore.kernel.org/all/[email protected]/
> >
> > We see that this still happens on the latest mainline rc regardless of
> > x86_64 or arm64 arch. Please note that this is a "W=1" warning. In our
> > tests, it can be reproduced with gcc-12 and gcc-13, but not with other
> > versions of gcc.
>
> Okay, I have a different version of gcc. Can you see if this change fixes it or
> not ?

This fixes the warning for "supply-%d". Seems there are other remaining
warnings as shown below. Would you like to take a look as well?

CC drivers/opp/debugfs.o
drivers/opp/debugfs.c: In function 'bw_name_read':
drivers/opp/debugfs.c:43:42: error: '%.62s' directive argument is null [-Werror=format-overflow=]
43 | i = scnprintf(buf, sizeof(buf), "%.62s\n", icc_get_name(path));
| ^~~~~
drivers/opp/debugfs.c: In function 'opp_debug_create_one':
drivers/opp/debugfs.c:90:55: error: '%d' directive output may be truncated writing between 1 and 11 bytes into a region of size 4 [-Werror=format-truncation=]
90 | snprintf(name, sizeof(name), "rate_hz_%d", i);
| ^~
In function 'opp_debug_create_clks',
inlined from 'opp_debug_create_one' at drivers/opp/debugfs.c:162:2:
drivers/opp/debugfs.c:90:46: note: directive argument in the range [-2147483644, 2147483646]
90 | snprintf(name, sizeof(name), "rate_hz_%d", i);
| ^~~~~~~~~~~~
drivers/opp/debugfs.c:90:17: note: 'snprintf' output between 10 and 20 bytes into a destination of size 12
90 | snprintf(name, sizeof(name), "rate_hz_%d", i);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Best Regards,
Yujie

>
> diff --git a/drivers/opp/debugfs.c b/drivers/opp/debugfs.c
> index ec030b19164a..986c08e18d05 100644
> --- a/drivers/opp/debugfs.c
> +++ b/drivers/opp/debugfs.c
> @@ -100,7 +100,7 @@ static void opp_debug_create_supplies(struct dev_pm_opp *opp,
> int i;
>
> for (i = 0; i < opp_table->regulator_count; i++) {
> - char name[15];
> + char name[19];
>
> snprintf(name, sizeof(name), "supply-%d", i);
>
> --
> viresh

2024-03-04 11:21:21

by Viresh Kumar

[permalink] [raw]
Subject: Re: drivers/opp/debugfs.c:48:54: warning: '%d' directive output may be truncated writing between 1 and 11 bytes into a region of size 8

On 04-03-24, 16:30, Yujie Liu wrote:
> This fixes the warning for "supply-%d". Seems there are other remaining
> warnings as shown below. Would you like to take a look as well?

How about this instead to fix all warnings (drop the earlier change
please) ?

diff --git a/drivers/opp/debugfs.c b/drivers/opp/debugfs.c
index ec030b19164a..a9ebfdf0b6a1 100644
--- a/drivers/opp/debugfs.c
+++ b/drivers/opp/debugfs.c
@@ -37,10 +37,12 @@ static ssize_t bw_name_read(struct file *fp, char __user *userbuf,
size_t count, loff_t *ppos)
{
struct icc_path *path = fp->private_data;
+ const char *name = icc_get_name(path);
char buf[64];
- int i;
+ int i = 0;

- i = scnprintf(buf, sizeof(buf), "%.62s\n", icc_get_name(path));
+ if (name)
+ i = scnprintf(buf, sizeof(buf), "%.62s\n", name);

return simple_read_from_buffer(userbuf, count, ppos, buf, i);
}
@@ -56,11 +58,11 @@ static void opp_debug_create_bw(struct dev_pm_opp *opp,
struct dentry *pdentry)
{
struct dentry *d;
- char name[20];
+ char name[] = "icc-path-XXXXXXXXXX"; /* Integers can take 10 chars max */
int i;

for (i = 0; i < opp_table->path_count; i++) {
- snprintf(name, sizeof(name), "icc-path-%.1d", i);
+ snprintf(name, sizeof(name), "icc-path-%d", i);

/* Create per-path directory */
d = debugfs_create_dir(name, pdentry);
@@ -78,7 +80,7 @@ static void opp_debug_create_clks(struct dev_pm_opp *opp,
struct opp_table *opp_table,
struct dentry *pdentry)
{
- char name[12];
+ char name[] = "rate_hz_XXXXXXXXXX"; /* Integers can take 10 chars max */
int i;

if (opp_table->clk_count == 1) {
@@ -100,7 +102,7 @@ static void opp_debug_create_supplies(struct dev_pm_opp *opp,
int i;

for (i = 0; i < opp_table->regulator_count; i++) {
- char name[15];
+ char name[] = "supply-XXXXXXXXXX"; /* Integers can take 10 chars max */

snprintf(name, sizeof(name), "supply-%d", i);

--
viresh

2024-03-05 05:44:58

by Yujie Liu

[permalink] [raw]
Subject: Re: drivers/opp/debugfs.c:48:54: warning: '%d' directive output may be truncated writing between 1 and 11 bytes into a region of size 8

On Mon, Mar 04, 2024 at 04:51:02PM +0530, Viresh Kumar wrote:
> On 04-03-24, 16:30, Yujie Liu wrote:
> > This fixes the warning for "supply-%d". Seems there are other remaining
> > warnings as shown below. Would you like to take a look as well?
>
> How about this instead to fix all warnings (drop the earlier change
> please) ?

We see the following errors after applying the new diff:

CC drivers/opp/debugfs.o
drivers/opp/debugfs.c: In function 'opp_debug_create_one':
drivers/opp/debugfs.c:92:57: error: 'snprintf' output may be truncated before the last format character [-Werror=format-truncation=]
92 | snprintf(name, sizeof(name), "rate_hz_%d", i);
| ^
In function 'opp_debug_create_clks',
inlined from 'opp_debug_create_one' at drivers/opp/debugfs.c:164:2:
drivers/opp/debugfs.c:92:17: note: 'snprintf' output between 10 and 20 bytes into a destination of size 19
92 | snprintf(name, sizeof(name), "rate_hz_%d", i);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
drivers/opp/debugfs.c: In function 'opp_debug_create_one':
drivers/opp/debugfs.c:107:56: error: 'snprintf' output may be truncated before the last format character [-Werror=format-truncation=]
107 | snprintf(name, sizeof(name), "supply-%d", i);
| ^
In function 'opp_debug_create_supplies',
inlined from 'opp_debug_create_one' at drivers/opp/debugfs.c:165:2:
drivers/opp/debugfs.c:107:17: note: 'snprintf' output between 9 and 19 bytes into a destination of size 18
107 | snprintf(name, sizeof(name), "supply-%d", i);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


An integer has positive and negative limits, and the range is from
-2147483648 to 2147483647 in decimal, so it seems that an integer can
have 11 digits at most.

>
> diff --git a/drivers/opp/debugfs.c b/drivers/opp/debugfs.c
> index ec030b19164a..a9ebfdf0b6a1 100644
> --- a/drivers/opp/debugfs.c
> +++ b/drivers/opp/debugfs.c
> @@ -37,10 +37,12 @@ static ssize_t bw_name_read(struct file *fp, char __user *userbuf,
> size_t count, loff_t *ppos)
> {
> struct icc_path *path = fp->private_data;
> + const char *name = icc_get_name(path);
> char buf[64];
> - int i;
> + int i = 0;
>
> - i = scnprintf(buf, sizeof(buf), "%.62s\n", icc_get_name(path));
> + if (name)
> + i = scnprintf(buf, sizeof(buf), "%.62s\n", name);
>
> return simple_read_from_buffer(userbuf, count, ppos, buf, i);
> }
> @@ -56,11 +58,11 @@ static void opp_debug_create_bw(struct dev_pm_opp *opp,
> struct dentry *pdentry)
> {
> struct dentry *d;
> - char name[20];
> + char name[] = "icc-path-XXXXXXXXXX"; /* Integers can take 10 chars max */
> int i;
>
> for (i = 0; i < opp_table->path_count; i++) {
> - snprintf(name, sizeof(name), "icc-path-%.1d", i);
> + snprintf(name, sizeof(name), "icc-path-%d", i);
>
> /* Create per-path directory */
> d = debugfs_create_dir(name, pdentry);
> @@ -78,7 +80,7 @@ static void opp_debug_create_clks(struct dev_pm_opp *opp,
> struct opp_table *opp_table,
> struct dentry *pdentry)
> {
> - char name[12];
> + char name[] = "rate_hz_XXXXXXXXXX"; /* Integers can take 10 chars max */
> int i;
>
> if (opp_table->clk_count == 1) {
> @@ -100,7 +102,7 @@ static void opp_debug_create_supplies(struct dev_pm_opp *opp,
> int i;
>
> for (i = 0; i < opp_table->regulator_count; i++) {
> - char name[15];
> + char name[] = "supply-XXXXXXXXXX"; /* Integers can take 10 chars max */
>
> snprintf(name, sizeof(name), "supply-%d", i);
>
> --
> viresh

2024-03-05 06:11:15

by Viresh Kumar

[permalink] [raw]
Subject: Re: drivers/opp/debugfs.c:48:54: warning: '%d' directive output may be truncated writing between 1 and 11 bytes into a region of size 8

On 05-03-24, 13:37, Yujie Liu wrote:
> An integer has positive and negative limits, and the range is from
> -2147483648 to 2147483647 in decimal, so it seems that an integer can
> have 11 digits at most.

Ah, thanks. Fixed and pushed for linux-next. Diff:

diff --git a/drivers/opp/debugfs.c b/drivers/opp/debugfs.c
index ec030b19164a..105de7c3274a 100644
--- a/drivers/opp/debugfs.c
+++ b/drivers/opp/debugfs.c
@@ -37,10 +37,12 @@ static ssize_t bw_name_read(struct file *fp, char __user *userbuf,
size_t count, loff_t *ppos)
{
struct icc_path *path = fp->private_data;
+ const char *name = icc_get_name(path);
char buf[64];
- int i;
+ int i = 0;

- i = scnprintf(buf, sizeof(buf), "%.62s\n", icc_get_name(path));
+ if (name)
+ i = scnprintf(buf, sizeof(buf), "%.62s\n", name);

return simple_read_from_buffer(userbuf, count, ppos, buf, i);
}
@@ -56,11 +58,11 @@ static void opp_debug_create_bw(struct dev_pm_opp *opp,
struct dentry *pdentry)
{
struct dentry *d;
- char name[20];
+ char name[] = "icc-path-XXXXXXXXXXX"; /* Integers can take 11 chars max */
int i;

for (i = 0; i < opp_table->path_count; i++) {
- snprintf(name, sizeof(name), "icc-path-%.1d", i);
+ snprintf(name, sizeof(name), "icc-path-%d", i);

/* Create per-path directory */
d = debugfs_create_dir(name, pdentry);
@@ -78,7 +80,7 @@ static void opp_debug_create_clks(struct dev_pm_opp *opp,
struct opp_table *opp_table,
struct dentry *pdentry)
{
- char name[12];
+ char name[] = "rate_hz_XXXXXXXXXXX"; /* Integers can take 11 chars max */
int i;

if (opp_table->clk_count == 1) {
@@ -100,7 +102,7 @@ static void opp_debug_create_supplies(struct dev_pm_opp *opp,
int i;

for (i = 0; i < opp_table->regulator_count; i++) {
- char name[15];
+ char name[] = "supply-XXXXXXXXXXX"; /* Integers can take 11 chars max */

snprintf(name, sizeof(name), "supply-%d", i);


--
viresh