|
2 | 2 | // Copyright (c) Microsoft Corporation. All rights reserved.
|
3 | 3 | // SPDX-License-Identifier: Apache-2.0
|
4 | 4 | //
|
| 5 | +#include "ctmacros.hpp" |
| 6 | + |
| 7 | +#include <stddef.h> |
| 8 | +#include <errno.h> |
| 9 | +#include <string.h> |
| 10 | +#ifndef _MSC_VER |
| 11 | +#include <stdint.h> |
| 12 | +#else |
| 13 | +#include <limits.h> |
| 14 | +#endif |
| 15 | + |
5 | 16 | #ifndef ANNEX_K_HPP
|
6 | 17 | #define ANNEX_K_HPP
|
7 | 18 |
|
|
18 | 29 |
|
19 | 30 | #endif
|
20 | 31 |
|
| 32 | +// restrict keyword needs c99 and above. |
| 33 | +#if __STDC_VERSION_ < 199901L |
| 34 | +# define restrict |
| 35 | +#endif |
| 36 | + |
| 37 | +#ifndef RSIZE_MAX |
| 38 | +#define RSIZE_MAX (SIZE_MAX >> 1) |
| 39 | +typedef size_t rsize_t; |
| 40 | +typedef int errno_t; |
| 41 | +#endif |
| 42 | + |
| 43 | +namespace MAT_NS_BEGIN |
| 44 | +{ |
| 45 | +class BoundCheckFunctions |
| 46 | +{ |
| 47 | +private: |
| 48 | +static bool oneds_buffer_region_overlap(const char *buffer1, size_t buffer1_len, const char *buffer2, size_t buffer2_len) |
| 49 | +{ |
| 50 | + if (buffer2 >= buffer1) |
| 51 | + { |
| 52 | + if (buffer1 + buffer1_len - 1 > buffer2 ) |
| 53 | + { |
| 54 | + return true; |
| 55 | + } |
| 56 | + } |
| 57 | + else |
| 58 | + { |
| 59 | + if (buffer2 + buffer2_len - 1 > buffer1) |
| 60 | + { |
| 61 | + return true; |
| 62 | + } |
| 63 | + } |
| 64 | + return false; |
| 65 | +} |
| 66 | + |
| 67 | +public: |
| 68 | +// prototype - https://en.cppreference.com/w/c/string/byte/strlen |
| 69 | +// Returns the length of the given null-terminated byte string |
| 70 | +// - returns zero if str is a null pointer |
| 71 | +// - returns strsz if the null character was not found in the first strsz bytes of str. |
| 72 | + |
| 73 | +static size_t oneds_strnlen_s(const char *str, size_t strsz) |
| 74 | +{ |
| 75 | + if ( str == NULL) |
| 76 | + { |
| 77 | + return 0; |
| 78 | + } |
| 79 | + return strnlen(str, strsz); |
| 80 | +} |
| 81 | + |
| 82 | +// prototype - https://en.cppreference.com/w/c/string/byte/strncpy (strcpy, strcpy_s) |
| 83 | +// Copies at most count characters of the character array pointed to by src |
| 84 | +//(including the terminating null character, but not any of the characters that follow |
| 85 | +// the null character) to character array pointed to by dest. |
| 86 | +// Handles error conditions at runtime - |
| 87 | +// - src or dest is a null pointer |
| 88 | +// - destsz is zero or greater than RSIZE_MAX |
| 89 | +// - count is greater than RSIZE_MAX |
| 90 | +// - count is greater or equal destsz, but destsz is less or equal strnlen_s(src, count), in other words, truncation would occur |
| 91 | +// - overlap would occur between the source and the destination strings |
| 92 | +static errno_t oneds_strncpy_s(char * restrict dest, rsize_t destsz, const char *restrict src, rsize_t count) |
| 93 | +{ |
| 94 | +#if (defined __STDC_LIB_EXT1__) || ( defined _MSC_VER) |
| 95 | + return strncpy_s(dest, destsz, src, count); |
| 96 | +#else |
| 97 | + if ((dest == NULL) || (destsz == 0) || (destsz > RSIZE_MAX)) |
| 98 | + { |
| 99 | + return EINVAL; |
| 100 | + } |
| 101 | + if (src == NULL) |
| 102 | + { |
| 103 | + dest = NULL; |
| 104 | + return EINVAL; |
| 105 | + } |
| 106 | + if (count > RSIZE_MAX){ |
| 107 | + dest = NULL; |
| 108 | + return EINVAL; |
| 109 | + } |
| 110 | + if (count >= destsz && destsz <= oneds_strnlen_s(src, count)) |
| 111 | + { |
| 112 | + dest = NULL; |
| 113 | + return EINVAL; |
| 114 | + } |
| 115 | + |
| 116 | + rsize_t src_len_to_read = oneds_strnlen_s(src, count) + 1 ; |
| 117 | + if (src_len_to_read < count) |
| 118 | + { |
| 119 | + src_len_to_read = count; |
| 120 | + } |
| 121 | + |
| 122 | + // donot allow overflow |
| 123 | + if (oneds_buffer_region_overlap(dest, destsz, src, src_len_to_read)) { |
| 124 | + dest = NULL; |
| 125 | + return EINVAL; |
| 126 | + } |
| 127 | + if (src_len_to_read < destsz) |
| 128 | + { |
| 129 | + src_len_to_read = destsz; |
| 130 | + } |
| 131 | + strncpy(dest, src, src_len_to_read); |
| 132 | + return 0; |
21 | 133 | #endif
|
| 134 | +} |
| 135 | + |
| 136 | +// prototype - https://en.cppreference.com/w/c/string/byte/memcpy |
| 137 | +// Copies count characters from the object pointed to by src to the |
| 138 | +// object pointed to by dest. Both objects are interpreted as arrays |
| 139 | +// of unsigned char. |
| 140 | +// |
| 141 | +// Handles error conditions at runtime - |
| 142 | +// - dest or src is a null pointer |
| 143 | +// - destsz or count is greater than RSIZE_MAX |
| 144 | +// - count is greater than destsz (buffer overflow would occur) |
| 145 | +// - the source and the destination objects overlap |
| 146 | +// |
| 147 | +// In case of error, the entire destination range [dest, dest+destsz) is zeroed out |
| 148 | +// (if both dest and destsz are valid)) |
22 | 149 |
|
| 150 | +static errno_t oneds_memcpy_s( void *restrict dest, rsize_t destsz, |
| 151 | + const void *restrict src, rsize_t count ) |
| 152 | +{ |
| 153 | +#if (defined __STDC_LIB_EXT1__) || ( defined _MSC_VER) |
| 154 | + return memcpy_s(dest, destsz, src, count); |
| 155 | +#else |
| 156 | + if (dest == NULL) |
| 157 | + { |
| 158 | + return EINVAL; |
| 159 | + } |
| 160 | + if (destsz > RSIZE_MAX) |
| 161 | + { |
| 162 | + return EINVAL; |
| 163 | + } |
| 164 | + if (src == NULL) |
| 165 | + { |
| 166 | + memset(dest, 0, destsz); |
| 167 | + return EINVAL; |
| 168 | + } |
| 169 | + if (count > RSIZE_MAX || count > destsz) |
| 170 | + { |
| 171 | + memset(dest, 0, destsz); |
| 172 | + return EINVAL; |
| 173 | + } |
| 174 | + // donot allow overflow |
| 175 | + if (oneds_buffer_region_overlap((char *)dest, destsz, (char *)src, count)) { |
| 176 | + memset(dest, 0, destsz); |
| 177 | + return EINVAL; |
| 178 | + } |
| 179 | + void *result = memcpy(dest, src, count); |
| 180 | + if (result == (void *)NULL) |
| 181 | + { |
| 182 | + return -1; |
| 183 | + } |
| 184 | + return 0; |
| 185 | +#endif |
| 186 | +} |
| 187 | +}; |
| 188 | +} |
| 189 | +MAT_NS_END |
| 190 | +#endif |
0 commit comments