ldb: version 1.5.2
[Samba.git] / lib / util / string_wrappers.h
blob1feea8cbc071adc846ddce913787cc3f305831d1
1 /*
2 Unix SMB/CIFS implementation.
4 string wrappers, for checking string sizes
6 Copyright (C) Andrew Tridgell 1994-2011
7 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2003
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.
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.
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/>.
23 #ifndef _STRING_WRAPPERS_H
24 #define _STRING_WRAPPERS_H
26 #define strlcpy_base(dest, src, base, size) \
27 do { \
28 const char *_strlcpy_base_src = (const char *)src; \
29 strlcpy((dest), _strlcpy_base_src? _strlcpy_base_src : "", (size)-PTR_DIFF((dest),(base))); \
30 } while (0)
32 /* String copy functions - macro hell below adds 'type checking' (limited,
33 but the best we can do in C) */
35 #define fstrcpy(d,s) \
36 do { \
37 const char *_fstrcpy_src = (const char *)(s); \
38 strlcpy((d),_fstrcpy_src ? _fstrcpy_src : "",sizeof(fstring)); \
39 } while (0)
41 #define fstrcat(d,s) \
42 do { \
43 const char *_fstrcat_src = (const char *)(s); \
44 strlcat((d),_fstrcat_src ? _fstrcat_src : "",sizeof(fstring)); \
45 } while (0)
46 #define unstrcpy(d,s) \
47 do { \
48 const char *_unstrcpy_src = (const char *)(s); \
49 strlcpy((d),_unstrcpy_src ? _unstrcpy_src : "",sizeof(unstring)); \
50 } while (0)
52 #ifdef HAVE_COMPILER_WILL_OPTIMIZE_OUT_FNS
54 /* We need a number of different prototypes for our
55 non-existent fuctions */
56 char * __unsafe_string_function_usage_here__(void);
58 size_t __unsafe_string_function_usage_here_size_t__(void);
60 NTSTATUS __unsafe_string_function_usage_here_NTSTATUS__(void);
62 #define CHECK_STRING_SIZE(d, len) (sizeof(d) != (len) && sizeof(d) != sizeof(char *))
64 /* if the compiler will optimize out function calls, then use this to tell if we are
65 have the correct types (this works only where sizeof() returns the size of the buffer, not
66 the size of the pointer). */
68 #define push_string_check(dest, src, dest_len, flags) \
69 (CHECK_STRING_SIZE(dest, dest_len) \
70 ? __unsafe_string_function_usage_here_size_t__() \
71 : push_string_check_fn(dest, src, dest_len, flags))
73 #define srvstr_push(base_ptr, smb_flags2, dest, src, dest_len, flags, ret_len) \
74 (CHECK_STRING_SIZE(dest, dest_len) \
75 ? __unsafe_string_function_usage_here_NTSTATUS__() \
76 : srvstr_push_fn(base_ptr, smb_flags2, dest, src, dest_len, flags, ret_len))
78 /* This allows the developer to choose to check the arguments to
79 strlcpy. if the compiler will optimize out function calls, then
80 use this to tell if we are have the correct size buffer (this works only
81 where sizeof() returns the size of the buffer, not the size of the
82 pointer), so stack and static variables only */
84 #define checked_strlcpy(dest, src, size) \
85 (sizeof(dest) != (size) \
86 ? __unsafe_string_function_usage_here_size_t__() \
87 : strlcpy(dest, src, size))
89 #else
91 #define push_string_check push_string_check_fn
92 #define srvstr_push srvstr_push_fn
93 #define checked_strlcpy strlcpy
95 #endif
97 #endif