1 /*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2014-2021. All rights reserved.
3 * Licensed under Mulan PSL v2.
4 * You can use this software according to the terms and conditions of the Mulan PSL v2.
5 * You may obtain a copy of Mulan PSL v2 at:
6 * http://license.coscl.org.cn/MulanPSL2
7 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
8 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
9 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
10 * See the Mulan PSL v2 for more details.
11 * Description: strcat_s function
12 * Create: 2014-02-25
13 */
14
15 #include "securecutil.h"
16
17 /*
18 * Befor this function, the basic parameter checking has been done
19 */
SecDoCat(char * strDest,size_t destMax,const char * strSrc)20 SECUREC_INLINE errno_t SecDoCat(char *strDest, size_t destMax, const char *strSrc)
21 {
22 size_t destLen;
23 size_t srcLen;
24 size_t maxSrcLen;
25 SECUREC_CALC_STR_LEN(strDest, destMax, &destLen);
26 /* Only optimize strSrc, do not apply this function to strDest */
27 maxSrcLen = destMax - destLen;
28 SECUREC_CALC_STR_LEN_OPT(strSrc, maxSrcLen, &srcLen);
29
30 if (SECUREC_CAT_STRING_IS_OVERLAP(strDest, destLen, strSrc, srcLen)) {
31 strDest[0] = '\0';
32 if (strDest + destLen <= strSrc && destLen == destMax) {
33 SECUREC_ERROR_INVALID_PARAMTER("strcat_s");
34 return EINVAL_AND_RESET;
35 }
36 SECUREC_ERROR_BUFFER_OVERLAP("strcat_s");
37 return EOVERLAP_AND_RESET;
38 }
39 if (srcLen + destLen >= destMax || strDest == strSrc) {
40 strDest[0] = '\0';
41 if (destLen == destMax) {
42 SECUREC_ERROR_INVALID_PARAMTER("strcat_s");
43 return EINVAL_AND_RESET;
44 }
45 SECUREC_ERROR_INVALID_RANGE("strcat_s");
46 return ERANGE_AND_RESET;
47 }
48 SECUREC_MEMCPY_WARP_OPT(strDest + destLen, strSrc, srcLen + 1); /* Single character length include \0 */
49 return EOK;
50 }
51
52 /*
53 * <FUNCTION DESCRIPTION>
54 * The strcat_s function appends a copy of the string pointed to by strSrc (including the terminating null character)
55 * to the end of the string pointed to by strDest.
56 * The initial character of strSrc overwrites the terminating null character of strDest.
57 * strcat_s will return EOVERLAP_AND_RESET if the source and destination strings overlap.
58 *
59 * Note that the second parameter is the total size of the buffer, not the
60 * remaining size.
61 *
62 * <INPUT PARAMETERS>
63 * strDest Null-terminated destination string buffer.
64 * destMax Size of the destination string buffer.
65 * strSrc Null-terminated source string buffer.
66 *
67 * <OUTPUT PARAMETERS>
68 * strDest is updated
69 *
70 * <RETURN VALUE>
71 * EOK Success
72 * EINVAL strDest is NULL and destMax != 0 and destMax <= SECUREC_STRING_MAX_LEN
73 * EINVAL_AND_RESET (strDest unterminated and all other parameters are valid) or
74 * (strDest != NULL and strSrc is NULL and destMax != 0 and destMax <= SECUREC_STRING_MAX_LEN)
75 * ERANGE destMax is 0 and destMax > SECUREC_STRING_MAX_LEN
76 * ERANGE_AND_RESET strDest have not enough space and all other parameters are valid and not overlap
77 * EOVERLAP_AND_RESET dest buffer and source buffer are overlapped and all parameters are valid
78 *
79 * If there is a runtime-constraint violation, strDest[0] will be set to the '\0' when strDest and destMax valid
80 */
strcat_s(char * strDest,size_t destMax,const char * strSrc)81 errno_t strcat_s(char *strDest, size_t destMax, const char *strSrc)
82 {
83 if (destMax == 0 || destMax > SECUREC_STRING_MAX_LEN) {
84 SECUREC_ERROR_INVALID_RANGE("strcat_s");
85 return ERANGE;
86 }
87 if (strDest == NULL || strSrc == NULL) {
88 SECUREC_ERROR_INVALID_PARAMTER("strcat_s");
89 if (strDest != NULL) {
90 strDest[0] = '\0';
91 return EINVAL_AND_RESET;
92 }
93 return EINVAL;
94 }
95 return SecDoCat(strDest, destMax, strSrc);
96 }
97
98 #if SECUREC_EXPORT_KERNEL_SYMBOL
99 EXPORT_SYMBOL(strcat_s);
100 #endif
101
102