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.

strncat_s.c 4.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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_DO_MEMCPY 1
  18. #include "securecutil.h"
  19. /*
  20. * Befor this function, the basic parameter checking has been done
  21. */
  22. static errno_t SecDoStrncat(char *strDest, size_t destMax, const char *strSrc, size_t count)
  23. {
  24. size_t destLen = SecStrMinLen(strDest, destMax);
  25. /* The strSrc is no longer optimized. The reason is that when count is small,
  26. * the efficiency of strnlen is higher than that of self realization.
  27. */
  28. size_t srcLen = SecStrMinLen(strSrc, count);
  29. if (SECUREC_CAT_STRING_IS_OVERLAP(strDest, destLen, strSrc, srcLen)) {
  30. strDest[0] = '\0';
  31. if (strDest + destLen <= strSrc && destLen == destMax) {
  32. SECUREC_ERROR_INVALID_PARAMTER("strncat_s");
  33. return EINVAL_AND_RESET;
  34. }
  35. SECUREC_ERROR_BUFFER_OVERLAP("strncat_s");
  36. return EOVERLAP_AND_RESET;
  37. }
  38. if (srcLen + destLen >= destMax || strDest == strSrc) {
  39. strDest[0] = '\0';
  40. if (destLen == destMax) {
  41. SECUREC_ERROR_INVALID_PARAMTER("strncat_s");
  42. return EINVAL_AND_RESET;
  43. }
  44. SECUREC_ERROR_INVALID_RANGE("strncat_s");
  45. return ERANGE_AND_RESET;
  46. }
  47. SecDoMemcpy(strDest + destLen, strSrc, srcLen); /* no terminator */
  48. *(strDest + destLen + srcLen) = '\0';
  49. return EOK;
  50. }
  51. /*
  52. * <FUNCTION DESCRIPTION>
  53. * The strncat_s function appends not more than n successive characters
  54. * (not including the terminating null character)
  55. * from the array pointed to by strSrc to the end of the string pointed to by strDest
  56. * The strncat_s function try to append the first D characters of strSrc to
  57. * the end of strDest, where D is the lesser of count and the length of strSrc.
  58. * If appending those D characters will fit within strDest (whose size is given
  59. * as destMax) and still leave room for a null terminator, then those characters
  60. * are appended, starting at the original terminating null of strDest, and a
  61. * new terminating null is appended; otherwise, strDest[0] is set to the null
  62. * character.
  63. *
  64. * <INPUT PARAMETERS>
  65. * strDest Null-terminated destination string.
  66. * destMax Size of the destination buffer.
  67. * strSrc Null-terminated source string.
  68. * count Number of character to append, or truncate.
  69. *
  70. * <OUTPUT PARAMETERS>
  71. * strDest is updated
  72. *
  73. * <RETURN VALUE>
  74. * EOK Success
  75. * EINVAL strDest is NULL and destMax != 0 and destMax <= SECUREC_STRING_MAX_LEN
  76. * EINVAL_AND_RESET (strDest unterminated and all other parameters are valid)or
  77. * (strDest != NULL and strSrc is NULL and destMax != 0 and destMax <= SECUREC_STRING_MAX_LEN)
  78. * ERANGE destMax is 0 and destMax > SECUREC_STRING_MAX_LEN
  79. * ERANGE_AND_RESET strDest have not enough space and all other parameters are valid and not overlap
  80. * EOVERLAP_AND_RESET dest buffer and source buffer are overlapped and all parameters are valid
  81. *
  82. * If there is a runtime-constraint violation, strDest[0] will be set to the '\0' when strDest and destMax valid
  83. */
  84. errno_t strncat_s(char *strDest, size_t destMax, const char *strSrc, size_t count)
  85. {
  86. if (destMax == 0 || destMax > SECUREC_STRING_MAX_LEN) {
  87. SECUREC_ERROR_INVALID_RANGE("strncat_s");
  88. return ERANGE;
  89. }
  90. if (strDest == NULL || strSrc == NULL) {
  91. SECUREC_ERROR_INVALID_PARAMTER("strncat_s");
  92. if (strDest != NULL) {
  93. strDest[0] = '\0';
  94. return EINVAL_AND_RESET;
  95. }
  96. return EINVAL;
  97. }
  98. if (count > SECUREC_STRING_MAX_LEN) {
  99. #ifdef SECUREC_COMPATIBLE_WIN_FORMAT
  100. if (count == (size_t)(-1)) {
  101. /* Windows internal functions may pass in -1 when calling this function */
  102. return SecDoStrncat(strDest, destMax, strSrc, destMax);
  103. }
  104. #endif
  105. strDest[0] = '\0';
  106. SECUREC_ERROR_INVALID_RANGE("strncat_s");
  107. return ERANGE_AND_RESET;
  108. }
  109. return SecDoStrncat(strDest, destMax, strSrc, count);
  110. }
  111. #if SECUREC_IN_KERNEL
  112. EXPORT_SYMBOL(strncat_s);
  113. #endif