Thread (11 messages) 11 messages, 3 authors, 2021-03-23

RE: [PATCH v2 3/4] fs: unicode: Use strscpy() instead of strncpy()

From: David Laight <hidden>
Date: 2021-03-18 15:42:04
Also in: lkml

From: Shreeya Patel
Sent: 18 March 2021 14:13

On 18/03/21 7:03 pm, Shreeya Patel wrote:
quoted
Following warning was reported by Kernel Test Robot.

In function 'utf8_parse_version',
inlined from 'utf8_load' at fs/unicode/utf8mod.c:195:7:
quoted
quoted
fs/unicode/utf8mod.c:175:2: warning: 'strncpy' specified bound 12 equals
destination size [-Wstringop-truncation]
175 |  strncpy(version_string, version, sizeof(version_string));
     |  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The -Wstringop-truncation warning highlights the unintended
uses of the strncpy function that truncate the terminating NULL
character from the source string.
Unlike strncpy(), strscpy() always null-terminates the destination string,
hence use strscpy() instead of strncpy().

Not sure if strscpy is preferable. Just found this article
https://lwn.net/Articles/659214/
Should I go for memcpy instead?
Which length would you give memcpy() ?
The compiler will moan if you try to read beyond the end of the
input string.

strscpy() is about the best of a bad lot.

I think (I'm not sure!) that a good string copy function should
return the number of bytes copies or the buffer length is truncated.
Then you can do repeated:
	off += xxxcpy(buf + off, buflen - off, xxxxx);
without any danger of writing beyond the buffer end, always
getting a '\0' terminated string, and being able to detect overflow
right at the end.

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help