[lkml]   [2002]   [Aug]   [28]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
Messages in this thread
SubjectRe: [TRIVIAL] strlen("literal string") -> (sizeof("literal string")-1)
>>>>> "JT" == Jim Treadway <> 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.
Instead, you end up examining each use of strlen() and make the
above "#define" only where it does not break; it is not worth
the aggravation. Something named "strlen" must work in all
situations, and "in this situation" is not good enough.
Otherwise it would confuse/surprise people.


*1* It fails on the following:

#define FOOBARBAZ &("foobarbaz"[0])

main(void) {
int sz;
if (__builtin_constant_p(FOOBARBAZ)) {
sz = sizeof(FOOBARBAZ) - 1;
printf("sizeof(FOOBARBAZ) -1 = %d\n", sz);
printf("strlen(FOOBARBAZ) = %d\n", strlen(FOOBARBAZ));

To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to
More majordomo info at
Please read the FAQ at

 \ /
  Last update: 2005-03-22 13:28    [W:0.101 / U:0.040 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site