• 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: vsnprintf_s  function
12  * Author: lishunda
13  * Create: 2014-02-25
14  */
15 
16 #include "secureprintoutput.h"
17 
18 #if SECUREC_ENABLE_VSNPRINTF
19 /*
20  * <FUNCTION DESCRIPTION>
21  *    The vsnprintf_s function is equivalent to the vsnprintf function
22  *     except for the parameter destMax/count and the explicit runtime-constraints violation
23  *    The vsnprintf_s function takes a pointer to an argument list, then formats
24  *    and writes up to count characters of the given data to the memory pointed
25  *    to by strDest and appends a terminating null.
26  *
27  * <INPUT PARAMETERS>
28  *    strDest                  Storage location for the output.
29  *    destMax                The size of the strDest for output.
30  *    count                    Maximum number of character to write(not including
31  *                                the terminating NULL)
32  *    format                   Format-control string.
33  *    argList                     pointer to list of arguments.
34  *
35  * <OUTPUT PARAMETERS>
36  *    strDest                is updated
37  *
38  * <RETURN VALUE>
39  *    return  the number of characters written, not including the terminating null
40  *    return -1 if an  error occurs.
41  *    return -1 if count < destMax and the output string  has been truncated
42  *
43  * If there is a runtime-constraint violation, strDest[0] will be set to the '\0' when strDest and destMax valid
44  */
vsnprintf_s(char * strDest,size_t destMax,size_t count,const char * format,va_list argList)45 int vsnprintf_s(char *strDest, size_t destMax, size_t count, const char *format, va_list argList)
46 {
47     int retVal;
48 
49     if (SECUREC_VSNPRINTF_PARAM_ERROR(format, strDest, destMax, count, SECUREC_STRING_MAX_LEN)) {
50         SECUREC_VSPRINTF_CLEAR_DEST(strDest, destMax, SECUREC_STRING_MAX_LEN);
51         SECUREC_ERROR_INVALID_PARAMTER("vsnprintf_s");
52         return -1;
53     }
54 
55     if (destMax > count) {
56         retVal = SecVsnprintfImpl(strDest, count + 1, format, argList);
57         if (retVal == SECUREC_PRINTF_TRUNCATE) {  /* To keep dest buffer not destroyed 2014.2.18 */
58             /* The string has been truncated, return  -1 */
59             return -1;          /* To skip error handler,  return strlen(strDest) or -1 */
60         }
61     } else {
62         retVal = SecVsnprintfImpl(strDest, destMax, format, argList);
63 #ifdef SECUREC_COMPATIBLE_WIN_FORMAT
64         if (retVal == SECUREC_PRINTF_TRUNCATE && count == (size_t)(-1)) {
65             return -1;
66         }
67 #endif
68     }
69 
70     if (retVal < 0) {
71         strDest[0] = '\0';      /* Empty the dest strDest */
72         if (retVal == SECUREC_PRINTF_TRUNCATE) {
73             /* Buffer too small */
74             SECUREC_ERROR_INVALID_RANGE("vsnprintf_s");
75         }
76         SECUREC_ERROR_INVALID_PARAMTER("vsnprintf_s");
77         return -1;
78     }
79 
80     return retVal;
81 }
82 #if SECUREC_IN_KERNEL
83 EXPORT_SYMBOL(vsnprintf_s);
84 #endif
85 #endif
86 
87 #if SECUREC_SNPRINTF_TRUNCATED
88 /*
89  * <FUNCTION DESCRIPTION>
90  *    The vsnprintf_truncated_s function is equivalent to the vsnprintf function
91  *     except for the parameter destMax/count and the explicit runtime-constraints violation
92  *    The vsnprintf_truncated_s function takes a pointer to an argument list, then formats
93  *    and writes up to count characters of the given data to the memory pointed
94  *    to by strDest and appends a terminating null.
95  *
96  * <INPUT PARAMETERS>
97  *    strDest                  Storage location for the output.
98  *    destMax                The size of the strDest for output.
99  *                                the terminating NULL)
100  *    format                   Format-control string.
101  *    argList                     pointer to list of arguments.
102  *
103  * <OUTPUT PARAMETERS>
104  *    strDest                is updated
105  *
106  * <RETURN VALUE>
107  *    return  the number of characters written, not including the terminating null
108  *    return -1 if an  error occurs.
109  *    return destMax-1 if output string  has been truncated
110  *
111  * If there is a runtime-constraint violation, strDest[0] will be set to the '\0' when strDest and destMax valid
112  */
vsnprintf_truncated_s(char * strDest,size_t destMax,const char * format,va_list argList)113 int vsnprintf_truncated_s(char *strDest, size_t destMax, const char *format, va_list argList)
114 {
115     int retVal;
116 
117     if (SECUREC_VSPRINTF_PARAM_ERROR(format, strDest, destMax, SECUREC_STRING_MAX_LEN)) {
118         SECUREC_VSPRINTF_CLEAR_DEST(strDest, destMax, SECUREC_STRING_MAX_LEN);
119         SECUREC_ERROR_INVALID_PARAMTER("vsnprintf_truncated_s");
120         return -1;
121     }
122 
123     retVal = SecVsnprintfImpl(strDest, destMax, format, argList);
124     if (retVal < 0) {
125         if (retVal == SECUREC_PRINTF_TRUNCATE) {
126             return (int)(destMax - 1);  /* To skip error handler,  return strlen(strDest) */
127         }
128         strDest[0] = '\0';      /* Empty the dest strDest */
129         SECUREC_ERROR_INVALID_PARAMTER("vsnprintf_truncated_s");
130         return -1;
131     }
132 
133     return retVal;
134 }
135 #if SECUREC_IN_KERNEL
136 EXPORT_SYMBOL(vsnprintf_truncated_s);
137 #endif
138 #endif
139 
140