chiark / gitweb /
changelog: start 0.6.8
[secnet.git] / ac_prog_cc_no_writeable_strings.m4
1 dnl @synopsis AC_PROG_CC_NO_WRITEABLE_STRINGS(substvar [,hard])
2 dnl
3 dnl Try to find a compiler option that warns when a stringliteral is
4 dnl used in a place that could potentially modify the address. This
5 dnl should warn on giving an stringliteral to a function that asks of
6 dnl a non-const-modified char-pointer.
7 dnl
8 dnl The sanity check is done by looking at string.h which has a set
9 dnl of strcpy definitions that should be defined with const-modifiers
10 dnl to not emit a warning in all so many places.
11 dnl
12 dnl Currently this macro knows about GCC.
13 dnl hopefully will evolve to use:    Solaris C compiler,
14 dnl Digital Unix C compiler, C for AIX Compiler, HP-UX C compiler,
15 dnl and IRIX C compiler.
16 dnl
17 dnl @version $Id: ac_prog_cc_no_writeable_strings.m4,v 1.1 2002/02/20 16:18:18 steve Exp $
18 dnl @author Guido Draheim <guidod@gmx.de>
19
20 dnl  This is an older version of ax_cflags_no_writable_strings.m4
21 dnl  which is nowadays to be found in the Autoconf Archive.  Nowadays,
22 dnl  this file has this permission notice there::
23 dnl
24 dnl  Copyright (c) 2008 Guido U. Draheim <guidod@gmx.de>
25 dnl
26 dnl  This program is free software; you can redistribute it and/or modify it
27 dnl  under the terms of the GNU General Public License as published by the
28 dnl  Free Software Foundation; either version 3 of the License, or (at your
29 dnl  option) any later version.
30 dnl
31 dnl  This program is distributed in the hope that it will be useful, but
32 dnl  WITHOUT ANY WARRANTY; without even the implied warranty of
33 dnl  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
34 dnl  Public License for more details.
35 dnl
36 dnl  You should have received a copy of the GNU General Public License along
37 dnl  with this program. If not, see <https://www.gnu.org/licenses/>.
38 dnl
39 dnl  As a special exception, the respective Autoconf Macro's copyright owner
40 dnl  gives unlimited permission to copy, distribute and modify the configure
41 dnl  scripts that are the output of Autoconf when processing the Macro. You
42 dnl  need not follow the terms of the GNU General Public License when using
43 dnl  or distributing such scripts, even though portions of the text of the
44 dnl  Macro appear in them. The GNU General Public License (GPL) does govern
45 dnl  all other use of the material that constitutes the Autoconf Macro.
46 dnl
47 dnl  This special exception to the GPL applies to versions of the Autoconf
48 dnl  Macro released by the Autoconf Archive. When you make and distribute a
49 dnl  modified version of the Autoconf Macro, you may extend this special
50 dnl  exception to the GPL to apply to your modified version as well.
51
52
53 AC_DEFUN([AC_PROG_CC_NO_WRITEABLE_STRINGS], [
54   pushdef([CV],ac_cv_prog_cc_no_writeable_strings)dnl
55   hard=$2
56   if test -z "$hard"; then
57     msg="C to warn about writing to stringliterals"
58   else
59     msg="C to prohibit any write to stringliterals"
60   fi
61   AC_CACHE_CHECK($msg, CV, [
62   cat > conftest.c <<EOF
63 #include <string.h>
64 int main (void)
65 {
66    char test[[16]];
67    if (strcpy (test, "test")) return 0;
68    return 1;
69 }
70 EOF
71   dnl GCC
72   if test "$GCC" = "yes"; 
73   then
74         if test -z "$hard"; then
75             CV="-Wwrite-strings"
76         else
77             CV="-fno-writable-strings -Wwrite-strings"
78         fi
79
80         if test -n "`${CC-cc} -c $CV conftest.c 2>&1`" ; then
81             CV="suppressed: string.h"
82         fi
83
84   dnl Solaris C compiler
85   elif  $CC -flags 2>&1 | grep "Xc.*strict ANSI C" > /dev/null 2>&1 &&
86         $CC -c -xstrconst conftest.c > /dev/null 2>&1 &&
87         test -f conftest.o 
88   then
89         # strings go into readonly segment
90         CV="-xstrconst"
91
92         rm conftest.o
93         if test -n "`${CC-cc} -c $CV conftest.c 2>&1`" ; then
94              CV="suppressed: string.h"
95         fi
96   
97   dnl HP-UX C compiler
98   elif  $CC > /dev/null 2>&1 &&
99         $CC -c +ESlit conftest.c > /dev/null 2>&1 &&
100         test -f conftest.o 
101   then
102        # strings go into readonly segment
103         CV="+ESlit"
104         
105         rm conftest.o
106         if test -n "`${CC-cc} -c $CV conftest.c 2>&1`" ; then
107              CV="suppressed: string.h"
108         fi
109
110   dnl Digital Unix C compiler
111   elif ! $CC > /dev/null 2>&1 &&
112         $CC -c -readonly_strings conftest.c > /dev/null 2>&1 &&
113         test -f conftest.o
114   then  
115        # strings go into readonly segment
116         CV="-readonly_strings"
117         
118         rm conftest.o
119         if test -n "`${CC-cc} -c $CV conftest.c 2>&1`" ; then
120              CV="suppressed: string.h"
121         fi
122
123   dnl C for AIX Compiler
124
125   dnl IRIX C compiler
126         # -use_readonly_const is the default for IRIX C, 
127         # puts them into .rodata, but they are copied later.
128         # need to be "-G0 -rdatashared" for strictmode but
129         # I am not sure what effect that has really.
130
131   fi
132   rm -f conftest.*
133   ])
134   if test -z "[$]$1" ; then
135     if test -n "$CV" ; then
136       case "$CV" in
137         suppressed*) $1="" ;; # known but suppressed
138         *)  $1="$CV" ;;
139       esac
140     fi
141   fi
142   AC_SUBST($1)
143   popdef([CV])dnl
144 ])
145
146