From dd506ba9950befd2a73ab9a6b194831b8880b768 Mon Sep 17 00:00:00 2001 From: peilixia Date: Sun, 9 Oct 2022 15:53:43 +0800 Subject: [PATCH 1/9] modify dependence Signed-off-by: peilixia Change-Id: I3aff76bc9a919b110c99030f8306ab7631edd4ad --- base/include/securec.h | 538 ------------------ base/include/securectype.h | 279 --------- base/test/unittest/common/BUILD.gn | 14 - .../unittest/common/utils_securec_test.cpp | 141 ----- bundle.json | 4 - 5 files changed, 976 deletions(-) delete mode 100644 base/include/securec.h delete mode 100644 base/include/securectype.h delete mode 100644 base/test/unittest/common/utils_securec_test.cpp diff --git a/base/include/securec.h b/base/include/securec.h deleted file mode 100644 index 553eaf4..0000000 --- a/base/include/securec.h +++ /dev/null @@ -1,538 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef __SECUREC_H__5D13A042_DC3F_4ED9_A8D1_882811274C27 -#define __SECUREC_H__5D13A042_DC3F_4ED9_A8D1_882811274C27 - -/* Compile in kernel under macro control */ -#ifndef SECUREC_IN_KERNEL -#ifdef __KERNEL__ -#define SECUREC_IN_KERNEL 1 -#else -#define SECUREC_IN_KERNEL 0 -#endif -#endif - -/* If you need high performance, enable the SECUREC_WITH_PERFORMANCE_ADDONS macro, default is enable . - * The macro is automatically closed on the windows platform in securectyp.h. - */ -#ifndef SECUREC_WITH_PERFORMANCE_ADDONS -#if SECUREC_IN_KERNEL -#define SECUREC_WITH_PERFORMANCE_ADDONS 0 -#else -#define SECUREC_WITH_PERFORMANCE_ADDONS 1 -#endif -#endif - -#include "securectype.h" -#include - -#ifndef SECUREC_HAVE_ERRNO_H -#if SECUREC_IN_KERNEL -#define SECUREC_HAVE_ERRNO_H 0 -#else -#define SECUREC_HAVE_ERRNO_H 1 -#endif -#endif - -/* EINVAL ERANGE may defined in errno.h */ -#if SECUREC_HAVE_ERRNO_H -#include -#endif - -/* If stack size on some embedded platform is limited, you can define the following macro - * which will put some variables on heap instead of stack. - * SECUREC_STACK_SIZE_LESS_THAN_1K - */ - -/* define error code */ -#if !defined(__STDC_WANT_LIB_EXT1__) || (defined(__STDC_WANT_LIB_EXT1__) && (__STDC_WANT_LIB_EXT1__ == 0)) -#ifndef SECUREC_DEFINED_ERRNO_TYPE -#define SECUREC_DEFINED_ERRNO_TYPE -/* just check whether macrodefinition exists. */ -#ifndef errno_t -typedef int errno_t; -#endif -#endif -#endif - -/* success */ -#ifndef EOK -#define EOK (0) -#endif - -#ifndef EINVAL -/* The src buffer is not correct and destination buffer can't not be reset */ -#define EINVAL (22) -#endif - -#ifndef EINVAL_AND_RESET -/* Once the error is detected, the dest buffer must be rest! */ -#define EINVAL_AND_RESET (22 | 128) -#endif - -#ifndef ERANGE -/* The destination buffer is not long enough and destination buffer can not be reset */ -#define ERANGE (34) -#endif - -#ifndef ERANGE_AND_RESET -/* Once the error is detected, the dest buffer must be rest! */ -#define ERANGE_AND_RESET (34 | 128) -#endif - -#ifndef EOVERLAP_AND_RESET -/* Once the buffer overlap is detected, the dest buffer must be rest! */ -#define EOVERLAP_AND_RESET (54 | 128) -#endif - -/* if you need export the function of this library in Win32 dll, use __declspec(dllexport) */ -#ifdef SECUREC_IS_DLL_LIBRARY -#ifdef SECUREC_DLL_EXPORT -#define SECUREC_API __declspec(dllexport) -#else -#define SECUREC_API __declspec(dllimport) -#endif -#else -/* Standardized function declaration . If a security function is declared in the your code, - * it may cause a compilation alarm,Please delete the security function you declared - */ -#define SECUREC_API extern -#endif - -#ifndef SECUREC_SNPRINTF_TRUNCATED -#define SECUREC_SNPRINTF_TRUNCATED 1 -#endif - -#ifdef __cplusplus -extern "C" { -#endif - - /** - * @Description:The memset_s function copies the value of c (converted to an unsigned char) into each of the first count characters of the object pointed to by dest. - * @param dest - destination address - * @param destMax -The maximum length of destination buffer - * @param c - the value to be copied - * @param count -copies first count characters of dest - * @return EOK if there was no runtime-constraint violation - */ - SECUREC_API errno_t memset_s(void *dest, size_t destMax, int c, size_t count); - - /* The memset_s security function is not provided in Windows system, but other security functions are provided. In this case, can only use the memset_s function */ -#ifndef SECUREC_ONLY_DECLARE_MEMSET - - /** - * @Description:The wmemcpy_s function copies n successive wide characters from the object pointed to by src into the object pointed to by dest. - * @param dest - destination address - * @param destMax -The maximum length of destination buffer - * @param src -source address - * @param count -copies count wide characters from the src - * @return EOK if there was no runtime-constraint violation - */ -#if SECUREC_IN_KERNEL == 0 - SECUREC_API errno_t wmemcpy_s(wchar_t *dest, size_t destMax, const wchar_t *src, size_t count); -#endif - /** - * @Description:The memmove_s function copies n characters from the object pointed to by src into the object pointed to by dest. - * @param dest - destination address - * @param destMax -The maximum length of destination buffer - * @param src -source address - * @param count -copies count wide characters from the src - * @return EOK if there was no runtime-constraint violation - */ - SECUREC_API errno_t memmove_s(void *dest, size_t destMax, const void *src, size_t count); -#if SECUREC_IN_KERNEL == 0 - /** - * @Description:The wmemmove_s function copies n successive wide characters from the object pointed to by src into the object pointed to by dest. - * @param dest - destination address - * @param destMax -The maximum length of destination buffer - * @param src -source address - * @param count -copies count wide characters from the src - * @return EOK if there was no runtime-constraint violation - */ - SECUREC_API errno_t wmemmove_s(wchar_t *dest, size_t destMax, const wchar_t *src, size_t count); - - /** - * @Description:The wcscpy_s function copies the wide string pointed to by strSrc (including theterminating null wide character) into the array pointed to by strDest - * @param strDest - destination address - * @param destMax -The maximum length of destination buffer - * @param strSrc -source address - * @return EOK if there was no runtime-constraint violation - */ - SECUREC_API errno_t wcscpy_s(wchar_t *strDest, size_t destMax, const wchar_t *strSrc); - - /** - * @Description:The wcsncpy_s function copies not more than n successive wide characters (not including the terminating null wide character) - * from the array pointed to by strSrc to the array pointed to by strDest - * @param strDest - destination address - * @param destMax -The maximum length of destination buffer(including the terminating wide character) - * @param strSrc -source address - * @param count -copies count wide characters from the src - * @return EOK if there was no runtime-constraint violation - */ - SECUREC_API errno_t wcsncpy_s(wchar_t *strDest, size_t destMax, const wchar_t *strSrc, size_t count); - - /** - * @Description:The wcscat_s function appends a copy of the wide string pointed to by strSrc (including the terminating null wide character) - * to the end of the wide string pointed to by strDest - * @param strDest - destination address - * @param destMax -The maximum length of destination buffer(including the terminating wide character) - * @param strSrc -source address - * @return EOK if there was no runtime-constraint violation - */ - SECUREC_API errno_t wcscat_s(wchar_t *strDest, size_t destMax, const wchar_t *strSrc); - - /** - * @Description:The wcsncat_s function appends not more than n successive wide characters (not including the terminating null wide character) - * from the array pointed to by strSrc to the end of the wide string pointed to by strDest. - * @param strDest - destination address - * @param destMax -The maximum length of destination buffer(including the terminating wide character) - * @param strSrc -source address - * @param count -copies count wide characters from the src - * @return EOK if there was no runtime-constraint violation - */ - SECUREC_API errno_t wcsncat_s(wchar_t *strDest, size_t destMax, const wchar_t *strSrc, size_t count); - - /** - * @Description: The strtok_s function parses a string into a sequence of tokens,On the first call to strtok_s the string to be parsed should be specified in strToken. - * In each subsequent call that should parse the same string, strToken should be NULL - * @param strToken - the string to be delimited - * @param strDelimit -specifies a set of characters that delimit the tokens in the parsed string - * @param context -is a pointer to a char * variable that is used internally by strtok_s function - * @return:returns a pointer to the first character of a token, or a null pointer if there is no token or there is a runtime-constraint violation. - */ - SECUREC_API char *strtok_s(char *strToken, const char *strDelimit, char **context); - - /** - * @Description: The wcstok_s function is the wide-character equivalent of the strtok_s function - * @param strToken - the string to be delimited - * @param strDelimit -specifies a set of characters that delimit the tokens in the parsed string - * @param context -is a pointer to a char * variable that is used internally by strtok_s function - * @return:returns a pointer to the first character of a token, or a null pointer if there is no token or there is a runtime-constraint violation. - */ - SECUREC_API wchar_t *wcstok_s(wchar_t *strToken, const wchar_t *strDelimit, wchar_t **context); - - /** - * @Description: The sprintf_s function is equivalent to the sprintf function except for the parameter destMax and the explicit runtime-constraints violation - * @param strDest - produce output according to a format ,write to the character string strDest - * @param destMax - The maximum length of destination buffer(including the terminating null byte ('\0')) - * @param format - format string - * @return:success the number of characters printed(not including the terminating null byte ('\0')), If an error occurred return -1. - */ - SECUREC_API int sprintf_s(char *strDest, size_t destMax, const char *format, ...) SECUREC_ATTRIBUTE(3, 4); - - /** - * @Description: The swprintf_s function is the wide-character equivalent of the sprintf_s function - * @param strDest - produce output according to a format ,write to the character string strDest - * @param destMax - The maximum length of destination buffer(including the terminating null ) - * @param format - format string - * @return:success the number of characters printed(not including the terminating null wide characte), If an error occurred return -1. - */ - SECUREC_API int swprintf_s(wchar_t *strDest, size_t destMax, const wchar_t *format, ...); - - /** - * @Description: The vsprintf_s function is equivalent to the vsprintf function except for the parameter destMax and the explicit runtime-constraints violation - * @param strDest - produce output according to a format ,write to the character string strDest - * @param destMax - The maximum length of destination buffer(including the terminating null wide characte) - * @param format - format string - * @param arglist - instead of a variable number of arguments - * @return:return the number of characters printed(not including the terminating null byte ('\0')), If an error occurred return -1. - */ - SECUREC_API int vsprintf_s(char *strDest, size_t destMax, const char *format, - va_list arglist) SECUREC_ATTRIBUTE(3, 0); - - /** - * @Description: The vswprintf_s function is the wide-character equivalent of the vsprintf_s function - * @param strDest - produce output according to a format ,write to the character string strDest - * @param destMax - The maximum length of destination buffer(including the terminating null ) - * @param format - format string - * @param arglist - instead of a variable number of arguments - * @return:return the number of characters printed(not including the terminating null wide characte), If an error occurred return -1. - */ - SECUREC_API int vswprintf_s(wchar_t *strDest, size_t destMax, const wchar_t *format, va_list arglist); - - /** - * @Description: The vsnprintf_s function is equivalent to the vsnprintf function except for the parameter destMax/count and the explicit runtime-constraints violation - * @param strDest - produce output according to a format ,write to the character string strDest - * @param destMax - The maximum length of destination buffer(including the terminating null byte ('\0')) - * @param count - do not write more than count bytes to strDest(not including the terminating null byte ('\0')) - * @param format - format string - * @param arglist - instead of a variable number of arguments - * @return:return the number of characters printed(not including the terminating null byte ('\0')), If an error occurred return -1.Pay special attention to returning -1 when truncation occurs - */ - SECUREC_API int vsnprintf_s(char *strDest, size_t destMax, size_t count, const char *format, - va_list arglist) SECUREC_ATTRIBUTE(4, 0); - - /** - * @Description: The snprintf_s function is equivalent to the snprintf function except for the parameter destMax/count and the explicit runtime-constraints violation - * @param strDest - produce output according to a format ,write to the character string strDest - * @param destMax - The maximum length of destination buffer(including the terminating null byte ('\0')) - * @param count - do not write more than count bytes to strDest(not including the terminating null byte ('\0')) - * @param format - format string - * @return:return the number of characters printed(not including the terminating null byte ('\0')), If an error occurred return -1.Pay special attention to returning -1 when truncation occurs - */ - SECUREC_API int snprintf_s(char *strDest, size_t destMax, size_t count, const char *format, - ...) SECUREC_ATTRIBUTE(4, 5); - -#if SECUREC_SNPRINTF_TRUNCATED - /** - * @Description: The vsnprintf_truncated_s function is equivalent to the vsnprintf_s function except no count parameter and return value - * @param strDest - produce output according to a format ,write to the character string strDest - * @param destMax - The maximum length of destination buffer(including the terminating null byte ('\0')) - * @param format - format string - * @param arglist - instead of a variable number of arguments - * @return:return the number of characters printed(not including the terminating null byte ('\0')), If an error occurred return -1.Pay special attention to returning destMax - 1 when truncation occurs - */ - SECUREC_API int vsnprintf_truncated_s(char *strDest, size_t destMax, const char *format, - va_list arglist) SECUREC_ATTRIBUTE(3, 0); - - /** - * @Description: The snprintf_truncated_s function is equivalent to the snprintf_2 function except no count parameter and return value - * @param strDest - produce output according to a format ,write to the character string strDest - * @param destMax - The maximum length of destination buffer(including the terminating null byte ('\0')) - * @param format - format string - * @return:return the number of characters printed(not including the terminating null byte ('\0')), If an error occurred return -1.Pay special attention to returning destMax - 1 when truncation occurs - */ - SECUREC_API int snprintf_truncated_s(char *strDest, size_t destMax, const char *format, ...) SECUREC_ATTRIBUTE(3, 4); -#endif - /** - * @Description: The scanf_s function is equivalent to fscanf_s with the argument stdin interposed before the arguments to scanf_s - * @param format - format string - * @return:returns the number of input items assigned, If an error occurred return -1. - */ - SECUREC_API int scanf_s(const char *format, ...); - - /** - * @Description: The wscanf_s function is the wide-character equivalent of the scanf_s function - * @param format - format string - * @return:returns the number of input items assigned, If an error occurred return -1. - */ - SECUREC_API int wscanf_s(const wchar_t *format, ...); - - /** - * @Description: The vscanf_s function is equivalent to scanf_s, with the variable argument list replaced by arglist - * @param format - format string - * @param arglist - instead of a variable number of arguments - * @return:returns the number of input items assigned, If an error occurred return -1. - */ - SECUREC_API int vscanf_s(const char *format, va_list arglist); - - /** - * @Description: The vwscanf_s function is the wide-character equivalent of the vscanf_s function - * @param format - format string - * @param arglist - instead of a variable number of arguments - * @return:returns the number of input items assigned, If an error occurred return -1. - */ - SECUREC_API int vwscanf_s(const wchar_t *format, va_list arglist); - - /** - * @Description: The fscanf_s function is equivalent to fscanf except that the c, s, and [ conversion specifiers apply to a pair of arguments (unless assignment suppression is indicated by a*) - * @param stream - stdio file stream - * @param format - format string - * @return:returns the number of input items assigned, If an error occurred return -1. - */ - SECUREC_API int fscanf_s(FILE *stream, const char *format, ...); - - /** - * @Description: The fwscanf_s function is the wide-character equivalent of the fscanf_s function - * @param stream - stdio file stream - * @param format - format string - * @return:returns the number of input items assigned, If an error occurred return -1. - */ - SECUREC_API int fwscanf_s(FILE *stream, const wchar_t *format, ...); - - /** - * @Description: The vfscanf_s function is equivalent to fscanf_s, with the variable argument list replaced by arglist - * @param stream - stdio file stream - * @param format - format string - * @param arglist - instead of a variable number of arguments - * @return:returns the number of input items assigned, If an error occurred return -1. - */ - SECUREC_API int vfscanf_s(FILE *stream, const char *format, va_list arglist); - - /** - * @Description: The vfwscanf_s function is the wide-character equivalent of the vfscanf_s function - * @param stream - stdio file stream - * @param format - format string - * @param arglist - instead of a variable number of arguments - * @return:returns the number of input items assigned, If an error occurred return -1. - */ - SECUREC_API int vfwscanf_s(FILE *stream, const wchar_t *format, va_list arglist); - - /** - * @Description: The sscanf_s function is equivalent to fscanf_s, except that input is obtained from a string (specified by the argument buffer) rather than from a stream - * @param buffer - read character from buffer - * @param format - format string - * @return:returns the number of input items assigned, If an error occurred return -1. - */ - SECUREC_API int sscanf_s(const char *buffer, const char *format, ...); - - /** - * @Description: The swscanf_s function is the wide-character equivalent of the sscanf_s function - * @param buffer - read character from buffer - * @param format - format string - * @return:returns the number of input items assigned, If an error occurred return -1. - */ - SECUREC_API int swscanf_s(const wchar_t *buffer, const wchar_t *format, ...); - - /** - * @Description: The vsscanf_s function is equivalent to sscanf_s, with the variable argument list replaced by arglist - * @param buffer - read character from buffer - * @param format - format string - * @param arglist - instead of a variable number of arguments - * @return:returns the number of input items assigned, If an error occurred return -1. - */ - SECUREC_API int vsscanf_s(const char *buffer, const char *format, va_list arglist); - - /** - * @Description: The vswscanf_s function is the wide-character equivalent of the vsscanf_s function - * @param buffer - read character from buffer - * @param format - format string - * @param arglist - instead of a variable number of arguments - * @return:returns the number of input items assigned, If an error occurred return -1. - */ - SECUREC_API int vswscanf_s(const wchar_t *buffer, const wchar_t *format, va_list arglist); - - /** - * @Description:The gets_s function reads at most one less than the number of characters specified by destMax from the stream pointed to by stdin, into the array pointed to by buffer - * @param buffer - destination address - * @param destMax -The maximum length of destination buffer(including the terminating null character) - * @return buffer if there was no runtime-constraint violation,If an error occurred return NULL. - */ - SECUREC_API char *gets_s(char *buffer, size_t destMax); -#endif - - /** - * @Description:The memcpy_s function copies n characters from the object pointed to by src into the object pointed to by dest. - * @param dest - destination address - * @param destMax -The maximum length of destination buffer - * @param src -source address - * @param count -copies count characters from the src - * @return EOK if there was no runtime-constraint violation - */ - SECUREC_API errno_t memcpy_s(void *dest, size_t destMax, const void *src, size_t count); - - /** - * @Description:The strcpy_s function copies the string pointed to by strSrc (including the terminating null character) into the array pointed to by strDest - * @param strDest - destination address - * @param destMax -The maximum length of destination buffer(including the terminating null character) - * @param strSrc -source address - * @return EOK if there was no runtime-constraint violation - */ - SECUREC_API errno_t strcpy_s(char *strDest, size_t destMax, const char *strSrc); - - /** - * @Description:The strncpy_s function copies not more than n successive characters (not including the terminating null character) - * from the array pointed to by strSrc to the array pointed to by strDest - * @param strDest - destination address - * @param destMax -The maximum length of destination buffer(including the terminating null character) - * @param strSrc -source address - * @param count -copies count characters from the src - * @return EOK if there was no runtime-constraint violation - */ - SECUREC_API errno_t strncpy_s(char *strDest, size_t destMax, const char *strSrc, size_t count); - - /** - * @Description:The strcat_s function appends a copy of the string pointed to by strSrc (including the terminating null character) - * to the end of the string pointed to by strDest - * @param strDest - destination address - * @param destMax -The maximum length of destination buffer(including the terminating null wide character) - * @param strSrc -source address - * @return EOK if there was no runtime-constraint violation - */ - SECUREC_API errno_t strcat_s(char *strDest, size_t destMax, const char *strSrc); - - /** - * @Description:The strncat_s function appends not more than n successive characters (not including the terminating null character) - * from the array pointed to by strSrc to the end of the string pointed to by strDest. - * @param strDest - destination address - * @param destMax -The maximum length of destination buffer(including the terminating null character) - * @param strSrc -source address - * @param count -copies count characters from the src - * @return EOK if there was no runtime-constraint violation - */ - SECUREC_API errno_t strncat_s(char *strDest, size_t destMax, const char *strSrc, size_t count); -#if SECUREC_IN_KERNEL == 0 - /* those functions are used by macro ,must declare hare , also for without function declaration warning */ - extern errno_t strncpy_error(char *strDest, size_t destMax, const char *strSrc, size_t count); - extern errno_t strcpy_error(char *strDest, size_t destMax, const char *strSrc); -#endif -#endif - -#if SECUREC_WITH_PERFORMANCE_ADDONS - /* those functions are used by macro */ - extern errno_t memset_sOptAsm(void *dest, size_t destMax, int c, size_t count); - extern errno_t memset_sOptTc(void *dest, size_t destMax, int c, size_t count); - extern errno_t memcpy_sOptAsm(void *dest, size_t destMax, const void *src, size_t count); - extern errno_t memcpy_sOptTc(void *dest, size_t destMax, const void *src, size_t count); - -/* strcpy_sp is a macro, NOT a function in performance optimization mode. */ -#define strcpy_sp(dest, destMax, src) ((__builtin_constant_p((destMax)) && \ - __builtin_constant_p((src))) ? \ - SECUREC_STRCPY_SM((dest), (destMax), (src)) : \ - strcpy_s((dest), (destMax), (src))) - -/* strncpy_sp is a macro, NOT a function in performance optimization mode. */ -#define strncpy_sp(dest, destMax, src, count) ((__builtin_constant_p((count)) && \ - __builtin_constant_p((destMax)) && \ - __builtin_constant_p((src))) ? \ - SECUREC_STRNCPY_SM((dest), (destMax), (src), (count)) : \ - strncpy_s((dest), (destMax), (src), (count))) - -/* strcat_sp is a macro, NOT a function in performance optimization mode. */ -#define strcat_sp(dest, destMax, src) ((__builtin_constant_p((destMax)) && \ - __builtin_constant_p((src))) ? \ - SECUREC_STRCAT_SM((dest), (destMax), (src)) : \ - strcat_s((dest), (destMax), (src))) - - /* strncat_sp is a macro, NOT a function in performance optimization mode. */ -#define strncat_sp(dest, destMax, src, count) ((__builtin_constant_p((count)) && \ - __builtin_constant_p((destMax)) && \ - __builtin_constant_p((src))) ? \ - SECUREC_STRNCAT_SM((dest), (destMax), (src), (count)) : \ - strncat_s((dest), (destMax), (src), (count))) - -/* memcpy_sp is a macro, NOT a function in performance optimization mode. */ -#define memcpy_sp(dest, destMax, src, count) (__builtin_constant_p((count)) ? \ - (SECUREC_MEMCPY_SM((dest), (destMax), (src), (count))) : \ - (__builtin_constant_p((destMax)) ? \ - (((size_t)(destMax) > 0 && \ - (((unsigned long long)(destMax) & \ - (unsigned long long)(-2)) < SECUREC_MEM_MAX_LEN)) ? \ - memcpy_sOptTc((dest), (destMax), (src), (count)) : ERANGE ) : \ - memcpy_sOptAsm((dest), (destMax), (src), (count)))) - -/* memset_sp is a macro, NOT a function in performance optimization mode. */ -#define memset_sp(dest, destMax, c, count) (__builtin_constant_p((count)) ? \ - (SECUREC_MEMSET_SM((dest), (destMax), (c), (count))) : \ - (__builtin_constant_p((destMax)) ? \ - (((size_t)(destMax) > 0 && \ - (((unsigned long long)(destMax) & \ - (unsigned long long)(-2)) < SECUREC_MEM_MAX_LEN)) ? \ - memset_sOptTc((dest), (destMax), (c), (count)) : ERANGE ) : \ - memset_sOptAsm((dest), (destMax), (c), (count)))) -#else -#define strcpy_sp strcpy_s -#define strncpy_sp strncpy_s -#define strcat_sp strcat_s -#define strncat_sp strncat_s -#define memcpy_sp memcpy_s -#define memset_sp memset_s -#endif - -#ifdef __cplusplus -} -#endif /* __cplusplus */ -#endif /* __SECUREC_H__5D13A042_DC3F_4ED9_A8D1_882811274C27 */ diff --git a/base/include/securectype.h b/base/include/securectype.h deleted file mode 100644 index 029772c..0000000 --- a/base/include/securectype.h +++ /dev/null @@ -1,279 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -/* [Standardize-exceptions]: Performance-sensitive - * [reason]: Strict parameter verification has been done before use - */ - -#ifndef __SECURECTYPE_H__A7BBB686_AADA_451B_B9F9_44DACDAE18A7 -#define __SECURECTYPE_H__A7BBB686_AADA_451B_B9F9_44DACDAE18A7 - -#ifndef SECUREC_ONLY_DECLARE_MEMSET -/* Shielding VC symbol redefinition warning */ -#if defined(_MSC_VER) && (_MSC_VER >= 1400) -#ifdef __STDC_WANT_SECURE_LIB__ -#undef __STDC_WANT_SECURE_LIB__ -#endif -#define __STDC_WANT_SECURE_LIB__ 0 -#ifdef _CRTIMP_ALTERNATIVE -#undef _CRTIMP_ALTERNATIVE -#endif -#define _CRTIMP_ALTERNATIVE //comment microsoft *_s function -#endif -#endif - -#if SECUREC_IN_KERNEL -#include -#include -#else -#include -#include -#include -#endif - -/* if enable SECUREC_COMPATIBLE_WIN_FORMAT, the output format will be compatible to Windows. */ -#if (defined(_WIN32) || defined(_WIN64) || defined(_MSC_VER)) -#define SECUREC_COMPATIBLE_WIN_FORMAT -#endif - -#if defined(SECUREC_COMPATIBLE_WIN_FORMAT) -/* in windows platform, can't use optimized function for there is no __builtin_constant_p like function */ -/* If need optimized macro, can define this: define __builtin_constant_p(x) 0 */ -#ifdef SECUREC_WITH_PERFORMANCE_ADDONS -#undef SECUREC_WITH_PERFORMANCE_ADDONS -#define SECUREC_WITH_PERFORMANCE_ADDONS 0 -#endif -#endif - -#if defined(__VXWORKS__) || defined(__vxworks) || defined(__VXWORKS) || defined(_VXWORKS_PLATFORM_) || \ - defined(SECUREC_VXWORKS_VERSION_5_4) -#if !defined(SECUREC_VXWORKS_PLATFORM) -#define SECUREC_VXWORKS_PLATFORM -#endif -#endif - -/* if enable SECUREC_COMPATIBLE_LINUX_FORMAT, the output format will be compatible to Linux. */ -#if !(defined(SECUREC_COMPATIBLE_WIN_FORMAT) || defined(SECUREC_VXWORKS_PLATFORM)) -#define SECUREC_COMPATIBLE_LINUX_FORMAT -#endif -#ifdef SECUREC_COMPATIBLE_LINUX_FORMAT -#include -#endif - -/* add the -DSECUREC_SUPPORT_FORMAT_WARNING compiler option to support -Wformat. - * default does not check the format is that the same data type in the actual code - * in the product is different in the original data type definition of VxWorks and Linux. - */ -#ifndef SECUREC_SUPPORT_FORMAT_WARNING -#define SECUREC_SUPPORT_FORMAT_WARNING 0 -#endif - -/* SECUREC_PCLINT for tool do not recognize __attribute__ just for pclint */ -#if SECUREC_SUPPORT_FORMAT_WARNING && !defined(SECUREC_PCLINT) -#define SECUREC_ATTRIBUTE(x, y) __attribute__((format(printf, (x), (y)))) -#else -#define SECUREC_ATTRIBUTE(x, y) -#endif - -/* SECUREC_PCLINT for tool do not recognize __builtin_expect ,just for pclint */ -#if defined(__GNUC__) && \ - ((__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 3 /* above 3.4 */))) && \ - !defined(SECUREC_PCLINT) -/* This is a built-in function that can be used without a declaration, if you encounter an undeclared compilation alarm, - * you can add -DSECUREC_NEED_BUILTIN_EXPECT_DECLARE to compiler options - */ -#if defined(SECUREC_NEED_BUILTIN_EXPECT_DECLARE) -long __builtin_expect(long exp, long c); -#endif -#define SECUREC_LIKELY(x) __builtin_expect(!!(x), 1) -#define SECUREC_UNLIKELY(x) __builtin_expect(!!(x), 0) -#else -#define SECUREC_LIKELY(x) (x) -#define SECUREC_UNLIKELY(x) (x) -#endif - -/* define the max length of the string */ -#define SECUREC_STRING_MAX_LEN (0x7fffffffUL) -#define SECUREC_WCHAR_STRING_MAX_LEN (SECUREC_STRING_MAX_LEN / sizeof(wchar_t)) - -/* add SECUREC_MEM_MAX_LEN for memcpy and memmove */ -#define SECUREC_MEM_MAX_LEN (0x7fffffffUL) -#define SECUREC_WCHAR_MEM_MAX_LEN (SECUREC_MEM_MAX_LEN / sizeof(wchar_t)) - -#if SECUREC_STRING_MAX_LEN > 0x7fffffff -#error "max string is 2G" -#endif - -#if (defined(__GNUC__ ) && defined(__SIZEOF_POINTER__ )) -#if (__SIZEOF_POINTER__ != 4) && (__SIZEOF_POINTER__ != 8) -#error "unsupported system" -#endif -#endif - -#ifndef SECUREC_MALLOC -#define SECUREC_MALLOC(x) malloc((size_t)(x)) -#endif - -#ifndef SECUREC_FREE -#define SECUREC_FREE(x) free((void *)(x)) -#endif - -#if defined(_WIN64) || defined(WIN64) || defined(__LP64__) || defined(_LP64) -#define SECUREC_ON_64BITS -#endif - -#if (!defined(SECUREC_ON_64BITS) && defined(__GNUC__ ) && defined(__SIZEOF_POINTER__ )) -#if __SIZEOF_POINTER__ == 8 -#define SECUREC_ON_64BITS -#endif -#endif - -#if defined(__SVR4) || defined(__svr4__) -#define SECUREC_ON_SOLARIS -#endif - -#if (defined(__hpux) || defined(_AIX) || defined(SECUREC_ON_SOLARIS)) -#define SECUREC_ON_UNIX -#endif - -/* codes should run under the macro SECUREC_COMPATIBLE_LINUX_FORMAT in unknown system on default, - * and strtold. The function - * strtold is referenced first at ISO9899:1999(C99), and some old compilers can - * not support these functions. Here provides a macro to open these functions: - * SECUREC_SUPPORT_STRTOLD -- if defined, strtold will be used - */ -#ifndef SECUREC_SUPPORT_STRTOLD -#define SECUREC_SUPPORT_STRTOLD 0 -#if (defined(SECUREC_COMPATIBLE_LINUX_FORMAT)) -#if defined(__USE_ISOC99) || \ - (defined(_AIX) && defined(_ISOC99_SOURCE)) || \ - (defined(__hpux) && defined(__ia64)) || \ - (defined(SECUREC_ON_SOLARIS) && (!defined(_STRICT_STDC) && !defined(__XOPEN_OR_POSIX)) || \ - defined(_STDC_C99) || defined(__EXTENSIONS__)) -#undef SECUREC_SUPPORT_STRTOLD -#define SECUREC_SUPPORT_STRTOLD 1 -#endif -#endif -#if ((defined(SECUREC_WRLINUX_BELOW4) || defined(_WRLINUX_BELOW4_))) -#undef SECUREC_SUPPORT_STRTOLD -#define SECUREC_SUPPORT_STRTOLD 0 -#endif -#endif - -#if SECUREC_WITH_PERFORMANCE_ADDONS - -#ifndef SECUREC_TWO_MIN -#define SECUREC_TWO_MIN(a, b) ((a) < (b) ? (a) : (b)) -#endif - -/* for strncpy_s performance optimization */ -#define SECUREC_STRNCPY_SM(dest, destMax, src, count) \ - (((void *)dest != NULL && (void *)src != NULL && (size_t)destMax >0 && \ - (((unsigned long long)(destMax) & (unsigned long long)(-2)) < SECUREC_STRING_MAX_LEN) && \ - (SECUREC_TWO_MIN(count , strlen(src)) + 1) <= (size_t)destMax) ? \ - ((count < strlen(src))? (memcpy(dest, src, count), *((char *)dest + count) = '\0', EOK): \ - (memcpy(dest, src, strlen(src) + 1), EOK )) :(strncpy_error(dest, destMax, src, count))) - -#define SECUREC_STRCPY_SM(dest, destMax, src) \ - (((void *)dest != NULL && (void *)src != NULL && (size_t)destMax >0 && \ - (((unsigned long long)(destMax) & (unsigned long long)(-2)) < SECUREC_STRING_MAX_LEN) && \ - (strlen(src) + 1) <= (size_t)destMax)? (memcpy(dest, src, strlen(src) + 1), EOK): \ - (strcpy_error(dest, destMax, src))) - -/* for strcat_s performance optimization */ -#if defined(__GNUC__) -#define SECUREC_STRCAT_SM(dest, destMax, src) \ - ({ \ - int catRet = EOK; \ - if ((void *)dest != NULL && (void *)src != NULL && (size_t)(destMax) >0 && \ - (((unsigned long long)(destMax) & (unsigned long long)(-2)) < SECUREC_STRING_MAX_LEN)) { \ - char *catTmpDst = (dest); \ - size_t catRestSize = (destMax); \ - while(catRestSize > 0 && *catTmpDst) { \ - ++catTmpDst; \ - --catRestSize; \ - } \ - if (catRestSize == 0) { \ - catRet = EINVAL; \ - } else if ((strlen(src) + 1) <= catRestSize) { \ - (void)memcpy(catTmpDst, (src), strlen(src) + 1); \ - catRet = EOK; \ - } else { \ - catRet = ERANGE; \ - } \ - if (catRet != EOK) { \ - catRet = strcat_s((dest), (destMax), (src)); \ - } \ - } else { \ - catRet = strcat_s((dest), (destMax), (src)); \ - } \ - catRet; \ - }) -#else -#define SECUREC_STRCAT_SM(dest, destMax, src) strcat_s(dest, destMax, src) -#endif - -/* for strncat_s performance optimization */ -#if defined(__GNUC__) -#define SECUREC_STRNCAT_SM(dest, destMax, src, count) \ - ({ \ - int ncatRet = EOK; \ - if ((void *)dest != NULL && (void *)src != NULL && (size_t)destMax > 0 && \ - (((unsigned long long)(destMax) & (unsigned long long)(-2)) < SECUREC_STRING_MAX_LEN) && \ - (((unsigned long long)(count) & (unsigned long long)(-2)) < SECUREC_STRING_MAX_LEN)) { \ - char *ncatTmpDest = (dest); \ - size_t ncatRestSize = (destMax); \ - while(ncatRestSize > 0 && *ncatTmpDest) { \ - ++ncatTmpDest; \ - --ncatRestSize; \ - } \ - if (ncatRestSize == 0) { \ - ncatRet = EINVAL; \ - } else if ((SECUREC_TWO_MIN((count) , strlen(src)) + 1) <= ncatRestSize ) { \ - if ((count) < strlen(src)) { \ - (void)memcpy(ncatTmpDest, (src), (count)); \ - *(ncatTmpDest + (count)) = '\0'; \ - } else { \ - (void)memcpy(ncatTmpDest, (src), strlen(src) + 1); \ - } \ - } else { \ - ncatRet = ERANGE; \ - } \ - if (ncatRet != EOK) { \ - ncatRet = strncat_s((dest), (destMax), (src), (count)); \ - } \ - } else { \ - ncatRet = strncat_s((dest), (destMax), (src), (count)); \ - } \ - ncatRet; \ - }) -#else -#define SECUREC_STRNCAT_SM(dest, destMax, src, count) strncat_s(dest, destMax, src, count) -#endif - -/* SECUREC_MEMCPY_SM do NOT check buffer overlap by default */ -#define SECUREC_MEMCPY_SM(dest, destMax, src, count) \ - (!(((size_t)destMax == 0 )||(((unsigned long long)(destMax) & (unsigned long long)(-2)) > SECUREC_MEM_MAX_LEN) || \ - ((size_t)count > (size_t)destMax) || ((void *)dest) == NULL || ((void *)src == NULL))? \ - (memcpy(dest, src, count), EOK) : \ - (memcpy_s(dest, destMax, src, count))) - -#define SECUREC_MEMSET_SM(dest, destMax, c, count) \ - (!(((size_t)destMax == 0 ) || (((unsigned long long)(destMax) & (unsigned long long)(-2)) > SECUREC_MEM_MAX_LEN) || \ - ((void *)dest == NULL) || ((size_t)count > (size_t)destMax)) ? \ - (memset(dest, c, count), EOK) : \ - ( memset_s(dest, destMax, c, count))) - -#endif -#endif /* __SECURECTYPE_H__A7BBB686_AADA_451B_B9F9_44DACDAE18A7 */ diff --git a/base/test/unittest/common/BUILD.gn b/base/test/unittest/common/BUILD.gn index 0d7f4b4..22b36c9 100644 --- a/base/test/unittest/common/BUILD.gn +++ b/base/test/unittest/common/BUILD.gn @@ -39,19 +39,6 @@ ohos_unittest("UtilsStringTest") { ] } -##############################unittest########################################## -ohos_unittest("UtilsSecurecTest") { - module_out_path = module_output_path - sources = [ "utils_securec_test.cpp" ] - - configs = [ ":module_private_config" ] - - deps = [ - "//commonlibrary/c_utils/base:utils", - "//third_party/googletest:gtest_main", - ] -} - ##############################unittest########################################## ohos_unittest("UtilsDirectoryTest") { module_out_path = module_output_path @@ -309,7 +296,6 @@ group("unittest") { ":UtilsSafeBlockQueueTrackingTest", ":UtilsSafeMapTest", ":UtilsSafeQueueTest", - ":UtilsSecurecTest", ":UtilsSingletonTest", ":UtilsSortedVectorTest", ":UtilsStringTest", diff --git a/base/test/unittest/common/utils_securec_test.cpp b/base/test/unittest/common/utils_securec_test.cpp deleted file mode 100644 index e07af92..0000000 --- a/base/test/unittest/common/utils_securec_test.cpp +++ /dev/null @@ -1,141 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include "string_ex.h" -#include "securec.h" -using namespace testing::ext; -using namespace OHOS; - -class UtilsSecurecTest : public testing::Test -{ -public : - static void SetUpTestCase(void); - static void TearDownTestCase(void); - void SetUp(); - void TearDown(); -}; - -void UtilsSecurecTest::SetUpTestCase(void) -{ - // step 2: input testsuit setup step -} - -void UtilsSecurecTest::TearDownTestCase(void) -{ - // step 2: input testsuit teardown step -} - -void UtilsSecurecTest::SetUp(void) -{ - // step 3: input testcase setup step -} - -void UtilsSecurecTest::TearDown(void) -{ - // step 3: input testcase teardown step -} - -HWTEST_F(UtilsSecurecTest, test_memset_s_01, TestSize.Level0) -{ - char cBase[20]; - errno_t result = memset_s(reinterpret_cast(cBase), sizeof(char) * 20, 1, sizeof(char) * 20); - EXPECT_EQ(result, 0); -} - -HWTEST_F(UtilsSecurecTest, test_memset_s_02, TestSize.Level0) -{ - char cBase[20]; - errno_t result = memset_s(reinterpret_cast(cBase), sizeof(char) * 20, 1, sizeof(char) * 21); - EXPECT_NE(result, 0); -} - -HWTEST_F(UtilsSecurecTest, test_memcpy_s_01, TestSize.Level0) -{ - char cBase[20] = "memcpy_s"; - char cTemp[20]; - errno_t result = memcpy_s(reinterpret_cast(cTemp), sizeof(char) * 20, cBase, sizeof(cBase)); - EXPECT_EQ(result, 0); - EXPECT_EQ(0, strcmp(cTemp, cBase)); -} - -HWTEST_F(UtilsSecurecTest, test_memcpy_s_02, TestSize.Level0) -{ - char cBase[20] = "memcpy_s"; - char cTemp[5]; - errno_t result = memcpy_s(reinterpret_cast(cTemp), sizeof(char) * 5, cBase, sizeof(cBase)); - EXPECT_NE(result, 0); -} - -HWTEST_F(UtilsSecurecTest, test_strcpy_s_01, TestSize.Level0) -{ - const char cBase[] = "strcpy_base"; - char cTemp[20]; - errno_t result = strcpy_s(cTemp, sizeof(cTemp), cBase); - EXPECT_EQ(result, 0); - EXPECT_EQ(0, strcmp(cTemp, cBase)); -} - -HWTEST_F(UtilsSecurecTest, test_strcpy_s_02, TestSize.Level0) -{ - const char cBase[] = "strcpy_base"; - char cTemp[10]; - errno_t result = strcpy_s(cTemp, sizeof(cTemp), cBase); - EXPECT_NE(result, 0); -} - -HWTEST_F(UtilsSecurecTest, test_strncpy_s_01, TestSize.Level0) -{ - const char cBase[] = "strcpy_base"; - char cTemp[20]; - errno_t result = strncpy_s(cTemp, sizeof(cTemp), cBase, sizeof(cBase)); - EXPECT_EQ(result, 0); - EXPECT_EQ(0, strcmp(cTemp, cBase)); -} - -HWTEST_F(UtilsSecurecTest, test_strncpy_s_02, TestSize.Level0) -{ - const char cBase[] = "strcpy_base"; - char cTemp[10]; - errno_t result = strncpy_s(cTemp, sizeof(cTemp), cBase, (sizeof(cTemp) -1)); - EXPECT_EQ(result, 0); - result = strncpy_s(cTemp, sizeof(cTemp), cBase, sizeof(cBase)); - EXPECT_NE(result, 0); -} - -HWTEST_F(UtilsSecurecTest, test_strcat_s_01, TestSize.Level0) -{ - const char cBase[] = "strcpy_base"; - char cTemp[20] = "temp "; - errno_t result = strcat_s(cTemp, sizeof(cTemp), cBase); - EXPECT_EQ(result, 0); - EXPECT_EQ(0, strcmp(cTemp, "temp strcpy_base")); -} - -HWTEST_F(UtilsSecurecTest, test_strcat_s_02, TestSize.Level0) -{ - const char cBase[] = "strcpy_base"; - char cTemp[10]; - errno_t result = strcat_s(cTemp, sizeof(cTemp), cBase); - EXPECT_NE(result, 0); -} - -HWTEST_F(UtilsSecurecTest, test_sprintf_s_01, TestSize.Level0) -{ - char cBase[64] = { 0 }; - errno_t result = sprintf_s(cBase, sizeof(cBase), "%d", 12345); - EXPECT_EQ(string(cBase), "12345"); - EXPECT_NE(result, 0); -} \ No newline at end of file diff --git a/bundle.json b/bundle.json index d103312..f6b5a12 100644 --- a/bundle.json +++ b/bundle.json @@ -49,8 +49,6 @@ "safe_block_queue.h", "safe_map.h", "safe_queue.h", - "securec.h", - "securectype.h", "semaphore_ex.h", "singleton.h", "sorted_vector.h", @@ -84,8 +82,6 @@ "safe_block_queue.h", "safe_map.h", "safe_queue.h", - "securec.h", - "securectype.h", "semaphore_ex.h", "singleton.h", "sorted_vector.h", -- Gitee From 973157b0c2a3a0857fe5c26989ed87e1d686d4c0 Mon Sep 17 00:00:00 2001 From: openharmony_ci <120357966@qq.com> Date: Wed, 19 Oct 2022 00:33:59 +0000 Subject: [PATCH 2/9] =?UTF-8?q?=E5=9B=9E=E9=80=80=20'Pull=20Request=20!128?= =?UTF-8?q?=20:=20Delete=20securec.h,=20securectype.h,=20utils=5Fsecurec?= =?UTF-8?q?=5Ftest.cpp=20and=20modify=20compile=20file'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- base/include/securec.h | 538 ++++++++++++++++++ base/include/securectype.h | 279 +++++++++ base/test/unittest/common/BUILD.gn | 14 + .../unittest/common/utils_securec_test.cpp | 141 +++++ bundle.json | 4 + 5 files changed, 976 insertions(+) create mode 100644 base/include/securec.h create mode 100644 base/include/securectype.h create mode 100644 base/test/unittest/common/utils_securec_test.cpp diff --git a/base/include/securec.h b/base/include/securec.h new file mode 100644 index 0000000..553eaf4 --- /dev/null +++ b/base/include/securec.h @@ -0,0 +1,538 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __SECUREC_H__5D13A042_DC3F_4ED9_A8D1_882811274C27 +#define __SECUREC_H__5D13A042_DC3F_4ED9_A8D1_882811274C27 + +/* Compile in kernel under macro control */ +#ifndef SECUREC_IN_KERNEL +#ifdef __KERNEL__ +#define SECUREC_IN_KERNEL 1 +#else +#define SECUREC_IN_KERNEL 0 +#endif +#endif + +/* If you need high performance, enable the SECUREC_WITH_PERFORMANCE_ADDONS macro, default is enable . + * The macro is automatically closed on the windows platform in securectyp.h. + */ +#ifndef SECUREC_WITH_PERFORMANCE_ADDONS +#if SECUREC_IN_KERNEL +#define SECUREC_WITH_PERFORMANCE_ADDONS 0 +#else +#define SECUREC_WITH_PERFORMANCE_ADDONS 1 +#endif +#endif + +#include "securectype.h" +#include + +#ifndef SECUREC_HAVE_ERRNO_H +#if SECUREC_IN_KERNEL +#define SECUREC_HAVE_ERRNO_H 0 +#else +#define SECUREC_HAVE_ERRNO_H 1 +#endif +#endif + +/* EINVAL ERANGE may defined in errno.h */ +#if SECUREC_HAVE_ERRNO_H +#include +#endif + +/* If stack size on some embedded platform is limited, you can define the following macro + * which will put some variables on heap instead of stack. + * SECUREC_STACK_SIZE_LESS_THAN_1K + */ + +/* define error code */ +#if !defined(__STDC_WANT_LIB_EXT1__) || (defined(__STDC_WANT_LIB_EXT1__) && (__STDC_WANT_LIB_EXT1__ == 0)) +#ifndef SECUREC_DEFINED_ERRNO_TYPE +#define SECUREC_DEFINED_ERRNO_TYPE +/* just check whether macrodefinition exists. */ +#ifndef errno_t +typedef int errno_t; +#endif +#endif +#endif + +/* success */ +#ifndef EOK +#define EOK (0) +#endif + +#ifndef EINVAL +/* The src buffer is not correct and destination buffer can't not be reset */ +#define EINVAL (22) +#endif + +#ifndef EINVAL_AND_RESET +/* Once the error is detected, the dest buffer must be rest! */ +#define EINVAL_AND_RESET (22 | 128) +#endif + +#ifndef ERANGE +/* The destination buffer is not long enough and destination buffer can not be reset */ +#define ERANGE (34) +#endif + +#ifndef ERANGE_AND_RESET +/* Once the error is detected, the dest buffer must be rest! */ +#define ERANGE_AND_RESET (34 | 128) +#endif + +#ifndef EOVERLAP_AND_RESET +/* Once the buffer overlap is detected, the dest buffer must be rest! */ +#define EOVERLAP_AND_RESET (54 | 128) +#endif + +/* if you need export the function of this library in Win32 dll, use __declspec(dllexport) */ +#ifdef SECUREC_IS_DLL_LIBRARY +#ifdef SECUREC_DLL_EXPORT +#define SECUREC_API __declspec(dllexport) +#else +#define SECUREC_API __declspec(dllimport) +#endif +#else +/* Standardized function declaration . If a security function is declared in the your code, + * it may cause a compilation alarm,Please delete the security function you declared + */ +#define SECUREC_API extern +#endif + +#ifndef SECUREC_SNPRINTF_TRUNCATED +#define SECUREC_SNPRINTF_TRUNCATED 1 +#endif + +#ifdef __cplusplus +extern "C" { +#endif + + /** + * @Description:The memset_s function copies the value of c (converted to an unsigned char) into each of the first count characters of the object pointed to by dest. + * @param dest - destination address + * @param destMax -The maximum length of destination buffer + * @param c - the value to be copied + * @param count -copies first count characters of dest + * @return EOK if there was no runtime-constraint violation + */ + SECUREC_API errno_t memset_s(void *dest, size_t destMax, int c, size_t count); + + /* The memset_s security function is not provided in Windows system, but other security functions are provided. In this case, can only use the memset_s function */ +#ifndef SECUREC_ONLY_DECLARE_MEMSET + + /** + * @Description:The wmemcpy_s function copies n successive wide characters from the object pointed to by src into the object pointed to by dest. + * @param dest - destination address + * @param destMax -The maximum length of destination buffer + * @param src -source address + * @param count -copies count wide characters from the src + * @return EOK if there was no runtime-constraint violation + */ +#if SECUREC_IN_KERNEL == 0 + SECUREC_API errno_t wmemcpy_s(wchar_t *dest, size_t destMax, const wchar_t *src, size_t count); +#endif + /** + * @Description:The memmove_s function copies n characters from the object pointed to by src into the object pointed to by dest. + * @param dest - destination address + * @param destMax -The maximum length of destination buffer + * @param src -source address + * @param count -copies count wide characters from the src + * @return EOK if there was no runtime-constraint violation + */ + SECUREC_API errno_t memmove_s(void *dest, size_t destMax, const void *src, size_t count); +#if SECUREC_IN_KERNEL == 0 + /** + * @Description:The wmemmove_s function copies n successive wide characters from the object pointed to by src into the object pointed to by dest. + * @param dest - destination address + * @param destMax -The maximum length of destination buffer + * @param src -source address + * @param count -copies count wide characters from the src + * @return EOK if there was no runtime-constraint violation + */ + SECUREC_API errno_t wmemmove_s(wchar_t *dest, size_t destMax, const wchar_t *src, size_t count); + + /** + * @Description:The wcscpy_s function copies the wide string pointed to by strSrc (including theterminating null wide character) into the array pointed to by strDest + * @param strDest - destination address + * @param destMax -The maximum length of destination buffer + * @param strSrc -source address + * @return EOK if there was no runtime-constraint violation + */ + SECUREC_API errno_t wcscpy_s(wchar_t *strDest, size_t destMax, const wchar_t *strSrc); + + /** + * @Description:The wcsncpy_s function copies not more than n successive wide characters (not including the terminating null wide character) + * from the array pointed to by strSrc to the array pointed to by strDest + * @param strDest - destination address + * @param destMax -The maximum length of destination buffer(including the terminating wide character) + * @param strSrc -source address + * @param count -copies count wide characters from the src + * @return EOK if there was no runtime-constraint violation + */ + SECUREC_API errno_t wcsncpy_s(wchar_t *strDest, size_t destMax, const wchar_t *strSrc, size_t count); + + /** + * @Description:The wcscat_s function appends a copy of the wide string pointed to by strSrc (including the terminating null wide character) + * to the end of the wide string pointed to by strDest + * @param strDest - destination address + * @param destMax -The maximum length of destination buffer(including the terminating wide character) + * @param strSrc -source address + * @return EOK if there was no runtime-constraint violation + */ + SECUREC_API errno_t wcscat_s(wchar_t *strDest, size_t destMax, const wchar_t *strSrc); + + /** + * @Description:The wcsncat_s function appends not more than n successive wide characters (not including the terminating null wide character) + * from the array pointed to by strSrc to the end of the wide string pointed to by strDest. + * @param strDest - destination address + * @param destMax -The maximum length of destination buffer(including the terminating wide character) + * @param strSrc -source address + * @param count -copies count wide characters from the src + * @return EOK if there was no runtime-constraint violation + */ + SECUREC_API errno_t wcsncat_s(wchar_t *strDest, size_t destMax, const wchar_t *strSrc, size_t count); + + /** + * @Description: The strtok_s function parses a string into a sequence of tokens,On the first call to strtok_s the string to be parsed should be specified in strToken. + * In each subsequent call that should parse the same string, strToken should be NULL + * @param strToken - the string to be delimited + * @param strDelimit -specifies a set of characters that delimit the tokens in the parsed string + * @param context -is a pointer to a char * variable that is used internally by strtok_s function + * @return:returns a pointer to the first character of a token, or a null pointer if there is no token or there is a runtime-constraint violation. + */ + SECUREC_API char *strtok_s(char *strToken, const char *strDelimit, char **context); + + /** + * @Description: The wcstok_s function is the wide-character equivalent of the strtok_s function + * @param strToken - the string to be delimited + * @param strDelimit -specifies a set of characters that delimit the tokens in the parsed string + * @param context -is a pointer to a char * variable that is used internally by strtok_s function + * @return:returns a pointer to the first character of a token, or a null pointer if there is no token or there is a runtime-constraint violation. + */ + SECUREC_API wchar_t *wcstok_s(wchar_t *strToken, const wchar_t *strDelimit, wchar_t **context); + + /** + * @Description: The sprintf_s function is equivalent to the sprintf function except for the parameter destMax and the explicit runtime-constraints violation + * @param strDest - produce output according to a format ,write to the character string strDest + * @param destMax - The maximum length of destination buffer(including the terminating null byte ('\0')) + * @param format - format string + * @return:success the number of characters printed(not including the terminating null byte ('\0')), If an error occurred return -1. + */ + SECUREC_API int sprintf_s(char *strDest, size_t destMax, const char *format, ...) SECUREC_ATTRIBUTE(3, 4); + + /** + * @Description: The swprintf_s function is the wide-character equivalent of the sprintf_s function + * @param strDest - produce output according to a format ,write to the character string strDest + * @param destMax - The maximum length of destination buffer(including the terminating null ) + * @param format - format string + * @return:success the number of characters printed(not including the terminating null wide characte), If an error occurred return -1. + */ + SECUREC_API int swprintf_s(wchar_t *strDest, size_t destMax, const wchar_t *format, ...); + + /** + * @Description: The vsprintf_s function is equivalent to the vsprintf function except for the parameter destMax and the explicit runtime-constraints violation + * @param strDest - produce output according to a format ,write to the character string strDest + * @param destMax - The maximum length of destination buffer(including the terminating null wide characte) + * @param format - format string + * @param arglist - instead of a variable number of arguments + * @return:return the number of characters printed(not including the terminating null byte ('\0')), If an error occurred return -1. + */ + SECUREC_API int vsprintf_s(char *strDest, size_t destMax, const char *format, + va_list arglist) SECUREC_ATTRIBUTE(3, 0); + + /** + * @Description: The vswprintf_s function is the wide-character equivalent of the vsprintf_s function + * @param strDest - produce output according to a format ,write to the character string strDest + * @param destMax - The maximum length of destination buffer(including the terminating null ) + * @param format - format string + * @param arglist - instead of a variable number of arguments + * @return:return the number of characters printed(not including the terminating null wide characte), If an error occurred return -1. + */ + SECUREC_API int vswprintf_s(wchar_t *strDest, size_t destMax, const wchar_t *format, va_list arglist); + + /** + * @Description: The vsnprintf_s function is equivalent to the vsnprintf function except for the parameter destMax/count and the explicit runtime-constraints violation + * @param strDest - produce output according to a format ,write to the character string strDest + * @param destMax - The maximum length of destination buffer(including the terminating null byte ('\0')) + * @param count - do not write more than count bytes to strDest(not including the terminating null byte ('\0')) + * @param format - format string + * @param arglist - instead of a variable number of arguments + * @return:return the number of characters printed(not including the terminating null byte ('\0')), If an error occurred return -1.Pay special attention to returning -1 when truncation occurs + */ + SECUREC_API int vsnprintf_s(char *strDest, size_t destMax, size_t count, const char *format, + va_list arglist) SECUREC_ATTRIBUTE(4, 0); + + /** + * @Description: The snprintf_s function is equivalent to the snprintf function except for the parameter destMax/count and the explicit runtime-constraints violation + * @param strDest - produce output according to a format ,write to the character string strDest + * @param destMax - The maximum length of destination buffer(including the terminating null byte ('\0')) + * @param count - do not write more than count bytes to strDest(not including the terminating null byte ('\0')) + * @param format - format string + * @return:return the number of characters printed(not including the terminating null byte ('\0')), If an error occurred return -1.Pay special attention to returning -1 when truncation occurs + */ + SECUREC_API int snprintf_s(char *strDest, size_t destMax, size_t count, const char *format, + ...) SECUREC_ATTRIBUTE(4, 5); + +#if SECUREC_SNPRINTF_TRUNCATED + /** + * @Description: The vsnprintf_truncated_s function is equivalent to the vsnprintf_s function except no count parameter and return value + * @param strDest - produce output according to a format ,write to the character string strDest + * @param destMax - The maximum length of destination buffer(including the terminating null byte ('\0')) + * @param format - format string + * @param arglist - instead of a variable number of arguments + * @return:return the number of characters printed(not including the terminating null byte ('\0')), If an error occurred return -1.Pay special attention to returning destMax - 1 when truncation occurs + */ + SECUREC_API int vsnprintf_truncated_s(char *strDest, size_t destMax, const char *format, + va_list arglist) SECUREC_ATTRIBUTE(3, 0); + + /** + * @Description: The snprintf_truncated_s function is equivalent to the snprintf_2 function except no count parameter and return value + * @param strDest - produce output according to a format ,write to the character string strDest + * @param destMax - The maximum length of destination buffer(including the terminating null byte ('\0')) + * @param format - format string + * @return:return the number of characters printed(not including the terminating null byte ('\0')), If an error occurred return -1.Pay special attention to returning destMax - 1 when truncation occurs + */ + SECUREC_API int snprintf_truncated_s(char *strDest, size_t destMax, const char *format, ...) SECUREC_ATTRIBUTE(3, 4); +#endif + /** + * @Description: The scanf_s function is equivalent to fscanf_s with the argument stdin interposed before the arguments to scanf_s + * @param format - format string + * @return:returns the number of input items assigned, If an error occurred return -1. + */ + SECUREC_API int scanf_s(const char *format, ...); + + /** + * @Description: The wscanf_s function is the wide-character equivalent of the scanf_s function + * @param format - format string + * @return:returns the number of input items assigned, If an error occurred return -1. + */ + SECUREC_API int wscanf_s(const wchar_t *format, ...); + + /** + * @Description: The vscanf_s function is equivalent to scanf_s, with the variable argument list replaced by arglist + * @param format - format string + * @param arglist - instead of a variable number of arguments + * @return:returns the number of input items assigned, If an error occurred return -1. + */ + SECUREC_API int vscanf_s(const char *format, va_list arglist); + + /** + * @Description: The vwscanf_s function is the wide-character equivalent of the vscanf_s function + * @param format - format string + * @param arglist - instead of a variable number of arguments + * @return:returns the number of input items assigned, If an error occurred return -1. + */ + SECUREC_API int vwscanf_s(const wchar_t *format, va_list arglist); + + /** + * @Description: The fscanf_s function is equivalent to fscanf except that the c, s, and [ conversion specifiers apply to a pair of arguments (unless assignment suppression is indicated by a*) + * @param stream - stdio file stream + * @param format - format string + * @return:returns the number of input items assigned, If an error occurred return -1. + */ + SECUREC_API int fscanf_s(FILE *stream, const char *format, ...); + + /** + * @Description: The fwscanf_s function is the wide-character equivalent of the fscanf_s function + * @param stream - stdio file stream + * @param format - format string + * @return:returns the number of input items assigned, If an error occurred return -1. + */ + SECUREC_API int fwscanf_s(FILE *stream, const wchar_t *format, ...); + + /** + * @Description: The vfscanf_s function is equivalent to fscanf_s, with the variable argument list replaced by arglist + * @param stream - stdio file stream + * @param format - format string + * @param arglist - instead of a variable number of arguments + * @return:returns the number of input items assigned, If an error occurred return -1. + */ + SECUREC_API int vfscanf_s(FILE *stream, const char *format, va_list arglist); + + /** + * @Description: The vfwscanf_s function is the wide-character equivalent of the vfscanf_s function + * @param stream - stdio file stream + * @param format - format string + * @param arglist - instead of a variable number of arguments + * @return:returns the number of input items assigned, If an error occurred return -1. + */ + SECUREC_API int vfwscanf_s(FILE *stream, const wchar_t *format, va_list arglist); + + /** + * @Description: The sscanf_s function is equivalent to fscanf_s, except that input is obtained from a string (specified by the argument buffer) rather than from a stream + * @param buffer - read character from buffer + * @param format - format string + * @return:returns the number of input items assigned, If an error occurred return -1. + */ + SECUREC_API int sscanf_s(const char *buffer, const char *format, ...); + + /** + * @Description: The swscanf_s function is the wide-character equivalent of the sscanf_s function + * @param buffer - read character from buffer + * @param format - format string + * @return:returns the number of input items assigned, If an error occurred return -1. + */ + SECUREC_API int swscanf_s(const wchar_t *buffer, const wchar_t *format, ...); + + /** + * @Description: The vsscanf_s function is equivalent to sscanf_s, with the variable argument list replaced by arglist + * @param buffer - read character from buffer + * @param format - format string + * @param arglist - instead of a variable number of arguments + * @return:returns the number of input items assigned, If an error occurred return -1. + */ + SECUREC_API int vsscanf_s(const char *buffer, const char *format, va_list arglist); + + /** + * @Description: The vswscanf_s function is the wide-character equivalent of the vsscanf_s function + * @param buffer - read character from buffer + * @param format - format string + * @param arglist - instead of a variable number of arguments + * @return:returns the number of input items assigned, If an error occurred return -1. + */ + SECUREC_API int vswscanf_s(const wchar_t *buffer, const wchar_t *format, va_list arglist); + + /** + * @Description:The gets_s function reads at most one less than the number of characters specified by destMax from the stream pointed to by stdin, into the array pointed to by buffer + * @param buffer - destination address + * @param destMax -The maximum length of destination buffer(including the terminating null character) + * @return buffer if there was no runtime-constraint violation,If an error occurred return NULL. + */ + SECUREC_API char *gets_s(char *buffer, size_t destMax); +#endif + + /** + * @Description:The memcpy_s function copies n characters from the object pointed to by src into the object pointed to by dest. + * @param dest - destination address + * @param destMax -The maximum length of destination buffer + * @param src -source address + * @param count -copies count characters from the src + * @return EOK if there was no runtime-constraint violation + */ + SECUREC_API errno_t memcpy_s(void *dest, size_t destMax, const void *src, size_t count); + + /** + * @Description:The strcpy_s function copies the string pointed to by strSrc (including the terminating null character) into the array pointed to by strDest + * @param strDest - destination address + * @param destMax -The maximum length of destination buffer(including the terminating null character) + * @param strSrc -source address + * @return EOK if there was no runtime-constraint violation + */ + SECUREC_API errno_t strcpy_s(char *strDest, size_t destMax, const char *strSrc); + + /** + * @Description:The strncpy_s function copies not more than n successive characters (not including the terminating null character) + * from the array pointed to by strSrc to the array pointed to by strDest + * @param strDest - destination address + * @param destMax -The maximum length of destination buffer(including the terminating null character) + * @param strSrc -source address + * @param count -copies count characters from the src + * @return EOK if there was no runtime-constraint violation + */ + SECUREC_API errno_t strncpy_s(char *strDest, size_t destMax, const char *strSrc, size_t count); + + /** + * @Description:The strcat_s function appends a copy of the string pointed to by strSrc (including the terminating null character) + * to the end of the string pointed to by strDest + * @param strDest - destination address + * @param destMax -The maximum length of destination buffer(including the terminating null wide character) + * @param strSrc -source address + * @return EOK if there was no runtime-constraint violation + */ + SECUREC_API errno_t strcat_s(char *strDest, size_t destMax, const char *strSrc); + + /** + * @Description:The strncat_s function appends not more than n successive characters (not including the terminating null character) + * from the array pointed to by strSrc to the end of the string pointed to by strDest. + * @param strDest - destination address + * @param destMax -The maximum length of destination buffer(including the terminating null character) + * @param strSrc -source address + * @param count -copies count characters from the src + * @return EOK if there was no runtime-constraint violation + */ + SECUREC_API errno_t strncat_s(char *strDest, size_t destMax, const char *strSrc, size_t count); +#if SECUREC_IN_KERNEL == 0 + /* those functions are used by macro ,must declare hare , also for without function declaration warning */ + extern errno_t strncpy_error(char *strDest, size_t destMax, const char *strSrc, size_t count); + extern errno_t strcpy_error(char *strDest, size_t destMax, const char *strSrc); +#endif +#endif + +#if SECUREC_WITH_PERFORMANCE_ADDONS + /* those functions are used by macro */ + extern errno_t memset_sOptAsm(void *dest, size_t destMax, int c, size_t count); + extern errno_t memset_sOptTc(void *dest, size_t destMax, int c, size_t count); + extern errno_t memcpy_sOptAsm(void *dest, size_t destMax, const void *src, size_t count); + extern errno_t memcpy_sOptTc(void *dest, size_t destMax, const void *src, size_t count); + +/* strcpy_sp is a macro, NOT a function in performance optimization mode. */ +#define strcpy_sp(dest, destMax, src) ((__builtin_constant_p((destMax)) && \ + __builtin_constant_p((src))) ? \ + SECUREC_STRCPY_SM((dest), (destMax), (src)) : \ + strcpy_s((dest), (destMax), (src))) + +/* strncpy_sp is a macro, NOT a function in performance optimization mode. */ +#define strncpy_sp(dest, destMax, src, count) ((__builtin_constant_p((count)) && \ + __builtin_constant_p((destMax)) && \ + __builtin_constant_p((src))) ? \ + SECUREC_STRNCPY_SM((dest), (destMax), (src), (count)) : \ + strncpy_s((dest), (destMax), (src), (count))) + +/* strcat_sp is a macro, NOT a function in performance optimization mode. */ +#define strcat_sp(dest, destMax, src) ((__builtin_constant_p((destMax)) && \ + __builtin_constant_p((src))) ? \ + SECUREC_STRCAT_SM((dest), (destMax), (src)) : \ + strcat_s((dest), (destMax), (src))) + + /* strncat_sp is a macro, NOT a function in performance optimization mode. */ +#define strncat_sp(dest, destMax, src, count) ((__builtin_constant_p((count)) && \ + __builtin_constant_p((destMax)) && \ + __builtin_constant_p((src))) ? \ + SECUREC_STRNCAT_SM((dest), (destMax), (src), (count)) : \ + strncat_s((dest), (destMax), (src), (count))) + +/* memcpy_sp is a macro, NOT a function in performance optimization mode. */ +#define memcpy_sp(dest, destMax, src, count) (__builtin_constant_p((count)) ? \ + (SECUREC_MEMCPY_SM((dest), (destMax), (src), (count))) : \ + (__builtin_constant_p((destMax)) ? \ + (((size_t)(destMax) > 0 && \ + (((unsigned long long)(destMax) & \ + (unsigned long long)(-2)) < SECUREC_MEM_MAX_LEN)) ? \ + memcpy_sOptTc((dest), (destMax), (src), (count)) : ERANGE ) : \ + memcpy_sOptAsm((dest), (destMax), (src), (count)))) + +/* memset_sp is a macro, NOT a function in performance optimization mode. */ +#define memset_sp(dest, destMax, c, count) (__builtin_constant_p((count)) ? \ + (SECUREC_MEMSET_SM((dest), (destMax), (c), (count))) : \ + (__builtin_constant_p((destMax)) ? \ + (((size_t)(destMax) > 0 && \ + (((unsigned long long)(destMax) & \ + (unsigned long long)(-2)) < SECUREC_MEM_MAX_LEN)) ? \ + memset_sOptTc((dest), (destMax), (c), (count)) : ERANGE ) : \ + memset_sOptAsm((dest), (destMax), (c), (count)))) +#else +#define strcpy_sp strcpy_s +#define strncpy_sp strncpy_s +#define strcat_sp strcat_s +#define strncat_sp strncat_s +#define memcpy_sp memcpy_s +#define memset_sp memset_s +#endif + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* __SECUREC_H__5D13A042_DC3F_4ED9_A8D1_882811274C27 */ diff --git a/base/include/securectype.h b/base/include/securectype.h new file mode 100644 index 0000000..029772c --- /dev/null +++ b/base/include/securectype.h @@ -0,0 +1,279 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* [Standardize-exceptions]: Performance-sensitive + * [reason]: Strict parameter verification has been done before use + */ + +#ifndef __SECURECTYPE_H__A7BBB686_AADA_451B_B9F9_44DACDAE18A7 +#define __SECURECTYPE_H__A7BBB686_AADA_451B_B9F9_44DACDAE18A7 + +#ifndef SECUREC_ONLY_DECLARE_MEMSET +/* Shielding VC symbol redefinition warning */ +#if defined(_MSC_VER) && (_MSC_VER >= 1400) +#ifdef __STDC_WANT_SECURE_LIB__ +#undef __STDC_WANT_SECURE_LIB__ +#endif +#define __STDC_WANT_SECURE_LIB__ 0 +#ifdef _CRTIMP_ALTERNATIVE +#undef _CRTIMP_ALTERNATIVE +#endif +#define _CRTIMP_ALTERNATIVE //comment microsoft *_s function +#endif +#endif + +#if SECUREC_IN_KERNEL +#include +#include +#else +#include +#include +#include +#endif + +/* if enable SECUREC_COMPATIBLE_WIN_FORMAT, the output format will be compatible to Windows. */ +#if (defined(_WIN32) || defined(_WIN64) || defined(_MSC_VER)) +#define SECUREC_COMPATIBLE_WIN_FORMAT +#endif + +#if defined(SECUREC_COMPATIBLE_WIN_FORMAT) +/* in windows platform, can't use optimized function for there is no __builtin_constant_p like function */ +/* If need optimized macro, can define this: define __builtin_constant_p(x) 0 */ +#ifdef SECUREC_WITH_PERFORMANCE_ADDONS +#undef SECUREC_WITH_PERFORMANCE_ADDONS +#define SECUREC_WITH_PERFORMANCE_ADDONS 0 +#endif +#endif + +#if defined(__VXWORKS__) || defined(__vxworks) || defined(__VXWORKS) || defined(_VXWORKS_PLATFORM_) || \ + defined(SECUREC_VXWORKS_VERSION_5_4) +#if !defined(SECUREC_VXWORKS_PLATFORM) +#define SECUREC_VXWORKS_PLATFORM +#endif +#endif + +/* if enable SECUREC_COMPATIBLE_LINUX_FORMAT, the output format will be compatible to Linux. */ +#if !(defined(SECUREC_COMPATIBLE_WIN_FORMAT) || defined(SECUREC_VXWORKS_PLATFORM)) +#define SECUREC_COMPATIBLE_LINUX_FORMAT +#endif +#ifdef SECUREC_COMPATIBLE_LINUX_FORMAT +#include +#endif + +/* add the -DSECUREC_SUPPORT_FORMAT_WARNING compiler option to support -Wformat. + * default does not check the format is that the same data type in the actual code + * in the product is different in the original data type definition of VxWorks and Linux. + */ +#ifndef SECUREC_SUPPORT_FORMAT_WARNING +#define SECUREC_SUPPORT_FORMAT_WARNING 0 +#endif + +/* SECUREC_PCLINT for tool do not recognize __attribute__ just for pclint */ +#if SECUREC_SUPPORT_FORMAT_WARNING && !defined(SECUREC_PCLINT) +#define SECUREC_ATTRIBUTE(x, y) __attribute__((format(printf, (x), (y)))) +#else +#define SECUREC_ATTRIBUTE(x, y) +#endif + +/* SECUREC_PCLINT for tool do not recognize __builtin_expect ,just for pclint */ +#if defined(__GNUC__) && \ + ((__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 3 /* above 3.4 */))) && \ + !defined(SECUREC_PCLINT) +/* This is a built-in function that can be used without a declaration, if you encounter an undeclared compilation alarm, + * you can add -DSECUREC_NEED_BUILTIN_EXPECT_DECLARE to compiler options + */ +#if defined(SECUREC_NEED_BUILTIN_EXPECT_DECLARE) +long __builtin_expect(long exp, long c); +#endif +#define SECUREC_LIKELY(x) __builtin_expect(!!(x), 1) +#define SECUREC_UNLIKELY(x) __builtin_expect(!!(x), 0) +#else +#define SECUREC_LIKELY(x) (x) +#define SECUREC_UNLIKELY(x) (x) +#endif + +/* define the max length of the string */ +#define SECUREC_STRING_MAX_LEN (0x7fffffffUL) +#define SECUREC_WCHAR_STRING_MAX_LEN (SECUREC_STRING_MAX_LEN / sizeof(wchar_t)) + +/* add SECUREC_MEM_MAX_LEN for memcpy and memmove */ +#define SECUREC_MEM_MAX_LEN (0x7fffffffUL) +#define SECUREC_WCHAR_MEM_MAX_LEN (SECUREC_MEM_MAX_LEN / sizeof(wchar_t)) + +#if SECUREC_STRING_MAX_LEN > 0x7fffffff +#error "max string is 2G" +#endif + +#if (defined(__GNUC__ ) && defined(__SIZEOF_POINTER__ )) +#if (__SIZEOF_POINTER__ != 4) && (__SIZEOF_POINTER__ != 8) +#error "unsupported system" +#endif +#endif + +#ifndef SECUREC_MALLOC +#define SECUREC_MALLOC(x) malloc((size_t)(x)) +#endif + +#ifndef SECUREC_FREE +#define SECUREC_FREE(x) free((void *)(x)) +#endif + +#if defined(_WIN64) || defined(WIN64) || defined(__LP64__) || defined(_LP64) +#define SECUREC_ON_64BITS +#endif + +#if (!defined(SECUREC_ON_64BITS) && defined(__GNUC__ ) && defined(__SIZEOF_POINTER__ )) +#if __SIZEOF_POINTER__ == 8 +#define SECUREC_ON_64BITS +#endif +#endif + +#if defined(__SVR4) || defined(__svr4__) +#define SECUREC_ON_SOLARIS +#endif + +#if (defined(__hpux) || defined(_AIX) || defined(SECUREC_ON_SOLARIS)) +#define SECUREC_ON_UNIX +#endif + +/* codes should run under the macro SECUREC_COMPATIBLE_LINUX_FORMAT in unknown system on default, + * and strtold. The function + * strtold is referenced first at ISO9899:1999(C99), and some old compilers can + * not support these functions. Here provides a macro to open these functions: + * SECUREC_SUPPORT_STRTOLD -- if defined, strtold will be used + */ +#ifndef SECUREC_SUPPORT_STRTOLD +#define SECUREC_SUPPORT_STRTOLD 0 +#if (defined(SECUREC_COMPATIBLE_LINUX_FORMAT)) +#if defined(__USE_ISOC99) || \ + (defined(_AIX) && defined(_ISOC99_SOURCE)) || \ + (defined(__hpux) && defined(__ia64)) || \ + (defined(SECUREC_ON_SOLARIS) && (!defined(_STRICT_STDC) && !defined(__XOPEN_OR_POSIX)) || \ + defined(_STDC_C99) || defined(__EXTENSIONS__)) +#undef SECUREC_SUPPORT_STRTOLD +#define SECUREC_SUPPORT_STRTOLD 1 +#endif +#endif +#if ((defined(SECUREC_WRLINUX_BELOW4) || defined(_WRLINUX_BELOW4_))) +#undef SECUREC_SUPPORT_STRTOLD +#define SECUREC_SUPPORT_STRTOLD 0 +#endif +#endif + +#if SECUREC_WITH_PERFORMANCE_ADDONS + +#ifndef SECUREC_TWO_MIN +#define SECUREC_TWO_MIN(a, b) ((a) < (b) ? (a) : (b)) +#endif + +/* for strncpy_s performance optimization */ +#define SECUREC_STRNCPY_SM(dest, destMax, src, count) \ + (((void *)dest != NULL && (void *)src != NULL && (size_t)destMax >0 && \ + (((unsigned long long)(destMax) & (unsigned long long)(-2)) < SECUREC_STRING_MAX_LEN) && \ + (SECUREC_TWO_MIN(count , strlen(src)) + 1) <= (size_t)destMax) ? \ + ((count < strlen(src))? (memcpy(dest, src, count), *((char *)dest + count) = '\0', EOK): \ + (memcpy(dest, src, strlen(src) + 1), EOK )) :(strncpy_error(dest, destMax, src, count))) + +#define SECUREC_STRCPY_SM(dest, destMax, src) \ + (((void *)dest != NULL && (void *)src != NULL && (size_t)destMax >0 && \ + (((unsigned long long)(destMax) & (unsigned long long)(-2)) < SECUREC_STRING_MAX_LEN) && \ + (strlen(src) + 1) <= (size_t)destMax)? (memcpy(dest, src, strlen(src) + 1), EOK): \ + (strcpy_error(dest, destMax, src))) + +/* for strcat_s performance optimization */ +#if defined(__GNUC__) +#define SECUREC_STRCAT_SM(dest, destMax, src) \ + ({ \ + int catRet = EOK; \ + if ((void *)dest != NULL && (void *)src != NULL && (size_t)(destMax) >0 && \ + (((unsigned long long)(destMax) & (unsigned long long)(-2)) < SECUREC_STRING_MAX_LEN)) { \ + char *catTmpDst = (dest); \ + size_t catRestSize = (destMax); \ + while(catRestSize > 0 && *catTmpDst) { \ + ++catTmpDst; \ + --catRestSize; \ + } \ + if (catRestSize == 0) { \ + catRet = EINVAL; \ + } else if ((strlen(src) + 1) <= catRestSize) { \ + (void)memcpy(catTmpDst, (src), strlen(src) + 1); \ + catRet = EOK; \ + } else { \ + catRet = ERANGE; \ + } \ + if (catRet != EOK) { \ + catRet = strcat_s((dest), (destMax), (src)); \ + } \ + } else { \ + catRet = strcat_s((dest), (destMax), (src)); \ + } \ + catRet; \ + }) +#else +#define SECUREC_STRCAT_SM(dest, destMax, src) strcat_s(dest, destMax, src) +#endif + +/* for strncat_s performance optimization */ +#if defined(__GNUC__) +#define SECUREC_STRNCAT_SM(dest, destMax, src, count) \ + ({ \ + int ncatRet = EOK; \ + if ((void *)dest != NULL && (void *)src != NULL && (size_t)destMax > 0 && \ + (((unsigned long long)(destMax) & (unsigned long long)(-2)) < SECUREC_STRING_MAX_LEN) && \ + (((unsigned long long)(count) & (unsigned long long)(-2)) < SECUREC_STRING_MAX_LEN)) { \ + char *ncatTmpDest = (dest); \ + size_t ncatRestSize = (destMax); \ + while(ncatRestSize > 0 && *ncatTmpDest) { \ + ++ncatTmpDest; \ + --ncatRestSize; \ + } \ + if (ncatRestSize == 0) { \ + ncatRet = EINVAL; \ + } else if ((SECUREC_TWO_MIN((count) , strlen(src)) + 1) <= ncatRestSize ) { \ + if ((count) < strlen(src)) { \ + (void)memcpy(ncatTmpDest, (src), (count)); \ + *(ncatTmpDest + (count)) = '\0'; \ + } else { \ + (void)memcpy(ncatTmpDest, (src), strlen(src) + 1); \ + } \ + } else { \ + ncatRet = ERANGE; \ + } \ + if (ncatRet != EOK) { \ + ncatRet = strncat_s((dest), (destMax), (src), (count)); \ + } \ + } else { \ + ncatRet = strncat_s((dest), (destMax), (src), (count)); \ + } \ + ncatRet; \ + }) +#else +#define SECUREC_STRNCAT_SM(dest, destMax, src, count) strncat_s(dest, destMax, src, count) +#endif + +/* SECUREC_MEMCPY_SM do NOT check buffer overlap by default */ +#define SECUREC_MEMCPY_SM(dest, destMax, src, count) \ + (!(((size_t)destMax == 0 )||(((unsigned long long)(destMax) & (unsigned long long)(-2)) > SECUREC_MEM_MAX_LEN) || \ + ((size_t)count > (size_t)destMax) || ((void *)dest) == NULL || ((void *)src == NULL))? \ + (memcpy(dest, src, count), EOK) : \ + (memcpy_s(dest, destMax, src, count))) + +#define SECUREC_MEMSET_SM(dest, destMax, c, count) \ + (!(((size_t)destMax == 0 ) || (((unsigned long long)(destMax) & (unsigned long long)(-2)) > SECUREC_MEM_MAX_LEN) || \ + ((void *)dest == NULL) || ((size_t)count > (size_t)destMax)) ? \ + (memset(dest, c, count), EOK) : \ + ( memset_s(dest, destMax, c, count))) + +#endif +#endif /* __SECURECTYPE_H__A7BBB686_AADA_451B_B9F9_44DACDAE18A7 */ diff --git a/base/test/unittest/common/BUILD.gn b/base/test/unittest/common/BUILD.gn index 22b36c9..0d7f4b4 100644 --- a/base/test/unittest/common/BUILD.gn +++ b/base/test/unittest/common/BUILD.gn @@ -39,6 +39,19 @@ ohos_unittest("UtilsStringTest") { ] } +##############################unittest########################################## +ohos_unittest("UtilsSecurecTest") { + module_out_path = module_output_path + sources = [ "utils_securec_test.cpp" ] + + configs = [ ":module_private_config" ] + + deps = [ + "//commonlibrary/c_utils/base:utils", + "//third_party/googletest:gtest_main", + ] +} + ##############################unittest########################################## ohos_unittest("UtilsDirectoryTest") { module_out_path = module_output_path @@ -296,6 +309,7 @@ group("unittest") { ":UtilsSafeBlockQueueTrackingTest", ":UtilsSafeMapTest", ":UtilsSafeQueueTest", + ":UtilsSecurecTest", ":UtilsSingletonTest", ":UtilsSortedVectorTest", ":UtilsStringTest", diff --git a/base/test/unittest/common/utils_securec_test.cpp b/base/test/unittest/common/utils_securec_test.cpp new file mode 100644 index 0000000..e07af92 --- /dev/null +++ b/base/test/unittest/common/utils_securec_test.cpp @@ -0,0 +1,141 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include "string_ex.h" +#include "securec.h" +using namespace testing::ext; +using namespace OHOS; + +class UtilsSecurecTest : public testing::Test +{ +public : + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +void UtilsSecurecTest::SetUpTestCase(void) +{ + // step 2: input testsuit setup step +} + +void UtilsSecurecTest::TearDownTestCase(void) +{ + // step 2: input testsuit teardown step +} + +void UtilsSecurecTest::SetUp(void) +{ + // step 3: input testcase setup step +} + +void UtilsSecurecTest::TearDown(void) +{ + // step 3: input testcase teardown step +} + +HWTEST_F(UtilsSecurecTest, test_memset_s_01, TestSize.Level0) +{ + char cBase[20]; + errno_t result = memset_s(reinterpret_cast(cBase), sizeof(char) * 20, 1, sizeof(char) * 20); + EXPECT_EQ(result, 0); +} + +HWTEST_F(UtilsSecurecTest, test_memset_s_02, TestSize.Level0) +{ + char cBase[20]; + errno_t result = memset_s(reinterpret_cast(cBase), sizeof(char) * 20, 1, sizeof(char) * 21); + EXPECT_NE(result, 0); +} + +HWTEST_F(UtilsSecurecTest, test_memcpy_s_01, TestSize.Level0) +{ + char cBase[20] = "memcpy_s"; + char cTemp[20]; + errno_t result = memcpy_s(reinterpret_cast(cTemp), sizeof(char) * 20, cBase, sizeof(cBase)); + EXPECT_EQ(result, 0); + EXPECT_EQ(0, strcmp(cTemp, cBase)); +} + +HWTEST_F(UtilsSecurecTest, test_memcpy_s_02, TestSize.Level0) +{ + char cBase[20] = "memcpy_s"; + char cTemp[5]; + errno_t result = memcpy_s(reinterpret_cast(cTemp), sizeof(char) * 5, cBase, sizeof(cBase)); + EXPECT_NE(result, 0); +} + +HWTEST_F(UtilsSecurecTest, test_strcpy_s_01, TestSize.Level0) +{ + const char cBase[] = "strcpy_base"; + char cTemp[20]; + errno_t result = strcpy_s(cTemp, sizeof(cTemp), cBase); + EXPECT_EQ(result, 0); + EXPECT_EQ(0, strcmp(cTemp, cBase)); +} + +HWTEST_F(UtilsSecurecTest, test_strcpy_s_02, TestSize.Level0) +{ + const char cBase[] = "strcpy_base"; + char cTemp[10]; + errno_t result = strcpy_s(cTemp, sizeof(cTemp), cBase); + EXPECT_NE(result, 0); +} + +HWTEST_F(UtilsSecurecTest, test_strncpy_s_01, TestSize.Level0) +{ + const char cBase[] = "strcpy_base"; + char cTemp[20]; + errno_t result = strncpy_s(cTemp, sizeof(cTemp), cBase, sizeof(cBase)); + EXPECT_EQ(result, 0); + EXPECT_EQ(0, strcmp(cTemp, cBase)); +} + +HWTEST_F(UtilsSecurecTest, test_strncpy_s_02, TestSize.Level0) +{ + const char cBase[] = "strcpy_base"; + char cTemp[10]; + errno_t result = strncpy_s(cTemp, sizeof(cTemp), cBase, (sizeof(cTemp) -1)); + EXPECT_EQ(result, 0); + result = strncpy_s(cTemp, sizeof(cTemp), cBase, sizeof(cBase)); + EXPECT_NE(result, 0); +} + +HWTEST_F(UtilsSecurecTest, test_strcat_s_01, TestSize.Level0) +{ + const char cBase[] = "strcpy_base"; + char cTemp[20] = "temp "; + errno_t result = strcat_s(cTemp, sizeof(cTemp), cBase); + EXPECT_EQ(result, 0); + EXPECT_EQ(0, strcmp(cTemp, "temp strcpy_base")); +} + +HWTEST_F(UtilsSecurecTest, test_strcat_s_02, TestSize.Level0) +{ + const char cBase[] = "strcpy_base"; + char cTemp[10]; + errno_t result = strcat_s(cTemp, sizeof(cTemp), cBase); + EXPECT_NE(result, 0); +} + +HWTEST_F(UtilsSecurecTest, test_sprintf_s_01, TestSize.Level0) +{ + char cBase[64] = { 0 }; + errno_t result = sprintf_s(cBase, sizeof(cBase), "%d", 12345); + EXPECT_EQ(string(cBase), "12345"); + EXPECT_NE(result, 0); +} \ No newline at end of file diff --git a/bundle.json b/bundle.json index c4d70cf..9e7082e 100644 --- a/bundle.json +++ b/bundle.json @@ -49,6 +49,8 @@ "safe_block_queue.h", "safe_map.h", "safe_queue.h", + "securec.h", + "securectype.h", "semaphore_ex.h", "singleton.h", "sorted_vector.h", @@ -82,6 +84,8 @@ "safe_block_queue.h", "safe_map.h", "safe_queue.h", + "securec.h", + "securectype.h", "semaphore_ex.h", "singleton.h", "sorted_vector.h", -- Gitee From 80938a0756b905b06dd11abf0675b1ec24cd18bb Mon Sep 17 00:00:00 2001 From: peilixia Date: Wed, 19 Oct 2022 15:17:11 +0800 Subject: [PATCH 3/9] Delete securec.h, securectype.h, utils_securec_test.cpp and modify compile file Signed-off-by: peilixia --- base/include/securec.h | 538 ------------------ base/include/securectype.h | 279 --------- base/test/unittest/common/BUILD.gn | 14 - .../unittest/common/utils_securec_test.cpp | 141 ----- bundle.json | 4 - 5 files changed, 976 deletions(-) delete mode 100644 base/include/securec.h delete mode 100644 base/include/securectype.h delete mode 100644 base/test/unittest/common/utils_securec_test.cpp diff --git a/base/include/securec.h b/base/include/securec.h deleted file mode 100644 index 553eaf4..0000000 --- a/base/include/securec.h +++ /dev/null @@ -1,538 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef __SECUREC_H__5D13A042_DC3F_4ED9_A8D1_882811274C27 -#define __SECUREC_H__5D13A042_DC3F_4ED9_A8D1_882811274C27 - -/* Compile in kernel under macro control */ -#ifndef SECUREC_IN_KERNEL -#ifdef __KERNEL__ -#define SECUREC_IN_KERNEL 1 -#else -#define SECUREC_IN_KERNEL 0 -#endif -#endif - -/* If you need high performance, enable the SECUREC_WITH_PERFORMANCE_ADDONS macro, default is enable . - * The macro is automatically closed on the windows platform in securectyp.h. - */ -#ifndef SECUREC_WITH_PERFORMANCE_ADDONS -#if SECUREC_IN_KERNEL -#define SECUREC_WITH_PERFORMANCE_ADDONS 0 -#else -#define SECUREC_WITH_PERFORMANCE_ADDONS 1 -#endif -#endif - -#include "securectype.h" -#include - -#ifndef SECUREC_HAVE_ERRNO_H -#if SECUREC_IN_KERNEL -#define SECUREC_HAVE_ERRNO_H 0 -#else -#define SECUREC_HAVE_ERRNO_H 1 -#endif -#endif - -/* EINVAL ERANGE may defined in errno.h */ -#if SECUREC_HAVE_ERRNO_H -#include -#endif - -/* If stack size on some embedded platform is limited, you can define the following macro - * which will put some variables on heap instead of stack. - * SECUREC_STACK_SIZE_LESS_THAN_1K - */ - -/* define error code */ -#if !defined(__STDC_WANT_LIB_EXT1__) || (defined(__STDC_WANT_LIB_EXT1__) && (__STDC_WANT_LIB_EXT1__ == 0)) -#ifndef SECUREC_DEFINED_ERRNO_TYPE -#define SECUREC_DEFINED_ERRNO_TYPE -/* just check whether macrodefinition exists. */ -#ifndef errno_t -typedef int errno_t; -#endif -#endif -#endif - -/* success */ -#ifndef EOK -#define EOK (0) -#endif - -#ifndef EINVAL -/* The src buffer is not correct and destination buffer can't not be reset */ -#define EINVAL (22) -#endif - -#ifndef EINVAL_AND_RESET -/* Once the error is detected, the dest buffer must be rest! */ -#define EINVAL_AND_RESET (22 | 128) -#endif - -#ifndef ERANGE -/* The destination buffer is not long enough and destination buffer can not be reset */ -#define ERANGE (34) -#endif - -#ifndef ERANGE_AND_RESET -/* Once the error is detected, the dest buffer must be rest! */ -#define ERANGE_AND_RESET (34 | 128) -#endif - -#ifndef EOVERLAP_AND_RESET -/* Once the buffer overlap is detected, the dest buffer must be rest! */ -#define EOVERLAP_AND_RESET (54 | 128) -#endif - -/* if you need export the function of this library in Win32 dll, use __declspec(dllexport) */ -#ifdef SECUREC_IS_DLL_LIBRARY -#ifdef SECUREC_DLL_EXPORT -#define SECUREC_API __declspec(dllexport) -#else -#define SECUREC_API __declspec(dllimport) -#endif -#else -/* Standardized function declaration . If a security function is declared in the your code, - * it may cause a compilation alarm,Please delete the security function you declared - */ -#define SECUREC_API extern -#endif - -#ifndef SECUREC_SNPRINTF_TRUNCATED -#define SECUREC_SNPRINTF_TRUNCATED 1 -#endif - -#ifdef __cplusplus -extern "C" { -#endif - - /** - * @Description:The memset_s function copies the value of c (converted to an unsigned char) into each of the first count characters of the object pointed to by dest. - * @param dest - destination address - * @param destMax -The maximum length of destination buffer - * @param c - the value to be copied - * @param count -copies first count characters of dest - * @return EOK if there was no runtime-constraint violation - */ - SECUREC_API errno_t memset_s(void *dest, size_t destMax, int c, size_t count); - - /* The memset_s security function is not provided in Windows system, but other security functions are provided. In this case, can only use the memset_s function */ -#ifndef SECUREC_ONLY_DECLARE_MEMSET - - /** - * @Description:The wmemcpy_s function copies n successive wide characters from the object pointed to by src into the object pointed to by dest. - * @param dest - destination address - * @param destMax -The maximum length of destination buffer - * @param src -source address - * @param count -copies count wide characters from the src - * @return EOK if there was no runtime-constraint violation - */ -#if SECUREC_IN_KERNEL == 0 - SECUREC_API errno_t wmemcpy_s(wchar_t *dest, size_t destMax, const wchar_t *src, size_t count); -#endif - /** - * @Description:The memmove_s function copies n characters from the object pointed to by src into the object pointed to by dest. - * @param dest - destination address - * @param destMax -The maximum length of destination buffer - * @param src -source address - * @param count -copies count wide characters from the src - * @return EOK if there was no runtime-constraint violation - */ - SECUREC_API errno_t memmove_s(void *dest, size_t destMax, const void *src, size_t count); -#if SECUREC_IN_KERNEL == 0 - /** - * @Description:The wmemmove_s function copies n successive wide characters from the object pointed to by src into the object pointed to by dest. - * @param dest - destination address - * @param destMax -The maximum length of destination buffer - * @param src -source address - * @param count -copies count wide characters from the src - * @return EOK if there was no runtime-constraint violation - */ - SECUREC_API errno_t wmemmove_s(wchar_t *dest, size_t destMax, const wchar_t *src, size_t count); - - /** - * @Description:The wcscpy_s function copies the wide string pointed to by strSrc (including theterminating null wide character) into the array pointed to by strDest - * @param strDest - destination address - * @param destMax -The maximum length of destination buffer - * @param strSrc -source address - * @return EOK if there was no runtime-constraint violation - */ - SECUREC_API errno_t wcscpy_s(wchar_t *strDest, size_t destMax, const wchar_t *strSrc); - - /** - * @Description:The wcsncpy_s function copies not more than n successive wide characters (not including the terminating null wide character) - * from the array pointed to by strSrc to the array pointed to by strDest - * @param strDest - destination address - * @param destMax -The maximum length of destination buffer(including the terminating wide character) - * @param strSrc -source address - * @param count -copies count wide characters from the src - * @return EOK if there was no runtime-constraint violation - */ - SECUREC_API errno_t wcsncpy_s(wchar_t *strDest, size_t destMax, const wchar_t *strSrc, size_t count); - - /** - * @Description:The wcscat_s function appends a copy of the wide string pointed to by strSrc (including the terminating null wide character) - * to the end of the wide string pointed to by strDest - * @param strDest - destination address - * @param destMax -The maximum length of destination buffer(including the terminating wide character) - * @param strSrc -source address - * @return EOK if there was no runtime-constraint violation - */ - SECUREC_API errno_t wcscat_s(wchar_t *strDest, size_t destMax, const wchar_t *strSrc); - - /** - * @Description:The wcsncat_s function appends not more than n successive wide characters (not including the terminating null wide character) - * from the array pointed to by strSrc to the end of the wide string pointed to by strDest. - * @param strDest - destination address - * @param destMax -The maximum length of destination buffer(including the terminating wide character) - * @param strSrc -source address - * @param count -copies count wide characters from the src - * @return EOK if there was no runtime-constraint violation - */ - SECUREC_API errno_t wcsncat_s(wchar_t *strDest, size_t destMax, const wchar_t *strSrc, size_t count); - - /** - * @Description: The strtok_s function parses a string into a sequence of tokens,On the first call to strtok_s the string to be parsed should be specified in strToken. - * In each subsequent call that should parse the same string, strToken should be NULL - * @param strToken - the string to be delimited - * @param strDelimit -specifies a set of characters that delimit the tokens in the parsed string - * @param context -is a pointer to a char * variable that is used internally by strtok_s function - * @return:returns a pointer to the first character of a token, or a null pointer if there is no token or there is a runtime-constraint violation. - */ - SECUREC_API char *strtok_s(char *strToken, const char *strDelimit, char **context); - - /** - * @Description: The wcstok_s function is the wide-character equivalent of the strtok_s function - * @param strToken - the string to be delimited - * @param strDelimit -specifies a set of characters that delimit the tokens in the parsed string - * @param context -is a pointer to a char * variable that is used internally by strtok_s function - * @return:returns a pointer to the first character of a token, or a null pointer if there is no token or there is a runtime-constraint violation. - */ - SECUREC_API wchar_t *wcstok_s(wchar_t *strToken, const wchar_t *strDelimit, wchar_t **context); - - /** - * @Description: The sprintf_s function is equivalent to the sprintf function except for the parameter destMax and the explicit runtime-constraints violation - * @param strDest - produce output according to a format ,write to the character string strDest - * @param destMax - The maximum length of destination buffer(including the terminating null byte ('\0')) - * @param format - format string - * @return:success the number of characters printed(not including the terminating null byte ('\0')), If an error occurred return -1. - */ - SECUREC_API int sprintf_s(char *strDest, size_t destMax, const char *format, ...) SECUREC_ATTRIBUTE(3, 4); - - /** - * @Description: The swprintf_s function is the wide-character equivalent of the sprintf_s function - * @param strDest - produce output according to a format ,write to the character string strDest - * @param destMax - The maximum length of destination buffer(including the terminating null ) - * @param format - format string - * @return:success the number of characters printed(not including the terminating null wide characte), If an error occurred return -1. - */ - SECUREC_API int swprintf_s(wchar_t *strDest, size_t destMax, const wchar_t *format, ...); - - /** - * @Description: The vsprintf_s function is equivalent to the vsprintf function except for the parameter destMax and the explicit runtime-constraints violation - * @param strDest - produce output according to a format ,write to the character string strDest - * @param destMax - The maximum length of destination buffer(including the terminating null wide characte) - * @param format - format string - * @param arglist - instead of a variable number of arguments - * @return:return the number of characters printed(not including the terminating null byte ('\0')), If an error occurred return -1. - */ - SECUREC_API int vsprintf_s(char *strDest, size_t destMax, const char *format, - va_list arglist) SECUREC_ATTRIBUTE(3, 0); - - /** - * @Description: The vswprintf_s function is the wide-character equivalent of the vsprintf_s function - * @param strDest - produce output according to a format ,write to the character string strDest - * @param destMax - The maximum length of destination buffer(including the terminating null ) - * @param format - format string - * @param arglist - instead of a variable number of arguments - * @return:return the number of characters printed(not including the terminating null wide characte), If an error occurred return -1. - */ - SECUREC_API int vswprintf_s(wchar_t *strDest, size_t destMax, const wchar_t *format, va_list arglist); - - /** - * @Description: The vsnprintf_s function is equivalent to the vsnprintf function except for the parameter destMax/count and the explicit runtime-constraints violation - * @param strDest - produce output according to a format ,write to the character string strDest - * @param destMax - The maximum length of destination buffer(including the terminating null byte ('\0')) - * @param count - do not write more than count bytes to strDest(not including the terminating null byte ('\0')) - * @param format - format string - * @param arglist - instead of a variable number of arguments - * @return:return the number of characters printed(not including the terminating null byte ('\0')), If an error occurred return -1.Pay special attention to returning -1 when truncation occurs - */ - SECUREC_API int vsnprintf_s(char *strDest, size_t destMax, size_t count, const char *format, - va_list arglist) SECUREC_ATTRIBUTE(4, 0); - - /** - * @Description: The snprintf_s function is equivalent to the snprintf function except for the parameter destMax/count and the explicit runtime-constraints violation - * @param strDest - produce output according to a format ,write to the character string strDest - * @param destMax - The maximum length of destination buffer(including the terminating null byte ('\0')) - * @param count - do not write more than count bytes to strDest(not including the terminating null byte ('\0')) - * @param format - format string - * @return:return the number of characters printed(not including the terminating null byte ('\0')), If an error occurred return -1.Pay special attention to returning -1 when truncation occurs - */ - SECUREC_API int snprintf_s(char *strDest, size_t destMax, size_t count, const char *format, - ...) SECUREC_ATTRIBUTE(4, 5); - -#if SECUREC_SNPRINTF_TRUNCATED - /** - * @Description: The vsnprintf_truncated_s function is equivalent to the vsnprintf_s function except no count parameter and return value - * @param strDest - produce output according to a format ,write to the character string strDest - * @param destMax - The maximum length of destination buffer(including the terminating null byte ('\0')) - * @param format - format string - * @param arglist - instead of a variable number of arguments - * @return:return the number of characters printed(not including the terminating null byte ('\0')), If an error occurred return -1.Pay special attention to returning destMax - 1 when truncation occurs - */ - SECUREC_API int vsnprintf_truncated_s(char *strDest, size_t destMax, const char *format, - va_list arglist) SECUREC_ATTRIBUTE(3, 0); - - /** - * @Description: The snprintf_truncated_s function is equivalent to the snprintf_2 function except no count parameter and return value - * @param strDest - produce output according to a format ,write to the character string strDest - * @param destMax - The maximum length of destination buffer(including the terminating null byte ('\0')) - * @param format - format string - * @return:return the number of characters printed(not including the terminating null byte ('\0')), If an error occurred return -1.Pay special attention to returning destMax - 1 when truncation occurs - */ - SECUREC_API int snprintf_truncated_s(char *strDest, size_t destMax, const char *format, ...) SECUREC_ATTRIBUTE(3, 4); -#endif - /** - * @Description: The scanf_s function is equivalent to fscanf_s with the argument stdin interposed before the arguments to scanf_s - * @param format - format string - * @return:returns the number of input items assigned, If an error occurred return -1. - */ - SECUREC_API int scanf_s(const char *format, ...); - - /** - * @Description: The wscanf_s function is the wide-character equivalent of the scanf_s function - * @param format - format string - * @return:returns the number of input items assigned, If an error occurred return -1. - */ - SECUREC_API int wscanf_s(const wchar_t *format, ...); - - /** - * @Description: The vscanf_s function is equivalent to scanf_s, with the variable argument list replaced by arglist - * @param format - format string - * @param arglist - instead of a variable number of arguments - * @return:returns the number of input items assigned, If an error occurred return -1. - */ - SECUREC_API int vscanf_s(const char *format, va_list arglist); - - /** - * @Description: The vwscanf_s function is the wide-character equivalent of the vscanf_s function - * @param format - format string - * @param arglist - instead of a variable number of arguments - * @return:returns the number of input items assigned, If an error occurred return -1. - */ - SECUREC_API int vwscanf_s(const wchar_t *format, va_list arglist); - - /** - * @Description: The fscanf_s function is equivalent to fscanf except that the c, s, and [ conversion specifiers apply to a pair of arguments (unless assignment suppression is indicated by a*) - * @param stream - stdio file stream - * @param format - format string - * @return:returns the number of input items assigned, If an error occurred return -1. - */ - SECUREC_API int fscanf_s(FILE *stream, const char *format, ...); - - /** - * @Description: The fwscanf_s function is the wide-character equivalent of the fscanf_s function - * @param stream - stdio file stream - * @param format - format string - * @return:returns the number of input items assigned, If an error occurred return -1. - */ - SECUREC_API int fwscanf_s(FILE *stream, const wchar_t *format, ...); - - /** - * @Description: The vfscanf_s function is equivalent to fscanf_s, with the variable argument list replaced by arglist - * @param stream - stdio file stream - * @param format - format string - * @param arglist - instead of a variable number of arguments - * @return:returns the number of input items assigned, If an error occurred return -1. - */ - SECUREC_API int vfscanf_s(FILE *stream, const char *format, va_list arglist); - - /** - * @Description: The vfwscanf_s function is the wide-character equivalent of the vfscanf_s function - * @param stream - stdio file stream - * @param format - format string - * @param arglist - instead of a variable number of arguments - * @return:returns the number of input items assigned, If an error occurred return -1. - */ - SECUREC_API int vfwscanf_s(FILE *stream, const wchar_t *format, va_list arglist); - - /** - * @Description: The sscanf_s function is equivalent to fscanf_s, except that input is obtained from a string (specified by the argument buffer) rather than from a stream - * @param buffer - read character from buffer - * @param format - format string - * @return:returns the number of input items assigned, If an error occurred return -1. - */ - SECUREC_API int sscanf_s(const char *buffer, const char *format, ...); - - /** - * @Description: The swscanf_s function is the wide-character equivalent of the sscanf_s function - * @param buffer - read character from buffer - * @param format - format string - * @return:returns the number of input items assigned, If an error occurred return -1. - */ - SECUREC_API int swscanf_s(const wchar_t *buffer, const wchar_t *format, ...); - - /** - * @Description: The vsscanf_s function is equivalent to sscanf_s, with the variable argument list replaced by arglist - * @param buffer - read character from buffer - * @param format - format string - * @param arglist - instead of a variable number of arguments - * @return:returns the number of input items assigned, If an error occurred return -1. - */ - SECUREC_API int vsscanf_s(const char *buffer, const char *format, va_list arglist); - - /** - * @Description: The vswscanf_s function is the wide-character equivalent of the vsscanf_s function - * @param buffer - read character from buffer - * @param format - format string - * @param arglist - instead of a variable number of arguments - * @return:returns the number of input items assigned, If an error occurred return -1. - */ - SECUREC_API int vswscanf_s(const wchar_t *buffer, const wchar_t *format, va_list arglist); - - /** - * @Description:The gets_s function reads at most one less than the number of characters specified by destMax from the stream pointed to by stdin, into the array pointed to by buffer - * @param buffer - destination address - * @param destMax -The maximum length of destination buffer(including the terminating null character) - * @return buffer if there was no runtime-constraint violation,If an error occurred return NULL. - */ - SECUREC_API char *gets_s(char *buffer, size_t destMax); -#endif - - /** - * @Description:The memcpy_s function copies n characters from the object pointed to by src into the object pointed to by dest. - * @param dest - destination address - * @param destMax -The maximum length of destination buffer - * @param src -source address - * @param count -copies count characters from the src - * @return EOK if there was no runtime-constraint violation - */ - SECUREC_API errno_t memcpy_s(void *dest, size_t destMax, const void *src, size_t count); - - /** - * @Description:The strcpy_s function copies the string pointed to by strSrc (including the terminating null character) into the array pointed to by strDest - * @param strDest - destination address - * @param destMax -The maximum length of destination buffer(including the terminating null character) - * @param strSrc -source address - * @return EOK if there was no runtime-constraint violation - */ - SECUREC_API errno_t strcpy_s(char *strDest, size_t destMax, const char *strSrc); - - /** - * @Description:The strncpy_s function copies not more than n successive characters (not including the terminating null character) - * from the array pointed to by strSrc to the array pointed to by strDest - * @param strDest - destination address - * @param destMax -The maximum length of destination buffer(including the terminating null character) - * @param strSrc -source address - * @param count -copies count characters from the src - * @return EOK if there was no runtime-constraint violation - */ - SECUREC_API errno_t strncpy_s(char *strDest, size_t destMax, const char *strSrc, size_t count); - - /** - * @Description:The strcat_s function appends a copy of the string pointed to by strSrc (including the terminating null character) - * to the end of the string pointed to by strDest - * @param strDest - destination address - * @param destMax -The maximum length of destination buffer(including the terminating null wide character) - * @param strSrc -source address - * @return EOK if there was no runtime-constraint violation - */ - SECUREC_API errno_t strcat_s(char *strDest, size_t destMax, const char *strSrc); - - /** - * @Description:The strncat_s function appends not more than n successive characters (not including the terminating null character) - * from the array pointed to by strSrc to the end of the string pointed to by strDest. - * @param strDest - destination address - * @param destMax -The maximum length of destination buffer(including the terminating null character) - * @param strSrc -source address - * @param count -copies count characters from the src - * @return EOK if there was no runtime-constraint violation - */ - SECUREC_API errno_t strncat_s(char *strDest, size_t destMax, const char *strSrc, size_t count); -#if SECUREC_IN_KERNEL == 0 - /* those functions are used by macro ,must declare hare , also for without function declaration warning */ - extern errno_t strncpy_error(char *strDest, size_t destMax, const char *strSrc, size_t count); - extern errno_t strcpy_error(char *strDest, size_t destMax, const char *strSrc); -#endif -#endif - -#if SECUREC_WITH_PERFORMANCE_ADDONS - /* those functions are used by macro */ - extern errno_t memset_sOptAsm(void *dest, size_t destMax, int c, size_t count); - extern errno_t memset_sOptTc(void *dest, size_t destMax, int c, size_t count); - extern errno_t memcpy_sOptAsm(void *dest, size_t destMax, const void *src, size_t count); - extern errno_t memcpy_sOptTc(void *dest, size_t destMax, const void *src, size_t count); - -/* strcpy_sp is a macro, NOT a function in performance optimization mode. */ -#define strcpy_sp(dest, destMax, src) ((__builtin_constant_p((destMax)) && \ - __builtin_constant_p((src))) ? \ - SECUREC_STRCPY_SM((dest), (destMax), (src)) : \ - strcpy_s((dest), (destMax), (src))) - -/* strncpy_sp is a macro, NOT a function in performance optimization mode. */ -#define strncpy_sp(dest, destMax, src, count) ((__builtin_constant_p((count)) && \ - __builtin_constant_p((destMax)) && \ - __builtin_constant_p((src))) ? \ - SECUREC_STRNCPY_SM((dest), (destMax), (src), (count)) : \ - strncpy_s((dest), (destMax), (src), (count))) - -/* strcat_sp is a macro, NOT a function in performance optimization mode. */ -#define strcat_sp(dest, destMax, src) ((__builtin_constant_p((destMax)) && \ - __builtin_constant_p((src))) ? \ - SECUREC_STRCAT_SM((dest), (destMax), (src)) : \ - strcat_s((dest), (destMax), (src))) - - /* strncat_sp is a macro, NOT a function in performance optimization mode. */ -#define strncat_sp(dest, destMax, src, count) ((__builtin_constant_p((count)) && \ - __builtin_constant_p((destMax)) && \ - __builtin_constant_p((src))) ? \ - SECUREC_STRNCAT_SM((dest), (destMax), (src), (count)) : \ - strncat_s((dest), (destMax), (src), (count))) - -/* memcpy_sp is a macro, NOT a function in performance optimization mode. */ -#define memcpy_sp(dest, destMax, src, count) (__builtin_constant_p((count)) ? \ - (SECUREC_MEMCPY_SM((dest), (destMax), (src), (count))) : \ - (__builtin_constant_p((destMax)) ? \ - (((size_t)(destMax) > 0 && \ - (((unsigned long long)(destMax) & \ - (unsigned long long)(-2)) < SECUREC_MEM_MAX_LEN)) ? \ - memcpy_sOptTc((dest), (destMax), (src), (count)) : ERANGE ) : \ - memcpy_sOptAsm((dest), (destMax), (src), (count)))) - -/* memset_sp is a macro, NOT a function in performance optimization mode. */ -#define memset_sp(dest, destMax, c, count) (__builtin_constant_p((count)) ? \ - (SECUREC_MEMSET_SM((dest), (destMax), (c), (count))) : \ - (__builtin_constant_p((destMax)) ? \ - (((size_t)(destMax) > 0 && \ - (((unsigned long long)(destMax) & \ - (unsigned long long)(-2)) < SECUREC_MEM_MAX_LEN)) ? \ - memset_sOptTc((dest), (destMax), (c), (count)) : ERANGE ) : \ - memset_sOptAsm((dest), (destMax), (c), (count)))) -#else -#define strcpy_sp strcpy_s -#define strncpy_sp strncpy_s -#define strcat_sp strcat_s -#define strncat_sp strncat_s -#define memcpy_sp memcpy_s -#define memset_sp memset_s -#endif - -#ifdef __cplusplus -} -#endif /* __cplusplus */ -#endif /* __SECUREC_H__5D13A042_DC3F_4ED9_A8D1_882811274C27 */ diff --git a/base/include/securectype.h b/base/include/securectype.h deleted file mode 100644 index 029772c..0000000 --- a/base/include/securectype.h +++ /dev/null @@ -1,279 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -/* [Standardize-exceptions]: Performance-sensitive - * [reason]: Strict parameter verification has been done before use - */ - -#ifndef __SECURECTYPE_H__A7BBB686_AADA_451B_B9F9_44DACDAE18A7 -#define __SECURECTYPE_H__A7BBB686_AADA_451B_B9F9_44DACDAE18A7 - -#ifndef SECUREC_ONLY_DECLARE_MEMSET -/* Shielding VC symbol redefinition warning */ -#if defined(_MSC_VER) && (_MSC_VER >= 1400) -#ifdef __STDC_WANT_SECURE_LIB__ -#undef __STDC_WANT_SECURE_LIB__ -#endif -#define __STDC_WANT_SECURE_LIB__ 0 -#ifdef _CRTIMP_ALTERNATIVE -#undef _CRTIMP_ALTERNATIVE -#endif -#define _CRTIMP_ALTERNATIVE //comment microsoft *_s function -#endif -#endif - -#if SECUREC_IN_KERNEL -#include -#include -#else -#include -#include -#include -#endif - -/* if enable SECUREC_COMPATIBLE_WIN_FORMAT, the output format will be compatible to Windows. */ -#if (defined(_WIN32) || defined(_WIN64) || defined(_MSC_VER)) -#define SECUREC_COMPATIBLE_WIN_FORMAT -#endif - -#if defined(SECUREC_COMPATIBLE_WIN_FORMAT) -/* in windows platform, can't use optimized function for there is no __builtin_constant_p like function */ -/* If need optimized macro, can define this: define __builtin_constant_p(x) 0 */ -#ifdef SECUREC_WITH_PERFORMANCE_ADDONS -#undef SECUREC_WITH_PERFORMANCE_ADDONS -#define SECUREC_WITH_PERFORMANCE_ADDONS 0 -#endif -#endif - -#if defined(__VXWORKS__) || defined(__vxworks) || defined(__VXWORKS) || defined(_VXWORKS_PLATFORM_) || \ - defined(SECUREC_VXWORKS_VERSION_5_4) -#if !defined(SECUREC_VXWORKS_PLATFORM) -#define SECUREC_VXWORKS_PLATFORM -#endif -#endif - -/* if enable SECUREC_COMPATIBLE_LINUX_FORMAT, the output format will be compatible to Linux. */ -#if !(defined(SECUREC_COMPATIBLE_WIN_FORMAT) || defined(SECUREC_VXWORKS_PLATFORM)) -#define SECUREC_COMPATIBLE_LINUX_FORMAT -#endif -#ifdef SECUREC_COMPATIBLE_LINUX_FORMAT -#include -#endif - -/* add the -DSECUREC_SUPPORT_FORMAT_WARNING compiler option to support -Wformat. - * default does not check the format is that the same data type in the actual code - * in the product is different in the original data type definition of VxWorks and Linux. - */ -#ifndef SECUREC_SUPPORT_FORMAT_WARNING -#define SECUREC_SUPPORT_FORMAT_WARNING 0 -#endif - -/* SECUREC_PCLINT for tool do not recognize __attribute__ just for pclint */ -#if SECUREC_SUPPORT_FORMAT_WARNING && !defined(SECUREC_PCLINT) -#define SECUREC_ATTRIBUTE(x, y) __attribute__((format(printf, (x), (y)))) -#else -#define SECUREC_ATTRIBUTE(x, y) -#endif - -/* SECUREC_PCLINT for tool do not recognize __builtin_expect ,just for pclint */ -#if defined(__GNUC__) && \ - ((__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 3 /* above 3.4 */))) && \ - !defined(SECUREC_PCLINT) -/* This is a built-in function that can be used without a declaration, if you encounter an undeclared compilation alarm, - * you can add -DSECUREC_NEED_BUILTIN_EXPECT_DECLARE to compiler options - */ -#if defined(SECUREC_NEED_BUILTIN_EXPECT_DECLARE) -long __builtin_expect(long exp, long c); -#endif -#define SECUREC_LIKELY(x) __builtin_expect(!!(x), 1) -#define SECUREC_UNLIKELY(x) __builtin_expect(!!(x), 0) -#else -#define SECUREC_LIKELY(x) (x) -#define SECUREC_UNLIKELY(x) (x) -#endif - -/* define the max length of the string */ -#define SECUREC_STRING_MAX_LEN (0x7fffffffUL) -#define SECUREC_WCHAR_STRING_MAX_LEN (SECUREC_STRING_MAX_LEN / sizeof(wchar_t)) - -/* add SECUREC_MEM_MAX_LEN for memcpy and memmove */ -#define SECUREC_MEM_MAX_LEN (0x7fffffffUL) -#define SECUREC_WCHAR_MEM_MAX_LEN (SECUREC_MEM_MAX_LEN / sizeof(wchar_t)) - -#if SECUREC_STRING_MAX_LEN > 0x7fffffff -#error "max string is 2G" -#endif - -#if (defined(__GNUC__ ) && defined(__SIZEOF_POINTER__ )) -#if (__SIZEOF_POINTER__ != 4) && (__SIZEOF_POINTER__ != 8) -#error "unsupported system" -#endif -#endif - -#ifndef SECUREC_MALLOC -#define SECUREC_MALLOC(x) malloc((size_t)(x)) -#endif - -#ifndef SECUREC_FREE -#define SECUREC_FREE(x) free((void *)(x)) -#endif - -#if defined(_WIN64) || defined(WIN64) || defined(__LP64__) || defined(_LP64) -#define SECUREC_ON_64BITS -#endif - -#if (!defined(SECUREC_ON_64BITS) && defined(__GNUC__ ) && defined(__SIZEOF_POINTER__ )) -#if __SIZEOF_POINTER__ == 8 -#define SECUREC_ON_64BITS -#endif -#endif - -#if defined(__SVR4) || defined(__svr4__) -#define SECUREC_ON_SOLARIS -#endif - -#if (defined(__hpux) || defined(_AIX) || defined(SECUREC_ON_SOLARIS)) -#define SECUREC_ON_UNIX -#endif - -/* codes should run under the macro SECUREC_COMPATIBLE_LINUX_FORMAT in unknown system on default, - * and strtold. The function - * strtold is referenced first at ISO9899:1999(C99), and some old compilers can - * not support these functions. Here provides a macro to open these functions: - * SECUREC_SUPPORT_STRTOLD -- if defined, strtold will be used - */ -#ifndef SECUREC_SUPPORT_STRTOLD -#define SECUREC_SUPPORT_STRTOLD 0 -#if (defined(SECUREC_COMPATIBLE_LINUX_FORMAT)) -#if defined(__USE_ISOC99) || \ - (defined(_AIX) && defined(_ISOC99_SOURCE)) || \ - (defined(__hpux) && defined(__ia64)) || \ - (defined(SECUREC_ON_SOLARIS) && (!defined(_STRICT_STDC) && !defined(__XOPEN_OR_POSIX)) || \ - defined(_STDC_C99) || defined(__EXTENSIONS__)) -#undef SECUREC_SUPPORT_STRTOLD -#define SECUREC_SUPPORT_STRTOLD 1 -#endif -#endif -#if ((defined(SECUREC_WRLINUX_BELOW4) || defined(_WRLINUX_BELOW4_))) -#undef SECUREC_SUPPORT_STRTOLD -#define SECUREC_SUPPORT_STRTOLD 0 -#endif -#endif - -#if SECUREC_WITH_PERFORMANCE_ADDONS - -#ifndef SECUREC_TWO_MIN -#define SECUREC_TWO_MIN(a, b) ((a) < (b) ? (a) : (b)) -#endif - -/* for strncpy_s performance optimization */ -#define SECUREC_STRNCPY_SM(dest, destMax, src, count) \ - (((void *)dest != NULL && (void *)src != NULL && (size_t)destMax >0 && \ - (((unsigned long long)(destMax) & (unsigned long long)(-2)) < SECUREC_STRING_MAX_LEN) && \ - (SECUREC_TWO_MIN(count , strlen(src)) + 1) <= (size_t)destMax) ? \ - ((count < strlen(src))? (memcpy(dest, src, count), *((char *)dest + count) = '\0', EOK): \ - (memcpy(dest, src, strlen(src) + 1), EOK )) :(strncpy_error(dest, destMax, src, count))) - -#define SECUREC_STRCPY_SM(dest, destMax, src) \ - (((void *)dest != NULL && (void *)src != NULL && (size_t)destMax >0 && \ - (((unsigned long long)(destMax) & (unsigned long long)(-2)) < SECUREC_STRING_MAX_LEN) && \ - (strlen(src) + 1) <= (size_t)destMax)? (memcpy(dest, src, strlen(src) + 1), EOK): \ - (strcpy_error(dest, destMax, src))) - -/* for strcat_s performance optimization */ -#if defined(__GNUC__) -#define SECUREC_STRCAT_SM(dest, destMax, src) \ - ({ \ - int catRet = EOK; \ - if ((void *)dest != NULL && (void *)src != NULL && (size_t)(destMax) >0 && \ - (((unsigned long long)(destMax) & (unsigned long long)(-2)) < SECUREC_STRING_MAX_LEN)) { \ - char *catTmpDst = (dest); \ - size_t catRestSize = (destMax); \ - while(catRestSize > 0 && *catTmpDst) { \ - ++catTmpDst; \ - --catRestSize; \ - } \ - if (catRestSize == 0) { \ - catRet = EINVAL; \ - } else if ((strlen(src) + 1) <= catRestSize) { \ - (void)memcpy(catTmpDst, (src), strlen(src) + 1); \ - catRet = EOK; \ - } else { \ - catRet = ERANGE; \ - } \ - if (catRet != EOK) { \ - catRet = strcat_s((dest), (destMax), (src)); \ - } \ - } else { \ - catRet = strcat_s((dest), (destMax), (src)); \ - } \ - catRet; \ - }) -#else -#define SECUREC_STRCAT_SM(dest, destMax, src) strcat_s(dest, destMax, src) -#endif - -/* for strncat_s performance optimization */ -#if defined(__GNUC__) -#define SECUREC_STRNCAT_SM(dest, destMax, src, count) \ - ({ \ - int ncatRet = EOK; \ - if ((void *)dest != NULL && (void *)src != NULL && (size_t)destMax > 0 && \ - (((unsigned long long)(destMax) & (unsigned long long)(-2)) < SECUREC_STRING_MAX_LEN) && \ - (((unsigned long long)(count) & (unsigned long long)(-2)) < SECUREC_STRING_MAX_LEN)) { \ - char *ncatTmpDest = (dest); \ - size_t ncatRestSize = (destMax); \ - while(ncatRestSize > 0 && *ncatTmpDest) { \ - ++ncatTmpDest; \ - --ncatRestSize; \ - } \ - if (ncatRestSize == 0) { \ - ncatRet = EINVAL; \ - } else if ((SECUREC_TWO_MIN((count) , strlen(src)) + 1) <= ncatRestSize ) { \ - if ((count) < strlen(src)) { \ - (void)memcpy(ncatTmpDest, (src), (count)); \ - *(ncatTmpDest + (count)) = '\0'; \ - } else { \ - (void)memcpy(ncatTmpDest, (src), strlen(src) + 1); \ - } \ - } else { \ - ncatRet = ERANGE; \ - } \ - if (ncatRet != EOK) { \ - ncatRet = strncat_s((dest), (destMax), (src), (count)); \ - } \ - } else { \ - ncatRet = strncat_s((dest), (destMax), (src), (count)); \ - } \ - ncatRet; \ - }) -#else -#define SECUREC_STRNCAT_SM(dest, destMax, src, count) strncat_s(dest, destMax, src, count) -#endif - -/* SECUREC_MEMCPY_SM do NOT check buffer overlap by default */ -#define SECUREC_MEMCPY_SM(dest, destMax, src, count) \ - (!(((size_t)destMax == 0 )||(((unsigned long long)(destMax) & (unsigned long long)(-2)) > SECUREC_MEM_MAX_LEN) || \ - ((size_t)count > (size_t)destMax) || ((void *)dest) == NULL || ((void *)src == NULL))? \ - (memcpy(dest, src, count), EOK) : \ - (memcpy_s(dest, destMax, src, count))) - -#define SECUREC_MEMSET_SM(dest, destMax, c, count) \ - (!(((size_t)destMax == 0 ) || (((unsigned long long)(destMax) & (unsigned long long)(-2)) > SECUREC_MEM_MAX_LEN) || \ - ((void *)dest == NULL) || ((size_t)count > (size_t)destMax)) ? \ - (memset(dest, c, count), EOK) : \ - ( memset_s(dest, destMax, c, count))) - -#endif -#endif /* __SECURECTYPE_H__A7BBB686_AADA_451B_B9F9_44DACDAE18A7 */ diff --git a/base/test/unittest/common/BUILD.gn b/base/test/unittest/common/BUILD.gn index 0d7f4b4..22b36c9 100644 --- a/base/test/unittest/common/BUILD.gn +++ b/base/test/unittest/common/BUILD.gn @@ -39,19 +39,6 @@ ohos_unittest("UtilsStringTest") { ] } -##############################unittest########################################## -ohos_unittest("UtilsSecurecTest") { - module_out_path = module_output_path - sources = [ "utils_securec_test.cpp" ] - - configs = [ ":module_private_config" ] - - deps = [ - "//commonlibrary/c_utils/base:utils", - "//third_party/googletest:gtest_main", - ] -} - ##############################unittest########################################## ohos_unittest("UtilsDirectoryTest") { module_out_path = module_output_path @@ -309,7 +296,6 @@ group("unittest") { ":UtilsSafeBlockQueueTrackingTest", ":UtilsSafeMapTest", ":UtilsSafeQueueTest", - ":UtilsSecurecTest", ":UtilsSingletonTest", ":UtilsSortedVectorTest", ":UtilsStringTest", diff --git a/base/test/unittest/common/utils_securec_test.cpp b/base/test/unittest/common/utils_securec_test.cpp deleted file mode 100644 index e07af92..0000000 --- a/base/test/unittest/common/utils_securec_test.cpp +++ /dev/null @@ -1,141 +0,0 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include "string_ex.h" -#include "securec.h" -using namespace testing::ext; -using namespace OHOS; - -class UtilsSecurecTest : public testing::Test -{ -public : - static void SetUpTestCase(void); - static void TearDownTestCase(void); - void SetUp(); - void TearDown(); -}; - -void UtilsSecurecTest::SetUpTestCase(void) -{ - // step 2: input testsuit setup step -} - -void UtilsSecurecTest::TearDownTestCase(void) -{ - // step 2: input testsuit teardown step -} - -void UtilsSecurecTest::SetUp(void) -{ - // step 3: input testcase setup step -} - -void UtilsSecurecTest::TearDown(void) -{ - // step 3: input testcase teardown step -} - -HWTEST_F(UtilsSecurecTest, test_memset_s_01, TestSize.Level0) -{ - char cBase[20]; - errno_t result = memset_s(reinterpret_cast(cBase), sizeof(char) * 20, 1, sizeof(char) * 20); - EXPECT_EQ(result, 0); -} - -HWTEST_F(UtilsSecurecTest, test_memset_s_02, TestSize.Level0) -{ - char cBase[20]; - errno_t result = memset_s(reinterpret_cast(cBase), sizeof(char) * 20, 1, sizeof(char) * 21); - EXPECT_NE(result, 0); -} - -HWTEST_F(UtilsSecurecTest, test_memcpy_s_01, TestSize.Level0) -{ - char cBase[20] = "memcpy_s"; - char cTemp[20]; - errno_t result = memcpy_s(reinterpret_cast(cTemp), sizeof(char) * 20, cBase, sizeof(cBase)); - EXPECT_EQ(result, 0); - EXPECT_EQ(0, strcmp(cTemp, cBase)); -} - -HWTEST_F(UtilsSecurecTest, test_memcpy_s_02, TestSize.Level0) -{ - char cBase[20] = "memcpy_s"; - char cTemp[5]; - errno_t result = memcpy_s(reinterpret_cast(cTemp), sizeof(char) * 5, cBase, sizeof(cBase)); - EXPECT_NE(result, 0); -} - -HWTEST_F(UtilsSecurecTest, test_strcpy_s_01, TestSize.Level0) -{ - const char cBase[] = "strcpy_base"; - char cTemp[20]; - errno_t result = strcpy_s(cTemp, sizeof(cTemp), cBase); - EXPECT_EQ(result, 0); - EXPECT_EQ(0, strcmp(cTemp, cBase)); -} - -HWTEST_F(UtilsSecurecTest, test_strcpy_s_02, TestSize.Level0) -{ - const char cBase[] = "strcpy_base"; - char cTemp[10]; - errno_t result = strcpy_s(cTemp, sizeof(cTemp), cBase); - EXPECT_NE(result, 0); -} - -HWTEST_F(UtilsSecurecTest, test_strncpy_s_01, TestSize.Level0) -{ - const char cBase[] = "strcpy_base"; - char cTemp[20]; - errno_t result = strncpy_s(cTemp, sizeof(cTemp), cBase, sizeof(cBase)); - EXPECT_EQ(result, 0); - EXPECT_EQ(0, strcmp(cTemp, cBase)); -} - -HWTEST_F(UtilsSecurecTest, test_strncpy_s_02, TestSize.Level0) -{ - const char cBase[] = "strcpy_base"; - char cTemp[10]; - errno_t result = strncpy_s(cTemp, sizeof(cTemp), cBase, (sizeof(cTemp) -1)); - EXPECT_EQ(result, 0); - result = strncpy_s(cTemp, sizeof(cTemp), cBase, sizeof(cBase)); - EXPECT_NE(result, 0); -} - -HWTEST_F(UtilsSecurecTest, test_strcat_s_01, TestSize.Level0) -{ - const char cBase[] = "strcpy_base"; - char cTemp[20] = "temp "; - errno_t result = strcat_s(cTemp, sizeof(cTemp), cBase); - EXPECT_EQ(result, 0); - EXPECT_EQ(0, strcmp(cTemp, "temp strcpy_base")); -} - -HWTEST_F(UtilsSecurecTest, test_strcat_s_02, TestSize.Level0) -{ - const char cBase[] = "strcpy_base"; - char cTemp[10]; - errno_t result = strcat_s(cTemp, sizeof(cTemp), cBase); - EXPECT_NE(result, 0); -} - -HWTEST_F(UtilsSecurecTest, test_sprintf_s_01, TestSize.Level0) -{ - char cBase[64] = { 0 }; - errno_t result = sprintf_s(cBase, sizeof(cBase), "%d", 12345); - EXPECT_EQ(string(cBase), "12345"); - EXPECT_NE(result, 0); -} \ No newline at end of file diff --git a/bundle.json b/bundle.json index 9e7082e..c4d70cf 100644 --- a/bundle.json +++ b/bundle.json @@ -49,8 +49,6 @@ "safe_block_queue.h", "safe_map.h", "safe_queue.h", - "securec.h", - "securectype.h", "semaphore_ex.h", "singleton.h", "sorted_vector.h", @@ -84,8 +82,6 @@ "safe_block_queue.h", "safe_map.h", "safe_queue.h", - "securec.h", - "securectype.h", "semaphore_ex.h", "singleton.h", "sorted_vector.h", -- Gitee From 222b84f803cfd7fee03163d130d6487af8016835 Mon Sep 17 00:00:00 2001 From: huangyuchen Date: Thu, 20 Oct 2022 10:53:03 +0800 Subject: [PATCH 4/9] Fix multithreading-related bugs. 1. Fix a multithreading-related bug, which makes `Timer::Shutdown()` incorrectly skip detach/join process of `thread_`. 2. Fix a multithreading-related bug, which makes `Timer::Shutdown()` fail to stop the loop in `EventReactor::RunLoop()` as expected. Change-Id: I5ccafbfc123a72820155e656a1658683df2623a8 Signed-off-by: huangyuchen --- base/include/timer.h | 16 +++++++++++++++- base/src/event_reactor.cpp | 22 +++++++++++++++------- base/src/event_reactor.h | 19 +++++++++++++++---- base/src/timer.cpp | 12 ++++++++---- 4 files changed, 53 insertions(+), 16 deletions(-) diff --git a/base/include/timer.h b/base/include/timer.h index 8b09ee4..596c414 100644 --- a/base/include/timer.h +++ b/base/include/timer.h @@ -29,7 +29,21 @@ namespace OHOS { namespace Utils { - +/* + * Notice: + * 1. Timer should be set up(via Setup()) before use, and shutdown(via Shutdown()) before its deconstruction. + * + * 2. Timer should be set up first and then shutdown. + * Avoid delegating them to different threads since it may cause multithreading problem. + * + * 3. Set up Timer again would not reset this Timer, but return `TIMER_ERR_INVALID_VALUE`. + * If a reset operation is required, shut Timer down first and then set it up. + * + * 4. Parameter in Shutdown() determines whether the thread in Timer would be detach or join. + * (True(default) --> join; False --> detach). + * + Detach operation would cause possible multithreading problems, thus is not recommended. + * If a detach operation is required, availability of related objects used in `thread_` should be guaranteed. + */ class Timer { public: using TimerCallback = std::function; diff --git a/base/src/event_reactor.cpp b/base/src/event_reactor.cpp index e54d183..fb98bfb 100644 --- a/base/src/event_reactor.cpp +++ b/base/src/event_reactor.cpp @@ -27,7 +27,7 @@ namespace OHOS { namespace Utils { EventReactor::EventReactor() - :stopped_(true), demultiplexer_(new EventDemultiplexer()) + :loopReady_(false), switch_(false), demultiplexer_(new EventDemultiplexer()) { } @@ -51,19 +51,19 @@ void EventReactor::UpdateEventHandler(EventHandler* handler) } } -uint32_t EventReactor::StartUp() +uint32_t EventReactor::SetUp() { if (demultiplexer_ == nullptr) { return TIMER_ERR_INVALID_VALUE; } - uint32_t ret = demultiplexer_->StartUp(); + uint32_t ret = demultiplexer_->StartUp(); // return TIME_ERR_OK, if demultiplexer has been started. if (ret != 0) { UTILS_LOGE("demultiplexer startUp failed."); return ret; } - stopped_ = false; + loopReady_ = true; return TIMER_ERR_OK; } @@ -81,14 +81,22 @@ void EventReactor::RunLoop(int timeout) const UTILS_LOGE("demultiplexer_ is nullptr."); return; } - while (!stopped_) { + + while (loopReady_ && switch_) { demultiplexer_->Polling(timeout); } + + loopReady_ = false; +} + +void EventReactor::SwitchOn() +{ + switch_ = true; } -void EventReactor::StopLoop() +void EventReactor::SwitchOff() { - stopped_ = true; + switch_ = false; } uint32_t EventReactor::ScheduleTimer(const TimerCallback& cb, uint32_t interval, int& timerFd, bool once) diff --git a/base/src/event_reactor.h b/base/src/event_reactor.h index e4889aa..794e44a 100644 --- a/base/src/event_reactor.h +++ b/base/src/event_reactor.h @@ -45,12 +45,22 @@ public: EventReactor& operator=(const EventReactor&&) = delete; virtual ~EventReactor(); - uint32_t StartUp(); + uint32_t SetUp(); void CleanUp(); void RunLoop(int timeout) const; - void StopLoop(); - bool IsStopped() const { return stopped_; } + void SwitchOn(); + void SwitchOff(); + + bool IsLoopReady() const + { + return loopReady_; + } + + bool IsSwitchedOn() const + { + return switch_; + } void UpdateEventHandler(EventHandler* handler); void RemoveEventHandler(EventHandler* handler); @@ -59,7 +69,8 @@ public: void CancelTimer(int timerFd); private: - volatile bool stopped_; + mutable volatile bool loopReady_; // refers to whether reactor is ready to call RunLoop(). + volatile bool switch_; // a switch to enable while-loop in RunLoop(). true: start, false: stop. std::unique_ptr demultiplexer_; std::recursive_mutex mutex_; std::list> timerEventHandlers_; diff --git a/base/src/timer.cpp b/base/src/timer.cpp index cbc1e9d..4fc9a1d 100644 --- a/base/src/timer.cpp +++ b/base/src/timer.cpp @@ -31,6 +31,10 @@ Timer::Timer(const std::string& name, int timeoutMs) : name_(name), timeoutMs_(t uint32_t Timer::Setup() { + if (thread_.joinable()) { // avoid double assign to an active thread + return TIMER_ERR_INVALID_VALUE; + } + reactor_->SwitchOn(); std::thread loop_thread(std::bind(&Timer::MainLoop, this)); thread_.swap(loop_thread); @@ -39,12 +43,12 @@ uint32_t Timer::Setup() void Timer::Shutdown(bool useJoin) { - if (reactor_->IsStopped()) { + if (!thread_.joinable()) { UTILS_LOGD("timer has been stopped already"); return; } - reactor_->StopLoop(); + reactor_->SwitchOff(); if (timeoutMs_ == -1) { std::lock_guard lock(mutex_); if (intervalToTimers_.empty()) { @@ -128,7 +132,7 @@ void Timer::Unregister(uint32_t timerId) void Timer::MainLoop() { prctl(PR_SET_NAME, name_.c_str(), 0, 0, 0); - if (reactor_->StartUp() == TIMER_ERR_OK) { + if (reactor_->SetUp() == TIMER_ERR_OK) { reactor_->RunLoop(timeoutMs_); } reactor_->CleanUp(); @@ -172,7 +176,7 @@ void Timer::OnTimer(int timerFd) continue; } /* if stop, callback is forbidden */ - if (!reactor_->IsStopped()) { + if (reactor_->IsLoopReady() && reactor_->IsSwitchedOn()) { ptr->callback(); } -- Gitee From 494fd9797d6f45ff2c5ca164752da3cefea540f7 Mon Sep 17 00:00:00 2001 From: peilixia Date: Thu, 27 Oct 2022 11:57:14 +0000 Subject: [PATCH 5/9] ICSL modify Signed-off-by: peilixia --- base/src/event_reactor.cpp | 3 --- base/src/parcel.cpp | 22 +++++++++++----------- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/base/src/event_reactor.cpp b/base/src/event_reactor.cpp index fb98bfb..bb57ca6 100644 --- a/base/src/event_reactor.cpp +++ b/base/src/event_reactor.cpp @@ -103,9 +103,6 @@ uint32_t EventReactor::ScheduleTimer(const TimerCallback& cb, uint32_t interval, { std::lock_guard lock(mutex_); std::shared_ptr handler = std::make_shared(this, interval, once); - if (handler == nullptr) { - return TIMER_ERR_INVALID_VALUE; - } handler->SetTimerCallback(cb); uint32_t ret = handler->Initialize(); if (ret != TIMER_ERR_OK) { diff --git a/base/src/parcel.cpp b/base/src/parcel.cpp index ae218ee..441a4df 100644 --- a/base/src/parcel.cpp +++ b/base/src/parcel.cpp @@ -964,8 +964,8 @@ const std::string Parcel::ReadString() return std::string(); } - size_t readCapacity = dataLength + 1; - if ((readCapacity > (size_t)dataLength) && (readCapacity <= GetReadableBytes())) { + size_t readCapacity = static_cast(dataLength) + 1; + if (readCapacity <= GetReadableBytes()) { const uint8_t *dest = ReadBuffer(readCapacity); if (dest != nullptr) { const auto *str = reinterpret_cast(dest); @@ -990,8 +990,8 @@ bool Parcel::ReadString(std::string &value) return false; } - size_t readCapacity = dataLength + 1; - if ((readCapacity > (size_t)dataLength) && (readCapacity <= GetReadableBytes())) { + size_t readCapacity = static_cast(dataLength) + 1; + if (readCapacity <= GetReadableBytes()) { const uint8_t *dest = ReadBuffer(readCapacity); if (dest != nullptr) { const auto *str = reinterpret_cast(dest); @@ -1017,8 +1017,8 @@ const std::u16string Parcel::ReadString16() return std::u16string(); } - size_t readCapacity = (dataLength + 1) * sizeof(char16_t); - if ((readCapacity > (size_t)dataLength) && (readCapacity <= GetReadableBytes())) { + size_t readCapacity = (static_cast(dataLength) + 1) * sizeof(char16_t); + if ((readCapacity > (static_cast(dataLength))) && (readCapacity <= GetReadableBytes())) { const uint8_t *str = ReadBuffer(readCapacity); if (str != nullptr) { const auto *u16Str = reinterpret_cast(str); @@ -1043,7 +1043,7 @@ bool Parcel::ReadString16(std::u16string &value) return false; } - size_t readCapacity = (dataLength + 1) * sizeof(char16_t); + size_t readCapacity = (static_cast(dataLength) + 1) * sizeof(char16_t); if (readCapacity <= GetReadableBytes()) { const uint8_t *str = ReadBuffer(readCapacity); if (str != nullptr) { @@ -1075,8 +1075,8 @@ const std::u16string Parcel::ReadString16WithLength(int32_t &readLength) return std::u16string(); } - size_t readCapacity = (dataLength + 1) * sizeof(char16_t); - if ((readCapacity > (size_t)dataLength) && (readCapacity <= GetReadableBytes())) { + size_t readCapacity = (static_cast(dataLength) + 1) * sizeof(char16_t); + if ((readCapacity > (static_cast(dataLength))) && (readCapacity <= GetReadableBytes())) { const uint8_t *str = ReadBuffer(readCapacity); if (str != nullptr) { const auto *u16Str = reinterpret_cast(str); @@ -1106,8 +1106,8 @@ const std::string Parcel::ReadString8WithLength(int32_t &readLength) return std::string(); } - size_t readCapacity = (dataLength + 1) * sizeof(char); - if ((readCapacity > (size_t)dataLength) && (readCapacity <= GetReadableBytes())) { + size_t readCapacity = (static_cast(dataLength) + 1) * sizeof(char); + if (readCapacity <= GetReadableBytes()) { const uint8_t *str = ReadBuffer(readCapacity); if (str != nullptr) { const auto *u8Str = reinterpret_cast(str); -- Gitee From b4985abf472e94d11a928f9b02fd74edc53adcb0 Mon Sep 17 00:00:00 2001 From: peilixia Date: Tue, 8 Nov 2022 20:51:43 +0800 Subject: [PATCH 6/9] clean warning Signed-off-by: peilixia --- base/include/ashmem.h | 4 ++-- base/include/common_timer_errors.h | 2 +- base/include/datetime_ex.h | 12 ++++++------ base/include/errors.h | 2 +- base/include/parcel.h | 6 +++--- base/include/refbase.h | 20 ++++++++++---------- base/include/rwlock.h | 6 +++--- base/include/safe_block_queue.h | 4 ++-- base/include/safe_queue.h | 12 ++++++------ base/include/semaphore_ex.h | 9 ++++----- base/include/thread_ex.h | 1 + base/include/thread_pool.h | 2 +- base/include/unique_fd.h | 24 ++++++++++++------------ base/src/refbase.cpp | 24 ++++++++++++------------ 14 files changed, 64 insertions(+), 64 deletions(-) diff --git a/base/include/ashmem.h b/base/include/ashmem.h index 50f9b1f..b0460ed 100644 --- a/base/include/ashmem.h +++ b/base/include/ashmem.h @@ -16,7 +16,7 @@ #ifndef UTILS_BASE_ASHMEM_H #define UTILS_BASE_ASHMEM_H -#include +#include #include #include "refbase.h" #include "parcel.h" @@ -40,7 +40,7 @@ public: bool WriteToAshmem(const void *data, int32_t size, int32_t offset); const void *ReadFromAshmem(int32_t size, int32_t offset); Ashmem(int fd, int32_t size); - ~Ashmem(); + ~Ashmem() override; int GetAshmemFd() const { return memoryFd_; diff --git a/base/include/common_timer_errors.h b/base/include/common_timer_errors.h index 8b15592..2d244af 100644 --- a/base/include/common_timer_errors.h +++ b/base/include/common_timer_errors.h @@ -16,7 +16,7 @@ #ifndef UTILS_COMMON_TIMER_ERRORS_H #define UTILS_COMMON_TIMER_ERRORS_H -#include +#include #include "errors.h" #include "common_errors.h" diff --git a/base/include/datetime_ex.h b/base/include/datetime_ex.h index 42baf6e..c1279c9 100644 --- a/base/include/datetime_ex.h +++ b/base/include/datetime_ex.h @@ -29,32 +29,32 @@ constexpr int64_t MICROSEC_TO_NANOSEC = 1000; constexpr int SECONDS_PER_HOUR = 3600; // 60 * 60 constexpr int SECONDS_PER_DAY = 86400; // 60 * 60 * 24 -static constexpr inline int64_t SecToNanosec(int64_t sec) +constexpr inline int64_t SecToNanosec(int64_t sec) { return sec * SEC_TO_NANOSEC; } -static constexpr inline int64_t MillisecToNanosec(int64_t millise) +constexpr inline int64_t MillisecToNanosec(int64_t millise) { return millise * MILLISEC_TO_NANOSEC; } -static constexpr inline int64_t MicrosecToNanosec(int64_t microsec) +constexpr inline int64_t MicrosecToNanosec(int64_t microsec) { return microsec * MICROSEC_TO_NANOSEC; } -static constexpr inline int64_t NanosecToSec(int64_t nanosec) +constexpr inline int64_t NanosecToSec(int64_t nanosec) { return nanosec / SEC_TO_NANOSEC; } -static constexpr inline int64_t NanosecToMillisec(int64_t nanosec) +constexpr inline int64_t NanosecToMillisec(int64_t nanosec) { return nanosec / MILLISEC_TO_NANOSEC; } -static constexpr inline int64_t NanosecToMicrosec(int64_t nanosec) +constexpr inline int64_t NanosecToMicrosec(int64_t nanosec) { return nanosec / MICROSEC_TO_NANOSEC; } diff --git a/base/include/errors.h b/base/include/errors.h index df5af52..9dadfa0 100644 --- a/base/include/errors.h +++ b/base/include/errors.h @@ -16,7 +16,7 @@ #ifndef UTILS_BASE_ERRORS_H #define UTILS_BASE_ERRORS_H -#include +#include namespace OHOS { diff --git a/base/include/parcel.h b/base/include/parcel.h index 78f9185..881b39e 100644 --- a/base/include/parcel.h +++ b/base/include/parcel.h @@ -78,11 +78,11 @@ public: class DefaultAllocator : public Allocator { public: - virtual void *Alloc(size_t size) override; + void *Alloc(size_t size) override; - virtual void Dealloc(void *data) override; + void Dealloc(void *data) override; private: - virtual void *Realloc(void *data, size_t newSize) override; + void *Realloc(void *data, size_t newSize) override; }; class Parcel { diff --git a/base/include/refbase.h b/base/include/refbase.h index 8122d81..aa3a9fd 100644 --- a/base/include/refbase.h +++ b/base/include/refbase.h @@ -95,7 +95,7 @@ private: #ifdef DEBUG_REFBASE RefTracker* refTracker = nullptr; std::mutex trackerMutex; // To ensure refTracker be thread-safe - void GetNewTrace(const void* object); + void GetNewTrace(const void* objectId); void PrintTracker(); #endif }; @@ -144,7 +144,7 @@ private: class WeakRefCounter { public: - WeakRefCounter(RefCounter *base, void *cookie); + WeakRefCounter(RefCounter *counter, void *cookie); virtual ~WeakRefCounter(); @@ -166,13 +166,13 @@ class RefBase { public: RefBase(); - RefBase(const RefBase &refbase); + RefBase(const RefBase &); - RefBase &operator=(const RefBase &refbase); + RefBase &operator=(const RefBase &); - RefBase(RefBase &&refbase) noexcept; + RefBase(RefBase &&other) noexcept; - RefBase &operator=(RefBase &&refbase) noexcept; + RefBase &operator=(RefBase &&other) noexcept; virtual ~RefBase(); @@ -205,13 +205,13 @@ public: bool IsExtendLifeTimeSet(); - virtual void OnFirstStrongRef(const void *objectId); + virtual void OnFirstStrongRef(const void *); - virtual void OnLastStrongRef(const void *objectId); + virtual void OnLastStrongRef(const void *); - virtual void OnLastWeakRef(const void *objectId); + virtual void OnLastWeakRef(const void *); - virtual bool OnAttemptPromoted(const void *objectId); + virtual bool OnAttemptPromoted(const void *); private: RefCounter *refs_ = nullptr; diff --git a/base/include/rwlock.h b/base/include/rwlock.h index b53b863..fde028d 100644 --- a/base/include/rwlock.h +++ b/base/include/rwlock.h @@ -33,7 +33,7 @@ public: RWLock() : RWLock(true) {} explicit RWLock(bool writeFirst); - virtual ~RWLock() {} + ~RWLock() override {} void LockRead(); void UnLockRead(); @@ -61,7 +61,7 @@ public: rwLockable_.LockWrite(); } - ~UniqueWriteGuard() + ~UniqueWriteGuard() override { rwLockable_.UnLockWrite(); } @@ -83,7 +83,7 @@ public: rwLockable_.LockRead(); } - ~UniqueReadGuard() + ~UniqueReadGuard() override { rwLockable_.UnLockRead(); } diff --git a/base/include/safe_block_queue.h b/base/include/safe_block_queue.h index 0196c92..146877f 100644 --- a/base/include/safe_block_queue.h +++ b/base/include/safe_block_queue.h @@ -27,7 +27,7 @@ namespace OHOS { template class SafeBlockQueue { public: - SafeBlockQueue(int capacity) : maxSize_(capacity) + explicit SafeBlockQueue(int capacity) : maxSize_(capacity) { } @@ -116,7 +116,7 @@ protected: template class SafeBlockQueueTracking : public SafeBlockQueue { public: - SafeBlockQueueTracking(int capacity) : SafeBlockQueue(capacity) + explicit SafeBlockQueueTracking(int capacity) : SafeBlockQueue(capacity) { unfinishedTaskCount_ = 0; } diff --git a/base/include/safe_queue.h b/base/include/safe_queue.h index d960041..60ce809 100644 --- a/base/include/safe_queue.h +++ b/base/include/safe_queue.h @@ -34,10 +34,10 @@ public: } } - void Erase(T& Object) + void Erase(T& object) { std::lock_guard lock(mutex_); - deque_.remove(Object); + deque_.remove(object); } bool Empty() @@ -89,12 +89,12 @@ protected: using SafeQueueInner::deque_; using SafeQueueInner::mutex_; - virtual void DoPush(const T& pt) override + void DoPush(const T& pt) override { deque_.push_back(pt); } - virtual bool DoPop(T& pt) override + bool DoPop(T& pt) override { if (deque_.size() > 0) { pt = deque_.front(); @@ -113,12 +113,12 @@ protected: using SafeQueueInner::deque_; using SafeQueueInner::mutex_; - virtual void DoPush(const T& pt) override + void DoPush(const T& pt) override { deque_.push_back(pt); } - virtual bool DoPop(T& pt) override + bool DoPop(T& pt) override { if (deque_.size() > 0) { pt = deque_.back(); diff --git a/base/include/semaphore_ex.h b/base/include/semaphore_ex.h index f44eabc..b8d2c58 100644 --- a/base/include/semaphore_ex.h +++ b/base/include/semaphore_ex.h @@ -22,7 +22,7 @@ #include #include #include -#include // timespec since c11 +#include // timespec since c11 namespace OHOS { @@ -30,9 +30,9 @@ const int INVALID_SEMA_VALUE = -1; class NamedSemaphore : public NoCopyable { public: - NamedSemaphore(size_t); + explicit NamedSemaphore(size_t); NamedSemaphore(const std::string&, size_t); - ~NamedSemaphore(); + ~NamedSemaphore() override; bool Create(); bool Unlink(); @@ -56,8 +56,7 @@ private: class Semaphore : public NoCopyable { public: - Semaphore(int value = 1) : count_(value) {} - ~Semaphore() = default; + explicit Semaphore(int value = 1) : count_(value) {} void Wait(); void Post(); diff --git a/base/include/thread_ex.h b/base/include/thread_ex.h index 5ee6aad..3ff915d 100644 --- a/base/include/thread_ex.h +++ b/base/include/thread_ex.h @@ -59,6 +59,7 @@ protected: virtual bool Run() = 0; // Derived class must implement Run() private: + Thread(const Thread&) = delete; Thread& operator=(const Thread&) = delete; static int ThreadStart(void* args); ThreadStatus Join(); // pthread created as detached diff --git a/base/include/thread_pool.h b/base/include/thread_pool.h index e2d1838..06bac4e 100644 --- a/base/include/thread_pool.h +++ b/base/include/thread_pool.h @@ -37,7 +37,7 @@ public: // is restricted to 16 characters, including the terminating null byte ('\0'). // Please pay attention to the length of name(args here). explicit ThreadPool(const std::string &name = std::string()); - ~ThreadPool(); + ~ThreadPool () override; uint32_t Start(int threadsNum); void Stop(); diff --git a/base/include/unique_fd.h b/base/include/unique_fd.h index 24a00de..2ba27bf 100644 --- a/base/include/unique_fd.h +++ b/base/include/unique_fd.h @@ -146,39 +146,39 @@ private: }; template -bool operator==(const int& lhs, const UniqueFdAddDeletor& uniqueFd) +bool operator==(const int& lhs, const UniqueFdAddDeletor& rhs) { - return lhs == uniqueFd.fd_; + return lhs == rhs.fd_; } template -bool operator!=(const int& lhs, const UniqueFdAddDeletor& uniqueFd) +bool operator!=(const int& lhs, const UniqueFdAddDeletor& rhs) { - return !(lhs == uniqueFd.fd_); + return !(lhs == rhs.fd_); } template -bool operator>=(const int& lhs, const UniqueFdAddDeletor& uniqueFd) +bool operator>=(const int& lhs, const UniqueFdAddDeletor& rhs) { - return lhs >= uniqueFd.fd_; + return lhs >= rhs.fd_; } template -bool operator>(const int& lhs, const UniqueFdAddDeletor& uniqueFd) +bool operator>(const int& lhs, const UniqueFdAddDeletor& rhs) { - return lhs > uniqueFd.fd_; + return lhs > rhs.fd_; } template -bool operator<=(const int& lhs, const UniqueFdAddDeletor& uniqueFd) +bool operator<=(const int& lhs, const UniqueFdAddDeletor& rhs) { - return lhs <= uniqueFd.fd_; + return lhs <= rhs.fd_; } template -bool operator<(const int& lhs, const UniqueFdAddDeletor& uniqueFd) +bool operator<(const int& lhs, const UniqueFdAddDeletor& rhs) { - return lhs < uniqueFd.fd_; + return lhs < rhs.fd_; } using UniqueFd = UniqueFdAddDeletor; diff --git a/base/src/refbase.cpp b/base/src/refbase.cpp index fb46127..d5f3a58 100644 --- a/base/src/refbase.cpp +++ b/base/src/refbase.cpp @@ -136,10 +136,10 @@ RefTracker* RefTracker::PopTrace(const void* root) return ref; } -void RefCounter::GetNewTrace(const void* object) +void RefCounter::GetNewTrace(const void* objectId) { std::lock_guard lock(trackerMutex); - RefTracker* newTracker = new RefTracker(refTracker, object, atomicStrong_.load(std::memory_order_relaxed), + RefTracker* newTracker = new RefTracker(refTracker, objectId, atomicStrong_.load(std::memory_order_relaxed), atomicWeak_.load(std::memory_order_relaxed), atomicRefCount_.load(std::memory_order_relaxed), getpid(), gettid()); refTracker = newTracker; @@ -200,10 +200,10 @@ RefCounter::~RefCounter() #endif } -int RefCounter::IncStrongRefCount(const void* object) +int RefCounter::IncStrongRefCount(const void* objectId) { #ifdef DEBUG_REFBASE - GetNewTrace(object); + GetNewTrace(objectId); #endif int curCount = atomicStrong_.load(std::memory_order_relaxed); if (curCount >= 0) { @@ -216,10 +216,10 @@ int RefCounter::IncStrongRefCount(const void* object) return curCount; } -int RefCounter::DecStrongRefCount(const void* object) +int RefCounter::DecStrongRefCount(const void* objectId) { #ifdef DEBUG_REFBASE - GetNewTrace(object); + GetNewTrace(objectId); #endif int curCount = GetStrongRefCount(); if (curCount == INITIAL_PRIMARY_VALUE) { @@ -238,18 +238,18 @@ int RefCounter::GetStrongRefCount() return atomicStrong_.load(std::memory_order_relaxed); } -int RefCounter::IncWeakRefCount(const void* object) +int RefCounter::IncWeakRefCount(const void* objectId) { #ifdef DEBUG_REFBASE - GetNewTrace(object); + GetNewTrace(objectId); #endif return atomicWeak_.fetch_add(1, std::memory_order_relaxed); } -int RefCounter::DecWeakRefCount(const void* object) +int RefCounter::DecWeakRefCount(const void* objectId) { #ifdef DEBUG_REFBASE - GetNewTrace(object); + GetNewTrace(objectId); #endif int curCount = GetWeakRefCount(); if (curCount > 0) { @@ -437,8 +437,8 @@ RefBase::~RefBase() { if (refs_ != nullptr) { refs_->RemoveCallback(); - if ((refs_->IsLifeTimeExtended() && refs_->GetWeakRefCount() == 0) - || refs_->GetStrongRefCount() == INITIAL_PRIMARY_VALUE) { + if ((refs_->IsLifeTimeExtended() && refs_->GetWeakRefCount() == 0) || + refs_->GetStrongRefCount() == INITIAL_PRIMARY_VALUE) { refs_->DecRefCount(); } refs_ = nullptr; -- Gitee From 95249ecaf841e98bf3a1a752caf95dd82166294c Mon Sep 17 00:00:00 2001 From: fanxiaoyu Date: Sat, 12 Nov 2022 09:00:02 +0000 Subject: [PATCH 7/9] Add GetRefCounter interface Signed-off-by: fanxiaoyu --- base/include/refbase.h | 2 ++ base/src/refbase.cpp | 7 ++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/base/include/refbase.h b/base/include/refbase.h index 8122d81..e7df34a 100644 --- a/base/include/refbase.h +++ b/base/include/refbase.h @@ -188,6 +188,8 @@ public: WeakRefCounter *CreateWeakRef(void *cookie); + RefCounter *GetRefCounter() const; + void IncWeakRef(const void *objectId); void DecWeakRef(const void *objectId); diff --git a/base/src/refbase.cpp b/base/src/refbase.cpp index fb46127..ad5665e 100644 --- a/base/src/refbase.cpp +++ b/base/src/refbase.cpp @@ -361,7 +361,7 @@ bool RefCounter::AttemptIncStrong(const void *objectId) if (atomicStrong_.compare_exchange_weak(curCount, curCount + 1, std::memory_order_relaxed)) { break; } - curCount = atomicStrong_.load(std::memory_order_relaxed); + // curCount has been updated. } if (curCount <= 0) { DecWeakRefCount(objectId); @@ -511,6 +511,11 @@ void RefBase::IncWeakRef(const void *objectId) } } +RefCounter *RefBase::GetRefCounter() const +{ + return refs_; +} + void RefBase::DecWeakRef(const void *objectId) { if (refs_ != nullptr) { -- Gitee From f68baa6d22726a57448dec5b3c364b958eeeed1b Mon Sep 17 00:00:00 2001 From: peilixia Date: Wed, 16 Nov 2022 08:39:08 +0000 Subject: [PATCH 8/9] synchronize code Signed-off-by: peilixia --- base/include/singleton.h | 2 +- base/include/thread_pool.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/base/include/singleton.h b/base/include/singleton.h index b831f90..8a1320b 100644 --- a/base/include/singleton.h +++ b/base/include/singleton.h @@ -68,7 +68,7 @@ std::shared_ptr DelayedSingleton::GetInstance() if (instance_ == nullptr) { std::lock_guard lock(mutex_); if (instance_ == nullptr) { - std::shared_ptr temp(new T); + std::shared_ptr temp(new (std::nothrow) T); instance_ = temp; } } diff --git a/base/include/thread_pool.h b/base/include/thread_pool.h index 06bac4e..66c7907 100644 --- a/base/include/thread_pool.h +++ b/base/include/thread_pool.h @@ -37,7 +37,7 @@ public: // is restricted to 16 characters, including the terminating null byte ('\0'). // Please pay attention to the length of name(args here). explicit ThreadPool(const std::string &name = std::string()); - ~ThreadPool () override; + ~ThreadPool() override; uint32_t Start(int threadsNum); void Stop(); -- Gitee From 2a88a6b7441d309692630f0b9d791362a13480a8 Mon Sep 17 00:00:00 2001 From: liujialiang Date: Wed, 23 Nov 2022 07:59:40 +0000 Subject: [PATCH 9/9] Add rwlock test Add rwlock test under two situation. One rwlock is writefirst, another is not writefist. Signed-off-by: liujialiang Change-Id: I23f1bd941e96ae888c1dc92e8c8339cf373531f7 --- base/test/unittest/common/BUILD.gn | 14 ++ .../unittest/common/utils_rwlock_test.cpp | 128 ++++++++++++++++++ 2 files changed, 142 insertions(+) create mode 100644 base/test/unittest/common/utils_rwlock_test.cpp diff --git a/base/test/unittest/common/BUILD.gn b/base/test/unittest/common/BUILD.gn index 22b36c9..4381866 100644 --- a/base/test/unittest/common/BUILD.gn +++ b/base/test/unittest/common/BUILD.gn @@ -279,6 +279,19 @@ ohos_unittest("UtilsTimerTest") { ] } +############################################################################### +ohos_unittest("UtilsRWLockTest") { + module_out_path = module_output_path + sources = [ "utils_rwlock_test.cpp" ] + + configs = [ ":module_private_config" ] + + deps = [ + "//commonlibrary/c_utils/base:utils", + "//third_party/googletest:gtest_main", + ] +} + ############################################################################### group("unittest") { @@ -291,6 +304,7 @@ group("unittest") { ":UtilsDateTimeTest", ":UtilsDirectoryTest", ":UtilsParcelTest", + ":UtilsRWLockTest", ":UtilsRefbaseTest", ":UtilsSafeBlockQueueTest", ":UtilsSafeBlockQueueTrackingTest", diff --git a/base/test/unittest/common/utils_rwlock_test.cpp b/base/test/unittest/common/utils_rwlock_test.cpp new file mode 100644 index 0000000..dbd6d48 --- /dev/null +++ b/base/test/unittest/common/utils_rwlock_test.cpp @@ -0,0 +1,128 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include +#include + +#include "rwlock.h" + +using namespace testing::ext; +using namespace std; + +namespace OHOS { +namespace { + +class UtilsRWLockTest : public testing::Test {}; + +// This class is designed for test RWLock. "buf" is protected by "rwLock". +class TestRWLock { +public: + TestRWLock():rwLock(), buf() {} + + explicit TestRWLock(bool writeFirst):rwLock(writeFirst), buf() {} + + void WriteStr(const string& str) + { + rwLock.LockWrite(); + for (auto it = str.begin(); it != str.end(); it++) { + buf.push_back(*it); + this_thread::sleep_for(std::chrono::milliseconds(1)); + } + rwLock.UnLockWrite(); + return; + } + + void ReadStr(string& str) + { + rwLock.LockRead(); + for (auto it = buf.begin(); it != buf.end(); it++) { + str.push_back(*it); + this_thread::sleep_for(std::chrono::milliseconds(1)); + } + rwLock.UnLockRead(); + return; + } +private: + Utils::RWLock rwLock; + string buf; +}; + +const string WRITE_IN_1("write1"); +const string WRITE_IN_2("write2"); + +/* + * @tc.name: testRWLock001 + * @tc.desc: RWLock here is under write-first mode. If there are some writing operation waiting, + * reading will never happen. Reading operations will happen at the same time, when all writing operations + * have finished. + */ +HWTEST_F(UtilsRWLockTest, testRWLock001, TestSize.Level1) +{ + TestRWLock test; + + thread first(bind(&TestRWLock::WriteStr, ref(test), ref(WRITE_IN_1))); + this_thread::sleep_for(chrono::milliseconds(1)); + + string readOut1(""); + thread second(bind(&TestRWLock::ReadStr, ref(test), ref(readOut1))); + this_thread::sleep_for(chrono::milliseconds(1)); + + thread third(bind(&TestRWLock::WriteStr, ref(test), ref(WRITE_IN_2))); + this_thread::sleep_for(chrono::milliseconds(1)); + + string readOut2(""); + thread fourth(bind(&TestRWLock::ReadStr, ref(test), ref(readOut2))); + + first.join(); + second.join(); + third.join(); + fourth.join(); + + EXPECT_EQ(readOut1, WRITE_IN_1 + WRITE_IN_2); + EXPECT_EQ(readOut2, WRITE_IN_1 + WRITE_IN_2); +} + +/* + * @tc.name: testRWLock002 + * @tc.desc: RWLock here is not under write-first mode. So if there are writing and reading operations in queue + * with a writing mission running, they will compete when the writing mission completing. But what we can ensure + * is that reading operations in queue will happen at the same time. + */ +HWTEST_F(UtilsRWLockTest, testRWLock002, TestSize.Level1) +{ + TestRWLock test(false); + + thread first(bind(&TestRWLock::WriteStr, ref(test), ref(WRITE_IN_1))); + this_thread::sleep_for(chrono::milliseconds(1)); + + string readOut1(""); + thread second(bind(&TestRWLock::ReadStr, ref(test), ref(readOut1))); + this_thread::sleep_for(chrono::milliseconds(1)); + + thread third(bind(&TestRWLock::WriteStr, ref(test), ref(WRITE_IN_2))); + this_thread::sleep_for(chrono::milliseconds(1)); + + string readOut2(""); + thread fourth(bind(&TestRWLock::ReadStr, ref(test), ref(readOut2))); + + first.join(); + second.join(); + third.join(); + fourth.join(); + + EXPECT_EQ(readOut1, readOut2); +} +} // namespace +} // namespace OHOS \ No newline at end of file -- Gitee