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 "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 *******************************************************************************
40 */
vscanf_s(const char * format,va_list arglist)41 int vscanf_s(const char *format, va_list arglist)
42 {
43 int retVal; /* If initialization causes e838 */
44 SecFileStream fStr = SECUREC_INIT_SEC_FILE_STREAM;
45 /*
46 * "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 fStr.pf = stdin;
51 if (format == NULL || fStr.pf == NULL) {
52 SECUREC_ERROR_INVALID_PARAMTER("vscanf_s");
53 return SECUREC_SCANF_EINVAL;
54 }
55
56 fStr.flag = SECUREC_FROM_STDIN_FLAG;
57
58 SECUREC_LOCK_STDIN(0, fStr.pf);
59
60 retVal = SecInputS(&fStr, format, arglist);
61
62 SECUREC_UNLOCK_STDIN(0, fStr.pf);
63 if (retVal < 0) {
64 SECUREC_ERROR_INVALID_PARAMTER("vscanf_s");
65 return SECUREC_SCANF_EINVAL;
66 }
67 return retVal;
68 }
69
70
71