strlen("literal string") -> (sizeof("literal string")-1)
A message that is not in the archive (sent off-list or not yet received): [email protected]
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
"JT" == Jim Treadway <[email protected]> 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.
[Footnotes]
*1* It fails on the following:
#define FOOBARBAZ &("foobarbaz"[0])
void
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)); exit(0);
}
In message <[email protected]> you write:
Quoted 5 lines
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
"JT" == Jim Treadway <[email protected]> writes:
JT> Would redefining
strlen()as__strlen()and then usingJT> #define strlen(x) (
__builtin_constant_p(x)? (sizeof(x) - 1) : __strlen(x
))
Quoted 9 lines
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.
Hi,
Quoted 6 lines · Rusty Russell
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))
I must say that doesn't make the code any cleaner, which leads to it being not as clean as Keith suggested. It was a code cleanup, not a code messup.
Thunder
Signature · 4 lines
--./../...-/. -.--/---/..-/.-./..././.-../..-. .---/..-/.../- .- --/../-./..-/-/./--..-- ../.----./.-../.-.. --./../...-/. -.--/---/..- .- -/---/--/---/.-./.-./---/.--/.-.-.- --./.-/-.../.-./.././.-../.-.-.-
Quoted 6 lines · Thunder from the hill
Quoted 3 lines
#define strlen(x) \ (__builtin_constant_p(x) && sizeof(x) != sizeof(char *) ? (sizeof(x) - 1) : __strlen(x))I must say that doesn't make the code any cleaner, which leads to it being not as clean as Keith suggested. It was a code cleanup, not a code messup.
Sure the macro is fugly, but the code that uses it becomes cleaner which was the whole point here.
Dave
Signature · 2 lines
| Dave Jones. http://www.codemonkey.org.uk | SuSE Labs
In message <[email protected]> you w rite:
Quoted 5 lines
Hi,
Quoted 2 lines · Rusty Russell
If you really care about that, try:
Quoted 7 lines
Quoted 4 lines
/* 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))I must say that doesn't make the code any cleaner, which leads to it being not as clean as Keith suggested. It was a code cleanup, not a code messup.
Think harder.
This code would exist in one place: those (four) architectures which insist on having an inline strlen function. These guys already eat inline asm for breakfast: it's *their* jobs to jump through hoops so the driver writers can write simple code and have it work well.
Rusty.
--
Anyone who quotes me in their sig is an idiot. -- Rusty Russell.