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

Jani Nikula jani.nikula at intel.com
Fri May 24 09:04:56 UTC 2024


On Thu, 23 May 2024, Ville Syrjälä <ville.syrjala at 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 at intel.com>
>> 
>> ---
>> 
>> Cc: Philipp Zabel <p.zabel at pengutronix.de>
>> Cc: Shawn Guo <shawnguo at kernel.org>
>> Cc: Sascha Hauer <s.hauer at pengutronix.de>
>> Cc: Pengutronix Kernel Team <kernel at pengutronix.de>
>> Cc: Fabio Estevam <festevam at gmail.com>
>> Cc: dri-devel at lists.freedesktop.org
>> Cc: imx at 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, Intel


More information about the amd-gfx mailing list