On 11/29/18 at 6:50 PM, Jason Gunthorpe wrote: > On Thu, Nov 29, 2018 at 04:58:26PM -0500, Qian Cai wrote: > > drivers/infiniband/hw/mlx4/sysfs.c:360:2: warning: 'strncpy' output may be > > truncated copying 8 bytes from a string of length 31 > > [-Wstringop-truncation] > > > > Signed-off-by: Qian Cai <cai@xxxxxx> > > > > Changes since v3: > > * Removed an unused variable. > > > > since v2: > > * Simplified the whole things using snprintf() suggested by Jason. > > > > since v1: > > * Used strscpy() instead of snprintf() > > * Removed an unnecessary statement. > > > > drivers/infiniband/hw/mlx4/sysfs.c | 12 ++++-------- > > 1 file changed, 4 insertions(+), 8 deletions(-) > > > > diff --git a/drivers/infiniband/hw/mlx4/sysfs.c b/drivers/infiniband/hw/mlx4/sysfs.c > > index 752bdd5..d4f8a08 100644 > > +++ b/drivers/infiniband/hw/mlx4/sysfs.c > > @@ -353,16 +353,12 @@ static int add_port_entries(struct mlx4_ib_dev *device, int port_num) > > > > static void get_name(struct mlx4_ib_dev *dev, char *name, int i, int max) > > { > > - char base_name[9]; > > - > > - /* pci_name format is: bus:dev:func -> xxxx:yy:zz.n */ > > - strlcpy(name, pci_name(dev->dev->persist->pdev), max); > > - strncpy(base_name, name, 8); /*till xxxx:yy:*/ > > - base_name[8] = '\0'; > > - /* with no ARI only 3 last bits are used so when the fn is higher than 8 > > + /* pci_name format is: bus:dev:func -> xxxx:yy:zz.n > > + * with no ARI only 3 last bits are used so when the fn is higher than 8 > > * need to add it to the dev num, so count in the last number will be > > * modulo 8 */ > > - sprintf(name, "%s%.2d.%d", base_name, (i/8), (i%8)); > > + snprintf(name, max, "%s%.2d.%d", pci_name(dev->dev->persist->pdev), > > + (i/8), (i%8)); > > This doesn't look right, did you test it? > Since I have no mlx4 hardware (only have mlx5) at the moment, I did compiling and boot tests. > I suggested "%.8s%.2d.%d" as the format specifier I’ll fix that.