Re: [PATCH V2] Fix buffer size warning for strcpy

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, 23 Aug 2021, Tkaczyk, Mariusz wrote:
> On 19.08.2021 15:10, Nigel Croxon wrote:
> 
> > +	memset(ve->name, '\0', sizeof(ve->name));
> > +	if (name) {
> > +		int l = strlen(ve->name);
> > +		if (l > 16)
> > +			l = 16;
> > +		memcpy(ve->name, name, l);
> > +	}
> 
> What about:
> if (name)
> 	/*
> 	 * Name might not be null terminated.
> 	 */
> 	strncpy(ve->name, name, sizeof(ve->name));

I really like the idea of using strncpy().  I didn't realize it would
nul-pad to the full size, and that is exactly what we want.
So

  strncpy(ve->name, name?:"", sizeof(ve->name));

would be a complete solution.

Thanks,
NeilBrown



[Index of Archives]     [Linux RAID Wiki]     [ATA RAID]     [Linux SCSI Target Infrastructure]     [Linux Block]     [Linux IDE]     [Linux SCSI]     [Linux Hams]     [Device Mapper]     [Device Mapper Cryptographics]     [Kernel]     [Linux Admin]     [Linux Net]     [GFS]     [RPM]     [git]     [Yosemite Forum]


  Powered by Linux