| 1 | /*------------------------------------------------------------------ |
| 2 | * strcpyfldin_s.c |
| 3 | * |
| 4 | * November 2008, Bo Berry |
| 5 | * |
| 6 | * Copyright (c) 2008-2011 by Cisco Systems, Inc |
| 7 | * All rights reserved. |
| 8 | * |
| 9 | * Permission is hereby granted, free of charge, to any person |
| 10 | * obtaining a copy of this software and associated documentation |
| 11 | * files (the "Software"), to deal in the Software without |
| 12 | * restriction, including without limitation the rights to use, |
| 13 | * copy, modify, merge, publish, distribute, sublicense, and/or |
| 14 | * sell copies of the Software, and to permit persons to whom the |
| 15 | * Software is furnished to do so, subject to the following |
| 16 | * conditions: |
| 17 | * |
| 18 | * The above copyright notice and this permission notice shall be |
| 19 | * included in all copies or substantial portions of the Software. |
| 20 | * |
| 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 22 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 23 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 24 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 25 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 26 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 27 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 28 | * OTHER DEALINGS IN THE SOFTWARE. |
| 29 | *------------------------------------------------------------------ |
| 30 | */ |
| 31 | |
| 32 | #include "safeclib_private.h" |
| 33 | #include "safe_str_constraint.h" |
| 34 | #include "safe_str_lib.h" |
| 35 | |
| 36 | |
| 37 | /** |
| 38 | * NAME |
| 39 | * strcpyfldin_s |
| 40 | * |
| 41 | * SYNOPSIS |
| 42 | * #include "safe_str_lib.h" |
| 43 | * errno_t |
| 44 | * strcpyfldin_s(char *dest, rsize_t dmax, |
| 45 | * const char *src, rsize_t slen) |
| 46 | * |
| 47 | * DESCRIPTION |
| 48 | * The strcpyfldin_s function copies at most slen characters from the |
| 49 | * null terminated string pointed to by src into the fixed character |
| 50 | * array pointed to by dest. The copy operation stops on the null |
| 51 | * character if encountered and then continues to fill the field |
| 52 | * with nulls up to dmax characters. |
| 53 | * |
| 54 | * EXTENSION TO |
| 55 | * ISO/IEC TR 24731-1, Programming languages, environments |
| 56 | * and system software interfaces, Extensions to the C Library, |
| 57 | * Part I: Bounds-checking interfaces |
| 58 | * |
| 59 | * INPUT PARAMETERS |
| 60 | * dest pointer to character array that will be replaced by src. |
| 61 | * |
| 62 | * dmax restricted maximum length of dest |
| 63 | * |
| 64 | * src pointer to the null terminated string that will be copied |
| 65 | * into the character array pointed to by dest |
| 66 | * |
| 67 | * slen length of source |
| 68 | * |
| 69 | * OUTPUT PARAMETERS |
| 70 | * dest updated |
| 71 | * |
| 72 | * RUNTIME CONSTRAINTS |
| 73 | * Neither dest nor src shall be a null pointer. |
| 74 | * dmax shall not equal zero. |
| 75 | * dmax shall not be greater than RSIZE_MAX_STR. |
| 76 | * slen shall not equal zero. |
| 77 | * slen shall not exceed dmax |
| 78 | * Copying shall not take place between objects that overlap. |
| 79 | * If there is a runtime-constraint violation, then if dest |
| 80 | * is not a null pointer and dmax is greater than zero and |
| 81 | * not greater than RSIZE_MAX_STR, then strcpyfldin_s nulls dest. |
| 82 | * |
| 83 | * RETURN VALUE |
| 84 | * EOK successful operation |
| 85 | * ESNULLP NULL pointer |
| 86 | * ESZEROL zero length |
| 87 | * ESLEMAX length exceeds max limit |
| 88 | * ESOVRLP strings overlap |
| 89 | * |
| 90 | * ALSO SEE |
| 91 | * strcpyfld_s(), strcpyfldout_s(), |
| 92 | * |
| 93 | */ |
| 94 | errno_t |
| 95 | strcpyfldin_s (char *dest, rsize_t dmax, const char *src, rsize_t slen) |
| 96 | { |
| 97 | rsize_t orig_dmax; |
| 98 | char *orig_dest; |
| 99 | const char *overlap_bumper; |
| 100 | |
| 101 | if (dest == NULL) { |
| 102 | invoke_safe_str_constraint_handler("strcpyfldin_s: dest is null", |
| 103 | NULL, ESNULLP); |
| 104 | return (ESNULLP); |
| 105 | } |
| 106 | |
| 107 | if (dmax == 0) { |
| 108 | invoke_safe_str_constraint_handler("strcpyfldin_s: dmax is 0", |
| 109 | NULL, ESZEROL); |
| 110 | return (ESZEROL); |
| 111 | } |
| 112 | |
| 113 | if (dmax > RSIZE_MAX_STR) { |
| 114 | invoke_safe_str_constraint_handler("strcpyfldin_s: dmax exceeds max", |
| 115 | NULL, ESLEMAX); |
| 116 | return (ESLEMAX); |
| 117 | } |
| 118 | |
| 119 | if (src == NULL) { |
| 120 | /* null string to clear data */ |
| 121 | while (dmax) { *dest = '\0'; dmax--; dest++; } |
| 122 | |
| 123 | invoke_safe_str_constraint_handler("strcpyfldin_s: src is null", |
| 124 | NULL, ESNULLP); |
| 125 | return (ESNULLP); |
| 126 | } |
| 127 | |
| 128 | if (slen == 0) { |
| 129 | /* null string to clear data */ |
| 130 | while (dmax) { *dest = '\0'; dmax--; dest++; } |
| 131 | |
| 132 | invoke_safe_str_constraint_handler("strcpyfldin_s: slen is 0", |
| 133 | NULL, ESZEROL); |
| 134 | return (ESZEROL); |
| 135 | } |
| 136 | |
| 137 | if (slen > dmax) { |
| 138 | /* null string to clear data */ |
| 139 | while (dmax) { *dest = '\0'; dmax--; dest++; } |
| 140 | |
| 141 | invoke_safe_str_constraint_handler("strcpyfldin_s: slen exceeds max", |
| 142 | NULL, ESLEMAX); |
| 143 | return (ESLEMAX); |
| 144 | } |
| 145 | |
| 146 | |
| 147 | /* hold base of dest in case src was not copied */ |
| 148 | orig_dmax = dmax; |
| 149 | orig_dest = dest; |
| 150 | |
| 151 | if (dest < src) { |
| 152 | overlap_bumper = src; |
| 153 | |
| 154 | while (dmax > 0 && *src) { |
| 155 | |
| 156 | if (dest == overlap_bumper) { |
| 157 | dmax = orig_dmax; |
| 158 | dest = orig_dest; |
| 159 | |
| 160 | /* null string to eliminate partial copy */ |
| 161 | while (dmax) { *dest = '\0'; dmax--; dest++; } |
| 162 | |
| 163 | invoke_safe_str_constraint_handler( |
| 164 | "strcpyfldin_s: overlapping objects", |
| 165 | NULL, ESOVRLP); |
| 166 | return (ESOVRLP); |
| 167 | } |
| 168 | |
| 169 | dmax--; |
| 170 | *dest++ = *src++; |
| 171 | } |
| 172 | |
| 173 | } else { |
| 174 | overlap_bumper = dest; |
| 175 | |
| 176 | while (dmax > 0 && *src) { |
| 177 | |
| 178 | if (src == overlap_bumper) { |
| 179 | dmax = orig_dmax; |
| 180 | dest = orig_dest; |
| 181 | |
| 182 | /* null string to eliminate partial copy */ |
| 183 | while (dmax) { *dest = '\0'; dmax--; dest++; } |
| 184 | |
| 185 | invoke_safe_str_constraint_handler( |
| 186 | "strcpyfldin_s: overlapping objects", |
| 187 | NULL, ESOVRLP); |
| 188 | return (ESOVRLP); |
| 189 | } |
| 190 | |
| 191 | dmax--; |
| 192 | *dest++ = *src++; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | /* |
| 197 | * finish filling in the field with nulls if there is slack space |
| 198 | */ |
| 199 | while (dmax) { *dest = '\0'; dmax--; dest++; } |
| 200 | |
| 201 | return (EOK); |
| 202 | } |
| 203 | EXPORT_SYMBOL(strcpyfldin_s) |