s4/test/getnc_exop: Tune the the test to work against windows
[samba4-gss/samba4-gss.git] / lib / util / string_wrappers.h
blob7baf3cb21b3557477127aaebe33f24cab0110bc9
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 /* We need a number of different prototypes for our
27 non-existant fuctions */
28 char * __unsafe_string_function_usage_here__(void);
30 size_t __unsafe_string_function_usage_here_size_t__(void);
32 #ifdef HAVE_COMPILER_WILL_OPTIMIZE_OUT_FNS
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). */
38 #define CHECK_STRING_SIZE(d, len) (sizeof(d) != (len) && sizeof(d) != sizeof(char *))
40 #else /* HAVE_COMPILER_WILL_OPTIMIZE_OUT_FNS */
42 #endif /* HAVE_COMPILER_WILL_OPTIMIZE_OUT_FNS */
44 #define strlcpy_base(dest, src, base, size) \
45 strlcpy((dest), (src) ? (src) : "", (size)-PTR_DIFF((dest),(base)))
47 /* String copy functions - macro hell below adds 'type checking' (limited,
48 but the best we can do in C) */
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))
55 #ifdef HAVE_COMPILER_WILL_OPTIMIZE_OUT_FNS
57 /* if the compiler will optimize out function calls, then use this to tell if we are
58 have the correct types (this works only where sizeof() returns the size of the buffer, not
59 the size of the pointer). */
61 #define push_string_check(dest, src, dest_len, flags) \
62 (CHECK_STRING_SIZE(dest, dest_len) \
63 ? __unsafe_string_function_usage_here_size_t__() \
64 : push_string_check_fn(dest, src, dest_len, flags))
66 #define clistr_push(cli, dest, src, dest_len, flags) \
67 (CHECK_STRING_SIZE(dest, dest_len) \
68 ? __unsafe_string_function_usage_here_size_t__() \
69 : clistr_push_fn(cli, dest, src, dest_len, flags))
71 #define clistr_pull(inbuf, dest, src, dest_len, srclen, flags) \
72 (CHECK_STRING_SIZE(dest, dest_len) \
73 ? __unsafe_string_function_usage_here_size_t__() \
74 : clistr_pull_fn(inbuf, dest, src, dest_len, srclen, flags))
76 #define srvstr_push(base_ptr, smb_flags2, dest, src, dest_len, flags) \
77 (CHECK_STRING_SIZE(dest, dest_len) \
78 ? __unsafe_string_function_usage_here_size_t__() \
79 : srvstr_push_fn(base_ptr, smb_flags2, dest, src, dest_len, flags))
81 /* This allows the developer to choose to check the arguments to
82 strlcpy. if the compiler will optimize out function calls, then
83 use this to tell if we are have the correct size buffer (this works only
84 where sizeof() returns the size of the buffer, not the size of the
85 pointer), so stack and static variables only */
87 #define checked_strlcpy(dest, src, size) \
88 (sizeof(dest) != (size) \
89 ? __unsafe_string_function_usage_here_size_t__() \
90 : strlcpy(dest, src, size))
92 #else
94 #define push_string_check push_string_check_fn
95 #define clistr_push clistr_push_fn
96 #define clistr_pull clistr_pull_fn
97 #define srvstr_push srvstr_push_fn
98 #define checked_strlcpy strlcpy
100 #endif
102 #endif