[PATCH net-next v2] net: thunderx: Fix format-truncation warning in bgx_acpi_match_id()

Alok Tiwari posted 1 patch 3 months ago
There is a newer version of this series
drivers/net/ethernet/cavium/thunder/thunder_bgx.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
[PATCH net-next v2] net: thunderx: Fix format-truncation warning in bgx_acpi_match_id()
Posted by Alok Tiwari 3 months ago
The buffer bgx_sel used in snprintf() was too small to safely hold
the formatted string "BGX%d" for all valid bgx_id values. This caused
a -Wformat-truncation warning with Werror enabled during build.

Increase the buffer size from 5 to 8 and use sizeof(bgx_sel) in
snprintf() to ensure safety and suppress the warning.

Build warning:
  CC      drivers/net/ethernet/cavium/thunder/thunder_bgx.o
  drivers/net/ethernet/cavium/thunder/thunder_bgx.c: In function
‘bgx_acpi_match_id’:
  drivers/net/ethernet/cavium/thunder/thunder_bgx.c:1434:27: error: ‘%d’
directive output may be truncated writing between 1 and 3 bytes into a
region of size 2 [-Werror=format-truncation=]
    snprintf(bgx_sel, 5, "BGX%d", bgx->bgx_id);
                             ^~
  drivers/net/ethernet/cavium/thunder/thunder_bgx.c:1434:23: note:
directive argument in the range [0, 255]
    snprintf(bgx_sel, 5, "BGX%d", bgx->bgx_id);
                         ^~~~~~~
  drivers/net/ethernet/cavium/thunder/thunder_bgx.c:1434:2: note:
‘snprintf’ output between 5 and 7 bytes into a destination of size 5
    snprintf(bgx_sel, 5, "BGX%d", bgx->bgx_id);

compiler warning due to insufficient snprintf buffer size.

Signed-off-by: Alok Tiwari <alok.a.tiwari@oracle.com>
---
v1->v2
No changes. Targeting for net-next.
https://lore.kernel.org/all/20250708160957.GQ452973@horms.kernel.org/
---
 drivers/net/ethernet/cavium/thunder/thunder_bgx.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/net/ethernet/cavium/thunder/thunder_bgx.c b/drivers/net/ethernet/cavium/thunder/thunder_bgx.c
index 3b7ad744b2dd6..2eea3142eebea 100644
--- a/drivers/net/ethernet/cavium/thunder/thunder_bgx.c
+++ b/drivers/net/ethernet/cavium/thunder/thunder_bgx.c
@@ -1429,9 +1429,9 @@ static acpi_status bgx_acpi_match_id(acpi_handle handle, u32 lvl,
 {
 	struct acpi_buffer string = { ACPI_ALLOCATE_BUFFER, NULL };
 	struct bgx *bgx = context;
-	char bgx_sel[5];
+	char bgx_sel[8];
 
-	snprintf(bgx_sel, 5, "BGX%d", bgx->bgx_id);
+	snprintf(bgx_sel, sizeof(bgx_sel), "BGX%d", bgx->bgx_id);
 	if (ACPI_FAILURE(acpi_get_name(handle, ACPI_SINGLE_NAME, &string))) {
 		pr_warn("Invalid link device\n");
 		return AE_OK;
-- 
2.46.0

Re: [PATCH net-next v2] net: thunderx: Fix format-truncation warning in bgx_acpi_match_id()
Posted by Jakub Kicinski 2 months, 4 weeks ago
On Tue,  8 Jul 2025 10:52:43 -0700 Alok Tiwari wrote:
> Increase the buffer size from 5 to 8 and use sizeof(bgx_sel) in
> snprintf() to ensure safety and suppress the warning.
> 
> Build warning:
>   CC      drivers/net/ethernet/cavium/thunder/thunder_bgx.o
>   drivers/net/ethernet/cavium/thunder/thunder_bgx.c: In function
> ‘bgx_acpi_match_id’:
>   drivers/net/ethernet/cavium/thunder/thunder_bgx.c:1434:27: error: ‘%d’
> directive output may be truncated writing between 1 and 3 bytes into a
> region of size 2 [-Werror=format-truncation=]
>     snprintf(bgx_sel, 5, "BGX%d", bgx->bgx_id);
>                              ^~
>   drivers/net/ethernet/cavium/thunder/thunder_bgx.c:1434:23: note:
> directive argument in the range [0, 255]
>     snprintf(bgx_sel, 5, "BGX%d", bgx->bgx_id);
>                          ^~~~~~~
>   drivers/net/ethernet/cavium/thunder/thunder_bgx.c:1434:2: note:
> ‘snprintf’ output between 5 and 7 bytes into a destination of size 5
>     snprintf(bgx_sel, 5, "BGX%d", bgx->bgx_id);

Hm, why are you making it 8 when the max length is 7 ? 🤔️
-- 
pw-bot: cr
Re: Re: [PATCH net-next v2] net: thunderx: Fix format-truncation warning in bgx_acpi_match_id()
Posted by ALOK TIWARI 2 months, 4 weeks ago

On 7/11/2025 4:04 AM, Jakub Kicinski wrote:
> On Tue,  8 Jul 2025 10:52:43 -0700 Alok Tiwari wrote:
>> Increase the buffer size from 5 to 8 and use sizeof(bgx_sel) in
>> snprintf() to ensure safety and suppress the warning.
>>
>> Build warning:
>>    CC      drivers/net/ethernet/cavium/thunder/thunder_bgx.o
>>    drivers/net/ethernet/cavium/thunder/thunder_bgx.c: In function
>> ‘bgx_acpi_match_id’:
>>    drivers/net/ethernet/cavium/thunder/thunder_bgx.c:1434:27: error: ‘%d’
>> directive output may be truncated writing between 1 and 3 bytes into a
>> region of size 2 [-Werror=format-truncation=]
>>      snprintf(bgx_sel, 5, "BGX%d", bgx->bgx_id);
>>                               ^~
>>    drivers/net/ethernet/cavium/thunder/thunder_bgx.c:1434:23: note:
>> directive argument in the range [0, 255]
>>      snprintf(bgx_sel, 5, "BGX%d", bgx->bgx_id);
>>                           ^~~~~~~
>>    drivers/net/ethernet/cavium/thunder/thunder_bgx.c:1434:2: note:
>> ‘snprintf’ output between 5 and 7 bytes into a destination of size 5
>>      snprintf(bgx_sel, 5, "BGX%d", bgx->bgx_id);
> 
> Hm, why are you making it 8 when the max length is 7 ? 🤔️

yes, 7 is max range. I will send v3 with this change.

Thanks,
Alok

Re: [PATCH net-next v2] net: thunderx: Fix format-truncation warning in bgx_acpi_match_id()
Posted by Simon Horman 3 months ago
On Tue, Jul 08, 2025 at 10:52:43AM -0700, Alok Tiwari wrote:
> The buffer bgx_sel used in snprintf() was too small to safely hold
> the formatted string "BGX%d" for all valid bgx_id values. This caused
> a -Wformat-truncation warning with Werror enabled during build.
> 
> Increase the buffer size from 5 to 8 and use sizeof(bgx_sel) in
> snprintf() to ensure safety and suppress the warning.
> 
> Build warning:
>   CC      drivers/net/ethernet/cavium/thunder/thunder_bgx.o
>   drivers/net/ethernet/cavium/thunder/thunder_bgx.c: In function
> ‘bgx_acpi_match_id’:
>   drivers/net/ethernet/cavium/thunder/thunder_bgx.c:1434:27: error: ‘%d’
> directive output may be truncated writing between 1 and 3 bytes into a
> region of size 2 [-Werror=format-truncation=]
>     snprintf(bgx_sel, 5, "BGX%d", bgx->bgx_id);
>                              ^~
>   drivers/net/ethernet/cavium/thunder/thunder_bgx.c:1434:23: note:
> directive argument in the range [0, 255]
>     snprintf(bgx_sel, 5, "BGX%d", bgx->bgx_id);
>                          ^~~~~~~
>   drivers/net/ethernet/cavium/thunder/thunder_bgx.c:1434:2: note:
> ‘snprintf’ output between 5 and 7 bytes into a destination of size 5
>     snprintf(bgx_sel, 5, "BGX%d", bgx->bgx_id);
> 
> compiler warning due to insufficient snprintf buffer size.
> 
> Signed-off-by: Alok Tiwari <alok.a.tiwari@oracle.com>
> ---
> v1->v2
> No changes. Targeting for net-next.
> https://lore.kernel.org/all/20250708160957.GQ452973@horms.kernel.org/

Thanks for the update.

Reviewed-by: Simon Horman <horms@kernel.org>