diff mbox series

[3/4] drm/imx: fix -Wformat-truncation warning in imx_ldb_probe()

Message ID c257c4174743f35bafee8492ec60e1d2734450cf.1716479340.git.jani.nikula@intel.com (mailing list archive)
State New, archived
Headers show
Series drm: enable -Wformat-truncation | expand

Commit Message

Jani Nikula May 23, 2024, 3:51 p.m. UTC
Enabling -Wformat-truncation yields the following warning:

../drivers/gpu/drm/imx/ipuv3/imx-ldb.c: In function ‘imx_ldb_probe’:
../drivers/gpu/drm/imx/ipuv3/imx-ldb.c:658:57: error: ‘_sel’ directive output may be truncated writing 4 bytes into a region of size between 3 and 13 [-Werror=format-truncation=]
  658 |                 snprintf(clkname, sizeof(clkname), "di%d_sel", i);
      |                                                         ^~~~
../drivers/gpu/drm/imx/ipuv3/imx-ldb.c:658:17: note: ‘snprintf’ output between 8 and 18 bytes into a destination of size 16
  658 |                 snprintf(clkname, sizeof(clkname), "di%d_sel", i);
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Silence the warning by checking the snprintf() return value.

Signed-off-by: Jani Nikula <jani.nikula@intel.com>

---

Cc: Philipp Zabel <p.zabel@pengutronix.de>
Cc: Shawn Guo <shawnguo@kernel.org>
Cc: Sascha Hauer <s.hauer@pengutronix.de>
Cc: Pengutronix Kernel Team <kernel@pengutronix.de>
Cc: Fabio Estevam <festevam@gmail.com>
Cc: dri-devel@lists.freedesktop.org
Cc: imx@lists.linux.dev
---
 drivers/gpu/drm/imx/ipuv3/imx-ldb.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

Comments

Ville Syrjälä May 23, 2024, 5:32 p.m. UTC | #1
On Thu, May 23, 2024 at 06:51:08PM +0300, Jani Nikula wrote:
> Enabling -Wformat-truncation yields the following warning:
> 
> ../drivers/gpu/drm/imx/ipuv3/imx-ldb.c: In function ‘imx_ldb_probe’:
> ../drivers/gpu/drm/imx/ipuv3/imx-ldb.c:658:57: error: ‘_sel’ directive output may be truncated writing 4 bytes into a region of size between 3 and 13 [-Werror=format-truncation=]
>   658 |                 snprintf(clkname, sizeof(clkname), "di%d_sel", i);
>       |                                                         ^~~~
> ../drivers/gpu/drm/imx/ipuv3/imx-ldb.c:658:17: note: ‘snprintf’ output between 8 and 18 bytes into a destination of size 16
>   658 |                 snprintf(clkname, sizeof(clkname), "di%d_sel", i);
>       |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

If only the compiler could count to three...

> 
> Silence the warning by checking the snprintf() return value.
> 
> Signed-off-by: Jani Nikula <jani.nikula@intel.com>
> 
> ---
> 
> Cc: Philipp Zabel <p.zabel@pengutronix.de>
> Cc: Shawn Guo <shawnguo@kernel.org>
> Cc: Sascha Hauer <s.hauer@pengutronix.de>
> Cc: Pengutronix Kernel Team <kernel@pengutronix.de>
> Cc: Fabio Estevam <festevam@gmail.com>
> Cc: dri-devel@lists.freedesktop.org
> Cc: imx@lists.linux.dev
> ---
>  drivers/gpu/drm/imx/ipuv3/imx-ldb.c | 6 +++++-
>  1 file changed, 5 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/gpu/drm/imx/ipuv3/imx-ldb.c b/drivers/gpu/drm/imx/ipuv3/imx-ldb.c
> index 71d70194fcbd..46f779fe60ee 100644
> --- a/drivers/gpu/drm/imx/ipuv3/imx-ldb.c
> +++ b/drivers/gpu/drm/imx/ipuv3/imx-ldb.c
> @@ -654,8 +654,12 @@ static int imx_ldb_probe(struct platform_device *pdev)
>  	 */
>  	for (i = 0; i < 4; i++) {
>  		char clkname[16];
> +		int len;
> +
> +		len = snprintf(clkname, sizeof(clkname), "di%d_sel", i);
> +		if (len >= sizeof(clkname))
> +			dev_err(dev, "clkname truncated\n");
>  
> -		snprintf(clkname, sizeof(clkname), "di%d_sel", i);
>  		imx_ldb->clk_sel[i] = devm_clk_get(imx_ldb->dev, clkname);
>  		if (IS_ERR(imx_ldb->clk_sel[i])) {
>  			ret = PTR_ERR(imx_ldb->clk_sel[i]);
> -- 
> 2.39.2
Jani Nikula May 24, 2024, 9:04 a.m. UTC | #2
On Thu, 23 May 2024, Ville Syrjälä <ville.syrjala@linux.intel.com> wrote:
> On Thu, May 23, 2024 at 06:51:08PM +0300, Jani Nikula wrote:
>> Enabling -Wformat-truncation yields the following warning:
>> 
>> ../drivers/gpu/drm/imx/ipuv3/imx-ldb.c: In function ‘imx_ldb_probe’:
>> ../drivers/gpu/drm/imx/ipuv3/imx-ldb.c:658:57: error: ‘_sel’ directive output may be truncated writing 4 bytes into a region of size between 3 and 13 [-Werror=format-truncation=]
>>   658 |                 snprintf(clkname, sizeof(clkname), "di%d_sel", i);
>>       |                                                         ^~~~
>> ../drivers/gpu/drm/imx/ipuv3/imx-ldb.c:658:17: note: ‘snprintf’ output between 8 and 18 bytes into a destination of size 16
>>   658 |                 snprintf(clkname, sizeof(clkname), "di%d_sel", i);
>>       |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> If only the compiler could count to three...

I did not try, but apparently using %hhd would hide the issue too:

	snprintf(clkname, sizeof(clkname), "di%hhd_sel", i);

BR,
Jani.


>
>> 
>> Silence the warning by checking the snprintf() return value.
>> 
>> Signed-off-by: Jani Nikula <jani.nikula@intel.com>
>> 
>> ---
>> 
>> Cc: Philipp Zabel <p.zabel@pengutronix.de>
>> Cc: Shawn Guo <shawnguo@kernel.org>
>> Cc: Sascha Hauer <s.hauer@pengutronix.de>
>> Cc: Pengutronix Kernel Team <kernel@pengutronix.de>
>> Cc: Fabio Estevam <festevam@gmail.com>
>> Cc: dri-devel@lists.freedesktop.org
>> Cc: imx@lists.linux.dev
>> ---
>>  drivers/gpu/drm/imx/ipuv3/imx-ldb.c | 6 +++++-
>>  1 file changed, 5 insertions(+), 1 deletion(-)
>> 
>> diff --git a/drivers/gpu/drm/imx/ipuv3/imx-ldb.c b/drivers/gpu/drm/imx/ipuv3/imx-ldb.c
>> index 71d70194fcbd..46f779fe60ee 100644
>> --- a/drivers/gpu/drm/imx/ipuv3/imx-ldb.c
>> +++ b/drivers/gpu/drm/imx/ipuv3/imx-ldb.c
>> @@ -654,8 +654,12 @@ static int imx_ldb_probe(struct platform_device *pdev)
>>  	 */
>>  	for (i = 0; i < 4; i++) {
>>  		char clkname[16];
>> +		int len;
>> +
>> +		len = snprintf(clkname, sizeof(clkname), "di%d_sel", i);
>> +		if (len >= sizeof(clkname))
>> +			dev_err(dev, "clkname truncated\n");
>>  
>> -		snprintf(clkname, sizeof(clkname), "di%d_sel", i);
>>  		imx_ldb->clk_sel[i] = devm_clk_get(imx_ldb->dev, clkname);
>>  		if (IS_ERR(imx_ldb->clk_sel[i])) {
>>  			ret = PTR_ERR(imx_ldb->clk_sel[i]);
>> -- 
>> 2.39.2
diff mbox series

Patch

diff --git a/drivers/gpu/drm/imx/ipuv3/imx-ldb.c b/drivers/gpu/drm/imx/ipuv3/imx-ldb.c
index 71d70194fcbd..46f779fe60ee 100644
--- a/drivers/gpu/drm/imx/ipuv3/imx-ldb.c
+++ b/drivers/gpu/drm/imx/ipuv3/imx-ldb.c
@@ -654,8 +654,12 @@  static int imx_ldb_probe(struct platform_device *pdev)
 	 */
 	for (i = 0; i < 4; i++) {
 		char clkname[16];
+		int len;
+
+		len = snprintf(clkname, sizeof(clkname), "di%d_sel", i);
+		if (len >= sizeof(clkname))
+			dev_err(dev, "clkname truncated\n");
 
-		snprintf(clkname, sizeof(clkname), "di%d_sel", i);
 		imx_ldb->clk_sel[i] = devm_clk_get(imx_ldb->dev, clkname);
 		if (IS_ERR(imx_ldb->clk_sel[i])) {
 			ret = PTR_ERR(imx_ldb->clk_sel[i]);