diff --git a/Android.mk b/Android.mk index 0718ff96f..4ef1d9382 100644 --- a/Android.mk +++ b/Android.mk @@ -227,7 +227,6 @@ LOCAL_SRC_FILES := \ src/core/utils/child_supervision.cpp \ src/core/utils/heap.cpp \ src/core/utils/jam_detector.cpp \ - src/core/utils/missing_strlcpy.c \ src/core/utils/missing_strnlen.c \ src/core/utils/parse_cmdline.cpp \ src/core/utils/slaac_address.cpp \ diff --git a/BUILD.gn b/BUILD.gn index 94b47db40..1ead97666 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -159,7 +159,6 @@ static_library("lib-ot-core") { "src/core/utils/child_supervision.cpp", "src/core/utils/heap.cpp", "src/core/utils/jam_detector.cpp", - "src/core/utils/missing_strlcpy.c", "src/core/utils/missing_strnlen.c", "src/core/utils/parse_cmdline.cpp", "src/core/utils/slaac_address.cpp", diff --git a/configure.ac b/configure.ac index 2e9e6a9d8..aa14d1962 100644 --- a/configure.ac +++ b/configure.ac @@ -173,7 +173,6 @@ then # Here we guess conservative values for tests that require link checks # to test for these features. This will prevent these checks from # being performed later in the configuration process. - ac_cv_func_strlcpy=${ac_cv_func_strlcpy-no} ac_cv_func_strnlen=${ac_cv_func_strnlen-no} else AC_MSG_RESULT([no]) @@ -950,7 +949,6 @@ AC_CHECK_HEADERS([string.h]) # # Missing Functions # -AC_CHECK_FUNC([strlcpy], [AC_DEFINE([HAVE_STRLCPY], [1], [Define if strlcpy exists.])]) AC_CHECK_FUNC([strnlen], [AC_DEFINE([HAVE_STRNLEN], [1], [Define if strnlen exists.])]) # diff --git a/etc/cmake/checks.cmake b/etc/cmake/checks.cmake index 1fdea9aaa..9e47bcd15 100644 --- a/etc/cmake/checks.cmake +++ b/etc/cmake/checks.cmake @@ -28,7 +28,6 @@ include(CheckFunctionExists) -check_function_exists("strlcpy" HAVE_STRLCPY) check_function_exists("strnlen" HAVE_STRNLEN) configure_file(${PROJECT_SOURCE_DIR}/etc/cmake/openthread-config-generic.h.in ${PROJECT_BINARY_DIR}/etc/cmake/openthread-config-generic.h) diff --git a/etc/cmake/openthread-config-generic.h.in b/etc/cmake/openthread-config-generic.h.in index 352ffd0fb..278b8192a 100644 --- a/etc/cmake/openthread-config-generic.h.in +++ b/etc/cmake/openthread-config-generic.h.in @@ -26,8 +26,5 @@ * POSSIBILITY OF SUCH DAMAGE. */ -/* Define if strlcpy exists. */ -#cmakedefine01 HAVE_STRLCPY - /* Define if strnlen exists. */ #cmakedefine01 HAVE_STRNLEN diff --git a/src/cli/cli_coap.cpp b/src/cli/cli_coap.cpp index e9e660dc8..cf9418a6b 100644 --- a/src/cli/cli_coap.cpp +++ b/src/cli/cli_coap.cpp @@ -107,7 +107,7 @@ otError Coap::ProcessResource(int argc, char *argv[]) mResource.mContext = this; mResource.mHandler = &Coap::HandleRequest; - strlcpy(mUriPath, argv[1], kMaxUriLength); + strncpy(mUriPath, argv[1], sizeof(mUriPath) - 1); SuccessOrExit(error = otCoapAddResource(mInterpreter.mInstance, &mResource)); } else @@ -188,7 +188,7 @@ otError Coap::ProcessRequest(int argc, char *argv[]) if (argc > 2) { VerifyOrExit(strlen(argv[2]) < kMaxUriLength, error = OT_ERROR_INVALID_ARGS); - strlcpy(coapUri, argv[2], kMaxUriLength); + strncpy(coapUri, argv[2], sizeof(coapUri) - 1); } else { diff --git a/src/cli/cli_coap_secure.cpp b/src/cli/cli_coap_secure.cpp index a8d3ec640..3c14f7be3 100644 --- a/src/cli/cli_coap_secure.cpp +++ b/src/cli/cli_coap_secure.cpp @@ -123,7 +123,7 @@ otError CoapSecure::ProcessResource(int argc, char *argv[]) mResource.mContext = this; mResource.mHandler = &CoapSecure::HandleRequest; - strlcpy(mUriPath, argv[1], kMaxUriLength); + strncpy(mUriPath, argv[1], sizeof(mUriPath) - 1); SuccessOrExit(error = otCoapSecureAddResource(mInterpreter.mInstance, &mResource)); } else @@ -247,7 +247,7 @@ otError CoapSecure::ProcessRequest(int argc, char *argv[]) // CoAP-URI if (argc > (2 - indexShifter)) { - strlcpy(coapUri, argv[2 - indexShifter], kMaxUriLength); + strncpy(coapUri, argv[2 - indexShifter], sizeof(coapUri) - 1); } // CoAP-Type diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index d294895bf..c7b4e2444 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -178,7 +178,6 @@ set(COMMON_SOURCES utils/child_supervision.cpp utils/heap.cpp utils/jam_detector.cpp - utils/missing_strlcpy.c utils/missing_strnlen.c utils/parse_cmdline.cpp utils/slaac_address.cpp diff --git a/src/core/Makefile.am b/src/core/Makefile.am index 0c89b4584..d49efb72b 100644 --- a/src/core/Makefile.am +++ b/src/core/Makefile.am @@ -235,7 +235,6 @@ SOURCES_COMMON = \ utils/child_supervision.cpp \ utils/heap.cpp \ utils/jam_detector.cpp \ - utils/missing_strlcpy.c \ utils/missing_strnlen.c \ utils/parse_cmdline.cpp \ utils/slaac_address.cpp \ @@ -268,7 +267,6 @@ libopenthread_radio_a_SOURCES = \ radio/radio_callbacks.cpp \ radio/radio_platform.cpp \ thread/link_quality.cpp \ - utils/missing_strlcpy.c \ utils/missing_strnlen.c \ utils/parse_cmdline.cpp \ $(NULL) diff --git a/src/core/meshcop/commissioner.cpp b/src/core/meshcop/commissioner.cpp index 08200e65e..e5cd04842 100644 --- a/src/core/meshcop/commissioner.cpp +++ b/src/core/meshcop/commissioner.cpp @@ -275,7 +275,7 @@ otError Commissioner::AddJoiner(const Mac::ExtAddress *aEui64, const char *aPskd joiner->mAny = true; } - (void)strlcpy(joiner->mPsk, aPskd, sizeof(joiner->mPsk)); + strncpy(joiner->mPsk, aPskd, sizeof(joiner->mPsk) - 1); joiner->mValid = true; joiner->mExpirationTime = TimerMilli::GetNow() + Time::SecToMsec(aTimeout); @@ -308,7 +308,7 @@ otError Commissioner::GetNextJoinerInfo(uint16_t &aIterator, otJoinerInfo &aJoin aJoiner.mAny = mJoiners[index].mAny; aJoiner.mEui64 = mJoiners[index].mEui64; - strlcpy(aJoiner.mPsk, mJoiners[index].mPsk, sizeof(aJoiner.mPsk)); + strncpy(aJoiner.mPsk, mJoiners[index].mPsk, sizeof(aJoiner.mPsk) - 1); aJoiner.mExpirationTime = mJoiners[index].mExpirationTime - TimerMilli::GetNow(); aIterator = static_cast(index) + 1; ExitNow(); diff --git a/src/core/utils/missing_strlcpy.c b/src/core/utils/missing_strlcpy.c deleted file mode 100644 index 66ea1de4c..000000000 --- a/src/core/utils/missing_strlcpy.c +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright (c) 2016, The OpenThread Authors. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the - * names of its contributors may be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY - * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND - * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include "utils/wrap_string.h" - -size_t missing_strlcpy(char *dst, const char *src, size_t dstsize) -{ - const size_t slen = strlen(src); - - if (dstsize != 0) - { - dstsize--; - - if (slen < dstsize) - { - dstsize = slen; - } - - if (dstsize != 0) - { - memcpy(dst, src, dstsize); - } - - dst[dstsize] = 0; - } - - return slen; -} diff --git a/src/core/utils/wrap_string.h b/src/core/utils/wrap_string.h index c3e349868..057da272b 100644 --- a/src/core/utils/wrap_string.h +++ b/src/core/utils/wrap_string.h @@ -49,8 +49,6 @@ /* Prototypes for our missing function replacements */ -/* See: https://www.freebsd.org/cgi/man.cgi?query=strlcpy */ -WRAP_EXTERN_C size_t missing_strlcpy(char *dst, const char *src, size_t dstsize); /* See: https://www.freebsd.org/cgi/man.cgi?query=strnlen */ WRAP_EXTERN_C size_t missing_strnlen(const char *s, size_t maxlen); @@ -60,16 +58,8 @@ WRAP_EXTERN_C size_t missing_strnlen(const char *s, size_t maxlen); #define HAVE_STRNLEN 0 #endif -#ifndef HAVE_STRLCPY -#define HAVE_STRLCPY 0 -#endif - #if (!HAVE_STRNLEN) #define strnlen(S, N) missing_strnlen(S, N) #endif -#if (!HAVE_STRLCPY) -#define strlcpy(D, S, N) missing_strlcpy(D, S, N) -#endif - #endif // WRAP_STRING_H diff --git a/tests/unit/Makefile.am b/tests/unit/Makefile.am index 2b85a5fef..a33d2c7b7 100644 --- a/tests/unit/Makefile.am +++ b/tests/unit/Makefile.am @@ -122,7 +122,6 @@ check_PROGRAMS += \ test-priority-queue \ test-pskc \ test-string \ - test-strlcpy \ test-strnlen \ test-timer \ $(NULL) @@ -222,9 +221,6 @@ test_pskc_SOURCES = $(COMMON_SOURCES) test_pskc.cpp test_string_LDADD = $(COMMON_LDADD) test_string_SOURCES = $(COMMON_SOURCES) test_string.cpp -test_strlcpy_LDADD = $(COMMON_LDADD) -test_strlcpy_SOURCES = test_strlcpy.c - test_strnlen_LDADD = $(COMMON_LDADD) test_strnlen_SOURCES = test_strnlen.c @@ -263,7 +259,6 @@ PRETTY_FILES = \ $(test_spinel_decoder_SOURCES) \ $(test_spinel_encoder_SOURCES) \ $(test_string_SOURCES) \ - $(test_strlcpy_SOURCES) \ $(test_strnlen_SOURCES) \ $(test_timer_SOURCES) \ $(test_toolchain_SOURCES) \ diff --git a/tests/unit/test_strlcpy.c b/tests/unit/test_strlcpy.c deleted file mode 100644 index 8c9ac4364..000000000 --- a/tests/unit/test_strlcpy.c +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright (c) 2016-2017, The OpenThread Authors. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. Neither the name of the copyright holder nor the - * names of its contributors may be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY - * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND - * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include - -#include -#include -#include "utils/wrap_string.h" - -int main(int argc, char **argv) -{ - char string_a[8] = "foo"; - char string_b[] = "barbarbar"; - size_t ret = 0; - int errors = 0; - - (void)argc; - (void)argv; - ret = strlcpy(string_a, string_b, sizeof(string_a)); - - if (0 != strcmp(string_a, "barbarb")) - { - printf("strcmp failed\n"); - errors++; - } - - if (ret != 9) - { - printf("strlcpy return value is wrong (%d)\n", (int)ret); - errors++; - } - - if (errors != 0) - { - printf("FAIL\n"); - return EXIT_FAILURE; - } - - printf("OK\n"); - return EXIT_SUCCESS; -}