• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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: strncat_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  */
SecDoCatLimit(char * strDest,size_t destMax,const char * strSrc,size_t count)20 SECUREC_INLINE errno_t SecDoCatLimit(char *strDest, size_t destMax, const char *strSrc, size_t count)
21 {
22     size_t destLen;
23     size_t srcLen;
24     SECUREC_CALC_STR_LEN(strDest, destMax, &destLen);
25     /*
26      * The strSrc is no longer optimized. The reason is that when count is small,
27      * the efficiency of strnlen is higher than that of self realization.
28      */
29     SECUREC_CALC_STR_LEN(strSrc, count, &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("strncat_s");
35             return EINVAL_AND_RESET;
36         }
37         SECUREC_ERROR_BUFFER_OVERLAP("strncat_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("strncat_s");
44             return EINVAL_AND_RESET;
45         }
46         SECUREC_ERROR_INVALID_RANGE("strncat_s");
47         return ERANGE_AND_RESET;
48     }
49     SECUREC_MEMCPY_WARP_OPT(strDest + destLen, strSrc, srcLen);    /* No terminator */
50     *(strDest + destLen + srcLen) = '\0';
51     return EOK;
52 }
53 
54 /*
55  * <FUNCTION DESCRIPTION>
56  *    The strncat_s function appends not more than n successive  characters
57  *    (not including the terminating null  character)
58  *     from the array pointed to by strSrc to the end of the  string pointed to by strDest
59  *    The strncat_s function try to append the first D characters of strSrc to
60  *    the end of strDest, where D is the lesser of count and the length of strSrc.
61  *    If appending those D characters will fit within strDest (whose size is given
62  *    as destMax) and still leave room for a null terminator, then those characters
63  *    are appended, starting at the original terminating null of strDest, and a
64  *    new terminating null is appended; otherwise, strDest[0] is set to the null
65  *    character.
66  *
67  * <INPUT PARAMETERS>
68  *    strDest            Null-terminated destination string.
69  *    destMax            Size of the destination buffer.
70  *    strSrc             Null-terminated source string.
71  *    count              Number of character to append, or truncate.
72  *
73  * <OUTPUT PARAMETERS>
74  *    strDest            is updated
75  *
76  * <RETURN VALUE>
77  *    EOK                Success
78  *    EINVAL             strDest is  NULL and destMax != 0 and destMax <= SECUREC_STRING_MAX_LEN
79  *    EINVAL_AND_RESET      (strDest unterminated  and all other parameters are valid)or
80  *                        (strDest !=  NULL and strSrc is NULL and destMax != 0 and destMax <= SECUREC_STRING_MAX_LEN)
81  *    ERANGE                         destMax is 0 and destMax > SECUREC_STRING_MAX_LEN
82  *    ERANGE_AND_RESET      strDest have not enough space  and all other parameters are valid  and not overlap
83  *    EOVERLAP_AND_RESET   dest buffer and source buffer are overlapped and all  parameters are valid
84  *
85  *    If there is a runtime-constraint violation, strDest[0] will be set to the '\0' when strDest and destMax valid
86  */
strncat_s(char * strDest,size_t destMax,const char * strSrc,size_t count)87 errno_t strncat_s(char *strDest, size_t destMax, const char *strSrc, size_t count)
88 {
89     if (destMax == 0 || destMax > SECUREC_STRING_MAX_LEN) {
90         SECUREC_ERROR_INVALID_RANGE("strncat_s");
91         return ERANGE;
92     }
93 
94     if (strDest == NULL || strSrc == NULL) {
95         SECUREC_ERROR_INVALID_PARAMTER("strncat_s");
96         if (strDest != NULL) {
97             strDest[0] = '\0';
98             return EINVAL_AND_RESET;
99         }
100         return EINVAL;
101     }
102     if (count > SECUREC_STRING_MAX_LEN) {
103 #ifdef  SECUREC_COMPATIBLE_WIN_FORMAT
104         if (count == (size_t)(-1)) {
105             /* Windows internal functions may pass in -1 when calling this function */
106             return SecDoCatLimit(strDest, destMax, strSrc, destMax);
107         }
108 #endif
109         strDest[0] = '\0';
110         SECUREC_ERROR_INVALID_RANGE("strncat_s");
111         return ERANGE_AND_RESET;
112     }
113     return SecDoCatLimit(strDest, destMax, strSrc, count);
114 }
115 
116 #if SECUREC_EXPORT_KERNEL_SYMBOL
117 EXPORT_SYMBOL(strncat_s);
118 #endif
119 
120