| 1 |
// ================================================================================================= |
| 2 |
// Copyright 2020 Adobe |
| 3 |
// All Rights Reserved. |
| 4 |
// NOTICE: Adobe permits you to use, modify, and distribute this file in |
| 5 |
// accordance with the terms of the Adobe license agreement accompanying |
| 6 |
// it. If you have received this file from a source other than Adobe, |
| 7 |
// then your use, modification, or distribution of it requires the prior |
| 8 |
// written permission of Adobe. |
| 9 |
// ================================================================================================= |
| 10 |
|
| 11 |
#if AdobePrivate |
| 12 |
// ================================================================================================= |
| 13 |
// Change history |
| 14 |
// ============== |
| 15 |
// |
| 16 |
// Writers: |
| 17 |
// SKP Sunil Kishor Pathak |
| 18 |
// |
| 19 |
// mm-dd-yy who Description of changes, most recent on top |
| 20 |
// |
| 21 |
// 07-01-13 SKP First draft. |
| 22 |
// |
| 23 |
// ================================================================================================= |
| 24 |
#endif // AdobePrivate |
| 25 |
|
| 26 |
//Safe String APIs Version 1.0 |
| 27 |
|
| 28 |
#ifndef _H_SafeAPIs |
| 29 |
#define _H_SafeAPIs |
| 30 |
|
| 31 |
#include <stddef.h> /* Include standard ANSI C stuff: size_t, NULL etc */ |
| 32 |
#include <stdarg.h> |
| 33 |
#include "SuppressSAL.h" |
| 34 |
#include "SafeTypes.h" |
| 35 |
|
| 36 |
/* |
| 37 |
OVERVIEW: |
| 38 |
-------- |
| 39 |
This file contains APIs that will map unsafe/banned string functions to their safe equivalents. |
| 40 |
The objective is to make minimal change at the actual call site. |
| 41 |
|
| 42 |
There are two sets of APIs in this file. |
| 43 |
APIs in first set should be invoked for dynamic array buffers, and pointers to buffers. |
| 44 |
APIs in second set should be invoked for static array buffers. |
| 45 |
|
| 46 |
Note: Any of the following functions which take more than one argument does not verify whether |
| 47 |
the buffers provided to them are not overlapping. It currently expects that they do not overlap. |
| 48 |
*/ |
| 49 |
|
| 50 |
#define ERROR_SAFE_INVALID_PARAM -1 //Error for invalid parameters passed to the APIs |
| 51 |
#define ERROR_SAFE_BUFFER_OVERFLOW -2 //Error for buffer overflow that could happen on completing the operation |
| 52 |
|
| 53 |
/* |
| 54 |
NOTE: |
| 55 |
Defining STATIC_SAFE_API macro prior to #include "SafeMemoryAPIs.h" shall make all |
| 56 |
the functions defined in SafeMemoryAPIs.h to be static. This macro is intended to |
| 57 |
be used ONLY in those .c or .cpp files which needs the definition of the SafeMemoryAPIs |
| 58 |
in the local scope. This macro will help avoiding the linker error due to the redefined |
| 59 |
symbols from SafeMemoryAPIs. |
| 60 |
If STATIC_SAFE_API is defined, the warnings for unreferenced local functions will be removed. |
| 61 |
*/ |
| 62 |
|
| 63 |
#ifdef STATIC_SAFE_API |
| 64 |
#if (defined(_WIN32) || defined(_WIN64)) |
| 65 |
#pragma warning( disable : 4505 ) |
| 66 |
#elif defined(__GNUC__) |
| 67 |
#pragma GCC system_header |
| 68 |
#endif |
| 69 |
#endif |
| 70 |
|
| 71 |
#ifdef STATIC_SAFE_API |
| 72 |
#define CONDITIONAL_STATIC static |
| 73 |
#else |
| 74 |
#define CONDITIONAL_STATIC |
| 75 |
#endif |
| 76 |
|
| 77 |
#if __cplusplus |
| 78 |
extern "C" { |
| 79 |
#endif |
| 80 |
|
| 81 |
//String functions |
| 82 |
|
| 83 |
/* strnlen_safe |
| 84 |
Safe version of strlen. To replace strlen. |
| 85 |
It returns the number of characters in the string (excluding the terminating null). |
| 86 |
If there is no null terminator within the first maxSize bytes of the string then maxSize is returned |
| 87 |
to indicate the error condition; null-terminated strings have lengths strictly less than maxSize. |
| 88 |
|
| 89 |
ex: |
| 90 |
strnlen_safe("abcd", 5) & strnlen_safe("abcd", 4) will return 4. |
| 91 |
|
| 92 |
Please use the following replacements for the other Windows-only strnlen APIS. |
| 93 |
Unsafe/Deprecated | New Safe API |
| 94 |
_mbslen | _mbsnlen_s |
| 95 |
wcslen | Don't use it. |
| 96 |
*/ |
| 97 |
_Check_return_ CONDITIONAL_STATIC size_t strnlen_safe(_In_z_count_(maxSize) const char *s, |
| 98 |
_In_ size_t maxSize); |
| 99 |
|
| 100 |
/* strcpy_safe |
| 101 |
Safe version of strcpy. This should be used in place of strcpy, _tcscpy or strcpy. |
| 102 |
size - The max number of characters (in bytes) that are to be copied into the destination including the null terminator. |
| 103 |
Ensures no buffer overflow and ensures null-terminated strings. |
| 104 |
|
| 105 |
RETURNS: Zero if all the characters including the null terminator was copied successfully, negative otherwise. |
| 106 |
|
| 107 |
Note, that the string will be truncated to fit into the buffer. |
| 108 |
|
| 109 |
ex: char buffer[5]; |
| 110 |
strcpy_safe(buffer, 5, "abcde"); -- will make buffer = "abcd" and return ERROR_SAFE_BUFFER_OVERFLOW |
| 111 |
|
| 112 |
strcpy_safe(buffer, 5, "abc"); -- safe. will execute normally. |
| 113 |
|
| 114 |
Please use the following replacements for the other Windows-only strcpy APIS. |
| 115 |
Unsafe/Deprecated | New Safe API |
| 116 |
lstrcpy | StringCchCopy |
| 117 |
lstrcpyA | StringCchCopyA |
| 118 |
lstrcpyW | StringCchCopyW |
| 119 |
_mbscpy | _mbscpy_s |
| 120 |
wcscpy | Don't use it. |
| 121 |
*/ |
| 122 |
_Check_return_wat_ CONDITIONAL_STATIC SafeInt32 strcpy_safe(_Out_z_cap_(size) char *dest, |
| 123 |
_In_ size_t size, |
| 124 |
_In_z_ const char *src); |
| 125 |
|
| 126 |
/* |
| 127 |
strncpy_safe - To replace strncpy, strncpy and strlcpy |
| 128 |
RETURNS: Zero if successful, negative if unsuccessful. |
| 129 |
A zero return value implies that all of the requested characters from the string pointed to by src fit |
| 130 |
within the array pointed to by dest and that the result in dest is null terminated. |
| 131 |
|
| 132 |
NOTE: n is the number of characters to be copied excluding the NULL terminator. |
| 133 |
|
| 134 |
Also, if the length of the source string is greater than the buffer size, the source string will be truncated to fit |
| 135 |
the buffer. |
| 136 |
|
| 137 |
ex: char buffer[5]; |
| 138 |
strncpy_safe(buffer, 5, "abcde", 5); -- will safely truncate the source string and null terminate buffer. |
| 139 |
The contents of buffer = "abcd". However, |
| 140 |
this will return ERROR_SAFE_BUFFER_OVERFLOW. |
| 141 |
|
| 142 |
strncpy_safe(buffer, 5, "abc", 3); -- contents of buffer = "abc" and return 0. |
| 143 |
*/ |
| 144 |
_Check_return_wat_ CONDITIONAL_STATIC SafeInt32 strncpy_safe(_Out_z_cap_(size) char *dest, |
| 145 |
_In_ size_t size, |
| 146 |
_In_z_ const char *src, |
| 147 |
_In_ size_t n); |
| 148 |
|
| 149 |
/*strcat_safe - Safe version of strcat. To replace strcat. |
| 150 |
Ensures there is no buffer overflow and that the resultant string is null-terminated. |
| 151 |
NOTE: The second argument is the total size of the buffer and not the remaining size |
| 152 |
s1 is the destination to which atmost s1max characters of s2 are appended. |
| 153 |
|
| 154 |
RETURNS: Zero if successful, negative if unsuccessful. |
| 155 |
A zero return value implies that all of the requested characters from the string pointed |
| 156 |
to by s2 were appended to the string pointed to by s1 and that the result in s1 is null terminated. |
| 157 |
ex: char a[10]; |
| 158 |
a= "abcde" and buffer = "abc" |
| 159 |
|
| 160 |
strcat_safe(a, 10, buffer); -- safe. a will now be "abcdeabc" |
| 161 |
|
| 162 |
strcat_safe(a, 10, buffer); -- buffer overflow detected. Truncated a = 'abcdeabca'. |
| 163 |
ERROR_SAFE_BUFFER_OVERFLOW will be returned. |
| 164 |
|
| 165 |
Please use the following replacements for the other Windows-only strcat APIS. |
| 166 |
Unsafe/Deprecated | New Safe API |
| 167 |
lstrcat | StringCchCat |
| 168 |
lstrcatA | StringCchCatA |
| 169 |
lstrcatW | StringCchCatW |
| 170 |
_mbscat | _mbscat_s |
| 171 |
wcscat | Don't use it. |
| 172 |
*/ |
| 173 |
_Check_return_wat_ CONDITIONAL_STATIC SafeInt32 strcat_safe( _Inout_z_cap_( s1max ) char *s1, |
| 174 |
_In_ size_t s1max, |
| 175 |
_In_z_ const char *s2); |
| 176 |
|
| 177 |
/*strncat_safe - Safe version of strncat. To replace strncat and strlcat. |
| 178 |
Ensures there is no buffer overflow and that the resultant string is null-terminated. |
| 179 |
NOTE: The second argument is the total size of the buffer and not the remaining size |
| 180 |
s1 is the destination to which the first n characters of of s2 are appended. |
| 181 |
RETURNS: Zero if successful, an error code if unsuccessful. |
| 182 |
A zero return value implies that all the requested characters from the string pointed to by |
| 183 |
s2 were appended to the string pointed to by s1 and that the result in s1 is null terminated. |
| 184 |
|
| 185 |
ex: a[10]. |
| 186 |
a = "abcde" and buf = "abc". |
| 187 |
strncat_safe(a, 10, buf, 5) -- will be successful since the length |
| 188 |
of the src buffer is only 3. The contents |
| 189 |
of a = "abcdeabc" |
| 190 |
But, strncat_safe(a, 10, "abcde", 5) --- will set <a> to null and return ERROR_SAFE_BUFFER_OVERFLOW. |
| 191 |
Again, with a = "abcde" |
| 192 |
strncat_safe(a, 10, "abcde", 3) -- will be safe. And a = "abcdeabc". |
| 193 |
|
| 194 |
Please use the following replacements for the other Windows-only strncat APIS. |
| 195 |
Unsafe/Deprecated | New Safe API |
| 196 |
lstrncat | StringCchCatN |
| 197 |
lstrcatnA | StringCchCatNA |
| 198 |
lstrcatnW | StringCchCatNW |
| 199 |
_mbsncat | _mbsncat_s |
| 200 |
wcsncat | Don't use it. |
| 201 |
*/ |
| 202 |
_Check_return_wat_ CONDITIONAL_STATIC SafeInt32 strncat_safe(_Inout_z_cap_(s1max) char *s1, |
| 203 |
_In_ size_t s1max, |
| 204 |
_In_z_ const char *s2, |
| 205 |
_In_ size_t n); |
| 206 |
|
| 207 |
_Check_return_ CONDITIONAL_STATIC char *strchr_safe(_In_z_count_(maxSize) const char *s, |
| 208 |
_In_ size_t maxSize, |
| 209 |
_In_ char c); |
| 210 |
|
| 211 |
/* strtok_safe - Safe version of strtok. To replace strtok. |
| 212 |
strToken: String containing token or tokens. |
| 213 |
maxSize: The max number of characters (in bytes) that can be present in strToken including the null terminator. |
| 214 |
strDelimit: Set of delimiter characters. |
| 215 |
context: Used to store position information between calls to strtok_safe |
| 216 |
|
| 217 |
Note: The caller function has to see that strToken passed at the first call |
| 218 |
should be NULL terminated, without which buffer overflow can occur |
| 219 |
|
| 220 |
Example: |
| 221 |
static char str1[] = "?a???b,,,#c"; |
| 222 |
static char str2[] = "\t \t"; |
| 223 |
char *t, *ptr1, *ptr2; |
| 224 |
t = strtok_safe(str1, sizeof(str1), "?", &ptr1); |
| 225 |
t = strtok_safe(NULL, sizeof(str2), ",", &ptr1); |
| 226 |
|
| 227 |
will print |
| 228 |
|
| 229 |
Please use the following replacements for the other Windows-only strtok APIS. |
| 230 |
Unsafe/Deprecated New Safe API |
| 231 |
_mbstok _mbstok_s |
| 232 |
wcstok Don't use it. |
| 233 |
*/ |
| 234 |
_Check_return_ CONDITIONAL_STATIC char * strtok_safe(_Inout_opt_z_cap_(maxSize) char* strToken, |
| 235 |
_In_ size_t maxSize, |
| 236 |
_In_z_ const char *strDelimit, |
| 237 |
_Inout_ _Deref_prepost_opt_z_ char **context); |
| 238 |
|
| 239 |
//- Variable argument functions |
| 240 |
/* vprintf_safe: Safe replacement for vprintf. However, for non-Windows platforms, |
| 241 |
it does not validate format string and hence it is strongly |
| 242 |
advised not to pass untrusted strings as the format argument.*/ |
| 243 |
|
| 244 |
_Check_return_opt_ CONDITIONAL_STATIC SafeInt32 vprintf_safe(_In_z_ _Printf_format_string_ const char* format, |
| 245 |
va_list argp); |
| 246 |
|
| 247 |
/* vsprintf: Safe replacement for vsprintf. Avoids buffer overrun and ensures buffer is null-terminated. |
| 248 |
However, for non-Windows platforms, it does not validate format string and hence it is strongly |
| 249 |
advised not to pass untrusted strings as the format argument.*/ |
| 250 |
CONDITIONAL_STATIC SafeInt32 vsprintf_safe(_Out_z_cap_(size) char* buffer, |
| 251 |
_In_ size_t size, |
| 252 |
_In_z_ _Printf_format_string_ const char* format, |
| 253 |
va_list argp); |
| 254 |
/* vsnprintf_safe */ |
| 255 |
_Check_return_opt_ CONDITIONAL_STATIC SafeInt32 vsnprintf_safe(_Out_z_cap_(size) char *buffer, |
| 256 |
_In_ size_t size, |
| 257 |
_In_ size_t count, |
| 258 |
_In_z_ _Printf_format_string_ const char* format, |
| 259 |
va_list argp); |
| 260 |
|
| 261 |
/* vwprintf_safe: Safe replacement for vwprintf. However, for non-Windows platforms, |
| 262 |
it does not validate format string and hence it is strongly |
| 263 |
advised not to pass untrusted strings as the format argument.*/ |
| 264 |
_Check_return_opt_ CONDITIONAL_STATIC SafeInt32 vwprintf_safe(_In_z_ _Printf_format_string_ const wchar_t *format, |
| 265 |
va_list argp); |
| 266 |
|
| 267 |
// printf functions |
| 268 |
|
| 269 |
/* sprintf_safe. Safe replacement of sprintf. Safer version of sprintf. |
| 270 |
Returns the number of characters written into the buffer including the null terminator, or an error code. |
| 271 |
NOTE: We are still not using a completely safe version of sprintf on non-Windows platforms. The format string is not |
| 272 |
validated here. Format string could be used maliciously. |
| 273 |
But, with the below implementation, we will prevent buffer overruns on all platforms and give us runtime |
| 274 |
checks on Win that we use sprintf correctly. |
| 275 |
|
| 276 |
*/ |
| 277 |
_Check_return_opt_ CONDITIONAL_STATIC SafeInt32 sprintf_safe(_Out_z_cap_(size) char *buffer, |
| 278 |
_In_ size_t size, |
| 279 |
_In_z_ _Printf_format_string_ const char *format, |
| 280 |
...); |
| 281 |
|
| 282 |
/* printf_safe - Safe version of printf on Windows. Replaces printf. Still UNSAFE on MAC and Unix. |
| 283 |
NOTE: This is UNSAFE to use in Non-Debug mode on any platform other than Windows. |
| 284 |
Please avoid using printf on these platforms or ensure that the format string is not user defined. */ |
| 285 |
_Check_return_opt_ CONDITIONAL_STATIC SafeInt32 printf_safe(_In_z_ _Printf_format_string_ const char *format, |
| 286 |
...); |
| 287 |
|
| 288 |
/* snprintf_safe: Safe replacement for snprintf. Prevents buffer overrun and ensures buffer is null-terminated. |
| 289 |
For non-Windows platforms, this can still be misused by passing an invalid format string. |
| 290 |
This routine does not validate format string for non-Windows platforms and hence it is strongly |
| 291 |
advised not to pass untrusted strings as the format argument.*/ |
| 292 |
_Check_return_opt_ CONDITIONAL_STATIC SafeInt32 snprintf_safe(_Out_z_cap_(size) char* buffer, |
| 293 |
_In_ size_t size, |
| 294 |
_In_ size_t count, |
| 295 |
_In_z_ _Printf_format_string_ const char *format, |
| 296 |
...); |
| 297 |
|
| 298 |
/* wprintf_safe - Safe version of wprintf on Windows. Still UNSAFE on MAC and Unix. |
| 299 |
NOTE: This is UNSAFE to be used in Non-Debug mode on any platform other than Windows. |
| 300 |
Please avoid using printf on these platforms or ensure that the format string is not user defined. */ |
| 301 |
_Check_return_opt_ CONDITIONAL_STATIC SafeInt32 wprintf_safe(_In_z_ _Printf_format_string_ const wchar_t *format, |
| 302 |
...); |
| 303 |
|
| 304 |
#if (defined(_WIN32) || defined(_WIN64)) && defined __STDC_WANT_SECURE_LIB__ |
| 305 |
|
| 306 |
//splitpath and makepath |
| 307 |
|
| 308 |
/* splitpath_safe - Safe replacement for _splitpath |
| 309 |
makepath_safe - Safe replacement for _makepath |
| 310 |
These are Windows only routines and are not applicable to Mac and unix platforms. |
| 311 |
RETURN: zero if successful, an error code on failure. |
| 312 |
If any of the buffers is too short to hold the result, these functions clear all the |
| 313 |
buffers to empty strings and return a non-zero error code. */ |
| 314 |
|
| 315 |
/* |
| 316 |
If any of drive, dir, fname, or ext is NULL the corresponding size parameter must be zero. |
| 317 |
See corresponding documentation in MSDN for _splitpath_s. |
| 318 |
*/ |
| 319 |
_Check_return_wat_ CONDITIONAL_STATIC SafeInt32 splitpath_safe(_In_z_ const char* path, |
| 320 |
_Out_opt_z_cap_(driveSize) char* drive, |
| 321 |
_In_ size_t driveSize, |
| 322 |
_Out_opt_z_cap_(dirSize) char* dir, |
| 323 |
_In_ size_t dirSize, |
| 324 |
_Out_opt_z_cap_(fnameSize) char* fname, |
| 325 |
_In_ size_t fnameSize, |
| 326 |
_Out_opt_z_cap_(extSize) char* ext, |
| 327 |
_In_ size_t extSize); |
| 328 |
|
| 329 |
_Check_return_wat_ CONDITIONAL_STATIC SafeInt32 makepath_safe(_Out_z_cap_(size) char* path, |
| 330 |
_In_ size_t size, |
| 331 |
_In_opt_z_ const char* drive, |
| 332 |
_In_opt_z_ const char* dir, |
| 333 |
_In_opt_z_ const char* fname, |
| 334 |
_In_opt_z_ const char* ext); |
| 335 |
|
| 336 |
#endif |
| 337 |
|
| 338 |
/* The following are safe replacements for numeric conversion APIs which are not ANSI C. |
| 339 |
size is the size of the buffer. It should be large enough to accommodate the resultant string. |
| 340 |
For radix 2, (sizeof(int)*8 + 1), i.e. one character for each bit + 1 Null character. |
| 341 |
|
| 342 |
_itoa_safe - Replacement for itoa and _itoa. |
| 343 |
Returns: Zero if succesful, an error code on failure.*/ |
| 344 |
_Check_return_opt_ CONDITIONAL_STATIC SafeInt32 _itoa_safe(_In_ int val, |
| 345 |
_Out_z_cap_(size) char* buf, |
| 346 |
_In_ size_t size, |
| 347 |
_In_ int radix); |
| 348 |
|
| 349 |
/* _ltoa_safe - Replacement for ltoa and _ltoa */ |
| 350 |
_Check_return_opt_ CONDITIONAL_STATIC SafeInt32 _ltoa_safe(_In_ long val, |
| 351 |
_Out_z_cap_(size) char *buf, |
| 352 |
_In_ size_t size, |
| 353 |
_In_ int radix); |
| 354 |
|
| 355 |
/* _ultoa_safe - Replacement for ultoa and _ultoa */ |
| 356 |
_Check_return_opt_ CONDITIONAL_STATIC SafeInt32 _ultoa_safe(_In_ unsigned long val, |
| 357 |
_Out_z_cap_(size) char *buf, |
| 358 |
_In_ size_t size, |
| 359 |
_In_ int radix); |
| 360 |
|
| 361 |
/* _i64toa_safe - Replacement for i64toa and _i64toa */ |
| 362 |
_Check_return_opt_ CONDITIONAL_STATIC SafeInt32 _i64toa_safe(_In_ SafeInt64 val, |
| 363 |
_Out_z_cap_(size) char* buf, |
| 364 |
_In_ size_t size, |
| 365 |
_In_ int radix); |
| 366 |
|
| 367 |
/* _ui64toa_safe - Replacement for ui64toa and _ui64toa */ |
| 368 |
_Check_return_opt_ CONDITIONAL_STATIC SafeInt32 _ui64toa_safe(_In_ SafeUns64 val, |
| 369 |
_Out_z_cap_(size) char* buf, |
| 370 |
_In_ size_t size, |
| 371 |
_In_ int radix); |
| 372 |
|
| 373 |
#if (defined(_WIN32) || defined(_WIN64)) && defined __STDC_WANT_SECURE_LIB__ |
| 374 |
/* Win APIs only. Preferable don't use them */ |
| 375 |
|
| 376 |
/* _itow_safe - Replacement for itow and _itow */ |
| 377 |
_Check_return_wat_ CONDITIONAL_STATIC SafeInt32 _itow_safe(_In_ int val, |
| 378 |
_Out_z_cap_(size) wchar_t *buf, |
| 379 |
_In_ size_t size, |
| 380 |
_In_ int radix); |
| 381 |
|
| 382 |
/* _i64tow_safe - Replacement for i64tow and _i64tow */ |
| 383 |
_Check_return_wat_ CONDITIONAL_STATIC SafeInt32 _i64tow_safe(_In_ SafeInt64 val, |
| 384 |
_Out_z_cap_(size) wchar_t *buf, |
| 385 |
_In_ size_t size, |
| 386 |
_In_ int radix); |
| 387 |
|
| 388 |
/* _ui64tow_safe - Replacement for ui64tow and _ui64tow */ |
| 389 |
_Check_return_wat_ CONDITIONAL_STATIC SafeInt32 _ui64tow_safe(_In_ SafeUns64 val, |
| 390 |
_Out_z_cap_(size) wchar_t *buf, |
| 391 |
_In_ size_t size, |
| 392 |
_In_ int radix); |
| 393 |
|
| 394 |
/* _ltow_safe - Replacement for ltow and _ltow */ |
| 395 |
_Check_return_wat_ CONDITIONAL_STATIC SafeInt32 _ltow_safe(_In_ long val, |
| 396 |
_Out_z_cap_(size) wchar_t *str, |
| 397 |
_In_ size_t size, |
| 398 |
_In_ int radix); |
| 399 |
|
| 400 |
/* _ultow_safe - Replacement for ultow and _ultow */ |
| 401 |
_Check_return_wat_ CONDITIONAL_STATIC SafeInt32 _ultow_safe(_In_ unsigned long val, |
| 402 |
_Out_z_cap_(size) wchar_t *str, |
| 403 |
_In_ size_t size, |
| 404 |
_In_ int radix); |
| 405 |
#endif |
| 406 |
|
| 407 |
/* gets_safe - to replace gets. |
| 408 |
|
| 409 |
example: char line[21]; // room for 20 chars + '\0' |
| 410 |
gets_safe( line, 21 ); |
| 411 |
|
| 412 |
If more than 20 chars are entered, this will set line to NULL. |
| 413 |
|
| 414 |
NOTE: This routine relies entirely on the user to provide the appropriate size. It has no way of finding out |
| 415 |
if the buffer is actually smaller than the provided size. |
| 416 |
Hence, please make sure you provide the right size. |
| 417 |
|
| 418 |
For eg: char line[5]; |
| 419 |
gets_safe(line, 6); |
| 420 |
|
| 421 |
will end up reading 6 characters from stdin and overwrite the buffer. |
| 422 |
*/ |
| 423 |
_Check_return_wat_ CONDITIONAL_STATIC char * gets_safe( _Out_z_cap_(size) char* buffer, |
| 424 |
_In_ size_t size); |
| 425 |
|
| 426 |
#if __cplusplus |
| 427 |
} |
| 428 |
#endif |
| 429 |
|
| 430 |
#if __cplusplus // This guard is needed to avoid the templates getting included into C files. |
| 431 |
|
| 432 |
/* |
| 433 |
Methods which will be called for static array buffers. |
| 434 |
Here we will invoke safe version of APIs. |
| 435 |
*/ |
| 436 |
|
| 437 |
/* |
| 438 |
char buffer based APIs |
| 439 |
*/ |
| 440 |
|
| 441 |
template<size_t _Size> |
| 442 |
_Check_return_ inline SafeUns32 strnlen_safe(const char (&s)[_Size]) |
| 443 |
{ |
| 444 |
return strnlen_safe(s, _Size); |
| 445 |
} |
| 446 |
|
| 447 |
template<size_t _Size> |
| 448 |
_Check_return_wat_ inline SafeInt32 strcpy_safe(char (&dest)[_Size], |
| 449 |
_In_z_ const char *src) |
| 450 |
{ |
| 451 |
return strcpy_safe(dest, _Size, src); |
| 452 |
} |
| 453 |
|
| 454 |
template<size_t _Size> |
| 455 |
_Check_return_wat_ inline SafeInt32 strncpy_safe(char (&dest)[_Size], |
| 456 |
_In_z_ const char *src, |
| 457 |
_In_ size_t n) |
| 458 |
{ |
| 459 |
return strncpy_safe(dest, _Size, src, n); |
| 460 |
} |
| 461 |
|
| 462 |
template<size_t _Size> |
| 463 |
_Check_return_wat_ inline SafeInt32 strcat_safe(char (&dest)[_Size], |
| 464 |
_In_z_ const char *s2) |
| 465 |
{ |
| 466 |
return strcat_safe(dest, _Size, s2); |
| 467 |
} |
| 468 |
|
| 469 |
template<size_t _Size> |
| 470 |
_Check_return_wat_ inline SafeInt32 strncat_safe(char (&dest)[_Size], |
| 471 |
_In_z_ const char *s2, |
| 472 |
_In_ size_t n) |
| 473 |
{ |
| 474 |
return strncat_safe(dest, _Size, s2, n); |
| 475 |
} |
| 476 |
|
| 477 |
template<size_t _Size> |
| 478 |
_Check_return_ inline char *strchr_safe(const char (&s)[_Size], |
| 479 |
_In_ char c) |
| 480 |
{ |
| 481 |
return strchr_safe(s, _Size, c); |
| 482 |
} |
| 483 |
|
| 484 |
template<size_t _Size> |
| 485 |
inline SafeInt32 vsprintf_safe(char (&buffer)[_Size], |
| 486 |
_In_z_ _Printf_format_string_ const char* format, |
| 487 |
va_list argp) |
| 488 |
{ |
| 489 |
return vsprintf_safe(buffer, _Size, format, argp); |
| 490 |
} |
| 491 |
|
| 492 |
template<size_t _Size> |
| 493 |
_Check_return_opt_ inline SafeInt32 vsnprintf_safe(char (&buffer)[_Size], |
| 494 |
_In_ size_t count, |
| 495 |
_In_z_ _Printf_format_string_ const char* format, |
| 496 |
...) |
| 497 |
{ |
| 498 |
va_list argp; |
| 499 |
va_start(argp, format); |
| 500 |
SafeInt32 numBytes = vsnprintf_safe(buffer, _Size, count, format, argp); |
| 501 |
va_end(argp); |
| 502 |
return numBytes; |
| 503 |
} |
| 504 |
|
| 505 |
template<size_t _Size> |
| 506 |
inline SafeInt32 sprintf_safe(char (&buffer)[_Size], |
| 507 |
_In_z_ _Printf_format_string_ const char *format, |
| 508 |
...) |
| 509 |
{ |
| 510 |
va_list argp; |
| 511 |
va_start(argp, format); |
| 512 |
SafeInt32 numBytes = vsprintf_safe(buffer, _Size, format, argp); |
| 513 |
va_end(argp); |
| 514 |
return numBytes; |
| 515 |
} |
| 516 |
|
| 517 |
template<size_t _Size> |
| 518 |
_Check_return_opt_ inline SafeInt32 snprintf_safe(char (&buffer)[_Size], |
| 519 |
_In_ size_t count, |
| 520 |
_In_z_ _Printf_format_string_ const char *format, |
| 521 |
...) |
| 522 |
{ |
| 523 |
va_list argp; |
| 524 |
va_start(argp, format); |
| 525 |
SafeInt32 numBytes = vsnprintf_safe(buffer, _Size, count, format, argp); |
| 526 |
va_end(argp); |
| 527 |
return numBytes; |
| 528 |
} |
| 529 |
|
| 530 |
#if (defined(_WIN32) || defined(_WIN64)) && defined __STDC_WANT_SECURE_LIB__ |
| 531 |
|
| 532 |
template<size_t _DriveSize, size_t _DirSize, size_t _FNameSize, size_t _ExtSize> |
| 533 |
_Check_return_wat_ inline SafeInt32 splitpath_safe(_In_z_ const char* path, |
| 534 |
char (&drive)[_DriveSize], |
| 535 |
char (&dir)[_DirSize], |
| 536 |
char (&fname)[_FNameSize], |
| 537 |
char (&ext)[_ExtSize]) |
| 538 |
|
| 539 |
{ |
| 540 |
return splitpath_safe(path, drive, _DriveSize, dir, _DirSize, fname, _FNameSize, ext, _ExtSize); |
| 541 |
} |
| 542 |
|
| 543 |
template<size_t _Size> |
| 544 |
_Check_return_wat_ inline SafeInt32 makepath_safe(char (&path)[_Size], |
| 545 |
_In_opt_z_ const char* drive, |
| 546 |
_In_opt_z_ const char* dir, |
| 547 |
_In_opt_z_ const char* fname, |
| 548 |
_In_opt_z_ const char* ext) |
| 549 |
{ |
| 550 |
return makepath_safe(path, _Size, drive, dir, fname, ext); |
| 551 |
} |
| 552 |
|
| 553 |
#endif |
| 554 |
|
| 555 |
template<size_t _Size> |
| 556 |
_Check_return_opt_ inline SafeInt32 _itoa_safe(_In_ int val, |
| 557 |
char (&buf)[_Size], |
| 558 |
_In_ int radix) |
| 559 |
{ |
| 560 |
return _itoa_safe(val, buf, _Size, radix); |
| 561 |
} |
| 562 |
|
| 563 |
template<size_t _Size> |
| 564 |
_Check_return_opt_ inline SafeInt32 _ltoa_safe(_In_ long val, |
| 565 |
char (&buf)[_Size], |
| 566 |
_In_ int radix) |
| 567 |
{ |
| 568 |
return _ltoa_safe(val, buf, _Size, radix); |
| 569 |
} |
| 570 |
|
| 571 |
template<size_t _Size> |
| 572 |
_Check_return_opt_ inline SafeInt32 _ultoa_safe(_In_ unsigned long val, |
| 573 |
char (&buf)[_Size], |
| 574 |
_In_ int radix) |
| 575 |
{ |
| 576 |
return _ultoa_safe(val, buf, _Size, radix); |
| 577 |
} |
| 578 |
|
| 579 |
template<size_t _Size> |
| 580 |
_Check_return_opt_ inline SafeInt32 _i64toa_safe(_In_ SafeInt64 val, |
| 581 |
char (&buf)[_Size], |
| 582 |
_In_ int radix) |
| 583 |
{ |
| 584 |
return _i64toa_safe(val, buf, _Size, radix); |
| 585 |
} |
| 586 |
|
| 587 |
template<size_t _Size> |
| 588 |
_Check_return_opt_ inline SafeInt32 _ui64toa_safe(_In_ SafeUns64 val, |
| 589 |
char (&buf)[_Size], |
| 590 |
_In_ int radix) |
| 591 |
{ |
| 592 |
return _ui64toa_safe(val, buf, _Size, radix); |
| 593 |
} |
| 594 |
|
| 595 |
template<size_t _Size> |
| 596 |
inline char * gets_safe( char (&buffer)[_Size]) |
| 597 |
{ |
| 598 |
return gets_safe(buffer, _Size); |
| 599 |
} |
| 600 |
|
| 601 |
/* |
| 602 |
for wchar_t based buffers |
| 603 |
*/ |
| 604 |
#if (defined(_WIN32) || defined(_WIN64)) && defined __STDC_WANT_SECURE_LIB__ |
| 605 |
|
| 606 |
template<size_t _Size> |
| 607 |
_Check_return_wat_ inline SafeInt32 _itow_safe(_In_ int val, |
| 608 |
wchar_t (&buf)[_Size], |
| 609 |
_In_ int radix) |
| 610 |
{ |
| 611 |
return _itow_safe(val, buf, _Size, radix); |
| 612 |
} |
| 613 |
|
| 614 |
template<size_t _Size> |
| 615 |
_Check_return_wat_ inline SafeInt32 _i64tow_safe(_In_ SafeInt64 val, |
| 616 |
wchar_t (&buf)[_Size], |
| 617 |
_In_ int radix) |
| 618 |
{ |
| 619 |
return _i64tow_safe(val, buf, _Size, radix); |
| 620 |
} |
| 621 |
|
| 622 |
template<size_t _Size> |
| 623 |
_Check_return_wat_ inline SafeInt32 _ui64tow_safe(_In_ SafeUns64 val, |
| 624 |
wchar_t (&buf)[_Size], |
| 625 |
_In_ int radix) |
| 626 |
{ |
| 627 |
return _ui64tow_safe(val, buf, _Size, radix); |
| 628 |
} |
| 629 |
|
| 630 |
template<size_t _Size> |
| 631 |
_Check_return_wat_ inline SafeInt32 _ltow_safe(_In_ long val, |
| 632 |
wchar_t (&str)[_Size], |
| 633 |
_In_ int radix) |
| 634 |
{ |
| 635 |
return _ltow_safe(val, str, _Size, radix); |
| 636 |
} |
| 637 |
|
| 638 |
template<size_t _Size> |
| 639 |
_Check_return_wat_ inline SafeInt32 _ultow_safe(_In_ unsigned long val, |
| 640 |
wchar_t (&str)[_Size], |
| 641 |
_In_ int radix) |
| 642 |
{ |
| 643 |
return _ultow_safe(val, str, _Size, radix); |
| 644 |
} |
| 645 |
|
| 646 |
#endif |
| 647 |
|
| 648 |
#endif // __cplusplus |
| 649 |
|
| 650 |
/* |
| 651 |
The definition of SafeStringAPIs functions are included if STATIC_SAFE_API is defined. |
| 652 |
*/ |
| 653 |
#ifdef STATIC_SAFE_API |
| 654 |
#include "SafeStringAPIs.cpp" |
| 655 |
#endif |
| 656 |
|
| 657 |
#endif //End _H_SafeAPIs |