Re: [TRIVIAL] strlen("literal string") -> (sizeof("literal string")-1)

Rusty Russell
2002-08-29T03:12:33Z

In message <7vd6s2lc9c.fsf@siamese.dyndns.org> you write:
> >>>>> "JT" == Jim Treadway <jim@stardot-tech.com> writes:
>
> JT> Would redefining strlen() as __strlen() and then using
>
> JT> #define strlen(x) (__builtin_constant_p(x) ? (sizeof(x) - 1) : __strlen(x
))
>
> JT> work in this situation?
>
> I thought about that before I posted the previous patch, but
> rejected it.
>
> If it worked in all situations then it would have been great,
> but it fails in at least one way [*1*], so you cannot generally
> define the above in a header file which everybody includes.

If you really care about that, try:

/* Be paranoid in case someone uses strlen(&("FOO"[0])) */
#define strlen(x) \
(__builtin_constant_p(x) && sizeof(x) != sizeof(char *)
? (sizeof(x) - 1) : __strlen(x))

Rusty.
--
Anyone who quotes me in their sig is an idiot. -- Rusty Russell.