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.

strncpy_s.c 5.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. #if defined(SECUREC_COMPATIBLE_WIN_FORMAT)
  20. #define SECUREC_STRNCPY_PARAM_OK(strDest, destMax, strSrc, count) \
  21. (((destMax) > 0 && (destMax) <= SECUREC_STRING_MAX_LEN && (strDest) != NULL && (strSrc) != NULL && \
  22. ((count) <= SECUREC_STRING_MAX_LEN || (count) == ((size_t)(-1))) && (count) > 0))
  23. #else
  24. #define SECUREC_STRNCPY_PARAM_OK(strDest, destMax, strSrc, count) \
  25. (((destMax) > 0 && (destMax) <= SECUREC_STRING_MAX_LEN && (strDest) != NULL && (strSrc) != NULL && \
  26. (count) <= SECUREC_STRING_MAX_LEN && (count) > 0))
  27. #endif
  28. /*
  29. * Check Src Count Range
  30. */
  31. static errno_t CheckSrcCountRange(char *strDest, size_t destMax, const char *strSrc, size_t count)
  32. {
  33. size_t tmpDestMax = destMax;
  34. size_t tmpCount = count;
  35. const char *endPos = strSrc;
  36. /* use destMax and count as boundary checker and destMax must be greater than zero */
  37. while (*(endPos) != '\0' && tmpDestMax > 0 && tmpCount > 0) {
  38. ++endPos;
  39. --tmpCount;
  40. --tmpDestMax;
  41. }
  42. if (tmpDestMax == 0) {
  43. strDest[0] = '\0';
  44. SECUREC_ERROR_INVALID_RANGE("strncpy_s");
  45. return ERANGE_AND_RESET;
  46. }
  47. return EOK;
  48. }
  49. /*
  50. * Handling errors, when dest euqal src return EOK
  51. */
  52. errno_t strncpy_error(char *strDest, size_t destMax, const char *strSrc, size_t count)
  53. {
  54. if (destMax == 0 || destMax > SECUREC_STRING_MAX_LEN) {
  55. SECUREC_ERROR_INVALID_RANGE("strncpy_s");
  56. return ERANGE;
  57. } else if (strDest == NULL || strSrc == NULL) {
  58. SECUREC_ERROR_INVALID_PARAMTER("strncpy_s");
  59. if (strDest != NULL) {
  60. strDest[0] = '\0';
  61. return EINVAL_AND_RESET;
  62. }
  63. return EINVAL;
  64. } else if (count > SECUREC_STRING_MAX_LEN) {
  65. strDest[0] = '\0'; /* clear dest string */
  66. SECUREC_ERROR_INVALID_RANGE("strncpy_s");
  67. return ERANGE_AND_RESET;
  68. } else if (count == 0) {
  69. strDest[0] = '\0';
  70. return EOK;
  71. }
  72. return CheckSrcCountRange(strDest, destMax, strSrc, count);
  73. }
  74. /*
  75. * <FUNCTION DESCRIPTION>
  76. * The strncpy_s function copies not more than n successive characters (not including the terminating null character)
  77. * from the array pointed to by strSrc to the array pointed to by strDest.
  78. *
  79. * <INPUT PARAMETERS>
  80. * strDest Destination string.
  81. * destMax The size of the destination string, in characters.
  82. * strSrc Source string.
  83. * count Number of characters to be copied.
  84. *
  85. * <OUTPUT PARAMETERS>
  86. * strDest is updated
  87. *
  88. * <RETURN VALUE>
  89. * EOK Success
  90. * EINVAL strDest is NULL and destMax != 0 and destMax <= SECUREC_STRING_MAX_LEN
  91. * EINVAL_AND_RESET strDest != NULL and strSrc is NULL and destMax != 0 and destMax <= SECUREC_STRING_MAX_LEN
  92. * ERANGE destMax is 0 and destMax > SECUREC_STRING_MAX_LEN
  93. * ERANGE_AND_RESET strDest have not enough space and all other parameters are valid and not overlap
  94. * EOVERLAP_AND_RESET dest buffer and source buffer are overlapped and all parameters are valid
  95. *
  96. * If there is a runtime-constraint violation, strDest[0] will be set to the '\0' when strDest and destMax valid
  97. */
  98. errno_t strncpy_s(char *strDest, size_t destMax, const char *strSrc, size_t count)
  99. {
  100. if (SECUREC_STRNCPY_PARAM_OK(strDest, destMax, strSrc, count)) {
  101. size_t minCpLen; /* use it to store the maxi length limit */
  102. if (count < destMax) {
  103. minCpLen = SecStrMinLen(strSrc, count); /* no ending terminator */
  104. } else {
  105. size_t tmpCount = destMax;
  106. #ifdef SECUREC_COMPATIBLE_WIN_FORMAT
  107. if (count == ((size_t)(-1))) {
  108. tmpCount = destMax - 1;
  109. }
  110. #endif
  111. minCpLen = SecStrMinLen(strSrc, tmpCount);
  112. if (minCpLen == destMax) {
  113. strDest[0] = '\0';
  114. SECUREC_ERROR_INVALID_RANGE("strncpy_s");
  115. return ERANGE_AND_RESET;
  116. }
  117. }
  118. if (SECUREC_STRING_NO_OVERLAP(strDest, strSrc, minCpLen) || strDest == strSrc) {
  119. /* Not overlap */
  120. SecDoMemcpy(strDest, strSrc, minCpLen); /* copy string without terminator */
  121. strDest[minCpLen] = '\0';
  122. return EOK;
  123. } else {
  124. strDest[0] = '\0';
  125. SECUREC_ERROR_BUFFER_OVERLAP("strncpy_s");
  126. return EOVERLAP_AND_RESET;
  127. }
  128. }
  129. return strncpy_error(strDest, destMax, strSrc, count);
  130. }
  131. #if SECUREC_IN_KERNEL
  132. EXPORT_SYMBOL(strncpy_s);
  133. #endif