Re: bugs in strlen, strstr setlocale -- i386

Frank Bergmann <[email protected]>
Newsgroups gmane.linux.lib.dietlibc
Message-ID <[email protected]>
Hi,

at least 32-Bit Intel-Arch should not segfault due to jecxz:

> cat dietlibc-0.31/i386/strlen.S
[...]
strlen:
        movl    4(%esp),%ecx
        xorl    %eax,%eax
        jecxz   .Lnull
        decl    %eax
.Llbl:  incl    %eax
        cmpb    $0,(%ecx, %eax)
        jne     .Llbl
.Lnull: ret
[...]

Test:

> cat test_strlen.c
#include <string.h>
int main() {
  return strlen((char *)0);
}
> diet gcc -O2 -Wall -o test_strlen test_strlen.c
test_strlen.c: In function 'main':
test_strlen.c:3: warning: null argument where non-null required (argument 1)
> ./test_strlen;echo $?
0
>

It's not easy to keep optimizations for different archs always in a 
unified state.
But with some old K&R docs in mind I guess that the result of strlen(0) is 
not defined (see also gcc warning).

Frank

In 3 Apr 2009, Nikola Vladov cum veritate scripsit :

> #include <string.h>
> int main() {
>   return strlen((char *)0);
> }
> 
> This gives segmentation fault!  
> As a result strstr(0, "UTF-8") works also wrong.
[...]

-- 
#!/bin/sh
$0 &; $0 &

Sending unsolicited commercial email to this address may be a violation
of the Washington State Consumer Protection Act, chapter 19.86 RCW.
Das Verschicken unverlangter kommerzieller email an diese Adresse ist
verboten (LG Traunstein, 2 HK O 3755/97 vom 14.10.1997, CR 1998, 171f).
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.