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: vscanf_s function
12 * Author: lishunda
13 * Create: 2014-02-25
14 */
15
16 #include "secinput.h"
17
18 /*
19 * <FUNCTION DESCRIPTION>
20 * The vscanf_s function is equivalent to scanf_s, with the variable argument list replaced by argList,
21 * The vscanf_s function reads data from the standard input stream stdin and
22 * writes the data into the location that's given by argument. Each argument
23 * must be a pointer to a variable of a type that corresponds to a type specifier
24 * in format. If copying occurs between strings that overlap, the behavior is
25 * undefined.
26 *
27 * <INPUT PARAMETERS>
28 * format Format control string.
29 * argList pointer to list of arguments
30 *
31 * <OUTPUT PARAMETERS>
32 * argList the converted value stored in user assigned address
33 *
34 * <RETURN VALUE>
35 * Returns the number of fields successfully converted and assigned;
36 * the return value does not include fields that were read but not assigned.
37 * A return value of 0 indicates that no fields were assigned.
38 * return -1 if an error occurs.
39 */
vscanf_s(const char * format,va_list argList)40 int vscanf_s(const char *format, va_list argList)
41 {
42 int retVal; /* If initialization causes e838 */
43 SecFileStream fStr;
44 SECUREC_FILE_STREAM_FROM_STDIN(&fStr);
45 /*
46 * The "va_list" has different definition on different platform, so we can't use argList == NULL
47 * To determine it's invalid. If you has fixed platform, you can check some fields to validate it,
48 * such as "argList == NULL" or argList.xxx != NULL or *(size_t *)&argList != 0.
49 */
50 #if SECUREC_ENABLE_SCANF_FILE
51 if (format == NULL || fStr.pf == NULL) {
52 #else
53 if (format == NULL) {
54 #endif
55 SECUREC_ERROR_INVALID_PARAMTER("vscanf_s");
56 return SECUREC_SCANF_EINVAL;
57 }
58
59 #if SECUREC_ENABLE_SCANF_FILE
60 SECUREC_LOCK_STDIN(0, fStr.pf);
61 #endif
62 retVal = SecInputS(&fStr, format, argList);
63 #if SECUREC_ENABLE_SCANF_FILE
64 SECUREC_UNLOCK_STDIN(0, fStr.pf);
65 #endif
66 if (retVal < 0) {
67 SECUREC_ERROR_INVALID_PARAMTER("vscanf_s");
68 return SECUREC_SCANF_EINVAL;
69 }
70 return retVal;
71 }
72
73