Re: [PULL] http://kernellabs.com/hg/~mkrufky/cx23885

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

 



On Tue, 4 Aug 2009, Mauro Carvalho Chehab wrote:
> + strncpy(vin->name, "CX23885 Audio", 14);
> + vin->capability = V4L2_AUDCAP_STEREO;
>
> the "magic" size above is very ugly. Please find another way for it, like:
>
> #define CX23885_INPUT_NAME "CX23885 Audio"
> strncpy(vin->name, CX23885_INPUT_NAME, sizeof(CX23885_INPUT_NAME) + 1);

There is no need to add one here, as sizeof("foo") == 4, i.e. sizeof
includes the trailing \0 for a string literal.

But, there is no point to using strncpy() like this.  How could the
behavior possibly be any different than just strcpy()?  We know the string
literal must be NUL terminated.

The proper way to do this would be either:

strcpy(vin->name, "CX23885 Audio");
strncpy(vin->name, "CX23885 Audio", ARRAY_SIZE(vin->name));

The latter insures we don't overrun the end of vin->name, but IMHO it's
unnecessary since we know there is enough space.  Using ARRAY_SIZE()
instead of sizeof() should give us a compile error if vin->name were a
pointer instead of an array, while sizeof() would happily compile to
completely wrong code.
--
To unsubscribe from this list: send the line "unsubscribe linux-media" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Input]     [Video for Linux]     [Gstreamer Embedded]     [Mplayer Users]     [Linux USB Devel]     [Linux Audio Users]     [Linux Kernel]     [Linux SCSI]     [Yosemite Backpacking]
  Powered by Linux