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