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

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

 



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?

	David



--
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