1 /**
2 * Copyright 2020 Huawei Technologies Co., Ltd
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "secinput.h"
18
19 /*
20 * <FUNCTION DESCRIPTION>
21 * The vscanf_s function is equivalent to scanf_s, with the variable argument list replaced by argList,
22 * The vscanf_s function reads data from the standard input stream stdin and
23 * writes the data into the location that's given by argument. Each argument
24 * must be a pointer to a variable of a type that corresponds to a type specifier
25 * in format. If copying occurs between strings that overlap, the behavior is
26 * undefined.
27 *
28 * <INPUT PARAMETERS>
29 * format Format control string.
30 * argList pointer to list of arguments
31 *
32 * <OUTPUT PARAMETERS>
33 * argList the converted value stored in user assigned address
34 *
35 * <RETURN VALUE>
36 * Returns the number of fields successfully converted and assigned;
37 * the return value does not include fields that were read but not assigned.
38 * A return value of 0 indicates that no fields were assigned.
39 * return -1 if an error occurs.
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;
45 SECUREC_INIT_SEC_FILE_STREAM(fStr, SECUREC_FROM_STDIN_FLAG, stdin, 0, NULL, 0);
46 /*
47 * "va_list" has different definition on different platform, so we can't use argList == NULL
48 * to determine it's invalid. If you has fixed platform, you can check some fields to validate it,
49 * such as "argList == NULL" or argList.xxx != NULL or *(size_t *)&argList != 0.
50 */
51 if (format == NULL || fStr.pf == NULL) {
52 SECUREC_ERROR_INVALID_PARAMTER("vscanf_s");
53 return SECUREC_SCANF_EINVAL;
54 }
55
56 SECUREC_LOCK_STDIN(0, fStr.pf);
57
58 retVal = SecInputS(&fStr, format, argList);
59
60 SECUREC_UNLOCK_STDIN(0, fStr.pf);
61 if (retVal < 0) {
62 SECUREC_ERROR_INVALID_PARAMTER("vscanf_s");
63 return SECUREC_SCANF_EINVAL;
64 }
65 return retVal;
66 }
67
68
69