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.

vsprintf_s.c 2.6 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 "secureprintoutput.h"
  17. /*
  18. * <FUNCTION DESCRIPTION>
  19. * The vsprintf_s function is equivalent to the vsprintf function
  20. * except for the parameter destMax and the explicit runtime-constraints violation
  21. * The vsprintf_s function takes a pointer to an argument list, and then formats
  22. * and writes the given data to the memory pointed to by strDest.
  23. * The function differ from the non-secure versions only in that the secure
  24. * versions support positional parameters.
  25. *
  26. * <INPUT PARAMETERS>
  27. * strDest Storage location for the output.
  28. * destMax Size of strDest
  29. * format Format specification.
  30. * argList pointer to list of arguments
  31. *
  32. * <OUTPUT PARAMETERS>
  33. * strDest is updated
  34. *
  35. * <RETURN VALUE>
  36. * return the number of characters written, not including the terminating null character,
  37. * return -1 if an error occurs.
  38. *
  39. * If there is a runtime-constraint violation, strDest[0] will be set to the '\0' when strDest and destMax valid
  40. */
  41. int vsprintf_s(char *strDest, size_t destMax, const char *format, va_list argList)
  42. {
  43. int retVal; /* If initialization causes e838 */
  44. if (format == NULL || strDest == NULL || destMax == 0 || destMax > SECUREC_STRING_MAX_LEN) {
  45. if (strDest != NULL && destMax > 0 && destMax <= SECUREC_STRING_MAX_LEN) {
  46. strDest[0] = '\0';
  47. }
  48. SECUREC_ERROR_INVALID_PARAMTER("vsprintf_s");
  49. return -1;
  50. }
  51. retVal = SecVsnprintfImpl(strDest, destMax, format, argList);
  52. if (retVal < 0) {
  53. strDest[0] = '\0';
  54. if (retVal == SECUREC_PRINTF_TRUNCATE) {
  55. /* Buffer is too small */
  56. SECUREC_ERROR_INVALID_RANGE("vsprintf_s");
  57. }
  58. SECUREC_ERROR_INVALID_PARAMTER("vsprintf_s");
  59. return -1;
  60. }
  61. return retVal;
  62. }
  63. #if SECUREC_IN_KERNEL
  64. EXPORT_SYMBOL(vsprintf_s);
  65. #endif