Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

octeontx2-af: use unsigned int as iterator for unsigned values

The local variable i is used to iterate over unsigned
values. The lower bound of the loop is set to 0. While
the upper bound is cgx->lmac_count, where they lmac_count is
an u8. So the theoretical upper bound is 255.

As is, GCC can't see this range of values and warns that
a formatted string, which includes the %d representation of i,
may overflow the buffer provided.

GCC 15.1.0 says:

.../cgx.c: In function 'cgx_lmac_init':
.../cgx.c:1737:49: warning: '%d' directive writing between 1 and 11 bytes into a region of size between 4 and 6 [-Wformat-overflow=]
1737 | sprintf(lmac->name, "cgx_fwi_%d_%d", cgx->cgx_id, i);
| ^~
.../cgx.c:1737:37: note: directive argument in the range [-2147483641, 254]
1737 | sprintf(lmac->name, "cgx_fwi_%d_%d", cgx->cgx_id, i);
| ^~~~~~~~~~~~~~~
.../cgx.c:1737:17: note: 'sprintf' output between 12 and 24 bytes into a destination of size 16
1737 | sprintf(lmac->name, "cgx_fwi_%d_%d", cgx->cgx_id, i);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Empirically, changing the type of i from (signed) int to unsigned int
addresses this problem. I assume by allowing GCC to see the range of
values described above.

Also update the format specifiers for the integer values in the string
in question from %d to %u. This seems appropriate as they are now both
unsigned.

No functional change intended.
Compile tested only.

Signed-off-by: Simon Horman <horms@kernel.org>
Reviewed-by: Vadim Fedorenko <vadim.fedorenko@linux.dev>
Link: https://patch.msgid.link/20250724-octeontx2-af-unsigned-v1-1-c745c106e06f@kernel.org
Signed-off-by: Jakub Kicinski <kuba@kernel.org>

authored by

Simon Horman and committed by
Jakub Kicinski
9312ee76 fada2649

+3 -3
+3 -3
drivers/net/ethernet/marvell/octeontx2/af/cgx.c
··· 1706 1706 { 1707 1707 u8 max_dmac_filters; 1708 1708 struct lmac *lmac; 1709 + int err, filter; 1710 + unsigned int i; 1709 1711 u64 lmac_list; 1710 - int i, err; 1711 - int filter; 1712 1712 1713 1713 /* lmac_list specifies which lmacs are enabled 1714 1714 * when bit n is set to 1, LMAC[n] is enabled ··· 1734 1734 err = -ENOMEM; 1735 1735 goto err_lmac_free; 1736 1736 } 1737 - sprintf(lmac->name, "cgx_fwi_%d_%d", cgx->cgx_id, i); 1737 + sprintf(lmac->name, "cgx_fwi_%u_%u", cgx->cgx_id, i); 1738 1738 if (cgx->mac_ops->non_contiguous_serdes_lane) { 1739 1739 lmac->lmac_id = __ffs64(lmac_list); 1740 1740 lmac_list &= ~BIT_ULL(lmac->lmac_id);