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 20:14:27
> If a pointer passed to free() may ever have been derived from a
> string constant then there's a very nasty bug lurking somewhere.

Yes...but it's quite possible that a given data structure may contain
something which in some cases is freed, but in other cases never is,
and the pointer points to a string constant only in the "other cases".

For example, in a tree structure, the code is often designed such that
the root node is never freed.  You then have to do one of

1) Make the element pointer-to-const, and deconst it when freeing;

2) Make the element pointer-to-nonconst, and deconst it when assigning;

3) Don't use string literals even for the root;

4) Duplicate the struct definition, with a consted version for the root
   and a non-consted version for non-roots;

5) Disable or ignore the warnings (-Wno-cast-qual, -fwritable-strings,
   etc, I count here).

(4) is the most abstractly correct, but I consider the duplication bad
enough that I prefer (1) or (2) (or occasionally (3)) in practice.

/~\ 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