1 // 2 // corecrt_memcpy_s.h 3 // 4 // Copyright (c) Microsoft Corporation. All rights reserved. 5 // 6 // Inline definitions of memcpy_s and memmove_s 7 // 8 #pragma once 9 10 #include <corecrt.h> 11 #include <errno.h> 12 #include <vcruntime_string.h> 13 14 #pragma warning(push) 15 #pragma warning(disable: _UCRT_DISABLED_WARNINGS) 16 _UCRT_DISABLE_CLANG_WARNINGS 17 18 _CRT_BEGIN_C_HEADER 19 20 #ifndef _CRT_MEMCPY_S_INLINE 21 #define _CRT_MEMCPY_S_INLINE static __inline 22 #endif 23 24 #define _CRT_MEMCPY_S_VALIDATE_RETURN_ERRCODE(expr, errorcode) \ 25 { \ 26 int _Expr_val=!!(expr); \ 27 if (!(_Expr_val)) \ 28 { \ 29 errno = errorcode; \ 30 _invalid_parameter_noinfo(); \ 31 return errorcode; \ 32 } \ 33 } 34 35 #if !defined RC_INVOKED && !defined __midl && __STDC_WANT_SECURE_LIB__ 36 37 _Success_(return == 0) 38 _Check_return_opt_ 39 _CRT_MEMCPY_S_INLINE errno_t __CRTDECL memcpy_s( 40 _Out_writes_bytes_to_opt_(_DestinationSize, _SourceSize) void* const _Destination, 41 _In_ rsize_t const _DestinationSize, 42 _In_reads_bytes_opt_(_SourceSize) void const* const _Source, 43 _In_ rsize_t const _SourceSize 44 ) 45 { 46 if (_SourceSize == 0) 47 { 48 return 0; 49 } 50 51 _CRT_MEMCPY_S_VALIDATE_RETURN_ERRCODE(_Destination != NULL, EINVAL); 52 if (_Source == NULL || _DestinationSize < _SourceSize) 53 { 54 memset(_Destination, 0, _DestinationSize); 55 56 _CRT_MEMCPY_S_VALIDATE_RETURN_ERRCODE(_Source != NULL, EINVAL); 57 _CRT_MEMCPY_S_VALIDATE_RETURN_ERRCODE(_DestinationSize >= _SourceSize, ERANGE); 58 59 // Unreachable, but required to suppress /analyze warnings: 60 return EINVAL; 61 } 62 memcpy(_Destination, _Source, _SourceSize); 63 return 0; 64 } 65 66 _Check_return_wat_ 67 _CRT_MEMCPY_S_INLINE errno_t __CRTDECL memmove_s( 68 _Out_writes_bytes_to_opt_(_DestinationSize, _SourceSize) void* const _Destination, 69 _In_ rsize_t const _DestinationSize, 70 _In_reads_bytes_opt_(_SourceSize) void const* const _Source, 71 _In_ rsize_t const _SourceSize 72 ) 73 { 74 if (_SourceSize == 0) 75 { 76 return 0; 77 } 78 79 _CRT_MEMCPY_S_VALIDATE_RETURN_ERRCODE(_Destination != NULL, EINVAL); 80 _CRT_MEMCPY_S_VALIDATE_RETURN_ERRCODE(_Source != NULL, EINVAL); 81 _CRT_MEMCPY_S_VALIDATE_RETURN_ERRCODE(_DestinationSize >= _SourceSize, ERANGE); 82 83 memmove(_Destination, _Source, _SourceSize); 84 return 0; 85 } 86 87 #endif 88 89 #undef _CRT_MEMCPY_S_VALIDATE_RETURN_ERRCODE 90 91 _UCRT_RESTORE_CLANG_WARNINGS 92 #pragma warning(pop) // _UCRT_DISABLED_WARNINGS 93 _CRT_END_C_HEADER 94