Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #ifndef _CSF_COMMON_E58E5677_950A_424c_B6C2_CA180092E6A2_H |
michael@0 | 6 | #define _CSF_COMMON_E58E5677_950A_424c_B6C2_CA180092E6A2_H |
michael@0 | 7 | |
michael@0 | 8 | #include <assert.h> |
michael@0 | 9 | #include <memory> |
michael@0 | 10 | #include <vector> |
michael@0 | 11 | #include <stdlib.h> |
michael@0 | 12 | |
michael@0 | 13 | /* |
michael@0 | 14 | |
michael@0 | 15 | This header file defines: |
michael@0 | 16 | |
michael@0 | 17 | csf_countof |
michael@0 | 18 | csf_sprintf |
michael@0 | 19 | csf_vsprintf |
michael@0 | 20 | |
michael@0 | 21 | */ |
michael@0 | 22 | |
michael@0 | 23 | /* |
michael@0 | 24 | General security tip: Ensure that "format" is never a user-defined string. Format should ALWAYS be something that's built into your code, not |
michael@0 | 25 | user supplied. For example: never write: |
michael@0 | 26 | |
michael@0 | 27 | csf_sprintf(buffer, csf_countof(buffer), pUserSuppliedString); |
michael@0 | 28 | |
michael@0 | 29 | Instead write: |
michael@0 | 30 | |
michael@0 | 31 | csf_sprintf(buffer, csf_countof(buffer), "%s", pUserSuppliedString); |
michael@0 | 32 | |
michael@0 | 33 | */ |
michael@0 | 34 | |
michael@0 | 35 | #ifdef WIN32 |
michael@0 | 36 | #if !defined(_countof) |
michael@0 | 37 | #if !defined(__cplusplus) |
michael@0 | 38 | #define _countof(_Array) (sizeof(_Array) / sizeof(_Array[0])) |
michael@0 | 39 | #else |
michael@0 | 40 | extern "C++" |
michael@0 | 41 | { |
michael@0 | 42 | template <typename _CountofType, size_t _SizeOfArray> |
michael@0 | 43 | char (*_csf_countof_helper(_CountofType (&_Array)[_SizeOfArray]))[_SizeOfArray]; |
michael@0 | 44 | #define _countof(_Array) sizeof(*_csf_countof_helper(_Array)) |
michael@0 | 45 | } |
michael@0 | 46 | #endif |
michael@0 | 47 | #endif |
michael@0 | 48 | #else |
michael@0 | 49 | #define _countof(_Array) (sizeof(_Array) / sizeof(_Array[0])) |
michael@0 | 50 | #endif |
michael@0 | 51 | //csf_countof |
michael@0 | 52 | |
michael@0 | 53 | #define csf_countof(anArray) _countof(anArray) |
michael@0 | 54 | |
michael@0 | 55 | //csf_sprintf |
michael@0 | 56 | |
michael@0 | 57 | #ifdef _WIN32 |
michael@0 | 58 | //Unlike snprintf, sprintf_s guarantees that the buffer will be null-terminated (unless the buffer size is zero). |
michael@0 | 59 | #define csf_sprintf(/* char* */ buffer, /* size_t */ sizeOfBufferInCharsInclNullTerm, /* const char * */ format, ...)\ |
michael@0 | 60 | _snprintf_s (buffer, sizeOfBufferInCharsInclNullTerm, _TRUNCATE, format, __VA_ARGS__) |
michael@0 | 61 | #else |
michael@0 | 62 | #define csf_sprintf(/* char */ buffer, /* size_t */ sizeOfBufferInCharsInclNullTerm, /* const char * */ format, ...)\ |
michael@0 | 63 | snprintf (buffer, sizeOfBufferInCharsInclNullTerm, format, __VA_ARGS__);\ |
michael@0 | 64 | buffer[sizeOfBufferInCharsInclNullTerm-1] = '\0' |
michael@0 | 65 | #endif |
michael@0 | 66 | |
michael@0 | 67 | //csf_vsprintf |
michael@0 | 68 | |
michael@0 | 69 | #ifdef _WIN32 |
michael@0 | 70 | #define csf_vsprintf(/* char* */ buffer, /* size_t */ sizeOfBufferInCharsInclNullTerm, /* const char * */ format, /* va_list */ vaList)\ |
michael@0 | 71 | vsnprintf_s (buffer, sizeOfBufferInCharsInclNullTerm, _TRUNCATE, format, vaList);\ |
michael@0 | 72 | buffer[sizeOfBufferInCharsInclNullTerm-1] = '\0' |
michael@0 | 73 | #else |
michael@0 | 74 | #define csf_vsprintf(/* char */ buffer, /* size_t */ sizeOfBufferInCharsInclNullTerm, /* const char * */ format, /* va_list */ vaList)\ |
michael@0 | 75 | vsprintf (buffer, format, vaList);\ |
michael@0 | 76 | buffer[sizeOfBufferInCharsInclNullTerm-1] = '\0' |
michael@0 | 77 | #endif |
michael@0 | 78 | |
michael@0 | 79 | #endif |