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: vsprintf_s function
12 * Create: 2014-02-25
13 */
14
15 #include "secureprintoutput.h"
16
17 /*
18 * <FUNCTION DESCRIPTION>
19 * The vsprintf_s function is equivalent to the vsprintf function
20 * except for the parameter destMax and the explicit runtime-constraints violation
21 * The vsprintf_s function takes a pointer to an argument list, and then formats
22 * and writes the given data to the memory pointed to by strDest.
23 * The function differ from the non-secure versions only in that the secure
24 * versions support positional parameters.
25 *
26 * <INPUT PARAMETERS>
27 * strDest Storage location for the output.
28 * destMax Size of strDest
29 * format Format specification.
30 * argList pointer to list of arguments
31 *
32 * <OUTPUT PARAMETERS>
33 * strDest is updated
34 *
35 * <RETURN VALUE>
36 * return the number of characters written, not including the terminating null character,
37 * return -1 if an error occurs.
38 *
39 * If there is a runtime-constraint violation, strDest[0] will be set to the '\0' when strDest and destMax valid
40 */
vsprintf_s(char * strDest,size_t destMax,const char * format,va_list argList)41 int vsprintf_s(char *strDest, size_t destMax, const char *format, va_list argList)
42 {
43 int retVal; /* If initialization causes e838 */
44
45 if (SECUREC_VSPRINTF_PARAM_ERROR(format, strDest, destMax, SECUREC_STRING_MAX_LEN)) {
46 SECUREC_VSPRINTF_CLEAR_DEST(strDest, destMax, SECUREC_STRING_MAX_LEN);
47 SECUREC_ERROR_INVALID_PARAMTER("vsprintf_s");
48 return -1;
49 }
50
51 retVal = SecVsnprintfImpl(strDest, destMax, format, argList);
52 if (retVal < 0) {
53 strDest[0] = '\0';
54 if (retVal == SECUREC_PRINTF_TRUNCATE) {
55 /* Buffer is too small */
56 SECUREC_ERROR_INVALID_RANGE("vsprintf_s");
57 }
58 SECUREC_ERROR_INVALID_PARAMTER("vsprintf_s");
59 return -1;
60 }
61
62 return retVal;
63 }
64 #if SECUREC_EXPORT_KERNEL_SYMBOL
65 EXPORT_SYMBOL(vsprintf_s);
66 #endif
67
68