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.

strcat_s.c 4.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. #define SECUREC_INLINE_STR_LEN 1
  17. #define SECUREC_INLINE_STR_LEN_OPT 1
  18. #define SECUREC_INLINE_DO_MEMCPY 1
  19. #include "securecutil.h"
  20. /*
  21. * Befor this function, the basic parameter checking has been done
  22. */
  23. static errno_t SecDoStrcat(char *strDest, size_t destMax, const char *strSrc)
  24. {
  25. size_t destLen = SecStrMinLen(strDest, destMax);
  26. /* Only optimize strSrc, do not apply this function to strDest */
  27. size_t srcLen = SecStrMinLenOpt(strSrc, destMax - destLen);
  28. if (SECUREC_CAT_STRING_IS_OVERLAP(strDest, destLen, strSrc, srcLen)) {
  29. strDest[0] = '\0';
  30. if (strDest + destLen <= strSrc && destLen == destMax) {
  31. SECUREC_ERROR_INVALID_PARAMTER("strcat_s");
  32. return EINVAL_AND_RESET;
  33. }
  34. SECUREC_ERROR_BUFFER_OVERLAP("strcat_s");
  35. return EOVERLAP_AND_RESET;
  36. }
  37. if (srcLen + destLen >= destMax || strDest == strSrc) {
  38. strDest[0] = '\0';
  39. if (destLen == destMax) {
  40. SECUREC_ERROR_INVALID_PARAMTER("strcat_s");
  41. return EINVAL_AND_RESET;
  42. }
  43. SECUREC_ERROR_INVALID_RANGE("strcat_s");
  44. return ERANGE_AND_RESET;
  45. }
  46. SecDoMemcpy(strDest + destLen, strSrc, srcLen + 1); /* single character length include \0 */
  47. return EOK;
  48. }
  49. /*
  50. * <FUNCTION DESCRIPTION>
  51. * The strcat_s function appends a copy of the string pointed to by strSrc (including the terminating null character)
  52. * to the end of the string pointed to by strDest.
  53. * The initial character of strSrc overwrites the terminating null character of strDest.
  54. * strcat_s will return EOVERLAP_AND_RESET if the source and destination strings overlap.
  55. *
  56. * Note that the second parameter is the total size of the buffer, not the
  57. * remaining size.
  58. *
  59. * <INPUT PARAMETERS>
  60. * strDest Null-terminated destination string buffer.
  61. * destMax Size of the destination string buffer.
  62. * strSrc Null-terminated source string buffer.
  63. *
  64. * <OUTPUT PARAMETERS>
  65. * strDest is updated
  66. *
  67. * <RETURN VALUE>
  68. * EOK Success
  69. * EINVAL strDest is NULL and destMax != 0 and destMax <= SECUREC_STRING_MAX_LEN
  70. * EINVAL_AND_RESET (strDest unterminated and all other parameters are valid)or
  71. * (strDest != NULL and strSrc is NULL and destMax != 0 and destMax <= SECUREC_STRING_MAX_LEN)
  72. * ERANGE destMax is 0 and destMax > SECUREC_STRING_MAX_LEN
  73. * ERANGE_AND_RESET strDest have not enough space and all other parameters are valid and not overlap
  74. * EOVERLAP_AND_RESET dest buffer and source buffer are overlapped and all parameters are valid
  75. *
  76. * If there is a runtime-constraint violation, strDest[0] will be set to the '\0' when strDest and destMax valid
  77. */
  78. errno_t strcat_s(char *strDest, size_t destMax, const char *strSrc)
  79. {
  80. if (destMax == 0 || destMax > SECUREC_STRING_MAX_LEN) {
  81. SECUREC_ERROR_INVALID_RANGE("strcat_s");
  82. return ERANGE;
  83. }
  84. if (strDest == NULL || strSrc == NULL) {
  85. SECUREC_ERROR_INVALID_PARAMTER("strcat_s");
  86. if (strDest != NULL) {
  87. strDest[0] = '\0';
  88. return EINVAL_AND_RESET;
  89. }
  90. return EINVAL;
  91. }
  92. return SecDoStrcat(strDest, destMax, strSrc);
  93. }
  94. #if SECUREC_IN_KERNEL
  95. EXPORT_SYMBOL(strcat_s);
  96. #endif