strcat_s.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright (c) Huawei Technologies Co., Ltd. 2014-2021. All rights reserved.
  3. * Licensed under Mulan PSL v2.
  4. * You can use this software according to the terms and conditions of the Mulan PSL v2.
  5. * You may obtain a copy of Mulan PSL v2 at:
  6. * http://license.coscl.org.cn/MulanPSL2
  7. * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
  8. * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
  9. * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
  10. * See the Mulan PSL v2 for more details.
  11. * Description: strcat_s function
  12. * Create: 2014-02-25
  13. */
  14. #include "securecutil.h"
  15. /*
  16. * Befor this function, the basic parameter checking has been done
  17. */
  18. SECUREC_INLINE errno_t SecDoCat(char *strDest, size_t destMax, const char *strSrc)
  19. {
  20. size_t destLen;
  21. size_t srcLen;
  22. size_t maxSrcLen;
  23. SECUREC_CALC_STR_LEN(strDest, destMax, &destLen);
  24. /* Only optimize strSrc, do not apply this function to strDest */
  25. maxSrcLen = destMax - destLen;
  26. SECUREC_CALC_STR_LEN_OPT(strSrc, maxSrcLen, &srcLen);
  27. if (SECUREC_CAT_STRING_IS_OVERLAP(strDest, destLen, strSrc, srcLen)) {
  28. strDest[0] = '\0';
  29. if (strDest + destLen <= strSrc && destLen == destMax) {
  30. SECUREC_ERROR_INVALID_PARAMTER("strcat_s");
  31. return EINVAL_AND_RESET;
  32. }
  33. SECUREC_ERROR_BUFFER_OVERLAP("strcat_s");
  34. return EOVERLAP_AND_RESET;
  35. }
  36. if (srcLen + destLen >= destMax || strDest == strSrc) {
  37. strDest[0] = '\0';
  38. if (destLen == destMax) {
  39. SECUREC_ERROR_INVALID_PARAMTER("strcat_s");
  40. return EINVAL_AND_RESET;
  41. }
  42. SECUREC_ERROR_INVALID_RANGE("strcat_s");
  43. return ERANGE_AND_RESET;
  44. }
  45. SECUREC_MEMCPY_WARP_OPT(strDest + destLen, strSrc, srcLen + 1); /* Single character length include \0 */
  46. return EOK;
  47. }
  48. /*
  49. * <FUNCTION DESCRIPTION>
  50. * The strcat_s function appends a copy of the string pointed to by strSrc (including the terminating null character)
  51. * to the end of the string pointed to by strDest.
  52. * The initial character of strSrc overwrites the terminating null character of strDest.
  53. * strcat_s will return EOVERLAP_AND_RESET if the source and destination strings overlap.
  54. *
  55. * Note that the second parameter is the total size of the buffer, not the
  56. * remaining size.
  57. *
  58. * <INPUT PARAMETERS>
  59. * strDest Null-terminated destination string buffer.
  60. * destMax Size of the destination string buffer.
  61. * strSrc Null-terminated source string buffer.
  62. *
  63. * <OUTPUT PARAMETERS>
  64. * strDest is updated
  65. *
  66. * <RETURN VALUE>
  67. * EOK Success
  68. * EINVAL strDest is NULL and destMax != 0 and destMax <= SECUREC_STRING_MAX_LEN
  69. * EINVAL_AND_RESET (strDest unterminated and all other parameters are valid) or
  70. * (strDest != NULL and strSrc is NULL and destMax != 0 and destMax <= SECUREC_STRING_MAX_LEN)
  71. * ERANGE destMax is 0 and destMax > SECUREC_STRING_MAX_LEN
  72. * ERANGE_AND_RESET strDest have not enough space and all other parameters are valid and not overlap
  73. * EOVERLAP_AND_RESET dest buffer and source buffer are overlapped and all parameters are valid
  74. *
  75. * If there is a runtime-constraint violation, strDest[0] will be set to the '\0' when strDest and destMax valid
  76. */
  77. errno_t strcat_s(char *strDest, size_t destMax, const char *strSrc)
  78. {
  79. if (destMax == 0 || destMax > SECUREC_STRING_MAX_LEN) {
  80. SECUREC_ERROR_INVALID_RANGE("strcat_s");
  81. return ERANGE;
  82. }
  83. if (strDest == NULL || strSrc == NULL) {
  84. SECUREC_ERROR_INVALID_PARAMTER("strcat_s");
  85. if (strDest != NULL) {
  86. strDest[0] = '\0';
  87. return EINVAL_AND_RESET;
  88. }
  89. return EINVAL;
  90. }
  91. return SecDoCat(strDest, destMax, strSrc);
  92. }
  93. #if SECUREC_EXPORT_KERNEL_SYMBOL
  94. EXPORT_SYMBOL(strcat_s);
  95. #endif