4a5f51d96bc057b225fc665f001e6a88bdf18423
[samba.git] / lib / util / string_wrappers.h
1 /*
2    Unix SMB/CIFS implementation.
3
4    string wrappers, for checking string sizes
5
6    Copyright (C) Andrew Tridgell 1994-2011
7    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2003
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #ifndef _STRING_WRAPPERS_H
24 #define _STRING_WRAPPERS_H
25
26 /* We need a number of different prototypes for our
27    non-existant fuctions */
28 char * __unsafe_string_function_usage_here__(void);
29
30 size_t __unsafe_string_function_usage_here_size_t__(void);
31
32 #ifdef HAVE_COMPILER_WILL_OPTIMIZE_OUT_FNS
33
34 /* if the compiler will optimize out function calls, then use this to tell if we are
35    have the correct types (this works only where sizeof() returns the size of the buffer, not
36    the size of the pointer). */
37
38 #define CHECK_STRING_SIZE(d, len) (sizeof(d) != (len) && sizeof(d) != sizeof(char *))
39
40 #else /* HAVE_COMPILER_WILL_OPTIMIZE_OUT_FNS */
41
42 #endif /* HAVE_COMPILER_WILL_OPTIMIZE_OUT_FNS */
43
44 #define strlcpy_base(dest, src, base, size) \
45     strlcpy(dest, src, size-PTR_DIFF(dest,base))
46
47 /* String copy functions - macro hell below adds 'type checking' (limited,
48    but the best we can do in C) */
49
50 #define fstrcpy(d,s) strlcpy((d),(s) ? (s) : "",sizeof(fstring))
51 #define fstrcat(d,s) strlcpy((d),(s) ? (s) : "",sizeof(fstring))
52 #define nstrcpy(d,s) strlcpy((d), (s) ? (s) : "",sizeof(nstring))
53 #define unstrcpy(d,s) strlcpy((d), (s) ? (s) : "",sizeof(unstring))
54
55 /* the addition of the DEVELOPER checks in safe_strcpy means we must
56  * update a lot of code. To make this a little easier here are some
57  * functions that provide the lengths with less pain */
58
59 #ifdef HAVE_COMPILER_WILL_OPTIMIZE_OUT_FNS
60
61 /* if the compiler will optimize out function calls, then use this to tell if we are
62    have the correct types (this works only where sizeof() returns the size of the buffer, not
63    the size of the pointer). */
64
65 #define safe_strcpy(d, s, max_len) \
66     (CHECK_STRING_SIZE(d, max_len+1) \
67     ? __unsafe_string_function_usage_here__() \
68     : safe_strcpy_fn((d), (s), (max_len)))
69
70 #define safe_strcat(d, s, max_len) \
71     (CHECK_STRING_SIZE(d, max_len+1) \
72     ? __unsafe_string_function_usage_here__() \
73     : safe_strcat_fn((d), (s), (max_len)))
74
75 #define push_string_check(dest, src, dest_len, flags) \
76     (CHECK_STRING_SIZE(dest, dest_len) \
77     ? __unsafe_string_function_usage_here_size_t__() \
78     : push_string_check_fn(dest, src, dest_len, flags))
79
80 #define clistr_push(cli, dest, src, dest_len, flags) \
81     (CHECK_STRING_SIZE(dest, dest_len) \
82     ? __unsafe_string_function_usage_here_size_t__() \
83     : clistr_push_fn(cli, dest, src, dest_len, flags))
84
85 #define clistr_pull(inbuf, dest, src, dest_len, srclen, flags) \
86     (CHECK_STRING_SIZE(dest, dest_len) \
87     ? __unsafe_string_function_usage_here_size_t__() \
88     : clistr_pull_fn(inbuf, dest, src, dest_len, srclen, flags))
89
90 #define srvstr_push(base_ptr, smb_flags2, dest, src, dest_len, flags) \
91     (CHECK_STRING_SIZE(dest, dest_len) \
92     ? __unsafe_string_function_usage_here_size_t__() \
93     : srvstr_push_fn(base_ptr, smb_flags2, dest, src, dest_len, flags))
94
95 /* This allows the developer to choose to check the arguments to
96    strlcpy.  if the compiler will optimize out function calls, then
97    use this to tell if we are have the correct size buffer (this works only
98    where sizeof() returns the size of the buffer, not the size of the
99    pointer), so stack and static variables only */
100
101 #define checked_strlcpy(dest, src, size) \
102     (sizeof(dest) != (size) \
103     ? __unsafe_string_function_usage_here_size_t__() \
104      : strlcpy(dest, src, size))
105
106 #else
107
108 #define safe_strcpy safe_strcpy_fn
109 #define safe_strcat safe_strcat_fn
110 #define push_string_check push_string_check_fn
111 #define clistr_push clistr_push_fn
112 #define clistr_pull clistr_pull_fn
113 #define srvstr_push srvstr_push_fn
114 #define checked_strlcpy strlcpy
115
116 #endif
117
118 #endif