FS2_Open
Open source remastering of the Freespace 2 engine
safe_strings.h
Go to the documentation of this file.
1 #ifndef _SAFE_STRINGS_H_INCLUDED_
2 #define _SAFE_STRINGS_H_INCLUDED_
3 
4 /* It is a condition of use that safe_strings.cpp, safe_strings.h, safe_strings_test.cpp remain together.
5  *
6  * Maintained by portej05 - contact via PM on www.hard-light.net/forums
7  * Why have we got this, what is it for?
8  * VC2005+ define some safe string functions which check buffer sizes before doing anything
9  * Unfortunately, GCC and MACOS do not provide these functions, therefore, we must!
10  * (if only to reduce the amount of noise the static analysis tools are spitting out)
11  * They are part of ISO/IEC TR 24731 and may find their way into the CRTs at some point, at which
12  * point these functions must be removed from the engine.
13  * While these functions do not add a huge amount of benefit for heap-allocated strings, they
14  * can protect against a class of buffer overruns in stack allocated situations.
15  *
16  */
17 
18 /* Include logic:
19  * Used unless we have VC6
20  */
21 
22 /* errno_t, EINVAL, ERANGE, etc.. */
23 #include <errno.h>
24 #include <stdlib.h> /* size_t */
25 
26 /* Because errno_t is not (yet) standard, we define it here like this */
27 typedef int errno_t;
28 
29 #if ( !defined( _MSC_VER ) && !defined(NO_SAFE_STRINGS) ) || defined( _MSC_VER ) && _MSC_VER >= 1400 /*&& !defined(NDEBUG)*/ && !defined(NO_SAFE_STRINGS)
30 
31 /* In order to compile safe_strings_test.cpp, you must have this defined on the command line */
32 /* #define SAFESTRINGS_TEST_APP */
33 
34 /* Unlike their CRT counterparts, these do not call the invalid parameter handler
35  * However, they do call this macro
36  */
37 #ifndef SAFESTRINGS_TEST_APP
38 
39 # ifndef __safe_strings_error_handler
40 # define __safe_strings_error_handler( val ) Error(file, line,"%s: String error. Please Report.\nTrying to put into " SIZE_T_ARG " byte buffer:\n%s", #val, sizeInBytes,strSource)
41 # endif
42 
43 #else
44 
45 /* For testing only */
46 # define __safe_strings_error_handler( errnoVal ) extern void error_handler( int errnoValue, const char* errnoStr, const char* file, const char* function, int line );\
47  error_handler( errnoVal, #errnoVal, __FILE__, __FUNCTION__, __LINE__ );
48 #endif
49 
50 extern errno_t scp_strcpy_s( const char* file, int line, char* strDest, size_t sizeInBytes, const char* strSource );
51 extern errno_t scp_strcat_s( const char* file, int line, char* strDest, size_t sizeInBytes, const char* strSource );
52 
53 template< size_t size>
54 inline
55 errno_t scp_strcpy_s( const char* file, int line, char (&strDest)[ size ], const char* strSource )
56 {
57  return scp_strcpy_s( file, line, strDest, size, strSource );
58 }
59 
60 template< size_t size >
61 inline
62 errno_t scp_strcat_s( const char* file, int line, char (&strDest)[ size ], const char* strSource )
63 {
64  return scp_strcat_s( file, line, strDest, size, strSource );
65 }
66 
67 #define strcpy_s( ... ) scp_strcpy_s( __FILE__, __LINE__, __VA_ARGS__ )
68 #define strcat_s( ... ) scp_strcat_s( __FILE__, __LINE__, __VA_ARGS__ )
69 
70 #elif defined(_MSC_VER) && _MSC_VER < 1400 || defined(NO_SAFE_STRINGS)
71 
72 #pragma message("safe_strings disabled - this is not good!")
73 
74 inline errno_t strcpy_s( char* strDest, size_t sizeInBytes, const char* strSource )
75 {
76  strcpy( strDest, strSource );
77  return 0;
78 }
79 
80 inline errno_t strcat_s( char* strDest, size_t sizeInBytes, const char* strSource )
81 {
82  strcat( strDest, strSource );
83  return 0;
84 }
85 
86 inline errno_t strcpy_s( char* strDest, const char* strSource )
87 {
88  strcpy( strDest, strSource );
89  return 0;
90 }
91 
92 inline errno_t strcat_s( char* strDest, const char* strSource )
93 {
94  strcat( strDest, strSource );
95  return 0;
96 }
97 
98 #endif
99 
100 #endif // _SAFE_STRINGS_H_INCLUDED_
GLsizeiptr size
Definition: Glext.h:5496
errno_t scp_strcat_s(const char *file, int line, char *strDest, size_t sizeInBytes, const char *strSource)
#define strcat_s(...)
Definition: safe_strings.h:68
int errno_t
Definition: safe_strings.h:27
errno_t scp_strcpy_s(const char *file, int line, char *strDest, size_t sizeInBytes, const char *strSource)
#define strcpy_s(...)
Definition: safe_strings.h:67