• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "securec.h"
17 
18 /*******************************************************************************
19  * <FUNCTION DESCRIPTION>
20  *    The snprintf_s function is equivalent to the snprintf function
21  *    except for the parameter destMax/count and the explicit runtime-constraints violation
22  *    The snprintf_s function formats and stores count or fewer characters in
23  *    strDest and appends a terminating null. Each argument (if any) is converted
24  *    and output according to the corresponding format specification in format.
25  *    The formatting is consistent with the printf family of functions; If copying
26  *    occurs between strings that overlap, the behavior is undefined.
27  *
28  * <INPUT PARAMETERS>
29  *    strDest                 Storage location for the output.
30  *    destMax                 The size of the storage location for output. Size
31  *                                 in bytes for snprintf_s or size in words for snwprintf_s.
32  *    count                    Maximum number of character to store.
33  *    format                  Format-control string.
34  *    ...                        Optional arguments.
35  *
36  * <OUTPUT PARAMETERS>
37  *    strDest                 is updated
38  *
39  * <RETURN VALUE>
40  *    return  the number of characters written, not including the terminating null
41  *    return -1 if an  error occurs.
42  *    return -1 if count < destMax and the output string  has been truncated
43  *
44  * If there is a runtime-constraint violation, strDest[0] will be set to the '\0' when strDest and destMax valid
45  *******************************************************************************
46  */
snprintf_s(char * strDest,size_t destMax,size_t count,const char * format,...)47 int snprintf_s(char *strDest, size_t destMax, size_t count, const char *format, ...)
48 {
49     int ret;                    /* If initialization causes  e838 */
50     va_list arglist;
51 
52     va_start(arglist, format);
53 #ifndef __MINGW32__
54     ret = vsnprintf_s(strDest, destMax, count, format, arglist);
55 #else
56     ret = vsnprintf_truncated_s(strDest, destMax, format, arglist);
57 #endif
58     va_end(arglist);
59     (void)arglist;              /* to clear e438 last value assigned not used , the compiler will optimize this code */
60 
61     return ret;
62 }
63 
64 #if SECUREC_SNPRINTF_TRUNCATED
snprintf_truncated_s(char * strDest,size_t destMax,const char * format,...)65 int snprintf_truncated_s(char *strDest, size_t destMax, const char *format, ...)
66 {
67     int ret;                    /* If initialization causes  e838 */
68     va_list arglist;
69 
70     va_start(arglist, format);
71     ret = vsnprintf_truncated_s(strDest, destMax, format, arglist);
72     va_end(arglist);
73     (void)arglist;              /* to clear e438 last value assigned not used , the compiler will optimize this code */
74 
75     return ret;
76 }
77 #endif
78 
79 
80