Merge commit 'release-4-0-0alpha15' into master4-tmp
[nivanova/samba-autobuild/.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 do { \
46         const char *_strlcpy_base_src = (const char *)src; \
47         strlcpy((dest), _strlcpy_base_src? _strlcpy_base_src : "", (size)-PTR_DIFF((dest),(base))); \
48 } while (0)
49
50 /* String copy functions - macro hell below adds 'type checking' (limited,
51    but the best we can do in C) */
52
53 #define fstrcpy(d,s) \
54 do { \
55         const char *_fstrcpy_src = (const char *)(s); \
56         strlcpy((d),_fstrcpy_src ? _fstrcpy_src : "",sizeof(fstring)); \
57 } while (0)
58
59 #define fstrcat(d,s) \
60 do { \
61         const char *_fstrcat_src = (const char *)(s); \
62         strlcat((d),_fstrcat_src ? _fstrcat_src : "",sizeof(fstring)); \
63 } while (0)
64 #define nstrcpy(d,s) \
65 do { \
66         const char *_nstrcpy_src = (const char *)(s); \
67         strlcpy((d),_nstrcpy_src ? _nstrcpy_src : "",sizeof(fstring)); \
68 } while (0)
69 #define unstrcpy(d,s) \
70 do { \
71         const char *_unstrcpy_src = (const char *)(s); \
72         strlcpy((d),_unstrcpy_src ? _unstrcpy_src : "",sizeof(fstring)); \
73 } while (0)
74
75 #ifdef HAVE_COMPILER_WILL_OPTIMIZE_OUT_FNS
76
77 /* if the compiler will optimize out function calls, then use this to tell if we are
78    have the correct types (this works only where sizeof() returns the size of the buffer, not
79    the size of the pointer). */
80
81 #define push_string_check(dest, src, dest_len, flags) \
82     (CHECK_STRING_SIZE(dest, dest_len) \
83     ? __unsafe_string_function_usage_here_size_t__() \
84     : push_string_check_fn(dest, src, dest_len, flags))
85
86 #define clistr_push(cli, dest, src, dest_len, flags) \
87     (CHECK_STRING_SIZE(dest, dest_len) \
88     ? __unsafe_string_function_usage_here_size_t__() \
89     : clistr_push_fn(cli, dest, src, dest_len, flags))
90
91 #define clistr_pull(inbuf, dest, src, dest_len, srclen, flags) \
92     (CHECK_STRING_SIZE(dest, dest_len) \
93     ? __unsafe_string_function_usage_here_size_t__() \
94     : clistr_pull_fn(inbuf, dest, src, dest_len, srclen, flags))
95
96 #define srvstr_push(base_ptr, smb_flags2, dest, src, dest_len, flags) \
97     (CHECK_STRING_SIZE(dest, dest_len) \
98     ? __unsafe_string_function_usage_here_size_t__() \
99     : srvstr_push_fn(base_ptr, smb_flags2, dest, src, dest_len, flags))
100
101 /* This allows the developer to choose to check the arguments to
102    strlcpy.  if the compiler will optimize out function calls, then
103    use this to tell if we are have the correct size buffer (this works only
104    where sizeof() returns the size of the buffer, not the size of the
105    pointer), so stack and static variables only */
106
107 #define checked_strlcpy(dest, src, size) \
108     (sizeof(dest) != (size) \
109     ? __unsafe_string_function_usage_here_size_t__() \
110      : strlcpy(dest, src, size))
111
112 #else
113
114 #define push_string_check push_string_check_fn
115 #define clistr_push clistr_push_fn
116 #define clistr_pull clistr_pull_fn
117 #define srvstr_push srvstr_push_fn
118 #define checked_strlcpy strlcpy
119
120 #endif
121
122 #endif