diff mbox series

drm/amd: Fix the size of a buffer in amdgpu_vcn_idle_work_handler()

Message ID d8a9cda0c4c391458ddd63d1be88f2a757f6a5d0.1695410820.git.christophe.jaillet@wanadoo.fr (mailing list archive)
State New, archived
Headers show
Series drm/amd: Fix the size of a buffer in amdgpu_vcn_idle_work_handler() | expand

Commit Message

Christophe JAILLET Sept. 22, 2023, 7:27 p.m. UTC
In order to be sure that fw_name is not truncated, this buffer should be
at least 41 bytes long.

Let the compiler compute the correct length by itself.

When building with W=1, this fixes the following warnings:

  drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c: In function ‘amdgpu_vcn_early_init’:
  drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c:95:58: error: ‘snprintf’ output may be truncated before the last format character [-Werror=format-truncation=]
     95 |         snprintf(fw_name, sizeof(fw_name), "amdgpu/%s.bin", ucode_prefix);
        |                                                          ^
  drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c:95:9: note: ‘snprintf’ output between 12 and 41 bytes into a destination of size 40
     95 |         snprintf(fw_name, sizeof(fw_name), "amdgpu/%s.bin", ucode_prefix);
        |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Fixes: 69939009bde7 ("drm/amd: Load VCN microcode during early_init")
Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Christian König Sept. 25, 2023, 6:35 a.m. UTC | #1
Am 22.09.23 um 21:27 schrieb Christophe JAILLET:
> In order to be sure that fw_name is not truncated, this buffer should be
> at least 41 bytes long.
>
> Let the compiler compute the correct length by itself.
>
> When building with W=1, this fixes the following warnings:
>
>    drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c: In function ‘amdgpu_vcn_early_init’:
>    drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c:95:58: error: ‘snprintf’ output may be truncated before the last format character [-Werror=format-truncation=]
>       95 |         snprintf(fw_name, sizeof(fw_name), "amdgpu/%s.bin", ucode_prefix);
>          |                                                          ^
>    drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c:95:9: note: ‘snprintf’ output between 12 and 41 bytes into a destination of size 40
>       95 |         snprintf(fw_name, sizeof(fw_name), "amdgpu/%s.bin", ucode_prefix);
>          |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> Fixes: 69939009bde7 ("drm/amd: Load VCN microcode during early_init")
> Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
> ---
>   drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c
> index c93f3a4c0e31..f8cd55a0d1f0 100644
> --- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c
> +++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c
> @@ -88,7 +88,7 @@ static void amdgpu_vcn_idle_work_handler(struct work_struct *work);
>   int amdgpu_vcn_early_init(struct amdgpu_device *adev)
>   {
>   	char ucode_prefix[30];
> -	char fw_name[40];
> +	char fw_name[sizeof(ucode_prefix) + sizeof("amdgpu/.bin") - 1];

When that string constant is used multiple times it should probably be a 
constant.

But I think this patch is superfluous to begin with because the 
ucode_prefix is never fully used.

What we should probably rather to is to define some 
AMDGPU_MAX_UCODE_LENGTH and use that instead.

Regards,
Christian.

>   	int r;
>   
>   	amdgpu_ucode_ip_version_decode(adev, UVD_HWIP, ucode_prefix, sizeof(ucode_prefix));
diff mbox series

Patch

diff --git a/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c b/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c
index c93f3a4c0e31..f8cd55a0d1f0 100644
--- a/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c
+++ b/drivers/gpu/drm/amd/amdgpu/amdgpu_vcn.c
@@ -88,7 +88,7 @@  static void amdgpu_vcn_idle_work_handler(struct work_struct *work);
 int amdgpu_vcn_early_init(struct amdgpu_device *adev)
 {
 	char ucode_prefix[30];
-	char fw_name[40];
+	char fw_name[sizeof(ucode_prefix) + sizeof("amdgpu/.bin") - 1];
 	int r;
 
 	amdgpu_ucode_ip_version_decode(adev, UVD_HWIP, ucode_prefix, sizeof(ucode_prefix));