Subject: Re: pkgsrc NetBSD 3.99.3/i386 bulk build results 2005-04-23
To: None <tech-pkg@netbsd.org>
From: Roland Illig <roland.illig@gmx.de>
List: tech-pkg
Date: 04/25/2005 22:45:02
Greg Troxel wrote:
> I note that strstr takes a const char * first argument, and returns a
> non-const char * pointer into it.  This seems broken, but the man page
> claims it is C90-compliant.  memmem takes const void * but returns
> void *, which seems similarly broken, since it provides an unchecked
> way to drop const from a pointer.

That's because C90 and C99 are lacking function overloading. (C++ 
defines the "correct" versions.) Because of this possible usage:

     char buf[256];

     strcpy(buf, "abcdef");
     *strstr(buf, "a") = 'A';

the return type cannot be changed to "const char *". C99 does not 
explicitly state that the string returned by strstr shall not be 
modified (not even in the rationale), but the general principle that 
string literals may reside in read-only memory forbids write access to 
them (6.5.4#6.2).

The nicest solution would be to offer two functions:

     char *strstr(char *, const char *);
     const char *strcstr(const char *, const char *);

But I doubt that will happen in the next few years. :(

Roland