r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text
[Samba/bb.git] / source / include / safe_string.h
blob68be38df75a747b2f512955924a80e0951161a60
1 /*
2 Unix SMB/CIFS implementation.
3 Safe string handling routines.
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2003
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #ifndef _SAFE_STRING_H
22 #define _SAFE_STRING_H
24 #ifndef _SPLINT_ /* http://www.splint.org */
26 /* Some macros to ensure people don't use buffer overflow vulnerable string
27 functions. */
29 #ifdef bcopy
30 #undef bcopy
31 #endif /* bcopy */
32 #define bcopy(src,dest,size) __ERROR__XX__NEVER_USE_BCOPY___;
34 #ifdef strcpy
35 #undef strcpy
36 #endif /* strcpy */
37 #define strcpy(dest,src) __ERROR__XX__NEVER_USE_STRCPY___;
39 #ifdef strcat
40 #undef strcat
41 #endif /* strcat */
42 #define strcat(dest,src) __ERROR__XX__NEVER_USE_STRCAT___;
44 #ifdef sprintf
45 #undef sprintf
46 #endif /* sprintf */
47 #define sprintf __ERROR__XX__NEVER_USE_SPRINTF__;
50 * strcasecmp/strncasecmp aren't an error, but it means you're not thinking about
51 * multibyte. Don't use them. JRA.
53 #ifdef strcasecmp
54 #undef strcasecmp
55 #endif
56 #define strcasecmp __ERROR__XX__NEVER_USE_STRCASECMP__;
58 #ifdef strncasecmp
59 #undef strncasecmp
60 #endif
61 #define strncasecmp __ERROR__XX__NEVER_USE_STRNCASECMP__;
63 #endif /* !_SPLINT_ */
65 #ifdef DEVELOPER
66 #define SAFE_STRING_FUNCTION_NAME FUNCTION_MACRO
67 #define SAFE_STRING_LINE __LINE__
68 #else
69 #define SAFE_STRING_FUNCTION_NAME ("")
70 #define SAFE_STRING_LINE (0)
71 #endif
73 /* We need a number of different prototypes for our
74 non-existant fuctions */
75 char * __unsafe_string_function_usage_here__(void);
77 size_t __unsafe_string_function_usage_here_size_t__(void);
79 size_t __unsafe_string_function_usage_here_char__(void);
81 #ifdef HAVE_COMPILER_WILL_OPTIMIZE_OUT_FNS
83 /* if the compiler will optimize out function calls, then use this to tell if we are
84 have the correct types (this works only where sizeof() returns the size of the buffer, not
85 the size of the pointer). */
87 #define CHECK_STRING_SIZE(d, len) (sizeof(d) != (len) && sizeof(d) != sizeof(char *))
89 #define fstrterminate(d) (CHECK_STRING_SIZE(d, sizeof(fstring)) \
90 ? __unsafe_string_function_usage_here_char__() \
91 : (((d)[sizeof(fstring)-1]) = '\0'))
92 #define pstrterminate(d) (CHECK_STRING_SIZE(d, sizeof(pstring)) \
93 ? __unsafe_string_function_usage_here_char__() \
94 : (((d)[sizeof(pstring)-1]) = '\0'))
96 #define wpstrcpy(d,s) ((sizeof(d) != sizeof(wpstring) && sizeof(d) != sizeof(smb_ucs2_t *)) \
97 ? __unsafe_string_function_usage_here__() \
98 : safe_strcpy_w((d),(s),sizeof(wpstring)))
99 #define wpstrcat(d,s) ((sizeof(d) != sizeof(wpstring) && sizeof(d) != sizeof(smb_ucs2_t *)) \
100 ? __unsafe_string_function_usage_here__() \
101 : safe_strcat_w((d),(s),sizeof(wpstring)))
102 #define wfstrcpy(d,s) ((sizeof(d) != sizeof(wfstring) && sizeof(d) != sizeof(smb_ucs2_t *)) \
103 ? __unsafe_string_function_usage_here__() \
104 : safe_strcpy_w((d),(s),sizeof(wfstring)))
105 #define wfstrcat(d,s) ((sizeof(d) != sizeof(wfstring) && sizeof(d) != sizeof(smb_ucs2_t *)) \
106 ? __unsafe_string_function_usage_here__() \
107 : safe_strcat_w((d),(s),sizeof(wfstring)))
109 #define push_pstring_base(dest, src, pstring_base) \
110 (CHECK_STRING_SIZE(pstring_base, sizeof(pstring)) \
111 ? __unsafe_string_function_usage_here_size_t__() \
112 : push_ascii(dest, src, sizeof(pstring)-PTR_DIFF(dest,pstring_base)-1, STR_TERMINATE))
114 #else /* HAVE_COMPILER_WILL_OPTIMIZE_OUT_FNS */
116 #define fstrterminate(d) (((d)[sizeof(fstring)-1]) = '\0')
117 #define pstrterminate(d) (((d)[sizeof(pstring)-1]) = '\0')
119 #define wpstrcpy(d,s) safe_strcpy_w((d),(s),sizeof(wpstring))
120 #define wpstrcat(d,s) safe_strcat_w((d),(s),sizeof(wpstring))
121 #define wfstrcpy(d,s) safe_strcpy_w((d),(s),sizeof(wfstring))
122 #define wfstrcat(d,s) safe_strcat_w((d),(s),sizeof(wfstring))
124 #define push_pstring_base(dest, src, pstring_base) \
125 push_ascii(dest, src, sizeof(pstring)-PTR_DIFF(dest,pstring_base)-1, STR_TERMINATE)
127 #endif /* HAVE_COMPILER_WILL_OPTIMIZE_OUT_FNS */
129 #define safe_strcpy_base(dest, src, base, size) \
130 safe_strcpy(dest, src, size-PTR_DIFF(dest,base)-1)
132 /* String copy functions - macro hell below adds 'type checking' (limited,
133 but the best we can do in C) and may tag with function name/number to
134 record the last 'clobber region' on that string */
136 #define pstrcpy(d,s) safe_strcpy((d), (s),sizeof(pstring)-1)
137 #define pstrcat(d,s) safe_strcat((d), (s),sizeof(pstring)-1)
138 #define fstrcpy(d,s) safe_strcpy((d),(s),sizeof(fstring)-1)
139 #define fstrcat(d,s) safe_strcat((d),(s),sizeof(fstring)-1)
140 #define nstrcpy(d,s) safe_strcpy((d), (s),sizeof(nstring)-1)
141 #define unstrcpy(d,s) safe_strcpy((d), (s),sizeof(unstring)-1)
143 /* the addition of the DEVELOPER checks in safe_strcpy means we must
144 * update a lot of code. To make this a little easier here are some
145 * functions that provide the lengths with less pain */
146 #define pstrcpy_base(dest, src, pstring_base) \
147 safe_strcpy(dest, src, sizeof(pstring)-PTR_DIFF(dest,pstring_base)-1)
150 /* Inside the _fn variants of these is a call to clobber_region(), -
151 * which might destroy the stack on a buggy function. We help the
152 * debugging process by putting the function and line who last caused
153 * a clobbering into a static buffer. If the program crashes at
154 * address 0xf1f1f1f1 then this function is probably, but not
155 * necessarily, to blame. */
157 /* overmalloc_safe_strcpy: DEPRECATED! Used when you know the
158 * destination buffer is longer than maxlength, but you don't know how
159 * long. This is not a good situation, because we can't do the normal
160 * sanity checks. Don't use in new code! */
162 #define overmalloc_safe_strcpy(dest,src,maxlength) safe_strcpy_fn(SAFE_STRING_FUNCTION_NAME, SAFE_STRING_LINE,dest,src,maxlength)
163 #define safe_strcpy(dest,src,maxlength) safe_strcpy_fn2(SAFE_STRING_FUNCTION_NAME, SAFE_STRING_LINE,dest,src,maxlength)
164 #define safe_strcat(dest,src,maxlength) safe_strcat_fn2(SAFE_STRING_FUNCTION_NAME, SAFE_STRING_LINE,dest,src,maxlength)
165 #define push_string(base_ptr, dest, src, dest_len, flags) push_string_fn2(SAFE_STRING_FUNCTION_NAME, SAFE_STRING_LINE, base_ptr, dest, src, dest_len, flags)
166 #define pull_string(base_ptr, smb_flags2, dest, src, dest_len, src_len, flags) pull_string_fn2(SAFE_STRING_FUNCTION_NAME, SAFE_STRING_LINE, base_ptr, smb_flags2, dest, src, dest_len, src_len, flags)
167 #define clistr_push(cli, dest, src, dest_len, flags) clistr_push_fn2(SAFE_STRING_FUNCTION_NAME, SAFE_STRING_LINE, cli, dest, src, dest_len, flags)
168 #define clistr_pull(cli, dest, src, dest_len, src_len, flags) clistr_pull_fn2(SAFE_STRING_FUNCTION_NAME, SAFE_STRING_LINE, cli, dest, src, dest_len, src_len, flags)
169 #define srvstr_push(base_ptr, dest, src, dest_len, flags) srvstr_push_fn2(SAFE_STRING_FUNCTION_NAME, SAFE_STRING_LINE, base_ptr, dest, src, dest_len, flags)
171 #define alpha_strcpy(dest,src,other_safe_chars,maxlength) alpha_strcpy_fn(SAFE_STRING_FUNCTION_NAME,SAFE_STRING_LINE,dest,src,other_safe_chars,maxlength)
172 #define StrnCpy(dest,src,n) StrnCpy_fn(SAFE_STRING_FUNCTION_NAME,SAFE_STRING_LINE,dest,src,n)
174 #ifdef HAVE_COMPILER_WILL_OPTIMIZE_OUT_FNS
176 /* if the compiler will optimize out function calls, then use this to tell if we are
177 have the correct types (this works only where sizeof() returns the size of the buffer, not
178 the size of the pointer). */
180 #define safe_strcpy_fn2(fn_name, fn_line, d, s, max_len) \
181 (CHECK_STRING_SIZE(d, max_len+1) \
182 ? __unsafe_string_function_usage_here__() \
183 : safe_strcpy_fn(fn_name, fn_line, (d), (s), (max_len)))
185 #define safe_strcat_fn2(fn_name, fn_line, d, s, max_len) \
186 (CHECK_STRING_SIZE(d, max_len+1) \
187 ? __unsafe_string_function_usage_here__() \
188 : safe_strcat_fn(fn_name, fn_line, (d), (s), (max_len)))
190 #define push_string_fn2(fn_name, fn_line, base_ptr, dest, src, dest_len, flags) \
191 (CHECK_STRING_SIZE(dest, dest_len) \
192 ? __unsafe_string_function_usage_here_size_t__() \
193 : push_string_fn(fn_name, fn_line, base_ptr, dest, src, dest_len, flags))
195 #define pull_string_fn2(fn_name, fn_line, base_ptr, smb_flags2, dest, src, dest_len, src_len, flags) \
196 (CHECK_STRING_SIZE(dest, dest_len) \
197 ? __unsafe_string_function_usage_here_size_t__() \
198 : pull_string_fn(fn_name, fn_line, base_ptr, smb_flags2, dest, src, dest_len, src_len, flags))
200 #define clistr_push_fn2(fn_name, fn_line, cli, dest, src, dest_len, flags) \
201 (CHECK_STRING_SIZE(dest, dest_len) \
202 ? __unsafe_string_function_usage_here_size_t__() \
203 : clistr_push_fn(fn_name, fn_line, cli, dest, src, dest_len, flags))
205 #define clistr_pull_fn2(fn_name, fn_line, cli, dest, src, dest_len, srclen, flags) \
206 (CHECK_STRING_SIZE(dest, dest_len) \
207 ? __unsafe_string_function_usage_here_size_t__() \
208 : clistr_pull_fn(fn_name, fn_line, cli, dest, src, dest_len, srclen, flags))
210 #define srvstr_push_fn2(fn_name, fn_line, base_ptr, dest, src, dest_len, flags) \
211 (CHECK_STRING_SIZE(dest, dest_len) \
212 ? __unsafe_string_function_usage_here_size_t__() \
213 : srvstr_push_fn(fn_name, fn_line, base_ptr, dest, src, dest_len, flags))
215 #else
217 #define safe_strcpy_fn2 safe_strcpy_fn
218 #define safe_strcat_fn2 safe_strcat_fn
219 #define push_string_fn2 push_string_fn
220 #define pull_string_fn2 pull_string_fn
221 #define clistr_push_fn2 clistr_push_fn
222 #define clistr_pull_fn2 clistr_pull_fn
223 #define srvstr_push_fn2 srvstr_push_fn
225 #endif
227 #endif