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