Subject: Re: toolchain/22118: make won't compile with -Wcast-qual -Wstrict-prototypes and more
To: NetBSD Toolchain Technical Discussion List <tech-toolchain@NetBSD.org>
From: der Mouse <mouse@Rodents.Montreal.QC.CA>
List: tech-toolchain
Date: 07/14/2003 19:00:40
> So, just as when [...], the only portable way to initialize a struct
> iovec for writev() (without having GCC complain to you and without
> using -fwritable-strings) is to allocate writable storage and copy
> the string constants into it.
I disagree.
What's nonportable about this?
...
void *deconst(const void *cvp)
{
void *vp;
bcopy(&cvp,&vp,sizeof(void *));
return(vp);
}
...
struct iovec v[3];
...
v[2].iov_base = deconst("some long string here");
(If you want to be pedantic about BSD vs SysV religion, replace the
bcopy call with the corresponding memset call. You could also replace
it with a loop copying sizeof(void *) chars.)
It certainly "works", in that it it produces no warnings for me, with
-Wcast-qual -Wwrite-strings.
> In C it is simply impossible to portably discard 'const' without
> copying the data from read-only storage to writable storage.
What's nonportable about the above? Note it's copying only
sizeof(void *) bytes, regardless of the data pointed to; if this is
what you meant, I submit that you phrased it badly ("the data" appears
to refer to the data that will not be written to, the string contents
in my example).
> Hmmm.... I just noticed that GCC with '-Write-strings" does allow
> string constants to be assigned to non-const char arrays without
> complaint (even if the definitions are global or static):
> char fooarray[] = "abc";
This is not an assignment; it is an initialization. Arrays cannot be
assigned.
/~\ The ASCII der Mouse
\ / Ribbon Campaign
X Against HTML mouse@rodents.montreal.qc.ca
/ \ Email! 7D C8 61 52 5D E7 2D 39 4E F1 31 3E E8 B3 27 4B