You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

vscanf_s.c 2.6 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. #include "secinput.h"
  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. */
  39. int vscanf_s(const char *format, va_list argList)
  40. {
  41. int retVal; /* If initialization causes e838 */
  42. SecFileStream fStr;
  43. SECUREC_INIT_SEC_FILE_STREAM(fStr, SECUREC_FROM_STDIN_FLAG, stdin, 0, NULL, 0);
  44. /*
  45. * "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 (format == NULL || fStr.pf == NULL) {
  50. SECUREC_ERROR_INVALID_PARAMTER("vscanf_s");
  51. return SECUREC_SCANF_EINVAL;
  52. }
  53. SECUREC_LOCK_STDIN(0, fStr.pf);
  54. retVal = SecInputS(&fStr, format, argList);
  55. SECUREC_UNLOCK_STDIN(0, fStr.pf);
  56. if (retVal < 0) {
  57. SECUREC_ERROR_INVALID_PARAMTER("vscanf_s");
  58. return SECUREC_SCANF_EINVAL;
  59. }
  60. return retVal;
  61. }