Subject: Re: toolchain/22118: make won't compile with -Wcast-qual -Wstrict-prototypes and more
To: der Mouse <mouse@Rodents.Montreal.QC.CA>
From: Greg A. Woods <woods@weird.com>
List: tech-toolchain
Date: 07/13/2003 18:10:17
[ On Saturday, July 12, 2003 at 16:27:39 (-0400), der Mouse wrote: ]
> Subject: Re: toolchain/22118: make won't compile with -Wcast-qual -Wstrict-prototypes and more
>
> (If the interface were designed to be used const-poisoned,
> writev would use a different struct from readv.)

That's the key issue indeed.  Interfaces which use the same data
structure definitions for both reading and writing cannot, in C at
least, declare read-only storage for the write function(s) and writable
storage for the read function(s).

So, just as when initializing a DBT structure from string constants, 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.  These
examples are one place where the programmer definitely knows more than
the compiler and it probably makes sense to ignore the compiler
warnings.

>  How is doing this
> unsafe or nonportable?  Or does it not count as discarding const?

In C it is simply impossible to portably discard 'const' without copying
the data from read-only storage to writable storage.

The alternatives which effectively re-declare the storage to be writable
assume the programmer really does know what he or she is doing.  I think I
prefer the warning to any contruct which hides what's really happening.

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):

	const char *fooptr = "abc";
	char fooarray[] = "abc";

That solves my "problem" with using string constants with DBT.....  :-)

If I understand the details correctly there probably should be a warning
for the assignment in the second definition above, though the
description of -Wwrite-strings is very explicit about only warning when
a "char *" is used as the lvalue....  :-)

I don't know of (or at least don't have access to) any other C compilers
that implement read-only string constants by default and which also have
good const qualifier checking.

> > Since a "volatile" and "const" make no real sense togethr in a cast
> 
> ...huh?  volatile const makes perfect sense: the object must be read
> exactly as the source implies, but the source promises it will not
> write to it through that path.  A device register is a typical example.

Hmmmm.... yes, you're right.  Read-only device registers are indeed both
volatile and const.

> Discarding volatile is just as verboten as discarding const.

Indeed.

-- 
								Greg A. Woods

+1 416 218-0098;            <g.a.woods@ieee.org>;           <woods@robohack.ca>
Planix, Inc. <woods@planix.com>; VE3TCP; Secrets of the Weird <woods@weird.com>