source: trunk/server/source3/include/safe_string.h@ 590

Last change on this file since 590 was 414, checked in by Herwig Bauernfeind, 16 years ago

Samba 3.5.0: Initial import

File size: 7.8 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3 Safe string handling routines.
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Andrew Bartlett <[email protected]> 2003
6
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.
11
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.
16
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/>.
19*/
20
21#ifndef _SAFE_STRING_H
22#define _SAFE_STRING_H
23
24#ifndef _SPLINT_ /* http://www.splint.org */
25
26/* Some macros to ensure people don't use buffer overflow vulnerable string
27 functions. */
28
29#ifdef bcopy
30#undef bcopy
31#endif /* bcopy */
32#define bcopy(src,dest,size) __ERROR__XX__NEVER_USE_BCOPY___;
33
34#ifdef strcpy
35#undef strcpy
36#endif /* strcpy */
37#define strcpy(dest,src) __ERROR__XX__NEVER_USE_STRCPY___;
38
39#ifdef strcat
40#undef strcat
41#endif /* strcat */
42#define strcat(dest,src) __ERROR__XX__NEVER_USE_STRCAT___;
43
44#ifdef sprintf
45#undef sprintf
46#endif /* sprintf */
47#define sprintf __ERROR__XX__NEVER_USE_SPRINTF__;
48
49/*
50 * strcasecmp/strncasecmp aren't an error, but it means you're not thinking about
51 * multibyte. Don't use them. JRA.
52 */
53#ifdef strcasecmp
54#undef strcasecmp
55#endif
56#define strcasecmp __ERROR__XX__NEVER_USE_STRCASECMP__;
57
58#ifdef strncasecmp
59#undef strncasecmp
60#endif
61#define strncasecmp __ERROR__XX__NEVER_USE_STRNCASECMP__;
62
63#endif /* !_SPLINT_ */
64
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
72
73/* We need a number of different prototypes for our
74 non-existant fuctions */
75char * __unsafe_string_function_usage_here__(void);
76
77size_t __unsafe_string_function_usage_here_size_t__(void);
78
79size_t __unsafe_string_function_usage_here_char__(void);
80
81#ifdef HAVE_COMPILER_WILL_OPTIMIZE_OUT_FNS
82
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). */
86
87#define CHECK_STRING_SIZE(d, len) (sizeof(d) != (len) && sizeof(d) != sizeof(char *))
88
89#else /* HAVE_COMPILER_WILL_OPTIMIZE_OUT_FNS */
90
91#endif /* HAVE_COMPILER_WILL_OPTIMIZE_OUT_FNS */
92
93#define safe_strcpy_base(dest, src, base, size) \
94 safe_strcpy(dest, src, size-PTR_DIFF(dest,base)-1)
95
96/* String copy functions - macro hell below adds 'type checking' (limited,
97 but the best we can do in C) and may tag with function name/number to
98 record the last 'clobber region' on that string */
99