On 7/27/25 5:07 AM, Linus Torvalds wrote:
On Sat, 26 Jul 2025 at 16:19, Kees Cook <k...@kernel.org> wrote:
That works for me! I just get twitchy around seeing memcpy used for strings. :)
if we're gonna NUL after the memcpy, just use strscpy_pad().
I do worry a tiny bit about performance.
Because 'memcpy+set last byte to NUL' really is just a couple of
instructions when we're talking small constant-sized arrays.
strscpy_pad() isn't horrible, but it's still at another level. And
most of the cost is that "return the length" which people often don't
care about.
Dang, I wish we had some compiler trick to say "if the value isn't
used, do X, if it _is_ used do Y".
It's such a trivial thing in the compiler itself, and the information
is there, but I don't think it is exposed in any useful way.
In fact, it *is* exposed in one way I can think of:
__attribute__((__warn_unused_result__))
but not in a useful form for actually generating different code.
Some kind of "__builtin_if_used(x,y)" where it picks 'x' if the value
is used, and 'y' if it isn't would be lovely for this.
Then you could do things like
#define my_helper(x) \
__builtin_if_used( \
full_semantics(x), \
simpler_version(x))
when having a return value means extra work and most people don't care.
Maybe it exists in some form that I haven't thought of?
Any compiler people around?
Sorry for the delay in reply, but I was checking with some *compiler*
folks and unfortunately couldn't find an equivalent of the above
*helper* support.
I am not a compiler expert though and relied mostly on my digging of the
'gcc' code and advise from folks working in compiler world.
In case there are no new suggestions, I think we can go ahead with
"strscpy_pad()" or "get_task_array()" in place of "get_task_comm()"
which is implement in the following manner:
static __always_inline void
__cstr_array_copy(char *dst,
const char *src, __kernel_size_t size)
{
memcpy(dst, src, size);
dst[size] = 0;
}
#define get_task_array(a,b) \
__cstr_array_copy(dst, src, __must_be_array(dst))
Please let me know.
Thanks,
Bhupesh