RE: [PATCH 2/9] libusbg: Always add '\0' at end of string

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

 



> -----Original Message-----
> From: linux-usb-owner@xxxxxxxxxxxxxxx [mailto:linux-usb-
> owner@xxxxxxxxxxxxxxx] On Behalf Of David Laight
> Sent: Friday, September 12, 2014 11:34 AM
> To: 'Krzysztof Opasiak'; matt.porter@xxxxxxxxxx; linux-
> usb@xxxxxxxxxxxxxxx
> Cc: s.wadas@xxxxxxxxxxx; k.lewandowsk@xxxxxxxxxxx;
> andrzej.p@xxxxxxxxxxx; m.szyprowski@xxxxxxxxxxx;
> philippedeswert@xxxxxxxxx
> Subject: RE: [PATCH 2/9] libusbg: Always add '\0' at end of string
> 
> From: Krzysztof Opasiak
> > strncpy() may not append trailing '\0' so let's append
> > it always at end of string to avoid getting into troubles.
> 
> Silently truncating strings just gives other errors.
> You really need to verify that it doesn't matter.
> ...
> > diff --git a/src/usbg.c b/src/usbg.c
> > index 0cc778b..785c01a 100644
> > --- a/src/usbg.c
> > +++ b/src/usbg.c
> > @@ -1324,10 +1324,12 @@ size_t
> usbg_get_configfs_path_len(usbg_state *s)
> >  int usbg_get_configfs_path(usbg_state *s, char *buf, size_t len)
> >  {
> >  	int ret = USBG_SUCCESS;
> > -	if (s && buf)
> > +	if (s && buf) {
> >  		strncpy(buf, s->path, len);
> > -	else
> > +		buf[len - 1] = '\0';
> > +	} else {
> >  		ret = USBG_ERROR_INVALID_PARAM;
> > +	}
> >
> >  	return ret;
> >  }
> 
> I would use the much shorter form:
> 	if (!s || !buf)
> 		return USBG_ERROR_INVALID_PARAM;
> 
> 	buf[--len] = 0;
> 	strncpy(buf, s->path, len);
> 	return USBG_SUCCESS;
> 
> Although can either 's' or 'buf' actually be NULL?
> If you are being over-defensive can 'len' be zero?
> 

Oh that's true. It can be done in shorter form.
Yes, we should also check if len is 0.
Will be fixed in v2.

Thanks,
Krzysztof Opasiak


--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html




[Index of Archives]     [Linux Media]     [Linux Input]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [Old Linux USB Devel Archive]

  Powered by Linux