Re: [PATCH next 2/3] fortify: Optimise strnlen()
From: David Laight
Date: Thu Apr 16 2026 - 10:22:54 EST
On Tue, 31 Mar 2026 16:51:26 -0700
Kees Cook <kees@xxxxxxxxxx> wrote:
> On Tue, Mar 31, 2026 at 11:09:14PM +0100, David Laight wrote:
> > Any uses should be replaced by __builtin_strlen().
>
> When I looked at this before, __builtin_strlen() flip to run-time strlen
> on non-constant strings, which is why I had to jump through all the
> hoops to avoid calling it in those cases.
>
I've (finally) looked at this. - and found some extra strlen() calls.
The bloat-o-meter also showed a few places that shrank - not looked at them.
I'll look again, probably doing:
if (__builtin_constant(str[0]) {
len = __builting_strlen(str);
if (__builtin_constant(len)) {
/* got a constant string */
...
return ...;
}
}
/* non-constant */
...
It only matters for a couple of functions where you don't want the
actual length, trying to factor it out is probably too fragile.
David