• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2014-2020. 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  * Author: lishunda
13  * Create: 2014-02-25
14  */
15 
16 #include "securecutil.h"
17 
18 /*
19  * Befor this function, the basic parameter checking has been done
20  */
SecDoCat(char * strDest,size_t destMax,const char * strSrc)21 SECUREC_INLINE errno_t SecDoCat(char *strDest, size_t destMax, const char *strSrc)
22 {
23     size_t destLen;
24     size_t srcLen;
25     size_t maxSrcLen;
26     SECUREC_CALC_STR_LEN(strDest, destMax, &destLen);
27     /* Only optimize strSrc, do not apply this function to strDest */
28     maxSrcLen = destMax - destLen;
29     SECUREC_CALC_STR_LEN_OPT(strSrc, maxSrcLen, &srcLen);
30 
31     if (SECUREC_CAT_STRING_IS_OVERLAP(strDest, destLen, strSrc, srcLen)) {
32         strDest[0] = '\0';
33         if (strDest + destLen <= strSrc && destLen == destMax) {
34             SECUREC_ERROR_INVALID_PARAMTER("strcat_s");
35             return EINVAL_AND_RESET;
36         }
37         SECUREC_ERROR_BUFFER_OVERLAP("strcat_s");
38         return EOVERLAP_AND_RESET;
39     }
40     if (srcLen + destLen >= destMax || strDest == strSrc) {
41         strDest[0] = '\0';
42         if (destLen == destMax) {
43             SECUREC_ERROR_INVALID_PARAMTER("strcat_s");
44             return EINVAL_AND_RESET;
45         }
46         SECUREC_ERROR_INVALID_RANGE("strcat_s");
47         return ERANGE_AND_RESET;
48     }
49     SECUREC_MEMCPY_WARP_OPT(strDest + destLen, strSrc, srcLen + 1); /* Single character length  include \0 */
50     return EOK;
51 }
52 
53 /*
54  * <FUNCTION DESCRIPTION>
55  *  The strcat_s function appends a copy of the string pointed to by strSrc (including the terminating null character)
56  *  to the end of the  string pointed to by strDest.
57  *  The initial character of strSrc overwrites the terminating null character of strDest.
58  *  strcat_s will return EOVERLAP_AND_RESET if the source and destination strings overlap.
59  *
60  *  Note that the second parameter is the total size of the buffer, not the
61  *  remaining size.
62  *
63  * <INPUT PARAMETERS>
64  *  strDest             Null-terminated destination string buffer.
65  *  destMax             Size of the destination string buffer.
66  *  strSrc              Null-terminated source string buffer.
67  *
68  * <OUTPUT PARAMETERS>
69  *    strDest             is updated
70  *
71  * <RETURN VALUE>
72  *    EOK                 Success
73  *    EINVAL              strDest is  NULL and destMax != 0 and destMax <= SECUREC_STRING_MAX_LEN
74  *    EINVAL_AND_RESET    (strDest unterminated  and all other parameters are valid) or
75  *                        (strDest != NULL and strSrc is NULL and destMax != 0 and destMax <= SECUREC_STRING_MAX_LEN)
76  *    ERANGE              destMax is 0 and destMax > SECUREC_STRING_MAX_LEN
77  *    ERANGE_AND_RESET      strDest have not enough space  and all other parameters are valid  and not overlap
78  *    EOVERLAP_AND_RESET   dest buffer and source buffer are overlapped and all  parameters are valid
79  *
80  *    If there is a runtime-constraint violation, strDest[0] will be set to the '\0' when strDest and destMax valid
81  */
strcat_s(char * strDest,size_t destMax,const char * strSrc)82 errno_t strcat_s(char *strDest, size_t destMax, const char *strSrc)
83 {
84     if (destMax == 0 || destMax > SECUREC_STRING_MAX_LEN) {
85         SECUREC_ERROR_INVALID_RANGE("strcat_s");
86         return ERANGE;
87     }
88     if (strDest == NULL || strSrc == NULL) {
89         SECUREC_ERROR_INVALID_PARAMTER("strcat_s");
90         if (strDest != NULL) {
91             strDest[0] = '\0';
92             return EINVAL_AND_RESET;
93         }
94         return EINVAL;
95     }
96     return SecDoCat(strDest, destMax, strSrc);
97 }
98 
99 #if SECUREC_IN_KERNEL
100 EXPORT_SYMBOL(strcat_s);
101 #endif
102 
103