1 /**
2 * Copyright 2020 Huawei Technologies Co., Ltd
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #define SECUREC_INLINE_STR_LEN 1
18 #define SECUREC_INLINE_DO_MEMCPY 1
19
20 #include "securecutil.h"
21
22 #if defined(SECUREC_COMPATIBLE_WIN_FORMAT)
23 #define SECUREC_STRNCPY_PARAM_OK(strDest, destMax, strSrc, count) \
24 (((destMax) > 0 && (destMax) <= SECUREC_STRING_MAX_LEN && (strDest) != NULL && (strSrc) != NULL && \
25 ((count) <= SECUREC_STRING_MAX_LEN || (count) == ((size_t)(-1))) && (count) > 0))
26 #else
27 #define SECUREC_STRNCPY_PARAM_OK(strDest, destMax, strSrc, count) \
28 (((destMax) > 0 && (destMax) <= SECUREC_STRING_MAX_LEN && (strDest) != NULL && (strSrc) != NULL && \
29 (count) <= SECUREC_STRING_MAX_LEN && (count) > 0))
30 #endif
31
32 /*
33 * Check Src Count Range
34 */
CheckSrcCountRange(char * strDest,size_t destMax,const char * strSrc,size_t count)35 static errno_t CheckSrcCountRange(char *strDest, size_t destMax, const char *strSrc, size_t count)
36 {
37 size_t tmpDestMax = destMax;
38 size_t tmpCount = count;
39 const char *endPos = strSrc;
40
41 /* use destMax and count as boundary checker and destMax must be greater than zero */
42 while (*(endPos) != '\0' && tmpDestMax > 0 && tmpCount > 0) {
43 ++endPos;
44 --tmpCount;
45 --tmpDestMax;
46 }
47 if (tmpDestMax == 0) {
48 strDest[0] = '\0';
49 SECUREC_ERROR_INVALID_RANGE("strncpy_s");
50 return ERANGE_AND_RESET;
51 }
52 return EOK;
53 }
54
55 /*
56 * Handling errors, when dest euqal src return EOK
57 */
strncpy_error(char * strDest,size_t destMax,const char * strSrc,size_t count)58 errno_t strncpy_error(char *strDest, size_t destMax, const char *strSrc, size_t count)
59 {
60 if (destMax == 0 || destMax > SECUREC_STRING_MAX_LEN) {
61 SECUREC_ERROR_INVALID_RANGE("strncpy_s");
62 return ERANGE;
63 } else if (strDest == NULL || strSrc == NULL) {
64 SECUREC_ERROR_INVALID_PARAMTER("strncpy_s");
65 if (strDest != NULL) {
66 strDest[0] = '\0';
67 return EINVAL_AND_RESET;
68 }
69 return EINVAL;
70 } else if (count > SECUREC_STRING_MAX_LEN) {
71 strDest[0] = '\0'; /* clear dest string */
72 SECUREC_ERROR_INVALID_RANGE("strncpy_s");
73 return ERANGE_AND_RESET;
74 } else if (count == 0) {
75 strDest[0] = '\0';
76 return EOK;
77 }
78
79 return CheckSrcCountRange(strDest, destMax, strSrc, count);
80 }
81
82 /*
83 * <FUNCTION DESCRIPTION>
84 * The strncpy_s function copies not more than n successive characters (not including the terminating null character)
85 * from the array pointed to by strSrc to the array pointed to by strDest.
86 *
87 * <INPUT PARAMETERS>
88 * strDest Destination string.
89 * destMax The size of the destination string, in characters.
90 * strSrc Source string.
91 * count Number of characters to be copied.
92 *
93 * <OUTPUT PARAMETERS>
94 * strDest is updated
95 *
96 * <RETURN VALUE>
97 * EOK Success
98 * EINVAL strDest is NULL and destMax != 0 and destMax <= SECUREC_STRING_MAX_LEN
99 * EINVAL_AND_RESET strDest != NULL and strSrc is NULL and destMax != 0 and destMax <= SECUREC_STRING_MAX_LEN
100 * ERANGE destMax is 0 and destMax > SECUREC_STRING_MAX_LEN
101 * ERANGE_AND_RESET strDest have not enough space and all other parameters are valid and not overlap
102 * EOVERLAP_AND_RESET dest buffer and source buffer are overlapped and all parameters are valid
103 *
104 * If there is a runtime-constraint violation, strDest[0] will be set to the '\0' when strDest and destMax valid
105 */
strncpy_s(char * strDest,size_t destMax,const char * strSrc,size_t count)106 errno_t strncpy_s(char *strDest, size_t destMax, const char *strSrc, size_t count)
107 {
108 if (SECUREC_STRNCPY_PARAM_OK(strDest, destMax, strSrc, count)) {
109 size_t minCpLen; /* use it to store the maxi length limit */
110 if (count < destMax) {
111 minCpLen = SecStrMinLen(strSrc, count); /* no ending terminator */
112 } else {
113 size_t tmpCount = destMax;
114 #ifdef SECUREC_COMPATIBLE_WIN_FORMAT
115 if (count == ((size_t)(-1))) {
116 tmpCount = destMax - 1;
117 }
118 #endif
119 minCpLen = SecStrMinLen(strSrc, tmpCount);
120 if (minCpLen == destMax) {
121 strDest[0] = '\0';
122 SECUREC_ERROR_INVALID_RANGE("strncpy_s");
123 return ERANGE_AND_RESET;
124 }
125 }
126 if (SECUREC_STRING_NO_OVERLAP(strDest, strSrc, minCpLen) || strDest == strSrc) {
127 /* Not overlap */
128 SecDoMemcpy(strDest, strSrc, minCpLen); /* copy string without terminator */
129 strDest[minCpLen] = '\0';
130 return EOK;
131 } else {
132 strDest[0] = '\0';
133 SECUREC_ERROR_BUFFER_OVERLAP("strncpy_s");
134 return EOVERLAP_AND_RESET;
135 }
136 }
137 return strncpy_error(strDest, destMax, strSrc, count);
138 }
139
140 #if SECUREC_IN_KERNEL
141 EXPORT_SYMBOL(strncpy_s);
142 #endif
143
144