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